Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: What am I doing wrong with 'split'

by Laurent_R (Abbot)
on Aug 11, 2013 at 10:45 UTC ( #1048998=note: print w/replies, xml ) Need Help??

in reply to What am I doing wrong with 'split'

I think the point that you are perhaps missing is that split really uses a regular expression as pattern for splitting the input, not simply a character, whether printable or not. Just to illustrate this point:

$ perl -e '$s="foobaaarbaz"; print join " ", split /ba+r/, $s;' foo baz

As you can see, this code was able to determine that "baaar" matches the pattern /ba+r/ and split the string on it. And, BTW, this gives you much more expressive power once you realize that. In the example you gave, the problem comes from the fact that you use a string in your $sep variable, and that sting contains characters which are special in regexes. This string is then converted to a regex and gets messed up. You don't have the problem if you define $sep as a regular expression instead of a string:

$ perl -e '$s="85|mat\@com";$sep=qr/\|/;($key,$email)=split($sep,$s); +print "$key\t$email\n";' 85 mat@com

Defining the $sep variable as a regex, using $sep=qr/\|/, suffices to solve the issue.

To see how your string is transformed into a regex and compare to the real regex, consider this session under the Perl debugger:

DB<1> $s = "\|" DB<2> $t = qr/$s/ DB<3> p $t (?^:|) DB<4> $u = qr/\|/ DB<5> p $u (?^:\|)

As you can see, your $sep variable is transformed into a simple alternation (the $t variable), it is no longer the escaped | ('\|') that you need (the $u variable). A simple alternation ('|') pattern will split the string into individual characters, and your code was retrieving the first two elements of the list output by split, i.e. the first two characters of your string.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1048998]
[choroba]: I don't think they're multiple people. I was told "he or she" sounds old-fashioned and "they" is the way to say it
[pryrt]: Those others were definitely offensive or unequivocably rude, I agree.
[choroba]: (well, it comes from the 14th century, so labelling it as "modern" doesn't seem appropriate)
[Your Mother]: "They" is becoming accepted but it irritates me sometimes. I tend to just pick she or he randomly or try to use "one."
LanX thinks it is appropriate here :)
[Your Mother]: They would think so.
[jdporter]: ok, I need a recipe for piping lines "through" an external program which is itself a filter
[jdporter]: without using a tmp file
[1nickt]: tobyink perl -MTypes::Standard= is_Int -Mstrict -wE 'say 1 if is_Int 1.0'
[jdporter]: so that I can use the existing expand unix util. Otherwise, I'll probably use Text::Tabs.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2017-05-24 20:24 GMT
Find Nodes?
    Voting Booth?