perlguyjoe has asked for the wisdom of the Perl Monks concerning the following question:
Here is the script:ID=1 First=John Last=Doe AGE=42 ID=2 First=Jane Last=Doe AGE=35 ID=3 First=Jack Last=Doe AGE=17 ID=4 First=Jill Last=Doe AGE=15
Here is what I am getting as output:#!/usr/bin/perl use Data::Dumper; use strict; use warnings; getPeople(); sub getPeople { my ( $id, $first, $last, $age ); my $file = 'list.txt'; my $people; my $cntr; open( LIST, "< $file" ) or die "Can't open $file : $!"; #my @lines = split /\n/, $str; #foreach my $line (@lines) { while (my $row = <LIST> ) { $cntr++; my ($id, $first, $last, $age ) = split( /\s/, $row ); $id = (split( /=/, $id ))[1]; $first = (split( /=/, $first ))[1]; $last = (split( /=/, $last ))[1]; $age = (split( /=/, $age ))[1]; $people = { "$cntr" => { 'id' => "$id", 'first' => "$first", 'last' => "$last", 'age' => "$age" } }; } print Dumper $people; close LIST; }
I would expect to get something like this:$VAR1 = { '4' => { 'first' => 'Jill', 'last' => 'Doe', 'id' => '4', 'age' => '15' } };
$VAR1 = { '4' => { 'first' => 'Jill', 'last' => 'Doe', 'id' => '4', 'age' => '15' } '3' => { 'first' => 'Jack', 'last' => 'Doe', 'id' => '3', 'age' => '17' } '2' => { 'first' => 'Jane', 'last' => 'Doe', 'id' => '2', 'age' => '35' } '1' => { 'first' => 'John', 'last' => 'Doe', 'id' => '1', 'age' => '42' } };
|
|---|