Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5841621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:54:48+00:00 2026-05-22T11:54:48+00:00

Perl 5.14 gives us the extended each function which operates on arrays as well

  • 0

Perl 5.14 gives us the extended each function which operates on arrays as well as hashes:

When called in list context, returns a 2-element list consisting of the key and value for the next element of a hash, or the index and value for the next element of an array, so that you can iterate over it. When called in scalar context, returns only the key (not the value) in a hash, or the index in an array.

An example using list context works:

perl -E 'say $^V'

v5.14.0

perl -E '@a = (1..10); while (my ($i, $elem) = each @a) {say "\$a[$i] = $elem"}'

$a[0] = 1
$a[1] = 2
$a[2] = 3
$a[3] = 4
$a[4] = 5
$a[5] = 6
$a[6] = 7
$a[7] = 8
$a[8] = 9
$a[9] = 10

however in scalar context, I get nothing:

perl -E '@a = (1..10); while (my $i = each @a) {say $i}'

Can anyone offer any insight? I have a feeling that this will be a head slapper when someone points out my error, but perhaps not.

Edit: In fact the while loop has nothing to do with it:

perl -E '@a = (1..10); $i = each @array; say $i'

gives no output either. s'@array'@a' oops.

Edit 2:

As per daxim’s comment:

perl -MDevel::Peek -E'@a = (1..10); Dump each @a'

SV = IV(0x161ce58) at 0x161ce68
  REFCNT = 1
  FLAGS = (TEMP,IOK,pIOK)
  IV = 0

however I have no idea what that tells me.

Edit 3:

It seems that the loop exits because the first index is 0, or false. I have filed a bug ( http://rt.perl.org/rt3/Ticket/Display.html?id=90888 ) since this doesn’t seem to be the desired behavior.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:54:48+00:00Added an answer on May 22, 2026 at 11:54 am

    You need to do while (defined( my $i = each @array )) { say $i } otherwise it’ll stop at the first index (0) since it is false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It appears that using perldoc perl gives the list of, e.g. perlre, perlvar, etc.
Perl and PHP do this with backticks. For example, $output = `ls`; Returns a
I made a login script in Perl which has to run on Mac OS
I've got a string that contains multiple substrings, each of which contains one or
How do you get Perl to stop and give a stack trace when you
How do I get Perl to read the contents of a given directory into
Perl has OOP features, but they are somewhat rarely used. How do you create
PERL? Perl? perl? What's good style? I know the answer—I just wanted to make
Perl seems to be killing my array whenever I read a file: my @files
Perl uses reference counting for GC, and it's quite easy to make a circular

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.