Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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]
[stevieb]: I'm learning that no matter how good a piece of software is at what it's supposed to do, it's a piece of shit without providing any API documentation. (Talking about C++ here currently). Seriously... write software for an embedded platform and make...
[stevieb]: guess based on the sparse examples, and the header files that are spread out illogically?
[stevieb]: document the public facing functions/methods people... ALL OF THEM.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2017-05-28 23:43 GMT
Find Nodes?
    Voting Booth?