Solved

grep hash ref from array of hashes perl

Posted on 2013-12-15
11
714 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
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!

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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

688 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