juve has asked for the wisdom of the Perl Monks concerning the following question:

PROBLEM How can i delete a repeated hash in a foreach loop like the one below.
Can someone give an example for deleting BOTH 1)repeated regular hash, 2)repeated hash of hashes.
This is an example of what i have:
foreach $student (sort keys %class) { print "$class{$student}"; }

Thanks -- goooooo! Juve

Replies are listed 'Best First'.
Re: Delete repeated Hash's / HOH's
by RMGir (Prior) on Apr 10, 2002 at 16:40 UTC
    I don't understand what you mean by "repeated hash"...

    The same key can't appear twice in a hash, so that can't be it.

    Do you mean a case where the same VALUE appears twice?

    Could you post more details about what you're trying to do? It's possible that more information about what (or why) you're trying to do will help us help you :)
    --
    Mike

Re: Delete repeated Hash's / HOH's
by juve (Initiate) on Apr 10, 2002 at 17:35 UTC
    I should have been clearer in my initial post.sorry!
    This is a question of how to deal with two hash's with the same value. Not a case of having to values in a hash.

    How can i handle repeated data in my hashes? Both HOH or %hash.

    Here is a sample hash:

    $students{"Sally Cummings"}=( { "year"=>"1", "GPA"=>"3.3", "major"=>"Undecided", "email"=>"scummings\@school.edu" }

    Here is a sample HOH:

    %students = (); $students{"Nick Plato"}=( { "year"=>"2", "GPA"=>"2.5", "major"=>"Phys. Ed.", "email"=>"nplato\@school.edu" } ); $students{"Mary Pitts"}=( { "year"=>"4", "GPA"=>"4", "major"=>"Economics", "major"=>"Economics", "email"=>"mpitts\@school.edu" } ); $students{"Sally Cummings"}=( { "year"=>"1", "GPA"=>"3.3", "major"=>"Undecided", "major"=>"Undecided", "email"=>"scummings\@school.edu" } $students{"Sally Cummings"}=( { "year"=>"1", "GPA"=>"3.3", "major"=>"Undecided", "major"=>"Undecided", "email"=>"scummings\@school.edu" } );

    Look at the above examples closely for all repeated stuff.
      Clear as mud...
      As RMGir already pointed out, you can't have two identical keys in a hash. So, in your case you can't have two Sallys in %students. The second assignment simply overwrites the first.

      Also, I suggest you read perldoc perldata and perldoc perldsc as I sense a fair amount of confusion on your part.

      --perlplexer
      You won't see the repeated items, like major, since the hash will only hold the last value.
      $ perl -d Default die handler restored. Loading DB routines from perl5db.pl version 1.07 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. $students{"Mary Pitts"}=( { "year"=>"4", "GPA"=>"4", "major"=>"Economics", "major"=>"Economics", "email"=>"mpitts\@school.edu" } ); print "Mike was here..."; main::(-:1): $students{"Mary Pitts"}=( main::(-:2): { main::(-:3): "year"=>"4", main::(-:4): "GPA"=>"4", main::(-:5): "major"=>"Economics", main::(-:6): "major"=>"Economics", main::(-:7): "email"=>"mpitts\@school.edu" DB<1> n main::(-:11): print "Mike was here..."; DB<1> x %students 0 'Mary Pitts' 1 HASH(0xa038a8c) 'GPA' => 4 'email' => 'mpitts@school.edu' 'major' => 'Economics' 'year' => 4
      See? Only one major...

      Are you saying you want an error to occur when a duplicate value is attempted? A large debate occurred on p5p recently (a few months ago) about clamped hashes, but I don't remember what the outcome was, or whether the feature will be in 5.8.
      --
      Mike