Amoe has asked for the wisdom of the Perl Monks concerning the following question:

This should be a really easy question, and thus I probably shouldn't trouble the Monastery with it, but it's giving me hell. Super Search turned up nothing for me, suprisingly (probably bad searching technique). I can't figure out how to loop over a list and skip with next until an arbitrary string entered by the user matches the element. After that string is matched (should only happen once) we don't care about matching it any more, so basically all elements after the matching element are used. Here's some code to demonstrate the gist of what I want to do, but it doesn't work.
use strict; use warnings; use Getopt::Std; my %switches; getopts('s:', \%switches); my @array = qw(the quick fox jumped over the lazy dog); ITEM: foreach my $item (@array) { my $seen; if ($switches{s}) { ($seen = $switches{s} eq $item) if (!$seen); next ITEM if (!$seen); } print "$item\n"; }
The string to match is the argument to the -s switch, by the way.

--
my one true love

Replies are listed 'Best First'.
(Ovid) Re: skipping until an alement is matched
by Ovid (Cardinal) on Nov 01, 2001 at 23:53 UTC

    If I understood you correctly, here's a quick way to match what's in an array:

    my $switch = 'quick'; my @array = qw(the quick fox jumped over the lazy dog); my %matches = map {$_,1} @array; my $match = exists $matches{ $switch } ? 1 : 0; print $match;

    Note that this technique is only useful if you have many items to match. Otherwise, use grep:

    my $match = grep { /$switch/ } @array;

    With the latter, $match is set to zero if no match is found. Otherwise, it's set to one.

    Cheers,
    Ovid

    Update: Rereading my response and I've concluded that I haven't had enough coffee. I wonder whose question I was answering? :)

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: skipping until an element is matched
by Vavoom (Scribe) on Nov 01, 2001 at 23:54 UTC
    You need to move the my $seen; line out of the loop. You are effectively clearing $seen each time you start the loop. Otherwise your code seems to do what you want.

    Vavoom
Re: skipping until an alement is matched
by dvergin (Monsignor) on Nov 02, 2001 at 10:42 UTC
    Other posts have offered some good counsel. But for the sake of discussion, let's look at the approach you were working on and see what a solution using that model would look like.

    Here's the bare logic of a stripped-down solution modelled after your approach (let's make it self-contained for testing purposes):

    my $goal = 'over'; my @array = qw(the quick fox jumped over the lazy dog); foreach my $item (@array) { next unless $item =~ /$goal/; print $item; }
    But we're continuing to loop even after we find the correct element. Wasted work. So we need to track things a little more carefully. Perhaps something like this (leaving out the loop variable just for fun):
    my $goal = 'over'; my @array = qw(the quick fox jumped over the lazy dog); for (@array) { next unless /$goal/; print $_; last }
    But what if you still want to know after the loop if the search succeeded and use that value...
    my $goal = 'over'; my @array = qw(the quick fox jumped over the lazy dog); my $found_item; foreach my $item (@array) { next unless $item =~ /$goal/; $found_item = $item; last; } if ($found_item) { print $found_item; # do other stuff }
    Or you could wrap the if block inside the loop like this:
    my $goal = 'over'; my @array = qw(the quick fox jumped over the lazy dog); my $found; foreach my $item (@array) { if ($item =~ /$goal/) { $found = 1; # in case we need it later print $item; # do other stuff last; } }
    In this case $item goes out of scope at the end of the loop and is irretrevable for use after that. Which is tidy if you don't need it later or annoying if you do.

    Somewhere among those Ways To Do It you should find something that matches your need. HTH ...David

Re: skipping until an alement is matched
by Purdy (Hermit) on Nov 01, 2001 at 23:49 UTC
    This works for me - if I execute this with 'fox', then I just get "fox" as the output (and not "the" and "quick" in front of it).