bioMan has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: $| pre and post increment and deincrement behavior
by ikegami (Patriarch) on Nov 30, 2005 at 19:43 UTC | |
|
Re: $| pre and post increment and deincrement behavior
by Roy Johnson (Monsignor) on Nov 30, 2005 at 19:47 UTC | |
|
Re: $| pre and post increment and deincrement behavior
by GrandFather (Saint) on Nov 30, 2005 at 19:55 UTC | |
|
Re: $| pre and post increment and deincrement behavior
by Anonymous Monk on Nov 30, 2005 at 21:19 UTC | |
by robin (Chaplain) on Dec 01, 2005 at 14:05 UTC |