Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Index number repeating in for loop

by AnomalousMonk (Canon)
on Jun 28, 2013 at 21:30 UTC ( #1041352=note: print w/replies, xml ) Need Help??


in reply to Index number repeating in for loop

A quick solution:

>perl -wMstrict -le "my @text = qw/A B C D E F G H I J/; ;; I: for my $i (0 .. $#text) { next I unless $text[$i] eq 'F'; print qq{$text[$i] is at index $i}; last I; } " F is at index 5

Updates: Some comments on your OPed code:

  •  $element = "F"   This expression assigns a string to a variable. Because the assigned string is always true, the true block of the if-statement is always executed. You may have intended to test for equality, and in that case you would need to use  eq to test for string equality (and not  == for numeric equality): e.g.  $element eq 'F'. (Already pointed out by hdb.)
  •  $index_number++;   This statement is within the true block of the if-statement which, had you been testing for equality to 'F', would only have been executed once for the entire set of example data. You would really need to count each and every index up to the target element value in order to get a valid index.
  •  $index_number   You have explicitly commented out warnings and strictures in your OPed code. The  $index_number variable should, IMHO, have been defined as a lexical (and perhaps even explicitly initialized to 0) outside the for-loop so it could be used inside the loop for index counting:
        my $index_number = 0;
    Without strictures and an explicit lexical definition, this variable is, horror of horrors, a global variable (shudder). (See johngg's second example here.)

And here's another code example using a new capability of each available for arrays since Perl version 5.12:

>perl -wMstrict -le "my @text = ('A' .. 'J'); ;; I: while (my ($i, $element) = each @text) { next I unless $element eq 'F'; print qq{$element is at index $i}; last I; } " F is at index 5

More: And here's another variation on grep as suggested by davido. Note that it does not stop when it finds the first target match, but searches all elements of the array.

>perl -wMstrict -le "my @text = qw/A B C D E F G H F I J/; ;; my @F_in_it = grep { $text[$_] eq 'F' } 0 .. $#text; ;; print qq{these indices have an 'F': @F_in_it}; " these indices have an 'F': 5 8

Replies are listed 'Best First'.
Re^2: Index number repeating in for loop
by DW10 (Initiate) on Jun 28, 2013 at 21:47 UTC

    Thank you all. That's incredibly helpful! I'll be sure to read up on Equality vs Assignment. Cheers

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1041352]
help
Chatterbox?
[Corion]: Nicholas Clark++ # read for a multichar block-delimiter by scanning for the last char, and only then check whether the whole delimiter was read.
[Corion]: At least if you're appending the data read to a larger buffer, this means you avoid the situation of "a delimiter was found but the other half has not been read yet"
choroba misses the context
[Corion]: choroba: Optimizing how Perl reads source code on startup (and then processes it line-by-line)
[Corion]: But in general, it seems to be an interesting approach I should think about - whenever I'm searching for something, to consider if I could search for the end of the token instead of the start of the token

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2016-12-06 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (107 votes). Check out past polls.