Solved

push

Posted on 2009-05-07
10
303 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

12 Experts available now in Live!

Get 1:1 Help Now