Solved

push

Posted on 2009-05-07
10
309 Views
Last Modified: 2012-05-06
I have a HoH and an empty hash, I would like to push the values of the $key into a hash. I started my code already but i need some help.
$key = 2;
$hash = {};
$interhash = { '1'=> {'crystal rock' => '1914'},
                       '2'=>{'design' => '6.0'}};

desired outcome:
$hash = {'design' => '6.0'};
if ($key =~ /^[+-]?\d+$/ && $alpha <= $key && $key <= $omega)#condition is necessary

        {

            push(@{$hash,{interhash}->{$value}});            

           

        }

Open in new window

0
Comment
Question by:cucugirl
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 4

Expert Comment

by:binkzz
ID: 24326802
hi,

I'm not sure why you're trying to push into an associative array. Are you expecting to have more than one value per key? If you'll only have one value per key, you can just simply say:

$hash{$key} = $value;
0
 

Author Comment

by:cucugirl
ID: 24327354
The $key is a user's input, i put 2 here to make it  clearer.  If the key is 1 for example, i'd like the hash to look like this:

$key = 1;
$hash = {};
$interhash = { '1'=> {'crystal rock' => '1914'},
                       '2'=>{'design' => '6.0'}};

desired outcome:
$hash = {'crystal rock' => '1914'};
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 150 total points
ID: 24327532

#Reference to same data

$hash = $interhash{$key}
 

#copy of data

$hash = {%{$interhash{$key}}};

Open in new window

0
 
LVL 69

Expert Comment

by:Callandor
ID: 24327609
Push and pop are used for arrays.  This is contrary to "I would like to push the values of the $key into a hash."  If you really want the results in a hash, then binkzz already provided the general technique: $hash{$key} = $value;

Now you have to pull a specific entry from a HoH using a key, and replace $value with that code.  The assignment then should be easy.
0
 
LVL 84

Expert Comment

by:ozo
ID: 24327635

%hash = (%hash,%{$interhash{$key}})
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 39

Expert Comment

by:Adam314
ID: 24327669
>>$hash{$key} = $value;
This would work if the variable was %hash (a hash variable).  The desired variable is $hash, which is a reference to a hash, not a hash.
The code I posted shows 2 ways: one having $hash refer to the same hash as the values of $interhash, and one having $hash refer to a new hash with the same data as the values of the values of $interhash.
0
 
LVL 84

Expert Comment

by:ozo
ID: 24327739
$hash = {%$hash,map{%{$interhash->{$_}}} grep{/^[+-]?\d+$/ && $alpha <= $_ && $_ <= $omega} keys %$interhash}
0
 

Author Comment

by:cucugirl
ID: 24327758
#copy of data
$hash = {%{$interhash{$key}}};

if i use this, i get the following error: Can't use an undefined value as a HASH reference but when i print interhash i have a HoH
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 24327809
that works for
%interhash = ( '1'=> {'crystal rock' => '1914'},
                       '2'=>{'design' => '6.0'});

for
$interhash = { '1'=> {'crystal rock' => '1914'},
                       '2'=>{'design' => '6.0'}};
it would be
$hash = {%{$interhash->{$key}}};

I made the same mistake in my first answer, and also misread $hash = {'design' => '6.0'}; with %hash = ('design' => '6.0');
0
 
LVL 84

Expert Comment

by:ozo
ID: 24327922
note that unlike a push, $hash = {%{$interhash->{$key}}} deletes any keys that may have been in %$hash before

$hash = {(%$hash,%{$interhash{$key}}}
would be like push in that any keys that were in %$hash previously would remain (with possibly new values)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now