Thanks again. I think you have come up with the simplest way to do this, although I modified the code slightly (below - doesn't run but is a simplified example of what I was doing). Originally I was wondering if there was a built-in technique that I was missing, but it appears not.

Thanks again

#!/usr/bin/perl -ws use strict; open my $fh, '<', 'file.txt' or die $!; my @lines = ( "this is line 1; foo \n", "this is line 2: bar \n", "this is line 3: foobar \n", ); print "PRINTING FROM FH\n"; working_routine($fh); print "\n\nPRINTING FROM ARRAY\n"; working_routine(\@lines); sub create_iter { my $arg = shift; if (ref $arg eq 'GLOB') { return <$arg>; } elsif (ref $arg eq 'ARRAY') { return shift @$arg; } else { die "Unknown type\n"} } sub working_routine{ my ($data) = @_; my $flag = 0; while(my $line = create_iter($data)){ if($line =~ /matches something/){ next; } elsif($line =~ /matches something else/ and ! $flag){ create_iter($data); my $newline = create_iter($data); dosomethingto($newline); } elsif($line =~ /gotcha/ && $flag){ morework($line); } else{ next; } } }

In reply to Re^4: Loop through array or filehandle by markdibley
in thread Loop through array or filehandle by markdibley

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.