in reply to Re^2: Learning about hashes
in thread Learning about hashes

A hash stores a key and a value so it doesn't make sense to assign an odd number of elements to a hash. Perhaps your code should look like:

my %eslist = ($record[3] => 1);

True laziness is hard work

Replies are listed 'Best First'.
Re^4: Learning about hashes
by molson (Acolyte) on Oct 20, 2009 at 19:45 UTC
    Okay, I need help! :) Here is my code so far. What I want to do is send all the unique values in %eslist to a text file. eslist has numbers in it ranging from 1 to 5 digits. I've tried variations on getting eslist into a uniqe file but none of them work so I'm not including them here because I think they are completely wrong and just confusing me. Thanks for any help or direction you can give me.
    use strict; use warnings; my %eslist; open(inFile, "< C:\\input.txt") || die "Unable to open file - $!\n"; open( OUTFILE, "> C:\\output.txt" ) || die "Unable to open write file! - $!\n"; while(<inFile>) { my @record = split /\|/, $_; my $tn = $record[0]; my $err = $record[1]; my $ent = $record[2]; %eslist = ($record[3] => 1); my $coid = $record[4]; } close inFile; close OUTFILE;

      First lets get the general advice out of the way:

      • using strictures (use strict; use warnings;) - very good
      • not using three parameter open - bad
      • not using lexical file handles (see example code below) - bad
      • checking open - very good
      • not providing sample data - vary bad
      • not providing sample output- vary bad
      • providing sample code - excellent

      The following may be what you are looking for. If not, it's at least a good starting point for reframing your question:

      use strict; use warnings; my $sampleData = <<EOF_DATA; 17|5|176|1288|1387 163|1|523|30|6 2592|2|9|1288|6174 152|1|170|1845|473 3|13|50|30|1766 EOF_DATA my %esList; open my $inFile, '<', \$sampleData or die "Unable to open file - $!\n" +; while (defined (my $line = <$inFile>)) { my ($tn, $err, $ent, $es, $coid) = split /\|/, $line; $esList{$es} = $line if ! exists $esList{$es}; } close $inFile; print $esList{$_} for sort {$a <=> $b} keys %esList;

      Prints:

      163|1|523|30|6 17|5|176|1288|1387 152|1|170|1845|473

      True laziness is hard work