my @args = ( split/,/, $line ); my $size = $#args; # ugly hack to rejoin args that were incorrectly split (internal commas) for ( my $i = 0; $i < $size; $i++ ) { my $depth = 0; $depth++ while $args[$i]=~/[(]/g; $depth-- while $args[$i]=~/[)]/g; if ( $depth ) { splice ( @args, $i, 2, $args[$i].",".$args[$i+1] ); $i--; $size--; } }