help has asked for the wisdom of the Perl Monks concerning the following question:

hello i`m totaly new to perl, i have a problem... i know
what i have to do but i don`t know how to do it.
i have a file with data like this
"
8294750274534+4821 kudu AUG
3249230958867+9462 firkt BOD
482048348344+8467 kwirk NAR
3242359960+5487 mana GOW
3948585747757+48 ala SAN
348838482348+6487 kudu AEG
4234985745534+82 pira AEG
...
"

is it possible somehow to do the following
(lets take first line from file)

"8294750274534+4821 kudu AUG"

i want to have $var1=8294750274534 var2=4821 var3=kudu
var4=AUG
and to do that with every line. is it possible? if u can`t
show me how to do that plz give me a clue what should
i use to do these actions and where to get info about
it.
etc... i`m not sure but as i understand i should use
regexp or smth like that? thanks for reply.

Replies are listed 'Best First'.
Re: select line from file
by Zaxo (Archbishop) on Nov 24, 2003 at 19:19 UTC

    The first argument of split is a regular expression, use it like this,

    while (<FH>) { chomp; my ($first, $second, $third, $fourth) = split /\+|\s/; # do stuff }
    I changed your variable names to look less like an array, and added sigils to make it look like perl.

    After Compline,
    Zaxo

Re: select line from file
by Zed_Lopez (Chaplain) on Nov 24, 2003 at 19:24 UTC

    Yes, a regexp could be an approach this problem. So could split.

    Searching the monastery on these topics would be fruitful also.

    This sounds a lot like homework. You'll find you get more specific answers when you demonstrate you've actually tried writing some code and ask specific questions about specific problems you've encountered.

Re: select line from file
by duff (Parson) on Nov 24, 2003 at 19:39 UTC
Re: select line from file
by Art_XIV (Hermit) on Nov 24, 2003 at 21:04 UTC

    There's More Than One Way To Do It, of course, and the following will provide some examples, in order from least idiomatic to most idiomatic. You can see it run if you plop it into your editor.

    use strict; use Data::Dumper; #fill an array with content of __DATA__ my @data; while (<DATA>) { chomp; push @data, $_ } #ugliest way - use regex & matches - shudder! print "Ugliest Way:\n"; foreach my $line (@data) { $line =~ /^(\d+)\+(\d+)\s(\w+)\s(\w+)/; my $first = $1; my $second = $2; my $third = $3; my $fourth = $4; print "$first:$second:$third:$fourth\n"; } #slightly less ugly way - exploit list context print "\nSlightly Less Ugly Way:\n"; foreach my $line (@data) { my ($first, $second, $third, $fourth) = $line =~ /^(\d+)\+(\d+)\s(\w+)\s(\w+)/; print "$first:$second:$third:$fourth\n"; } #decent way - use split instead of regex print "\nDecent way:\n"; foreach my $line (@data) { my ($first, $second, $third, $fourth) = split (/\+| /, $line); print "$first:$second:$third:$fourth\n"; } #okay way - load values into a hash for later use #assuming the first element is the one on which lookups will #occur most frequently. Hashes are great for finding keys quickly. print "\nOkay Way:\n"; my %hash1; foreach my $line (@data) { my ($first, $second, $third, $fourth) = split (/\+| /, $line); $hash1{$first}{SECOND} = $second; $hash1{$first}{THIRD} = $third; $hash1{$first}{FOURTH} = $fourth; } print Dumper(%hash1); #equally okay way - load values into two-dim array print "\nEqually Okay Way:\n"; my @array; foreach my $line (@data) { #square brackets give you a ref to the array produced #by the split push @array, [split (/\+| /, $line)]; } print Dumper(@array); 1; __DATA__ 8294750274534+4821 kudu AUG 3249230958867+9462 firkt BOD 482048348344+8467 kwirk NAR 3242359960+5487 mana GOW 3948585747757+48 ala SAN 348838482348+6487 kudu AEG 4234985745534+82 pira AEG

    There are even more elegant ways of accomplishing this, and probably a CPAN module or two, but you have to walk before you can run. ;)

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
      #decent way - use split instead of regex

      Umm, pardon me, but doesn't your split (/\+| /, $line); uses regex in its /\+| / part?

Re: select line from file
by BUU (Prior) on Nov 24, 2003 at 19:21 UTC
    A) Your grammar is atrocious.
    B) Use open to open the file, <> to read lines from the file and split to seperate the parts.