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

is there a way to create an array out of a string or an integer? for example:
if I have $val = 238; I want to get an array @arr that looks like the following: $arr[0] = 2; $arr[1] = 3; $arr[2] = 8; or the same thing for a string if I have $val = "hello"; I want: $arr[0] = "h"; $arr[1] = "e"; $arr[2] = "l"; $arr[3] = "l"; $arr[4] = "o"; thanks

Replies are listed 'Best First'.
Re: making an array out of a string or integer
by ColtsFoot (Chaplain) on Feb 12, 2002 at 15:50 UTC
    Yes sure use split()
    $val = 12345; @valary = split('', $val); foreach $item (@valary) { print $item,qq(\n); }
    The '' parameter in split() says split on nothing
    Hope that helps
Re: making an array out of a string or integer
by rdfield (Priest) on Feb 12, 2002 at 15:58 UTC
    @arr = split //,$val;
    perlfunc is your friend.

    rdfield

Re: making an array out of a string or integer
by dreadpiratepeter (Priest) on Feb 12, 2002 at 16:01 UTC
    split(//,$val) will work. Remember, if you use it like a string it tends to behave like a string.

    -pete
    Entropy is not what is used to be.