in reply to How can I read a line of text from a file into an array?
Or for a compact solution using the same logic. You could replace everything after the chomp with: my %hash = map {split /,/} grep !/^,$/, split /'/, $data;open HANDLE, 'testhash.txt' or die "Can't open: $!"; my $data = <HANDLE>; close HANDLE; chomp $data; my %hash; foreach my $pair (split /'/, $data) { next if $pair eq ''; # empty 'item' before first comma next if $pair =~ /^,$/; # throw away commas between pairs my ($key, $value) = split /,/, $pair; $hash{$key} = $value; } # demo use of the hash to access results foreach my $key (keys %hash) { print "$key $hash{$key}\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How can I read a line of text from a file into an array?
by fenners (Beadle) on Jul 17, 2001 at 19:53 UTC | |
by dvergin (Monsignor) on Jul 18, 2001 at 03:27 UTC | |
by fenners (Beadle) on Jul 19, 2001 at 00:04 UTC |