I have a problem with matching a member of an array with a member of another array, the regex match statements in the following code is providing answers i am not clear about. Any suggestions.
#!/usr/bin/perl -w use strict; #Matching a member of texlines with a member of update* #Last example, Fourth, is what needs to be done my @updateloc = ("LakeOsouth", "LakeOeast", "LakeO2", "LakeO"); my @updateloc1 = ("LakeOsouth/stuf", "LakeOeast/stuf", "LakeO2/stuf", +"LakeO/stuf"); my @texlines = ("LakeOsouth", "LakeOeast", "LakeO2", "LakeO"); # Works fine print "Zero\n"; foreach my $update (@updateloc){ for my $lin (@texlines){ if ($lin =~ /$update\b/){ if ($update =~ /$lin/) { print "match 1 reverse $lin $update\n"; }else{ print "bad match $update $lin\n"; } } } } # Getting 3 bad ones print "First\n"; foreach my $update (@updateloc){ for my $lin (@texlines){ if ($lin =~ /$update/){ if ($update =~ /$lin/) { print "match 1 reverse $lin $update\n"; }else{ print "bad match $update $lin\n"; } } } } # Getting 2 bad ones; the digit on end of $update iso a char is ok? print "Second\n"; foreach my $update (@updateloc){ for my $lin (@texlines){ if ($lin =~ /$update/g){ if ($update =~ /$lin/g) { print "match 1 reverse $lin $update\n"; }else{ print "bad match $update $lin\n"; } } } } # No good, anything to do with previous matches (the /g)? print "Third\n"; foreach my $update (@updateloc){ for my $lin (@texlines){ if ($lin =~ /$update/g){ if ($update =~ /$lin/g) { print "match 1 reverse $lin $update\n"; }else{ print "bad match $update $lin\n"; } } } } # Back to number Zero, so Third is influenced by Second? print "Third and a half\n"; foreach my $update (@updateloc){ for my $lin (@texlines){ if ($lin =~ /$update/){ if ($update =~ /$lin/) { print "match 1 reverse $lin $update\n"; }else{ print "bad match $update $lin\n"; } } } } #Finally the problem # This is what i need: match 1 reverse, doesn't happen print "Fourth\n"; foreach my $update (@updateloc1){ for my $lin (@texlines){ if ($lin =~ /$update/g){ if ($update =~ /$lin/g) { print "match 1 reverse $lin $update\n"; }else{ print "bad match $update $lin\n"; } }else{ if ($update =~ /$lin/g) { print "\tmatch 2 reverse $lin $update\n"; }else{ print "\tno match $update $lin\n"; } } } }

In reply to Match a chunk by dhasaan

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.