Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

push

Posted on 2009-05-07
10
Medium Priority
?
348 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 600 total points
ID: 24327532

#Reference to same data
$hash = $interhash{$key}
 
#copy of data
$hash = {%{$interhash{$key}}};

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 85

Expert Comment

by:ozo
ID: 24327635

%hash = (%hash,%{$interhash{$key}})
0
 
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 85

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 85

Assisted Solution

by:ozo
ozo earned 400 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 85

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

885 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