Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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]
[Corion]: choroba: Yeah - that's why the "old" airports of Berlin were cool, as they'd been built before ;)

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2017-02-23 12:33 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (346 votes). Check out past polls.