need help with arrays in Perl
-
I want to create an array of arrays of strings. Here's what I tried doing: my (@arrayOfArrays, @array); This is inside a FOR Loop { @array = (@array, "myString"); @arrayOfArrays = (@arrayOfArrays, \@array); } That should create an array of array references, where each reference points to an array of increasing size (i.e. one more element than the previous array). Now, what if I want to add a string value directly to one of the subarrays, say the 5th array (index=4). How would I do it? Is this how? $arrayOfArrays->[4] = ($arrayOfArrays->[4], "2ndString"); Probably not, because it's not working. Please help with this. Thanks.
-
I want to create an array of arrays of strings. Here's what I tried doing: my (@arrayOfArrays, @array); This is inside a FOR Loop { @array = (@array, "myString"); @arrayOfArrays = (@arrayOfArrays, \@array); } That should create an array of array references, where each reference points to an array of increasing size (i.e. one more element than the previous array). Now, what if I want to add a string value directly to one of the subarrays, say the 5th array (index=4). How would I do it? Is this how? $arrayOfArrays->[4] = ($arrayOfArrays->[4], "2ndString"); Probably not, because it's not working. Please help with this. Thanks.
(Assuming that
@arrayOfArrays
is still in the scope):$arrayOfArrays[4] = [ @{$arrayOfArrays[4]}, "2ndString" ];
Basically,@{$arrayOfArrays[4]}
dereferences the arrayref, then the[]
creates a new arrayref using the contents of@{$arrayOfArrays[4]}
and"2ndString"
. - Mike -
I want to create an array of arrays of strings. Here's what I tried doing: my (@arrayOfArrays, @array); This is inside a FOR Loop { @array = (@array, "myString"); @arrayOfArrays = (@arrayOfArrays, \@array); } That should create an array of array references, where each reference points to an array of increasing size (i.e. one more element than the previous array). Now, what if I want to add a string value directly to one of the subarrays, say the 5th array (index=4). How would I do it? Is this how? $arrayOfArrays->[4] = ($arrayOfArrays->[4], "2ndString"); Probably not, because it's not working. Please help with this. Thanks.
Oh yeah, if you want to modify the array directly instead of creating a new array:
push(@{$arrayOfArrays[4]},"2ndString");
- Mike