Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Splitting the record using the delimiter

by KurtSchwind (Chaplain)
on Sep 30, 2015 at 18:29 UTC ( [id://1143455]=note: print w/replies, xml ) Need Help??


in reply to Splitting the record using the delimiter

Above you told it to split on pipes. And what I get when I run it is "Hello\" which is split on pipes.

Are you trying to split on pipes except when preceded by a backslash? If so:

#!/usr/bin/perl use strict; my $id = 'Hi|Hello\|Sir'; my @code = split(/[^\\]\|/,$id); print $code[1]."\n";

Note: That since this now splits on a character followed by a pipe that $code[0] would be "H" and not "Hi". The short answer is you are looking for split to have a variable delimiter in this case. A pipe sometimes and no pipe at other times.

--
“For the Present is the point at which time touches eternity.” - CS Lewis

Replies are listed 'Best First'.
Re^2: Splitting the record using the delimiter
by graff (Chancellor) on Sep 30, 2015 at 23:30 UTC
    This is a case for a negative look-behind assertion:
    #!/usr/bin/perl use strict; my $id = 'Hi|Hello\|Sir'; my @code = split(/(?<!\\)\|/,$id); print $code[1]."\n";
    UPDATE: Sorry, I should have realized that I just repeated what kcott said in the previous reply.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1143455]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found