Home » Faqs » Perl variables » Array from string |
You have a string, but you want an array:
The list method:
#!/usr/bin/perl use strict; use warnings; my @array = (1, 2, 3); # Numbers my @array2 = ('bob', 'fred', 'jill'); # Strings
or the word method:
#!/usr/bin/perl use strict; use warnings; my @array = qw(1 2 3); # Numbers my @array2 = qw(bob fred jill); # Strings
or the string method:
#!/usr/bin/perl use strict; use warnings; my @array = split /\s+/, "1 2 3"; # Numbers my @array2 = split /\s+/, "bob fred jill"; # Strings
or just for numbers:
#!/usr/bin/perl use strict; use warnings; my @array = (1 .. 3);