Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Finding the index of a specific element in an array.

by i5513 (Pilgrim)
on Jan 24, 2012 at 23:28 UTC ( #949790=note: print w/replies, xml ) Need Help??

in reply to Finding the index of a specific element in an array.

Using regex features, maybe a good homework task:

use strict; use warnings; my @a=(2,4,7,5,8); my ($position, $spaces); # Elements have not " " ("@a" concatenate with spaces) if ("@a" =~ /(?:^|\ ) # Element searched maybe the first or not \K # Count previous group matched within ${^PREMATCH} var (?{ $position=pos; $spaces= () = ${^PREMATCH} =~ m, ,g; }) # Next group will be that which are we searching, s +o # we take note about where is it and how many space +s # should we ignore ("@a" add spaces) (8) # Element searched (?:\ |$) # Element searched maybe the last or not /xpg) { print "Position:" ,$position - $spaces, "\n"; }

In your example you can use index function:

my $pos=index (join ("",@array),7);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://949790]
[erix]: ( also: UniCorn Throne )
[zentara]: erix I hope both tusks pointed in the same direction :-)
LanX "♪..♫ Last night a DJ killed my wife ♪..♫ "
zentara...oO 1 more reason I prefer Perl to Python is I can't believe how long Python takes to compile, and you can't disable the myriad of tests
[LanX]: so that's where you've been the last two years? Compiling Python?
[zentara]: LanX no! I was learning why Krishna made me into a unicorn. :-)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2017-03-30 16:43 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (361 votes). Check out past polls.