Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^2: Split Decision

by Aragorn (Curate)
on Jan 06, 2005 at 14:37 UTC ( #419930=note: print w/ replies, xml ) Need Help??

in reply to Re: Split Decision
in thread Split Decision

'\n' will also work:

#!/usr/bin/perl my $string = "Bla\nBlo\nBloe"; print join("|", split('\n', $string));
This works, and the reason becomes clear when you run it through B::Deparse:
$ perl -MO=Deparse my $string = "Bla\nBlo\nBloe"; print join('|', split(/\n/, $string, 0)); syntax OK


Comment on Re^2: Split Decision
Select or Download Code
Replies are listed 'Best First'.
Re^3: Split Decision
by BrowserUk (Pope) on Jan 06, 2005 at 14:47 UTC

    You live and learn :) What a stupid special exception!

    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.

      It's not really a special exception, the first argument to split() is a regular expression, irrespective of what the delimiters are, the only special case there being the difference between / / and ' '.


      Some playing around shows it's not an exception:
      #!/usr/bin/perl my $string = "Bla\nBlo\nBloe"; print join("|", split('something', $string));
      This code is parsed as
      ~$ perl -MO=Deparse my $string = "Bla\nBlo\nBloe"; print join('|', split(/something/, $string, 0)); syntax OK
      The same goes for double quotes. split() expects a regex, but apparently will settle for things in quotes and interpret the string as a regex. Indeed, you live and learn :-)


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2015-11-30 04:56 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (757 votes), past polls