use strict; use warnings; use Data::Dumper; my $test="[a,b],c,'d',[e,[f,g,h,[i,j],k,l],m,n],o,p"; my @array = split /,/,$test; my ($open, $current, @res); foreach (@array) { $current .= $_ if ($current or /\[/); $current .= ', ' if !(/\]/); $open++ if (/\[/); if (/\]/) { $open--; $current .= ', ' if ($open) ; } unless ($open) { if ( $current eq ', ') { push @res, $_; } else { push @res, $current; } undef $current; } } print Dumper \@res;