in reply to Test if variable is equal to a bracket?

If you want to modify the array in place without creating a new array you can use splice. Because that is altering the number of elements in the array, you have to work in from the right.

use strict; use warnings; my @arr = qw{[ A ] B C [ D ] E F [ G ] H I}; my $idx = $#arr; while ( $idx >= 2 ) { if ( $arr[$idx] eq q{]} ) { splice @arr, $idx - 2, 0, join q{}, splice @arr, $idx - 2, 3; $idx -= 3; } else { $idx --; } } print qq{$_\n} for @arr;

The output is

[A] B C [D] E F [G] H I

I hope this is useful.

Cheers,

JohnGG