Hi. I am seeing some strange behaviour using Perl's pattern matching and capturing. It may be due to my lack of understanding of how it works, but I've been staring at it for a solid day and a half without success. Basically the pattern search is only being done once for the same string, even though I want to loop through an array of strings (some of the array elements are the same). Here's the test program:

#!/usr/bin/perl use strict ; ######################################################## ######## START MAIN #################### my @STAGE=( '/stage/core/HPUX/PA-RISC-64/64bit/rdbms/10.2.0.1.0/dvd2/clusterwar +e', '/stage/core/HPUX/PA-RISC-64/64bit/rdbms/patches/10.2.0.4.0/Disk1' ) ; my $i ; my $j ; for ($i=0; $i<=5; $i++){ my $o_vers ; $j = $i - int($i/2)*2 ; $o_vers = $1 if $STAGE[$j] =~ ?/rdbms/([\d.]+)/? ; $o_vers = $1 if $STAGE[$j] =~ ?/rdbms/patches/([\d.]+)/? ; print "Iteration# $i: Version is: $o_vers\n" ; }

This produces the following output:

[root@moops perl]# t10f.pl Iteration# 0: Version is: 10.2.0.1.0 Iteration# 1: Version is: 10.2.0.4.0 Iteration# 2: Version is: Iteration# 3: Version is: Iteration# 4: Version is: Iteration# 5: Version is: [root@moops perl]#

-Why is the version not being printed after the first 2 iterations through the loop? -Please help explain this behaviour to this Perl simpleton... Thanks in advance, Neville


In reply to Pattern matching & capturing strange behaviour. by NevilleG

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.