Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Help with Dumping data. Manipulate hashes output.

by oldwarrior32 (Sexton)
on Jun 05, 2012 at 17:02 UTC ( #974504=perlquestion: print w/ replies, xml ) Need Help??
oldwarrior32 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am new here and noob to Perl too, I would like to ask for some help.

I have this piece of code(I think the full program is not necessary for the matter discused):

my $data=  (Dumper $res->valueof('//listPhoneByNameResponse/return/phone'));

That part of the program brings the following output:

$VAR1 = { 'model' => 'Cisco 7911', 'name' => 'SEP00229004C80A', 'product' => 'Cisco 7911' }; $VAR2 = { 'model' => 'Cisco 7911', 'name' => 'SEP00229004C851', 'product' => 'Cisco 7911' }; $VAR3 = { 'model' => 'Cisco 7911', 'name' => 'SEP00229004CC92', 'product' => 'Cisco 7911'

I need to parse the output, to create a table. I could make a new program and process all the anonymous hashes, but is there a way to tell the "Dumper" function to bring only one value a time?

Something like this:

 print Dumper->${$VAR1}{"model"}; I think you got the idea. Any recomendation to parse a Data::Dumper output would be welcome. Thanks! I just want to say thank you all for your worthfull help. I just forgot about the Dumper module and made some modifications. At the end I just used the Text::Table module for practical purposes and it worked like a charm. Thanks again!

Comment on Help with Dumping data. Manipulate hashes output.
Select or Download Code
Re: Help with Dumping data. Manipulate hashes output.
by MidLifeXis (Prior) on Jun 05, 2012 at 17:38 UTC

    Is there a reason that instead of

    my $data= (Dumper $res->valueof('//listPhoneByNameResponse/return/phon +e'));
    you cannot just use the data structure directly?
    my $data= [ $res->valueof('//listPhoneByNameResponse/return/phone') ];
    Why do the round trip through Data::Dumper and eval?

    Update: made results into arrayref, as it appears that is the intent of the original code.

    --MidLifeXis

Re: Help with Dumping data. Manipulate hashes output.
by BrowserUk (Pope) on Jun 05, 2012 at 17:43 UTC

    If you don't want to dump everything, don't use Dumper.

    my $data = $res->valueof('//listPhoneByNameResponse/return/phone'); print "$_ => ", $data->{$_} for 'model';

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Help with Dumping data. Manipulate hashes output.
by Cristoforo (Chaplain) on Jun 05, 2012 at 18:32 UTC
    Here is a small program to illustrate building a table, (using Text::Table).

    #!/usr/bin/perl use strict; use warnings; use Text::Table; my $data = [ { 'model' => 'Cisco 7911', 'name' => 'SEP00229004C80A', 'product' => 'Cisco 7911' }, { 'model' => 'Cisco 7911', 'name' => 'SEP00229004C851', 'product' => 'Cisco 7911' }, { 'model' => 'Cisco 7911', 'name' => 'SEP00229004CC92', 'product' => 'Cisco 7911' }, ]; my @headers = qw/ Model Name Product /; my $tb = Text::Table->new( map {title => $_}, @headers ); for my $href (@$data) { $tb->load( [ @$href{ map lc, @headers } ] ); } print $tb;

    ***prints

    Model Name Product Cisco 7911 SEP00229004C80A Cisco 7911 Cisco 7911 SEP00229004C851 Cisco 7911 Cisco 7911 SEP00229004CC92 Cisco 7911

    Chris

Re: Help with Dumping data. Manipulate hashes output.
by oldwarrior32 (Sexton) on Jun 05, 2012 at 20:01 UTC

    Thanks! I just want to say thank you all for your worthfull help. I just forgot about the Dumper module and made some modifications. At the end I just used the Text::Table module for practical purposes and it worked like a charm. Thanks again!

    Note: I updated my first post when I wanted to reply like this. It seems I can't edit the updated post or I need to read the Perlmonks doc. Anyway sorry for that

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://974504]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-04-19 06:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (478 votes), past polls