new_perl
new_perl

Reputation: 7735

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 here?

Upvotes: 1

Views: 611

Answers (3)

mirod
mirod

Reputation: 16136

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

Upvotes: 9

ikegami
ikegami

Reputation: 385590

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!

Upvotes: 7

zoul
zoul

Reputation: 104065

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.

Upvotes: 4

Related Questions