Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: uninitialized string variable

by JediWizard (Deacon)
on Aug 13, 2010 at 16:41 UTC ( #854949=note: print w/replies, xml ) Need Help??


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

I'm not actually sure why you need the @ at all. I believe that what you probably want is $data[$i]->{"whatever"}. It looks to me as though the poster thinks that the @ is needed because @data is and array (native php coder?). The use of ->{"something"} tells me that $data[$i] contains a hashref, not an array ref. Even if $data[$i] was an array ref, you'd only need the @ to either a. dereference the array, or b. use an array slice.


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

—Andy Warhol

Replies are listed 'Best First'.
Re^3: uninitialized string variable
by jjw92 (Novice) on Aug 13, 2010 at 16:50 UTC

    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*

      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 $...).

      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
Node Status?
node history
Node Type: note [id://854949]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2021-06-15 13:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (72 votes). Check out past polls.

    Notices?