You are still reading your data one line at a time so your pattern matches are matching against one line at a time.

To see what is being read, you might experiment with the following program:

#!/usr/bin/perl -w use strict; use warnings; while (<DATA>) { print "start of loop\n"; print "\$_ = \"$_\"\n"; } __DATA__ <a>a, b</a> <b>a,b</b> <b>a,b </b>

To make <DATA> read all your DATA instead of just one line, you can set $/ before your while loop, as follows. Note the use of a block ({ }) and local so that $/ isn't affected elsewhere in the program.

#!/usr/bin/perl -w use strict; use warnings; { local $/; while (<DATA>) { print "start of loop\n"; print "\$_ = \"$_\"\n"; } } __DATA__ <a>a, b</a> <b>a,b</b> <b>a,b </b>

With $/ set to undef (which is what local $/ does), there is no need for the while loop - all the data is read on the first iteration. You can read all the data into a single variable as follows:

#!/usr/bin/perl -w use strict; use warnings; my $var = do { local $/; <DATA> }; print "\$var = \"$var\""; __DATA__ <a>a, b</a> <b>a,b</b> <b>a,b </b>

Then you can substitute against this variable as follows:

$var =~ s!<a>(.*?),(.*?)</a>!<a>$1A$2</a>!gs;

When you get that working, you might want to try with the following data:

__DATA__ This is to test. <a>a, b</a> <b>a,b</b> <b>a,b </b> <a>ab</a> <c>a,b</c> <a>a,b</a>

In reply to Re^3: trying to do a simple search by ig
in thread trying to do a simple search by texuser74

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.