I'm not comfortable with setting up key index columns but I may be able to answer some questions about a hash. With what about the concept of hash are you not comfortable?
The main things you need to know, in order to read a file into a hash areHow to declare a hash
How to add a key-value pair to a hash and that only scalar keys and values can be stored in a hash
How to check if a particular key exists in your hash, and if it does, how to access it
How to iterate through your keys so you can access all the key-value pairs
Given a file named b.txt which contains the following:
abcd,abrd,fun,D000,$12,$234,$212,$200,$200,$200
dear,dare,tun,D000,$12.00405,$234.08976,$212.09876,$200,$200,$200
here is an example of how you can read this file into a hash and then iterate through and print the keys and values:
#!/usr/bin/perl -w
use strict;
use warnings;
my $filename_b = '/home/david/Programming/Perl/b.txt';
my %b = %{read_into_hash($filename_b)};
foreach (keys %b) {
print "The key $_ has $b{$_} as its value\n";
my $aref = $b{$_};
print "$b{$_} is a reference to an array containing @{$aref}\n";
}
sub read_into_hash {
my $file = $_[0];
my %h = ();
open (my $fh, '<', $file);
while (<$fh>) {
my @fields = split(/,/);
my $hkey = join(",", @fields[0..3]);
$h{$hkey} = [@fields[4..9]];
}
return \%h;
} d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159