Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

push

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

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

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

719 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