in reply to Re^2: Finding target unspecified value
in thread Finding target unspecified value

I am working on the next part of this assignment, and having an issue utilizing an else statement. Using the above coding already in place I inputted this coding
print "Enter a target value to search for:"; use List::MoreUtils; chomp (my $search = <STDIN>); my $index = List::MoreUtils::first_index {$_ eq $search} @array; if ($search) { print "The last occurence of $search is at index: $index\n"; } else { print "Target is not found\n"; }
So the code runs, and asks for a target to search for. If its a number displayed, and I enter that number it tells me which index it is. If I enter a number that is not displayed I should see - Target is not found. So for instance the number 100 is not displayed. I see - The last occurrence of 100 is at index -1.

Replies are listed 'Best First'.
Re^4: Finding target unspecified value
by kcott (Archbishop) on Oct 21, 2013 at 05:58 UTC

    G'day Latnam,

    Welcome to the monastery.

    " If I enter a number that is not displayed I should see - Target is not found. So for instance the number 100 is not displayed. I see - The last occurrence of 100 is at index -1."

    Your problem is with this condition:

    if ($search) { ...

    Looking at the documentation for List::MoreUtils, under first_index, you'll see "Returns -1 if no such item could be found."; right beneath that, you'll see another function, last_index, which is probably what you want for "The last occurrence ...". [note corrected spelling]

    There are two instances where $search might be FALSE: just a newline is entered at the prompt, chomp removes the newline, $search now equals ''; a zero and a newline are entered at the prompt, chomp removes the newline, $search now equals '0'. Given zero might be a perfectly valid number to search for, if you actually wanted to check that something (other than just a newline) was entered, length would be a better check (i.e. if (length $search) { ...).

    Finally, writing List::MoreUtils::function_name is unnecessary: see how it's shown in the doco.

    Putting all that together, here's all the elements I've discussed: you'll probably just want a subset ot these.

    #!/usr/bin/env perl use strict; use warnings; use List::MoreUtils qw{first_index last_index}; my @array_to_search = qw{ 0 2 4 2 0 }; print 'Search: '; chomp( my $search = <STDIN> ); if (length $search) { my $index_1 = first_index { $_ eq $search } @array_to_search; if ($index_1 >= 0) { my $index_N = last_index { $_ eq $search } @array_to_search; print "First occurrence of '$search' at index: $index_1\n"; print "Last occurrence of '$search' at index: $index_N\n"; } else { print "'$search' not found.\n"; } } else { print "Nothing to search for!\n"; }

    Some sample runs:

    Search: Nothing to search for!
    Search: 100 '100' not found.
    Search: 0 First occurrence of '0' at index: 0 Last occurrence of '0' at index: 4
    Search: 2 First occurrence of '2' at index: 1 Last occurrence of '2' at index: 3
    Search: 4 First occurrence of '4' at index: 2 Last occurrence of '4' at index: 2

    -- Ken

Re^4: Finding target unspecified value
by Lennotoecom (Pilgrim) on Oct 20, 2013 at 23:23 UTC
    @a = (12, 18, 56, 76, 99); $in = <STDIN>; for(0..$#a){ die "found at $_\n" if $in == $a[$_]; } print "not found\n";
    or even prettier:
    @a = (12, 18, 56, 76, 99); $in = <STDIN>; sub{die "found at $_\n" if $in == $a[$_]}->() for(0..$#a); print "not found\n";
    or
    @a = (12, 18, 56, 76, 99); $in = <STDIN>; for(0..$#a){ $i = $_ if $in == $a[$_]; } $i == 0 ? print "not found\n" : print "found at $i\n";
Re^4: Finding target unspecified value
by Latnam (Novice) on Oct 20, 2013 at 23:39 UTC
    I need to rework this code again. The code works in finding a number at the index point. What I didn't account for is multiple accounts of the same number. So if my numbers were 10 22 16 13 22 19, and my target number is 22 it would say the last occurrence of 22 is at index 1, and it should be index 4.
      this:
      @a = (12, 18, 56, 76, 99); $in = <STDIN>; for(0..$#a){ $i = $_ if $in == $a[$_]; } $i == 0 ? print "not found\n" : print "found at $i\n";