I need the first two names only in a string.
my $myNames = "Jacob, Michael, Joshua, Matthew, Ethan, Andrew";
my $meNewNames = join ( ',',splice( split(/,/,$myNames), 0, 2));
Please rectify me if anything wrong in it or we can achive it in another way.
print "$meNewNames\n";
It throws the error.
Type of arg 1 to splice must be array (not split)
Thanks.
Well, like the error says, the first argument must be an array. The possible solutions:
Make an anonymous array reference out of your split return values. However, this only works in perl version 5.14 and up. You can do it more simply like this:
Use a subscript to take the first two values of your split. In this style, it is perhaps more readable to do: