Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Sad newbie question about Data::Dumper and array references..

by Clownburner (Monk)
on Mar 23, 2001 at 11:57 UTC ( [id://66588]=perlquestion: print w/replies, xml ) Need Help??

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

Ok, before you click that -- button, cut me some slack here, I'm still new.

I'm misunderstanding Data::Dumper's output apparently, because I'm unable to look at an array reference properly. I've read the docs, but maybe I'm just too tired to see what's wrong. If someone could quickly point out my mistake, I'd be forever grateful.

I'm doing this:
use Data::Dumper; print Dumper($data);
And getting back this:
$VAR1 = [ '0.001915', '0.000000', '0.007453', '0.000000' ];
So why does this
print $data[0];
return nothing? Shouldn't it return '0.001915'??

Thanks in advance. Sorry if this is Yet Another Ignorant Perl Question.
Signature void where prohibited by law.

Replies are listed 'Best First'.
Re: Sad newbie question about Data::Dumper and array references..
by busunsl (Vicar) on Mar 23, 2001 at 12:07 UTC
    No, it shouldn't, because $data is holding a reference to an array.
    Try
    print $data->[0];
      other than style is there a reason to prefer

      print $data->[0];

      over

      print @{$data}[0];

      --
      my $chainsaw = 'Perl';

        Nope

        I just find print $data->[0] looks more like dereferencing.

        not that i can think of. there might be others to correct me, though.

         print @{$data}[0] is too many bracey-curly-making-me-hit-shift characters. they perform the same tasks.

        It becomes apparent with more complex data structures, such as AoAoAoAoAoAoA's or HoHoHoHoHoHoHoH's, or any mutant variation thereof.      $data->[0]->[1]->[3]->[1]->[41]->[6] over      ${${${${${${$data}[0]}[1]}[3]}[1]}[41]}[6] Unless you're really good at counting brackets, I'd stick with the C-style dereferencing via '->'
Re: Sad newbie question about Data::Dumper and array references..
by Boldra (Deacon) on Mar 23, 2001 at 18:22 UTC
    I'm surprised noone has told you to do this:

    #!/usr/bin/perl -w

    you would then have got this:
    Use of uninitialized value in print at - line nn.
    (because @data is undefined)

    Which should always make you suspicious.
Re: Sad newbie question about Data::Dumper and array references..
by andye (Curate) on Mar 23, 2001 at 15:20 UTC
Re: Sad newbie question about Data::Dumper and array references..
by I0 (Priest) on Mar 23, 2001 at 12:09 UTC
    print $data->[0];
Re: Sad newbie question about Data::Dumper and array references..
by Anonymous Monk on Mar 25, 2001 at 00:12 UTC
    The problem is that you are confusing a reference to an array with the array itself. Your $data variable is a scalar (not an array) whose value is a reference to an array. You need to use indirection to get at the elements of the array. These are all equivalent:
    $$data[0] ${$data}[0] $data->[0]
    Personally, I prefer the third form but if you're trying to create obfuscated code use the first form.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-24 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found