Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: grep { } positional number of element

by Your Mother (Archbishop)
on Oct 23, 2022 at 21:23 UTC ( [id://11147615]=note: print w/replies, xml ) Need Help??


in reply to grep { } positional number of element

grep only goes through the actual elements of whatever is passed to it. No index is passed. You might want something like–

my @myArray = qw(alfa beta gamma); grep { print "$myArray[$_] at $_\n" } 0 .. $#myArray;

But that is *atrocious* style. grep is for filtering, not iterating. Use a for loop instead. Terse postfix version, you should generally prefer the expanded with a block–

print "$myArray[$_] at $_\n" for 0 .. $#myArray;

Replies are listed 'Best First'.
Re^2: grep { } positional number of element
by Fletch (Bishop) on Oct 24, 2022 at 02:12 UTC

    Seconding the style condemnation; similarly using map would be just as incorrect.

    Use grep to filter, map to transform, and for to iterate (or one of the List::* modules for anything else more specialized like a reduce).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2026-02-09 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.