Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Data::Dumper Dilema

by vagnerr (Prior)
on Feb 01, 2002 at 13:07 UTC ( [id://142669]=perlquestion: print w/replies, xml ) Need Help??

vagnerr has asked for the wisdom of the Perl Monks concerning the following question:

I have been recently working on some code that processes data returned from a third party function to generate reports. I wanted to make sure that the data I have been reciving is correct and so have used Data::Dumper to print it so I can check. My problem however is that whilst running the following call to show me the whole object returned:-
print Data::Dumper->Dump([$sb]);
produces
$VAR1 = bless( [ '100', [ [ '2593140', '2766860', '0', undef, undef, undef, undef, '0UI38923424239K', '2002-01-10 09:16:50', '1500', 'None Given', '£', 'GBP' ], [ '2513902', '2766861', '1', undef, undef, undef, undef, 'LKJS3892342423Y5S', '2002-01-10 09:16:54', '1500', 'None Given', '£', 'GBP' ], [ '2574165', '2766862', '0', undef, undef, undef, undef, '0ASJH2782121S', '2002-01-10 09:16:57', '1500', 'None Given', '£', 'GBP' ], ........etc
when I try and just display the subset of that data (ie just the bit that actualy interests me :-
print Data::Dumper->Dump($sb->[1]);
all I get is
$VAR1 = [ '2593140', '2766860', '0', undef, undef, undef, undef, '0UI38923424239K', '2002-01-10 09:16:50', '1500', 'None Given', '£', 'GBP' ];
ie I dont seem to be able to access all the rest.
On top of that if I go
print Data::Dumper->Dump($sb->[1]->[$anumber]);
where $anumber is 1,2,3,4 etc I get the first element of each of the successive arrays from the original
Its not been a huge problem as dumping the entire object showed me that all the data was indeed there. But I would like to know why Data::Dumper didn't produce the results I was expecting. Any ideas?

---If it doesn't fit use a bigger hammer

Replies are listed 'Best First'.
(ichimunki) Re: Data::Dumper Dilema
by ichimunki (Priest) on Feb 01, 2002 at 13:21 UTC
    Since it appears that Data::Dumper is performing as advertised, you'll need to adjust your expectations. *grin*

    What isn't clear from your question is which bits are interesting to you? Your first example gives you the entire data structure, obviously a bit long. Since your data structure is an array of arrays, your second example pulls out an entire array one level down. And finally, your third example is pulling an element from that second level down. Which is exactly what that syntax does.

    Now, I can only assume that what you wanted to do was see one element from a bunch of the arrays in the parent array. To do that, you'll want a loop structure:
    for my $x ( 0 .. @$sb+0 ) { print Data::Dumper->Dump( $sb->[$x][#element] ); }
    Where #element is the element number you are interested in.
Re (tilly) 1: Data::Dumper Dilema
by tilly (Archbishop) on Feb 01, 2002 at 15:05 UTC
    If you check the documentation for Data::Dumper you will find that the Dumper function exported from the module does what you want.

    The method-oriented call that you did expects to receive 2 arguments. The first will have all of your data, and the second says what your variables etc are named.

    However beyond that I am finding it really hard to believe that the code you are showing is producing the output you describe. $sb is a thing. It is being expanded into a list. That cannot happen if $sb is a normal variable, in my version of Perl I cannot even get it to happen with a tie. I would need to look in the guts to see if you can do it with magic in XS.

    If that actually is the code that produced that output, could you report back what the following code snipped produces, and the type of Perl you have?

    use strict; use Data::Dumper; tie my $foo, 'Demo', qw(Testing one two three); print Dumper($foo); package Demo; sub TIESCALAR { my $class = shift; bless [@_], $class; } sub FETCH { my $self = shift; wantarray ? @$self : $self->[0]; }
      I'll look into the Dumper function itself when I get the chance.

      The code realy does produce the output given. Talking to a college we came to the conclusion that the fact that $sb is actualy a class object rather than a simple data object might be causing confusion. Certainly Simon's solution does help convince it that it should be treating it as an array of arrays.

      I'll try the code you gave on the server in question tomorrow if I get time. Its a Linux box running perl 5.5 or 5.6 I dont remember the exact version.

      Thanks for your suggestions though.

      ---If it doesn't fit use a bigger hammer

Re: Data::Dumper Dilema
by simon.proctor (Vicar) on Feb 01, 2002 at 14:29 UTC
    You need to surround your additional calls with []. That should fix your problems. Something along the lines of:
    print Data::Dumper->Dump([$sb->[1]]);
    Hope that helps :)
      That fixed it. I'm getting the entire contents now thanks a lot ++

      ---If it doesn't fit use a bigger hammer

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-24 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found