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).
In reply to Index number repeating in for loop by DW10
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |