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


In reply to Re: skipping until an alement is matched by dvergin
in thread skipping until an alement is matched by Amoe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.