Solved

grep hash ref from array of hashes perl

Posted on 2013-12-15
11
668 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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