How to create an array from a string?

F

Francesco Moi

Hello.

I would like to create an array from a string. I tried with:
---------------------------
$string = "1 2 3";
@array1 = [1,2,3];
@array2 = split(/\s/, $string);
print $array1[0] . " - " . $array1[1] . " - " . $array1[2] . "\n";
print $array2[0] . " - " . $array2[1] . " - " . $array2[2] . "\n";
---------------------------

But I get:
----------------------------
ARRAY(0x1674e9c) - -
1 - 2 - 3
-----------------------------

I would like to generate an array of the same type of 'array1' from
'string'. Is it possible?

Thank you very much.
 
T

Tad McClellan

Francesco Moi said:
I would like to create an array from a string.


That is a bad thing to want.

Luckily, that isn't really what you want.

You want to _populate_ an array from a string, not create an array.

Yes?


my @array1 = split /\s+/, "1 2 3";
 
G

Gunnar Hjalmarsson

Francesco said:
I need to create an _anonymous_ array (sorry, I din't mention it)
to be included in @data, e.g.:

@data = (['John','Peter','Sophie'],[1,2,3]);

$string = "1 2 3";
@data = (['John','Peter','Sophie'], [split /\s/, $string]);

print "$data[0]->[0] - $data[0]->[1] - $data[0]->[2]\n";
print "$data[1]->[0] - $data[1]->[1] - $data[1]->[2]\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top