If I've the string "abcd", what's the shortest way to convert in @arr containing qw(a b c d)?
A string can be converted into an array using the split() function. @ARRAY = split (/REGEX/, $STRING); Where: @ARRAY is the array variable that will be assigned the resulting array.
If you need to split a string into characters, you can do this: @array = split(//); After this statement executes, @array will be an array of characters. split recognizes the empty pattern as a request to make every character into a separate array element.
Perl | shift() Function shift() function in Perl returns the first value in an array, removing it and shifting the elements of the array list to the left by one. Shift operation removes the value like pop but is taken from the start of the array instead of the end as in pop.
To empty an array in Perl, simply define the array to be equal to an empty array: # Here's an array containing stuff. my @stuff = ("one", "two", "three"); @stuff = (); # Now it's an empty array!
The simplest way is with split with a regex that matches anything.
my @arr = split //, "abcd";
my @arr = split //, "abcd";
my @arr = "abcd" =~ /./sg;
my @arr = unpack '(a)*', 'abcd';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With