David Gilden wrote:

> OK, one problem is solved, which was the path to the data file.
> 
> I know there is a simple way to read in a file like the following:
> 
> apple,2
> banana,4
> kiwi,1
> 
> 
> and produce a HASH. The code below works I get the feeling there is a more
> compact way to accomplish this.
> 
> -------
> 
> #!/usr/bin/perl -w
> 
> use strict;
> my %prices;
> 
> my $data = "/Users/dowda/Desktop/tmp test/test.txt";
> 
> open (FH,$data)  || die  "could not open $data $!";
> 
> local $/;
> 
> my $tmp = <FH>;
> my @tmp =  split (/\n/,$tmp);
> 
> close FH;
> 
> for (@tmp) {
> my ($k,$v)  = split /,/;
> $prices{$k} =$v;
> }
> 
> for my $key (keys %prices){
> 
> print "$key = $prices{$key}\n";
> 
> }

how about this: 

#!/usr/bin/perl
use warnings; 
use strict;

open (FH, "<", '/Users/dowda/Desktop/tmp test/test.txt') 
    or die "Unable to open input file: $!";

my %prices;
while (<FH>)
{
    chomp;
    my ($key, $value) = split /,/;
    warn "Duplicate key: $key\noldval: $prices{$key}\tnewval:$value\n"
        if defined $prices{$key};#always idiot proof the input ;)
    $prices{$key} = $value;
}

print "$_ = $prices{$key}\n" for sort keys %prices;

this work any better? 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to