in reply to Finding target unspecified value

You have numerous good answers above, but here are a few pointers, anyway:
  1. saying things like "I keep getting errors and I don't understand" doesn't help you NOR does it help us to help you. Quote the errors, verbatim; ditto warnings (and learn the difference).
     
  2. Unless this is homework, it seems to me likely that there's no useful purpose to redundant printing of the array NOR any useful end to generating the array with random numbers (between 10 and 100 or any other range). OTOH, if this is homework, level with us in the first place.
     
  3. This may be a 'duh!' on me, but I found I needed multiple readings of your narrative and code to feel confident that I could offer help. I was delighted to return and find you already had sample code... but had this been a busier day, I might well have (figuratively) thrown up my hands and said "If OP can't be clearer (i.e. more precise and un-ambiguous) than that, I can't be bothered to try to help."
     

Welcome to the Monastery, Latnam; your professed ambition to help others will stand you in good stead.

Replies are listed 'Best First'.
Re^2: Finding target unspecified value
by Latnam (Novice) on Oct 20, 2013 at 22:52 UTC
    This is indeed homework I am struggling with. I am attending a local community college, and I am interested in learning Perl. My professor has been helpful, and suggested to look for outside resources into understanding Perl outside of class. I don't wish to deceive anyone here by my questions, but at times I quite frankly don't understand how the coding works. I apologize for not being upfront with everyone here in the monastery. I will ensure that my questions are indeed clearer, and post what the output message I am reading when inputting coding.
      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.

        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

        @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";
        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.