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

I'm not completely up on references/dereferences and was hoping you monks could assist me.

if ($sth->rows > 0) { $next = @{$nextimg}; print "next is $next"; print "next2 is @{$nextimg}"; }
The above prints next is 1 and next2 is 77 with my code. I'm trying to get the 77, of course, but I'm trying to store it in a scalar and can't seem to get it right. I'm assuming the 1 is the return code for "all good!".

How would I assign the scalar the value of the array?

Replies are listed 'Best First'.
Re: Dereferencing arrays
by bart (Canon) on May 31, 2008 at 20:36 UTC
    I'm assuming the 1 is the return code for "all good!".
    No... If you do
    $scalar = @array;
    then what you assign to $scalar is the count of items in @array (array in scalar context). Apparently it contains one item.

    What you probably want (it's hard to say with the rest of the code not there) is getting the first item of the array. One way to achieve that is using parens: they make the assignment happen in list contxt, and the first item is assigned to $scalar:

    ($scalar) = @array;

    Applying this rule to your code, I think

    ($next) = @{$nextimg};
    will do what you want.

    Another way is to use an array index:

    $scalar = $array[0];
    Hence
    $next = ${$nextimg}[0];
Re: Dereferencing arrays
by ikegami (Patriarch) on May 31, 2008 at 20:37 UTC

    I'm assuming the 1 is the return code for "all good!".

    No, it's the number of elements in the array. Just like
    my $num_ele = @array; gives the number of elements in array,
    my $num_ele = @{$array_ref}; gives the number of elements in the referenced array,

    "foo @array bar" means ("foo " . join($", @array) . " bar"), so
    "foo @{$array_ref} bar" means ("foo " . join($", @{$array_ref}) . " bar")

    How would I assign the scalar the value of the array?

    Assigning an array to a scalar makes no sense. A scalar can hold the length of an array, an element of an array, or a reference to an array, but not an array.

    Your currently doing the first and you already have a reference to the array, so I presume you want to assign an element of the array. More specifically, I'm going to assume you want to assign the first element of the array.

    To copy the first element, you'd do
    $ele = $array[0];
    for an array and therefore
    $ele = ${$array_ref}[0];
    for an array reference. Of you could use the cleaner alternate syntax
    $ele = $array_ref->[0];

    I'm not completely up on references/dereferences and was hoping you monks could assist me.

    Useful references:
    Dereferencing Syntax
    References Quick Reference

    Update: Minor cleanup. Added the references.

Re: Dereferencing arrays
by MidLifeXis (Monsignor) on May 31, 2008 at 20:34 UTC

    Try this:

    use Data::Dumper; print Dumper($nextimg);

    and see if that helps to explain things a little.

    update: My guess is

    that $nextimg is a reference to an array. When you cast it to an array, and then assign an array to a scalar, you get the size of that array. When you stringify an array, you get the array joined together by one of the special variables (sorry, my documentation is hosed right now, not certain which one it is).

    --MidLifeXis