in reply to Re^2: Alternative to Substr?
in thread Alternative to Substr?

You should really take another (closer) look at perlre (or perhaps perlretut). Particularly the bits about using the match operator (m//).

my $count = 0; foreach (@input) { if (/DosID(.{6})/ and $count++ % 2) { $dosid = $1; } }

Of course, this overwrites the value of $dosid each time it finds a match, so you might need to change the logic a bit.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^4: Alternative to Substr?
by tachyon (Chancellor) on Sep 29, 2004 at 12:35 UTC

    There is one serious bug in this code and a second less serious bug.

    If a line does not contain DosID= then the short circuit means that the increment on $count never occurs so the skip alternation will breakdown
    The original logic looked at even index elements, this looks a odd ones
      If a line does not contain DosID= then the short circuit means that the increment on $count never occurs so the skip alternation will breakdown

      But the original poster said "I am only interested in every second occurence" so I deliberately wrote it that way - counting occurrences _not_ lines.

      The original logic looked at even index elements, this looks a odd ones

      Yep. There's a difference. Mine looks at "every second occurence" as requested (i.e. it skips the first and displays the second and so on). If that's wrong then you fix it by changing the postincrement to preincrement.

      And why did you hide your text?

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg