Tweet

How do I create an array from a string

You have a string, but you want an array:

Solution: There are many different ways to make arrays from hard coded values:

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);
[Top]