Solved

push

Posted on 2009-05-07
10
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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

617 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