Solved

grep hash ref from array of hashes perl

Posted on 2013-12-15
11
657 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 110
Existing perl code to be changed for ftp to sftp handling 14 165
perl to convert excel to csv 3 184
html form to write data to csv 24 97
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

23 Experts available now in Live!

Get 1:1 Help Now