Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i am totally new in perl.please help me.how can i make an array from a string.my outputs are saved as

 "[1,2]"

to an array (1,2)

Replies are listed 'Best First'.
Re: string to array
by toolic (Bishop) on Aug 01, 2011 at 14:39 UTC
Re: string to array
by jethro (Monsignor) on Aug 01, 2011 at 14:42 UTC

    Splitting a string into parts. There is a function for that called split

    $string=~s/\[(.*)\]/$1/; #removing [] @numbers= split /,/,$string;
Re: string to array
by JavaFan (Canon) on Aug 01, 2011 at 15:00 UTC
    Some, untested, ways:
    @array = $str =~ /[^][,]/g; @array = $str =~ /[0-9]/g; @array = split ',', substr $str, 1, -1; @array = do {my %h = split //, $str; grep {defined} values %h};
    It's hard to determine from a single, simple, example what you really want.
Re: string to array
by Your Mother (Archbishop) on Aug 01, 2011 at 15:24 UTC

    I don't recommend doing this (other answers were safer) but for the sake of completeness:

    perl -le 'my $aref = eval "[1,2]"; print join("+", @{$aref})' 1+2
Re: string to array
by moritz (Cardinal) on Aug 01, 2011 at 14:40 UTC
Re: string to array
by tritt (Novice) on Aug 01, 2011 at 14:40 UTC

    I think the best way is to use regular expressions (strongest and useful characteristic in perl) or even sprintf.

    I'll recomend you to take a look at PerlRE first