Table of Contents

A very FAQ

The following question has come up at least three times in the Perl-Win32-Users mailing list. Can you answer it ?

"My data is delimited by |, for example:
Why doesn't
@array=split /|/, $line;
work ?"

Why indeed. If you don't already know the answer, some simple troubleshooting steps can be applied. First, create a sample program and run it.


@array=split /|/,$line;

foreach (@array) { print "$_\n" }

The effect is to split each character. The | is returned. As it is the delimiter, | should be ignored, not returned.

At this point you should be thinking 'metacharacter'. A little research (looking at the documentation) will reveal that | is indeed a metacharacter, which means 'or', when inside a regex. So, in effect, the regex /|/ means 'nothing, or nothing'. The split is therefore performed on 'nothings', and there are 'nothings' in between each character. The solution is easy ; /\|/ .


@array=split /\|/,$line;

foreach (@array) { print "$_\n" }

So that's the fun stuff, destruction. Now to put it back together again with join .