in reply to Re^3: multi level hash terror - if statement to access specific elements
in thread multi level hash terror - if statement to access specific elements

Removing the print doesn't help - it is printing every last name in the database table. I just want it to print the surname of 'Gay' when it finds the first name 'Gay'. Only surnames should be printed whose first name is 'Gay' It seems to be printing every surname in the table.

for my $key (keys %names){ for my $key2(keys $names{$key}){ if ($names{$key}{$key2}[0] = 'Gay'){ print "Last Name:".$names{$key}{$key2}[1]."\n"; } } }
Thanks for the help here :)
  • Comment on Re^4: multi level hash terror - if statement to access specific elements
  • Download Code

Replies are listed 'Best First'.
Re^5: multi level hash terror - if statement to access specific elements
by beech (Parson) on Feb 13, 2016 at 04:08 UTC

    ...Removing the print doesn't help ... = 'Gay'

    Why are you still using the assignment operator instead of the string equality comparison operator eq?

      Sorry that's a typo...but using the correct equality operator still results in all surnames getting printed

        Sorry that's a typo...but using the correct equality operator still results in all surnames getting printed

        Are all the surnames "Gay"?

        Is this what you have?

        my %names = ( abe => { lincoln => [ 'Cheddar', 'Cheddar', ], }, george => { washington => [ 'Gay', 'Gay', ], }, ); for my $key ( keys %names ) { for my $key2 ( keys $names{$key} ) { if ( $names{$key}{$key2}[0] eq 'Gay' ) { print "Last Name:" . $names{$key}{$key2}[1] . "\n"; } } } __END__ Last Name:Gay

        There is no lincoln/cheddar printed

        … the correct equality operator …
        Did you notice that == is not the correct equality operator? Most strings numerically evaluate to zero, so
        'apples' == 'pears'
        is true, while
        'apples' eq 'pears'
        is false.