Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case insensative hash searches 1

Status
Not open for further replies.

columb

IS-IT--Management
Feb 5, 2004
1,231
EU
I have a code which takes its input in the form name,amount, i.e. a sample input file might read

Jones,10
Smith,20
Brown,33

All entries for the same name should be added together
My initial code is
Code:
use strict;

my %results;

open FH, "test.txt" or die "Can't open test.txt\n";

while (<FH>)
  {
  my ( $name, $amount ) = split /,/;
  $results{$name} and $results{$name} += $amount, next;
  $results{$name} = $amount;
  }
close FH;

foreach my $key ( keys %results )
  { print "$key has $results{$key} pounds\n"; }
What I would like is for entries
Brown,20
and
brown,15
to be added as the same key. Currently I get an entry for Brown and an entry for brown. Any pointers please?

Columb Healy
Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
Cheat and make them all lower case, e.g.

Code:
use strict;

my %results;

open FH, "test.txt" or die "Can't open test.txt\n";

while (<FH>)
  {
  my ( $name, $amount ) = split /,/;

  $name = lc($name);

  $results{$name} and $results{$name} += $amount, next;
  $results{$name} = $amount;
  }
close FH;

foreach my $key ( keys %results )
  { print "$key has $results{$key} pounds\n"; }
 
Thanks figmatalan

Of course what I really want is them all converted to first char upper case, all others lower case but I'll work that one out as an exercise for the student!

Columb Healy
Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top