Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://355774]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2018-01-19 10:47 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (217 votes). Check out past polls.