Linux.com

Re:The Perl way of iterating over an array

Posted by: Administrator on July 08, 2004 03:34 PM
<TT>I would do it more like

@users = (eric, billy, eddie, yngwie);
$\ = "\n" ; print for @users;

which works by setting the output record separator to a newline, then prints all members of a list using the implicit $_ variable, and separates them with the output record separator.

Just to show that you can do a lot with perl's carefully chosen 'default' variables mentioned in the article.

If you replace the middle semicolon with a &&, it becomes '($\ = "\n") && print for @users;' quite close to unreadable for a perl newbie, but nevertheless a valid perl one-liner.

BTW: the loop in de code above is not needed, ie. it is allowed to remove the 'for'.
</TT>

#

Return to SysAdmin to SysAdmin: Approaching Perl