Forum Moderators: coopster & phranque

Message Too Old, No Replies

help on splitting

         

kalesh

12:28 am on Sep 13, 2005 (gmt 0)



I have a text file with fields seperated by spaces and one record on a line
I am trying to split it line by line to enter it into mysql database
the code I have so far is

open(IN,"list.txt");
while($line=<IN>){
($fname,$lname,$email)=split /""/, $line;
#database code omitted
}
close (IN);
what happens is the the value in $line is placed in $fname and $lname and $email are left null
any help is appreciated

KevinADC

12:46 am on Sep 13, 2005 (gmt 0)

10+ Year Member



your code appears to have no space in the regexp, just double-quotes, try this:



open(IN,"list.txt") or die"$!";
while(my $line=<IN>){
my ($fname,$lname,$email)= split(/\s/,$line);
#database code omitted
}
close (IN);

which can be written more succinctly:



open(IN,"list.txt") or die "$!";
while(<IN>){
my ($fname,$lname,$email)= split(/\s/);
#database code omitted
}
close (IN);

make sure you start using "strict" with your perl scripts too and declaring your variables with "my".

KevinADC

12:48 am on Sep 13, 2005 (gmt 0)

10+ Year Member



you may also want to chomp the lines before processing the data:



open(IN,"list.txt") or die "$!";
while(<IN>){
chomp;
(rest of code block)
}

kalesh

12:51 am on Sep 13, 2005 (gmt 0)



thanks to you all got it right now