Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Array indices

by sauoq (Abbot)
on Sep 14, 2002 at 06:08 UTC ( [id://197811]=note: print w/replies, xml ) Need Help??


in reply to Array indices

For the most part you can get by without using indices at all. Here's a version of your code that avoids them.

my @firstnames = qw(john jacob jingle heimer schmidt); my @lastnames = qw(brown black gray greene purple); my @fullnames; for (@firstnames) { push @lastnames, my $lastname = shift @lastnames; push @fullnames, "$_ $lastname"; }

I imagine it may very well be possible to get by without them entirely but it would hardly be worthwhile to try. Retrieving an element in the middle of an array without using indices will always be an O(N) operation but retrieving it by index is O(1).

A good programmer may not need them but a good program often will.

Update: At Limbic~Region's request:

# Save the indices of elements in @search that match $PAT $i++,/$PAT/ and push @indices, $i-1 for @search;

-sauoq
"My two cents aren't worth a dime.";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-03-19 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found