My goal is to convert this
my @array=("red", "blue", "green", ["purple", "orange"]);
into this
my @array = ( ["red"], ["blue"], ["green"], ["purple", "orange"]);
Current test code:
my @array = ("red", "blue", "green", ["purple", "orange"] );
foreach $item ( @array ) {
#if this was Python, it would be as simple as:
# if is not instance(item, array):
# # item wasn't a list
# item = [item]
if(ref($item) ne 'ARRAY'){
#It's an array reference...
#you can read it with $item->[1]
#or dereference it uisng @newarray = @{$item}
#print "We've got an array!!\n";
print $item, "\n";
# keep a copy of our string
$temp = $item;
# re-use the variable but convert to an empty list
@item = ();
# add the temp-copy as first list item
@item[0] = $temp;
# print each list item (should be just one item)
print "$_\n" for $item;
}else{
#not an array in any way...
print "ALREADY an array!!\n";
}
}
# EXPECTED my @array=(["red"], ["blue"], ["green"], ["purple", "orange"]);
print @array , "\n";
foreach $item (@array){
if(ref($item) ne 'ARRAY'){
#
#say for $item;
print "didn't convert properly to array\n";
}
}