in reply to uninitialized string variable

@$data[$i] is the same as @{$data}[$i], maybe that's what you wanted, maybe you wanted @{$data[$i]} instead

Replies are listed 'Best First'.
Re^2: uninitialized string variable
by JediWizard (Deacon) on Aug 13, 2010 at 16:41 UTC

    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

      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.

        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