Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Functions in Perl

by dash2 (Hermit)
on May 23, 2004 at 22:02 UTC ( #355774=note: print w/ replies, xml ) Need Help??


in reply to Functions in Perl

One advantage (and sometimes, drawback) of Perl is that you can do things fast.

sub showargs{ print map {"$_ $_[$_]\n"} 0..$#_}

Doing things fast often leads to code that other people can't read. So don't assume this is good practice. But it's fun, and for a throwaway script it's acceptable.

An explanation:

map {code} @array runs code on every element of an array, just like a foreach loop. Inside the code block, each element of the array is aliased to $_.

0..$#array creates a list going from 0, to the index of the last element in @array. So 0..$#_ goes from 0 to the last element in @_, which is your array of arguments.

Finally, "$_ $_[$_]" prints first the aliased element, then the corresponding member of @_.

I'm sure some other monks can get this faster (and more obscure....)

A massive flamewar beneath your chosen depth has not been shown here


Comment on Re: Functions in Perl
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-08-30 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls