
japhy
Enthusiast
Jan 15, 2001, 5:56 PM
Post #8 of 10
(2472 views)
|
Re: How do I... User definable split character
[In reply to]
|
Can't Post
|
|
Ah, the problem is thus:
#!/usr/bin/perl $string = "this|that|those"; $sep = "\\|"; @a = split /$sep/, $string; print "@a\n"; # this that those print "Enter a separator: "; # enter the sequence: \\| chomp($sep = <STDIN>); @a = split /$sep/, $string; print "@a\n"; # t h i s | t h a t | t h o s e print "Enter a separator: "; # enter the character | chomp($sep = <STDIN>); @a = split /\Q$sep\E/, $string; print "@a\n"; # this that those You see, when the USER enters \\|, the split() function sees it and treats it as /\\|/, which is "a literal \ or nothing". But when you put the string \\| in a STRING IN THE PROGRAM, Perl changes the \\ to a \ right then. The solution is to use quotemeta(), or \Q...\E, like I showed. Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author
|