Solved

grep hash ref from array of hashes perl

Posted on 2013-12-15
11
642 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
  • 6
  • 5
11 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
right thanks a lot still a little green here
0
 
LVL 2

Author Closing Comment

by:hexfusion
Comment Utility
thanks a lot
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

9 Experts available now in Live!

Get 1:1 Help Now