splitting a file by a character

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dafydd
    New Member
    • Jan 2009
    • 3

    splitting a file by a character

    I got a multiline SQL file that I would like to split by ; and read into an array. However when I try to do
    Code:
    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)?
    Last edited by numberwhun; May 13 '09, 07:19 PM. Reason: Please use code tags!
  • Icecrack
    Recognized Expert New Member
    • Sep 2008
    • 174

    #2
    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?

    Comment

    • dafydd
      New Member
      • Jan 2009
      • 3

      #3
      solved

      I've solved this problem since I last posted here. Thanks for the response though. Here's what I used:
      Code:
      my $line = "";
      my $currentCommand = "";
      while(defined($line = <SCHEMA>)) {
      	$line =~ s/[\t\n]*//g;
      	# append to the current sql command
      	$currentCommand = $currentCommand . $line;
      	if($line =~ /;/) {
      		# this line ends an sql command, execute
      		$sth = $dbh->prepare($currentCommand);
      		$sth->execute();
      		# reset the current line
      		$currentCommand = "";
      	}
      }
      close(SCHEMA);
      Last edited by dafydd; May 4 '09, 05:43 AM. Reason: said thanks

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        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.

        Comment

        • dafydd
          New Member
          • Jan 2009
          • 3

          #5
          forgot to clarify the code's purpose

          Originally posted by numberwhun
          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.
          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.

          Comment

          Working...