A question about elegant ways to split a sequence into groups of identical characters and a response by Roy Johnson using the idiom --$| with grep peaked my interest. I asked a follow-up question about the use of the idiom and was directed to Perl Idoms Explained -$|++, but this didn't really answer my question.

So I would like to explore this idiom a little further. The original question asked how to split a string like 'xx556xx' into groups of identical characters e.g. xx, 55, 6, xx. Roy Johnson proposed the idiom --$| with grep as a possible solution. I have used the idiom $|++ in scripts written for Windows to force fflush after every print statement. So I wondered if ++$| could have been used with grep to solve the problem above. My code for looking at $| with incrementing or deincrementing with either pre or post implementation is shown below.

my $str = 'xx556xx'; my @x = grep --$|, $str =~ m/((.)\2*)/g; report('pre-deincrement', @x); my @x = grep $|--, $str =~ m/((.)\2*)/g; report('post-deincrement', @x); my @x = grep ++$|, $str =~ m/((.)\2*)/g; report('pre-increment', @x); my @x = grep $|++, $str =~ m/((.)\2*)/g; report('post-increment', @x); sub report{ my ($type, @y) = @_; print "\n\nautoflush = ", $|, " , $type\n"; print join ',', @y; }

The results are:

autoflush = 0 , pre-deincrement
xx,55,6,xx

autoflush = 0 , post-deincrement
x,5,6,x

autoflush = 1 , pre-increment
xx,x,55,5,6,6,xx,x

autoflush = 1 , post-increment
xx,x,55,5,6,6,xx,x

Only the pre-deincrement works although the post-deincrement returns a list of the unique characters from the sequence.

The list xx,x,55,5,6,6,xx,x is the list returned by m/((.)\2*)/g so pre- and post-incrementation don't appear to be doing anything other than returning that list from the match statement.

I thought autoflush could only have values of 0 or 1 and am confussed by the results obtained. I understand that incrementing zero will return one, but what does deincrementing zero in $| return?

Does $| get reset to the default value after each list element is examined by grep? The pre- and post-increment results suggest this is not the case.

It appears I'm going to have to be much more careful about how I use $|.

Update

Thanks for the responses. They really helped. I was just playing with Data::Dumper, which also helped me understand how $| sets and resets with various applications of $|++ and $|--. It looks like using increment and deincrement on $| can allow for some pretty neat effects, but like a firecracker can blow up in your face if you're not careful :-).

Mike


In reply to $| pre and post increment and deincrement behavior by bioMan

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.