DW10 has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to automatically find the index of a certain array value by searching through an array with a for loop. This results in either the for loop repeating each index, or when I use 'last' or 'exit' the index counter doesn't work. Any help would be appreciated. Here's a simple code for what I've been attempting: Ideally, I want my output to be: F is at index 5
#!/usr/bin/perl #use strict; #use warnings; my @text = qw/A B C D E F G H I J/; for my $element (@text){ if ($element = "F") { print "$element is at index: $index_number\n"; $index_number++; } #last if ($element = "F") }
Either prints the text 10 times, each with different index or if I use last, prints once (good) but doesn't give an index (bad).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Index number repeating in for loop
by AnomalousMonk (Archbishop) on Jun 28, 2013 at 21:30 UTC | |
by DW10 (Initiate) on Jun 28, 2013 at 21:47 UTC | |
by AnomalousMonk (Archbishop) on Jun 28, 2013 at 21:59 UTC | |
|
Re: Index number repeating in for loop
by davido (Cardinal) on Jun 28, 2013 at 22:13 UTC | |
|
Re: Index number repeating in for loop
by johngg (Canon) on Jun 28, 2013 at 21:38 UTC | |
|
Re: Index number repeating in for loop
by hdb (Monsignor) on Jun 28, 2013 at 21:27 UTC | |
by LanX (Saint) on Jun 28, 2013 at 21:33 UTC |