Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Strange behavior of array in scalar context

by xavier8854 (Novice)
on Jan 26, 2026 at 10:13 UTC ( [id://11167232]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
Why does scalar(@array) return 1 for an empty array ?
Code :
my @executions = $jobInfo->{'content'}{'executions'}; print Dumper (@executions, scalar(@executions));
result is :
$VAR1 = []; $VAR2 = 1;
I use this syntax regulary, never happened.
Thanks for your wisdom,
FTR, this is perl 5.38.2 on Ubuntu 22.0.4
Xavier

Replies are listed 'Best First'.
Re: Strange behavior of array in scalar context
by Corion (Patriarch) on Jan 26, 2026 at 10:19 UTC

    First of all, your debugging is flawed. You want to pass arrays and hashes as references to Data::Dumper:

    print Dumper \@executions, scalar(@executions);

    Second, you are copying the reference into @executions, not the contents. This means that @executions will only ever contain a single element:

    my @executions = $jobInfo->{'content'}{'executions'};

    What you likely wanted is to copy the array items:

    my @executions = $jobInfo->{'content'}{'executions'}->@*; # requires P +erl 5.36+ # Alternatively my @executions = @{ $jobInfo->{'content'}{'executions'} };
Re: Strange behavior of array in scalar context
by Anonymous Monk on Jan 27, 2026 at 14:50 UTC
    my @executions = $jobInfo->{'content'}{'executions'};

    This puts whatever happens to be returned on the right-hand side into the first slot of the array @executions. It just happens that what you get is from the RHS not an empty array, but an empty arrayref. And this arrayref happens to occupy the first slot of the array @executions. Therefore, the array @executions has one element.

    See what print Dumper \@executions; returns instead.

    To turn an arrayref into an array (i.e. copy its contents to an array), you need to dereference it, or you can just keep using it as an arrayref. I usually do the latter.

    # Dereference and copy contents my @executions = @{ $jobInfo->{'content'}{'executions'} }; # Just use it as an arrayref my $executions = $jobInfo->{'content'}{'executions'}; print scalar @$executions, "\n"; print "element 0 is ", $executions->[0], "\n";
Re: Strange behavior of array in scalar context
by xavier8854 (Novice) on Jan 26, 2026 at 10:19 UTC
    Stangely, the syntax :
    $#executions
    returns correctly 0

      That indicates that the array does have one element.

      my @x = (); my @y = (99); print "x count is $#x, y count is $#y\n";

      results in

      x count is -1, y count is 0

      🦛

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2026-02-09 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.