Prev: FAQ 9.11 How do I redirect to another page?
Next: FAQ 7.18 How can I access a dynamic variable while a similarly named lexical is in scope?
From: wolf on 3 Aug 2010 09:10 Thomas Andersson schrieb: > As the topic says. I ahve a settings file where each line contains 2 numbers > of varying length and I want to extract each number and assign to a > variable, how would I go about that? > > use SPLIT: split( /\s+/, $input) splits on any whitespace(s) including tab(s). split( /\t/, $input) splits on every tab. open (my $infile, '<', 'mynumbers.txt') or die; my ($input, $number1, $number2); while ($input = <$infile>) { chomp $input; ($number1, $number2) = split( /\s+/, $input); print $number1,'--',$number2,"\n"; } close $infile;
From: J. Gleixner on 3 Aug 2010 10:49 wolf wrote: > Thomas Andersson schrieb: >> As the topic says. I ahve a settings file where each line contains 2 >> numbers of varying length and I want to extract each number and assign >> to a variable, how would I go about that? >> > use SPLIT: split( /\s+/, $input) splits on any whitespace(s) including > tab(s). split( /\t/, $input) splits on every tab. > > open (my $infile, '<', 'mynumbers.txt') or die; Always include the error message: or die $!; Better would be to include the file name and more helpful details: or die "Can't open mynumbers.txt for read: $!"; > my ($input, $number1, $number2); Declare the variables in the smallest scope. > > while ($input = <$infile>) { while( my $input = <$infile> ) { > chomp $input; > ($number1, $number2) = split( /\s+/, $input); my ( $number1, $number2) = split( /\s+/, $input); > print $number1,'--',$number2,"\n"; > } > close $infile;
From: sln on 3 Aug 2010 11:32 On Tue, 03 Aug 2010 15:10:47 +0200, wolf <wolf(a)gsheep.com> wrote: >Thomas Andersson schrieb: >> As the topic says. I ahve a settings file where each line contains 2 numbers >> of varying length and I want to extract each number and assign to a >> variable, how would I go about that? >> >> >use SPLIT: split( /\s+/, $input) splits on any whitespace(s) including >tab(s). split( /\t/, $input) splits on every tab. > >open (my $infile, '<', 'mynumbers.txt') or die; >my ($input, $number1, $number2); > >while ($input = <$infile>) { > chomp $input; > ($number1, $number2) = split( /\s+/, $input); Don't forget to validate $number(s) or you could run into errors when doing stuff like if $number1 == $number2 So after the split() it could be validated something like $number1 =~ s/^\s+//; $number1 =~ s/\s+$//; if $number1 =~ /^[+-]?\d*?\.?\d+$/ # for non-exponent Or it can all be done in one line ($number1, $number2) = $input =~ /\s*([+-]?\d*?\.?\d+)\s+([+-]?\d*?\.?\d+)/; -sln
From: Thomas Andersson on 3 Aug 2010 22:50 Justin C wrote: > On 2010-08-03, Thomas Andersson <thomas(a)tifozi.net> wrote: >> As the topic says. I ahve a settings file where each line contains 2 >> numbers of varying length and I want to extract each number and >> assign to a variable, how would I go about that? > > TMTOWTDI, here's one, it may not be a good one. > How long are you numbers? Are they formatted? 2 variable length numbers that are tab delimiterd, someone suggest the split function which worked perfect. my ($cpid, $lproc) = split (/\t/, $pidlist);
From: wolf on 4 Aug 2010 06:44
sln(a)netherlands.com schrieb: > On Tue, 03 Aug 2010 15:10:47 +0200, wolf <wolf(a)gsheep.com> wrote: > >> Thomas Andersson schrieb: >>> As the topic says. I ahve a settings file where each line contains 2 numbers >>> of varying length and I want to extract each number and assign to a >>> variable, how would I go about that? >>> >>> >> use SPLIT: split( /\s+/, $input) splits on any whitespace(s) including >> tab(s). split( /\t/, $input) splits on every tab. >> >> open (my $infile, '<', 'mynumbers.txt') or die; >> my ($input, $number1, $number2); >> >> while ($input = <$infile>) { >> chomp $input; >> ($number1, $number2) = split( /\s+/, $input); > > Don't forget to validate $number(s) or you could run > into errors when doing stuff like > if $number1 == $number2 > > So after the split() it could be validated something like > $number1 =~ s/^\s+//; > $number1 =~ s/\s+$//; > if $number1 =~ /^[+-]?\d*?\.?\d+$/ # for non-exponent > > Or it can all be done in one line > ($number1, $number2) = $input =~ /\s*([+-]?\d*?\.?\d+)\s+([+-]?\d*?\.?\d+)/; > > -sln Dear gents, these are all valid arguments to improving the code :) However, since the original poster didn't even know how to wield SPLIT, i wanted to keep it as simple and un-confusing as possible, demonstrating just SPLIT as the important thing to handle. As such the code works well enough :p Cheers, wolf |