By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

splitting a file by a character

P: 3
I got a multiline SQL file that I would like to split by ; and read into an array. However when I try to do
Expand|Select|Wrap|Line Numbers
  1. my @array = split(";", <FH>);
and iterate over each element I get just the first line of the SQL file.

Does anyone know how they would get the array of sql statements (and no new lines)?
May 2 '09 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 174
so far what is all your code or at least how are you reading the next line of the sql file?

have you tried a while loop?
May 4 '09 #2

P: 3
I've solved this problem since I last posted here. Thanks for the response though. Here's what I used:
Expand|Select|Wrap|Line Numbers
  1. my $line = "";
  2. my $currentCommand = "";
  3. while(defined($line = <SCHEMA>)) {
  4.     $line =~ s/[\t\n]*//g;
  5.     # append to the current sql command
  6.     $currentCommand = $currentCommand . $line;
  7.     if($line =~ /;/) {
  8.         # this line ends an sql command, execute
  9.         $sth = $dbh->prepare($currentCommand);
  10.         $sth->execute();
  11.         # reset the current line
  12.         $currentCommand = "";
  13.     }
  14. }
  15. close(SCHEMA);
May 4 '09 #3

Expert Mod 2.5K+
P: 3,503
Your code, upon issuing the execute statement, will fetch the first row from sql, but what about each subsequent row? To do that, you will have to incorporate the fetchrow_array method in a loop.
May 13 '09 #4

P: 3
I'm sorry I forgot to mention in the title what those sql statements were. I'm not fetching anything from the database using this code. I'm issuing statements to create sql tables and such to postgresql using the DBI.
May 13 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.