in reply to Re^2: Test if variable is equal to a bracket?
in thread Test if variable is equal to a bracket?

Hi mdunnbass,

There's nothing wrong with your comparison if ($array[$i] eq '['); rather, I'm guessing it's a problem with the way you're iterating over the array, and trying to change it in-place.

I would recommend you use a temporary array for this, as it's easier to process, and there's no chance of messing up the original:

#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; my @array = ('A','B','C','[','D',']','E','F'); my @results = ( ); while (@array) { my $string = shift @array; if ($string eq '[' and @array > 2) { $string .= (join "", splice(@array, 0, 2)); } push @results, $string; } printf "Results => %s\n", Dumper(\@results);

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^4: Test if variable is equal to a bracket?
by mdunnbass (Monk) on May 17, 2007 at 23:59 UTC
    Thanks!

    I wound up using a slightly modified version of this, and it does the job perfectly.

    And thanks to everyone else for their advice.

    Matt