Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Printing an array using while loop

by Anonymous Monk
on Oct 07, 2018 at 20:19 UTC ( #1223643=note: print w/replies, xml ) Need Help??


in reply to Re: Printing an array using while loop
in thread Printing an array using while loop

# The solution to your problem is to put handles <> on your array: while(my $item = <@array>) { print("$item\n"); }
  • Comment on Re^2: Printing an array using while loop

Replies are listed 'Best First'.
Re^3: Printing an array using while loop
by haukex (Chancellor) on Oct 07, 2018 at 20:40 UTC
    put handles <> on your array: while(my $item = <@array>) { print("$item\n"); }

    Although this works in this particular case, I wouldn't recommend it: The <> actually doesn't do anything with "handles" (like <$filehandle>, which is readline), instead it's glob in disguise, and glob has several caveats. I would suggest a regular foreach, or perhaps each (Perl >= 5.12, which was not available in 2007, at the time of this thread).

    $ perl -MO=Deparse -e 'while(my $item = <@array>) { print "$item\n" }' while (defined(my $item = glob(join $", @array))) { do { use File::Glob (); print "$item\n" }; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1223643]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2019-07-17 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?