in reply to Remove empty element.

This should work better:

my $test="[1,2,3]"; my @var = $test =~ /\d+/g; print Dumper \@var;

Replies are listed 'Best First'.
Re^2: Remove empty element.
by vinoth.ree (Monsignor) on Jul 14, 2009 at 05:39 UTC

    This works fine for numbers only, the element may be any character, In this case what can I do ?

      my $test = "[1,2,3]"; my @var = $test =~ /[^][,]+/g; print Dumper \@var;

        Fantastic!! Works fine

      The second example given by ikegami above should work for you , provided you have a defined separator that doesn't conflict with your data.

        But I get the error as,

        Use of uninitialized value in split at santhosh.pl line 27. $VAR1 = [];

        Following is the code I tried

        use strict; use warnings; use Data::Dumper; my $test="[1,2second,3third,fourth]"; my ($list) = $test =~ /\[ ( [^\]] ) \]/x; my @var = split /,/, $list; print Dumper \@var;