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

if i had in one line: apples = 50, banana = 20, watermelons = 50, pears = 30, etc and it goes on like this how do i extract the datas, say 50, 20 , 50, 30 etc I have no clue when the data ends in that line

Replies are listed 'Best First'.
Re: extract data from line
by davido (Cardinal) on Jun 01, 2004 at 16:57 UTC
    Look in the Plain Old Documentation under "perlfunc -f split" for info on what this snippet does.

    my %info; foreach my $line ( @lines ) { foreach my $pair ( split /\s*,\s*/, $line ) { my ( $key, $value ) = split /\s*=\s*/, $pair; $info{$key} = $value; } }

    If your keys are more complex, ie allow embeded commas, and such, you'll have to come up with a more robust solution, but this works fine for simple datasets.


    Dave

Re: extract data from line
by duff (Parson) on Jun 01, 2004 at 16:54 UTC
    What have you tried? What are the constraints on the data? There are lots of ways to do it. You could use split, you could use a regular expression, you could do all sorts of things.
Re: extract data from line
by pizza_milkshake (Monk) on Jun 01, 2004 at 17:06 UTC
    perl -MData::Dumper -le'$_="a = 1, b = 2"; my %h = /(\S+) = (\d+)/g; p +rint Dumper \%h' $VAR1 = { 'a' => '1', 'b' => '2' };
    the regex of course should vary based on precisely what you'll need to match, this is an example.

    perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"