http://qs321.pair.com?node_id=1086886


in reply to Previous Line Matching Issues

My approach involves using a simple data structure to tell you about the past -- a stack:

perl -e '@stack = (); sub notify { return qq~!!!!@_[0]!!!~}; for(qw~+ +- + - + - + + - +~) {push @stack, $_; (@stack[-2] ne $_ )? print 0 : + print notify(1) };'

What's happening here:

One additional note: the first example essentially caches everything in the @stack. You can keep only what you need in there by discarding things at the head like this:

perl -e '@stack = (); sub notify { return qq~!!!!@_[0]!!!~}; for(qw~+ + - + - + - + + - +~) {push @stack, $_; (@stack[-2] ne $_ )? print 0 +: print notify(1); if (@stack >= 3){shift @stack} }; use Data::Dum +per; print Dumper \@stack;'

gives,

0000000!!!!1!!!00$VAR1 = [ '-', '+' ];

Celebrate Intellectual Diversity