Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: uninitialized string variable

by jjw92 (Novice)
on Aug 13, 2010 at 16:50 UTC ( [id://854951]=note: print w/replies, xml ) Need Help??


in reply to Re^2: uninitialized string variable
in thread uninitialized string variable

Yes, I am reasonably new to Perl, I am using Text::CSV::Slurp. The @ is used because $data is returned as a reference to an array of hashes, so:

@$data[0] -> {"data"}

Returns the first hash inside data, and points to the value associated with the key "data". *unless my understanding is wrong*

Replies are listed 'Best First'.
Re^4: uninitialized string variable
by suhailck (Friar) on Aug 13, 2010 at 17:57 UTC
    If $data is a reference to an AoH, then the following is sufficient
    perl -le '$data=[{ data => 1 },{a => 2, b => 3}];print $$data[0]->{dat +a}' 1
    And if you try the below code, you can see the error u got,
    perl -wle '$data=[{ data => 1 },{a => 2, b => 3}];print $$data[1]->{c +}' Use of uninitialized value in print at -e line 1.
    see perldsc for details.

      $ perl -wE '$data = [{"data" => "foo"}]; say @$data[0]->{"data"}' foo
      However, I would write that as
      $$data[0]{"data"}
      which doesn't use an unneeded arrow, and uses the more usual dereference sigil. (I guess this case isn't caught by "Scalar value @... is better written as $...).
        Isn't it the same as $data->[0]{data} I'd use?
Re^4: uninitialized string variable
by JediWizard (Deacon) on Aug 14, 2010 at 02:38 UTC

    This is only an opinion, but:

    I would rather keep to a single syntax for de-referencing when possible. As with most things in perl, there is more than one way to do it, and different ways have different strengths. In some cases using the @$arrayref; %$hashref; $$scalarref may be better, and in others $arrayref->[0]; $hashref->{key}; may be better. That having been said... @$data[$i]->{key} uses two different syntax's for de-referecing, in the same statement. I'd say that $data->[$i]{key} is much clearer.


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://854951]
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: (5)
As of 2024-04-23 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found