in reply to Reading Text File into Hash to Parse and Sort
Nothing in your post uses hashes, so we can't even imagine what kinds of problems you are having. But let's assume you want a hash to the different columns of each row. You ultimately want an array of hashes, one array element for each line in the file, each array element a hash with the columns.
#!/usr/bin/perl use strict; use warnings; my @columns = qw/Date ClosingPrice AveragePrice/; my (@lines,@array); open (FILE, '<', 'FB_LPWAP.txt') or die ("ERROR: Could not read file") +; foreach my $line (<FILE>){ push @array, do { my %h; @h{@columns} = split /\t/,$line; \%h }; } close (FILE); foreach my $row (@array) { foreach my $col (reverse keys %{$row}) { printf "%s=>%s ",$col,$row->{$col} } print "\n"; }
Update #1 - thanks to tye for the hash slice solution. Update #2 - by the time I figured all this out, there were already better solutions. Hope I learned something ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Reading Text File into Hash to Parse and Sort
by Perl_Derek (Initiate) on Apr 22, 2015 at 16:29 UTC | |
by Anonymous Monk on Apr 22, 2015 at 19:51 UTC |