S
Sara
split /,/,'cat,mouse,,eel,fish';
yields
0 'cat'
1 'mouse'
2 ''
3 'eel'
4 'fish'
Fine. Likewise:
split /,/,',,,cat,,mouse,eel,fish';
yields
0 ''
1 ''
2 ''
3 'cat'
4 ''
5 'mouse'
6 'eel'
7 'fish'
Everybody is happy. But
split /,/,'cat,mouse,eel,fish,,,';
yields
0 'cat'
1 'mouse'
2 'eel'
3 'fish'
Huh? Where did the trailing items go?
I work around this inconsistency by adding in "placeholders" like:
s/,,/,#,/g; s/,,/,#,/g;
then do the split,then remove the #'s. What a treat. Thanks Larry!
But I can't help but wonder- what programming advantage does this
offer and why was split designed to ignore some split candidates such
as these trailing items? And why only omit trailing items, and not
leading? Was there some presumption made about leading ones being more
meaningful than trailing? Very odd presumption if so!
To the programmer, it would be easier to make split consistent, and in
those cases when the programmer doesn't want empty trailing items he
can easily prepare the scalar to get rid of them:
s/,+$//;
which is a lot easier than identifiying a unique placeholder-
inserting it, splitting, then removing it.
Perl is pretty much self-consistent, in fact this is one of very few
cases I've encountered which lacks consistency. I'd be interested
though in hearing the arguments on why this was a beneficial language
design choice?
yields
0 'cat'
1 'mouse'
2 ''
3 'eel'
4 'fish'
Fine. Likewise:
split /,/,',,,cat,,mouse,eel,fish';
yields
0 ''
1 ''
2 ''
3 'cat'
4 ''
5 'mouse'
6 'eel'
7 'fish'
Everybody is happy. But
split /,/,'cat,mouse,eel,fish,,,';
yields
0 'cat'
1 'mouse'
2 'eel'
3 'fish'
Huh? Where did the trailing items go?
I work around this inconsistency by adding in "placeholders" like:
s/,,/,#,/g; s/,,/,#,/g;
then do the split,then remove the #'s. What a treat. Thanks Larry!
But I can't help but wonder- what programming advantage does this
offer and why was split designed to ignore some split candidates such
as these trailing items? And why only omit trailing items, and not
leading? Was there some presumption made about leading ones being more
meaningful than trailing? Very odd presumption if so!
To the programmer, it would be easier to make split consistent, and in
those cases when the programmer doesn't want empty trailing items he
can easily prepare the scalar to get rid of them:
s/,+$//;
which is a lot easier than identifiying a unique placeholder-
inserting it, splitting, then removing it.
Perl is pretty much self-consistent, in fact this is one of very few
cases I've encountered which lacks consistency. I'd be interested
though in hearing the arguments on why this was a beneficial language
design choice?