in reply to Match 2 same characters

Hi,

A simple array for-loop can do the trick...
#!/usr/bin/perl use strict; my @old_arrays = qw( aa aa bb bb cc dd ); my @new_arrays; push( @new_arrays, $old_arrays[0] ); for (my $i = 0; $i <= $#old_arrays; $i++) { push( @new_arrays, $old_arrays[$i] ) if ( ( $old_arrays[$i] ne $ol +d_arrays[$i-1] ) && $i > 0 ); } print "@new_arrays\n";

Replies are listed 'Best First'.
Re^2: Match 2 same characters
by johngg (Canon) on Mar 19, 2009 at 23:59 UTC

    A grep might be a bit simpler. I'm not sure why you push the first element outside of the loop and then have to test for element number inside. It seems to be an un-necessary complication.

    use strict; use warnings; my @pets = qw{ gerbil parrot hamster cat poodle goldfish }; my @doubles = grep m{(.)\1}, @pets; print qq{$_\n} for @doubles;

    The output

    parrot poodle

    I hope this is of interest.

    Cheers,

    JohnGG

      Hi JohnGG,

      I totally misunderstood the question. Hehehe...