Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data::Dumper

Status
Not open for further replies.

tar565

Programmer
Jan 24, 2005
39
IE
I am looking for a way to capture the ouptut I generate from the code below into a nice hash. The ouptut is in the following format:

$VAR1 = {
'Element' => [
#0
{
'Name' => 'Michael',
'ID' => '3'
},
#1
{
'Name' => 'Paul',
'ID' => '4'
},
#2
{
'Name' => 'Gerry',
'ID' => '5'
}
]
};

and I want a hash :
%Data{ID}={Name};

--------------------------------------------------------

I am extracting and dumping the data as follows:

# extract the data
my $data = $obj->extract($template, $document);

# print it out so we can see we've got what we want to
$Data::Dumper::Indent = 3; # pretty print with array indices
print Dumper($data);
----------------------------------------------------------

I can get the data through splitting and map(s///) but its messy. Is the an easy to overcome this.
 
what does $data look like before you Dump it, is not already in a hash?
--Paul

cigless ...
 
HASH(0x19e4650)

Yep that is it before I dump it.
 
and I want a hash :
%Data{ID}={Name};
i'm guessing this is what you mean?
Code:
#!perl
use strict;
use warnings;
use Data::Dumper;

# The data you posted, read back into a hash ref from the pretty-print:
my $temp;
{
    local $/;
    ($temp = <DATA>) =~ s/^\$.*=\s+//;;
}
my $data = eval $temp;

# Print it again to make sure we got it right
my $d = Data::Dumper->new([$data], [qw(*data)]);
print $d->Dump;

[b]# Build a new hash (not a ref) with the ID's from the old hash as keys
# and the names as values
my %newdata;
for my $k (keys %$data) {
    for my $j (@{$data->{$k}}) {
        $newdata{$j->{'ID'}} = $j->{'Name'};
    }
}
[/b]
# Print out the new hash.
$d = Data::Dumper->new([\%newdata], [qw(*newdata)]);
print $d->Dump;

__DATA__
$VAR1 = {
          'Element' => [
                         #0
                         {
                           'Name' => 'Michael',
                           'ID' => '3'
                         },
                         #1
                         {
                           'Name' => 'Paul',
                           'ID' => '4'
                         },
                         #2
                         {
                           'Name' => 'Gerry',
                           'ID' => '5'
                         }
                       ]
        };
Output:
Code:
%data = (
          'Element' => [
                         {
                           'ID' => '3',
                           'Name' => 'Michael'
                         },
                         {
                           'ID' => '4',
                           'Name' => 'Paul'
                         },
                         {
                           'ID' => '5',
                           'Name' => 'Gerry'
                         }
                       ]
        );
[b]%newdata = (
             '4' => 'Paul',
             '3' => 'Michael',
             '5' => 'Gerry'
           );[/b]
If this isn't what you had in mind, let me know.

HTH


 
Code:
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Indent = 3;

my $data = {};
$$data{'Element'} = [{'ID' => 1, 'Name' => "Steve" },
{'ID' => 2, 'Name' => "Fred" },
{'ID' => 3, 'Name' => "Joe"}];

my %nicehash;

foreach my $hashref (@{$data->{'Element'}}) {
    $nicehash{$hashref->{'ID'}} = $hashref->{'Name'};
}

print Dumper(%nicehash);
You can probably save a line of code by using map, but you said you didn't want to...

Output:
Code:
$VAR1 = '1';
$VAR2 = 'Steve';
$VAR3 = '3';
$VAR4 = 'Joe';
$VAR5 = '2';
$VAR6 = 'Fred';
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top