NevilleG has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pattern matching & capturing strange behaviour.
by ikegami (Patriarch) on Mar 22, 2011 at 00:54 UTC | |
|
Re: Pattern matching & capturing strange behaviour.
by wind (Priest) on Mar 22, 2011 at 00:52 UTC | |
by NevilleG (Initiate) on Mar 22, 2011 at 01:40 UTC |