Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

grep hash ref from array of hashes perl

Posted on 2013-12-15
11
Medium Priority
?
775 Views
Last Modified: 2013-12-15
I have an array which is populated with hash refs the format looks like this

$VAR1 = [
                  [
             {
              'name' => 'Mayotte',
              'show_states' => '0',
              'country_iso_code' => 'YT'
            },
            {
              'country_iso_code' => 'ZA',
              'show_states' => '0',
              'name' => 'South Africa'
            },
            {
              'country_iso_code' => 'ZM',
              'name' => 'Zambia',
              'show_states' => '0'
            },
            {
              'name' => 'Zimbabwe',
              'show_states' => '0',
              'country_iso_code' => 'ZW'
            }
          ]
];

Open in new window


I would like each hashref in the array to have it's own key so

$VAR1 = [
             {
              'name' => 'Mayotte',
              'show_states' => '0',
              'country_iso_code' => 'YT'
            },
];
$VAR2 = [
             {
              'name' => 'Mayotte',
              'show_states' => '0',
              'country_iso_code' => 'YT'
            },
];

Open in new window


How can I do this?
0
Comment
Question by:hexfusion
[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
  • 6
  • 5
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39720152
use Data::Dumper;
$VAR1 = [
                  [
             {
              'name' => 'Mayotte',
              'show_states' => '0',
              'country_iso_code' => 'YT'
            },
            {
              'country_iso_code' => 'ZA',
              'show_states' => '0',
              'name' => 'South Africa'
            },
            {
              'country_iso_code' => 'ZM',
              'name' => 'Zambia',
              'show_states' => '0'
            },
            {
              'name' => 'Zimbabwe',
              'show_states' => '0',
              'country_iso_code' => 'ZW'
            }
          ]
];

print Dumper @{[map[$_],@{$VAR1->[0]}]};
0
 
LVL 2

Author Comment

by:hexfusion
ID: 39720185
I might of worded this wrong or misunderstand you my @array has the output from dumper.

$VAR1 = [
                  [
             {
              'name' => 'Mayotte',
              'show_states' => '0',
              'country_iso_code' => 'YT'
            },
            {
              'country_iso_code' => 'ZA',
              'show_states' => '0',
              'name' => 'South Africa'
            },
            {
              'country_iso_code' => 'ZM',
              'name' => 'Zambia',
              'show_states' => '0'
            },
            {
              'name' => 'Zimbabwe',
              'show_states' => '0',
              'country_iso_code' => 'ZW'
            }
          ]
];

Open in new window


to get @array to now have this format

$VAR1 = [
             {
              'name' => 'Mayotte',
              'show_states' => '0',
              'country_iso_code' => 'YT'
            },
];
$VAR2 = [
             {
              'name' => 'Mayotte',
              'show_states' => '0',
              'country_iso_code' => 'YT'
            },
];

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 39720196
use Data::Dumper;
$VAR1 = [
                  [
             {
              'name' => 'Mayotte',
              'show_states' => '0',
              'country_iso_code' => 'YT'
            },
            {
              'country_iso_code' => 'ZA',
              'show_states' => '0',
              'name' => 'South Africa'
            },
            {
              'country_iso_code' => 'ZM',
              'name' => 'Zambia',
              'show_states' => '0'
            },
            {
              'name' => 'Zimbabwe',
              'show_states' => '0',
              'country_iso_code' => 'ZW'
            }
          ]
];
@array=($VAR1);
print Dumper @array;
print Dumper @{[map[$_],@{$array[0][0]}]};
print Dumper @{$array[0][0]};
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 84

Expert Comment

by:ozo
ID: 39720198
How did you create @array?
Your second $VAR1 and $VAR2 both look like the your first [$VAR1->[0][0]]
and your first $VAR1 looks like a reference to an array containing a single reference to an array of hash refs
0
 
LVL 2

Author Comment

by:hexfusion
ID: 39720220
I created this with the following

push @array, {'country_iso_code' => $country_code, 'name' => $country_name, 'show_states' => $has_state};

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 39720228
push @array, {'country_iso_code' => $country_code, 'name' => $country_name, 'show_states' => $has_state};
print Dumper @array;
#would have produced something like
$VAR1 = {
          'name' => undef,
          'show_states' => undef,
          'country_iso_code' => undef
        };

Did you instead do
print Dumper [\@array];
or
print Dumper [[@array]];
0
 
LVL 2

Author Comment

by:hexfusion
ID: 39720239
you know what it was my submodule was like this

sub foo{
[...]
return \@array;
}
I then call it with

my @array = Foo::Bar->new->foo
0
 
LVL 84

Expert Comment

by:ozo
ID: 39720250
Try

return @array;
...
my @array = Foo::Bar->new->foo

or

return \@array;
...
my @array = @{ Foo::Bar->new->foo}
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 2000 total points
ID: 39720265
And then you probably called
print Dumper \@array;
instead of
print Dumper @array;

And I'm also guessing you don't actually want the extra [ ] in $VAR2
0
 
LVL 2

Author Comment

by:hexfusion
ID: 39720273
right thanks a lot still a little green here
0
 
LVL 2

Author Closing Comment

by:hexfusion
ID: 39720275
thanks a lot
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

636 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