perl - Why does split return an array with every second element empty? -
i'm trying split string every 5 characters. array i'm getting split
isn't how i'm expecting it: indexes empty, parts i'm looking on odd indexes.
this version doesn't output anything:
use warnings; utilize strict; @ar = <data>; foreach (@ar){ @mkh = split (/(.{5})/,$_); print $mkh[2]; } __data__ aaaaabbbbbcccccdddddfffff
if replace print
line (odd indexes 1
, 3
):
print $mkh[1],"\n", $mkh[3];
the output first 2 parts:
aaaaa bbbbb
i don't understand this, expected able print first 2 parts this:
print $mkh[0],"\n", $mkh[1];
can explain wrong in code, , help me prepare it?
the first argument in split
pattern split on, i.e. describes separates fields. if set capturing groups in there (as do), added output of split specified in split
docs (last paragraph).
this isn't want - separator isn't grouping of 5 characters. you're looking split string every x characters. that, improve use:
my @mkh = (/...../g); # or @mkh = (/.{5}/g);
or 1 of other options you'll find in: how can split string chunks of 2 characters each in perl?
perl split
No comments:
Post a Comment