Hi,
This is my first question on Perl Monks.
I tried to follow all guidelines but if you spot any deviations please correct me.

I was trying a sample code just to understand the basics of match operator(=~)
but could not find out the reason for unepected output in different parts of the program given below.
#!/usr/bin/perl -wl use strict; use warnings; =head This is perl 5, version 14, subversion 2 (v5.14.2) built for i686-linu +x-gnu-thread-multi-64int (with 57 registered patches, see perl -V for more detail) =cut my $string = "blink mink chink sink wink dink nn kkkk"; print "Phase-1:"; my @cnt_arr = $string =~ /ink/g; print "count array = @cnt_arr"; print "substring is found ", scalar(@cnt_arr), " times"; #prints 6 print "\nPhase-2:"; #attempts to do the same without the use of @cnt_arr print "substring is found ", scalar($string =~ /ink/g), " times"; #prints only 1 print "after 1, string = $string"; print "substring is found ", scalar(($string =~ /ink/g)), " times"; #an attempt to convert to array context, but still prints 1 print "after 2, string = $string"; print "\nPhase-3:"; @cnt_arr = $string =~ /ink/g; print "count array = @cnt_arr"; print "substring is found ", scalar(@cnt_arr), " times"; #now prints 4 !! print "after 3, string = $string"; # $string content remains the same throughout but match operator #is giving different results in phase-1 and phase-3

Phase-1:
count array = ink ink ink ink ink ink
substring is found 6 times

Phase-2:
substring is found 1 times
after 1, string = blink mink chink sink wink dink nn kkkk
substring is found 1 times
after 2, string = blink mink chink sink wink dink nn kkkk

Phase-3:
count array = ink ink ink ink
substring is found 4 times
after 3, string = blink mink chink sink wink dink nn kkkk

The output of match operator in phase-3 is showing only 4 substring matches even though the content of variable $string remains the same.

Can you please explain why this is happening and how to overcome it so that the number of substring matches is consistent in phase-1 and phase-3.


In reply to Match operator giving unexpected output by ysreenu

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.