What does this split expression mean in Perl?
while (<>) {
chomp;
print join("\t", (split /:/)[0, 2, 1, 5] ), "\n";
}
What does (split /:/)[0, 2, 1, 5]
mean her开发者_JAVA技巧e?
It means
my @fields = split /:/, $_;
my @fields_to_display = ($fields[0], $fields[2], $fields[1], $fields[5]);
create a list by splitting the line on :, then take elements 0,2,1,5 of this list
It's a list slice.
Of the values returned by the split
, it returns the first (index 0), the third (index 2), the second (index 1) and the sixth (index 5), in that order.
Honestly, this should have been obvious if you had run the program. Go ahead and try it!
It splits the string stored in $_
(see perlvar) on given regular expression (in this case a single :
) and picks elements number 0, 2, 1 and 5 from the resulting array.
精彩评论