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!

checking for a hash key in an array of hashes

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
hello,

I'm strugling trying to write a simple check loop.

I have an array of hashes, I want to check the array and see if a hash key exists if it doesn't I want to push a hash into the array.

here is what I have...
Code:
    my @genmonths;

    foreach my $gendoc (@gendocs){
        if (! grep { $_ eq $gendoc->{'monthname} }@genmonths ) {
            push @genmonths, {monthname => $gendoc->{'monthname},month => $gendoc->{'month'}};
        }
    }

I know the grep is doing a simple scalar match against each array index, how do I expand the code to do a check for a hash key in each array index?

I've gone blank! I need to use 'exists' don't i ?






"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
oh and the title is a little missleading I want to check for a specific value of a given hash key in an array.

not if a key exists, but if a value of a given key exists within the array of hashes.

Hope that makes sense!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
basically ishnid, I have an array wich holds a hash for each avaialbale document.

I want to create a select list based on months , so I am looping the @gendocs which has a hash key of monthname, I want to create a separate array of hashes which contains indexes relative to each 'unique' month of monthname.

does that make more sense?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I worked it out in the end...
Code:
    my @genmonths;

    foreach my $gendoc (@gendocs){
      if($gendoc->{'Date'} ne "NA"){
        if (! grep { $_->{'monthname'} eq $gendoc->{'monthname'} } @genmonths ) {
            my $sel = 0;
            if($gendoc->{'monthnum'} == $curmonth){
                $sel = 1;
            }
            push @genmonths, {monthname => $gendoc->{'monthname'}, monthnum => $gendoc->{'monthnum'}, sel => $sel};
        }
      }
    }
:)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Your [tt]@genmonths[/tt] appears to be also, like [tt]@gendocs[/tt], an array of hashes, where each hash has only two keys, [tt]monthname[/tt] and [tt]months[/tt]. Of course your [tt]grep[/tt] won't function, as you are greping over hash references, not on real values.
If I understand your goal (but I'm not so sure of that), you should make [tt]@genmonths[/tt] a plain hash, where the keys are obtained from [tt]$gendoc{monthname}[/tt] and the values from [tt]$gendoc{month}[/tt].
This would give something like this (I'm used to dereferencing with [tt]$$[/tt], this is equivalent in this case to [tt]->[/tt]):
Code:
my%genmonths;
for$gendoc(@gendocs){
  $genmonths{$$gendoc{monthname}}=$$gendoc{month}if!exists$genmonths{$$gendoc{monthname}};
}
Of course this code assumes, as yours, that [tt]$gendoc{monthname}[/tt] and [tt]$gendoc{month}[/tt] do exist. You should insert a check for that unless you are sure from the generation of [tt]@gendocs[/tt] that those keys exist in all hashes.

Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top