David,

Instead of slurping up your data into an array, then looping over the array, you could 
simply loop over the file!

==========================================================

#!/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 $!";

while( <FH> ) {
    my( $k, $v ) = split /,/;
    $prices{$k}  = $v;
}

close FH;

for my $key (keys %prices) {
    print "$key = $prices{$key}\n";
}

--------------------------
David Olbersen 
iGuard Engineer
11415 West Bernardo Court 
San Diego, CA 92127 
1-858-676-2277 x2152


> -----Original Message-----
> From: David Gilden [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, March 18, 2003 10:56 AM
> To: [EMAIL PROTECTED]
> Subject: Re: PERL OSX --- reading a local text file
> 
> 
> 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";
> 
> }
> 
> 
> Thanks for any pointers here,
> 
> Dave
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

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

Reply via email to