Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Split Decision

by Ronnie (Scribe)
on Jan 06, 2005 at 14:11 UTC ( #419920=perlquestion: print w/ replies, xml ) Need Help??
Ronnie has asked for the wisdom of the Perl Monks concerning the following question:

Happy New Year All - I hope noone over-indulged with the Mead! This is one of my quick silly questions so enjoy! I'm trying to use split on \n with, so far, no success. Can this be done? (chomp is of no use as the records in question have \n embedded in many places. The suggestions that I've had so far are the things that I have tried (with nada success!)
my @lines = () ; # ($msg) = @ARGV ; # @lines = split /\n/,$msg ; # foreach (@lines) { print "\n$_\n" ; } # print "\n" ;
I've tried this with & without the double quotes round the \n in the split statement and the best I get now - and the word useless springs to mind -is one field, xnynz. So the split hasn't completely worked it's removed the \'s from the field only! Sorry guys but NONE of your suggestions work! Have any of you actually tried this? Cheers, Ronnie

Comment on Split Decision
Download Code
Re: Split Decision
by BrowserUk (Pope) on Jan 06, 2005 at 14:18 UTC

    Try "\n" instead.


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
      '\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 split.pl my $string = "Bla\nBlo\nBloe"; print join('|', split(/\n/, $string, 0)); split.pl syntax OK

      Arjen

        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.
Re: Split Decision
by gellyfish (Monsignor) on Jan 06, 2005 at 14:19 UTC

    Beats me, there should be no problem splitting on a '\n':

    my $test = "this\nis\na\ntest"; + my @foo = split /\n/, $test; + print +join '*', @foo;
    I would guess that infact what you think is a '\n' is something else.

    /J\

Re: Split Decision
by dragonchild (Archbishop) on Jan 06, 2005 at 14:43 UTC
    Change your string to 'x"\n"y"\n"z' and you'll be enlightened.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Split Decision
by duff (Vicar) on Jan 06, 2005 at 14:43 UTC

    Just because no one has said it yet ... The reason your code doesn't work is that you're splitting on "double-quote newline double-quote" rather than just splitting on "newline" as you intend.

Re: Split Decision
by perlsen (Chaplain) on Jan 07, 2005 at 11:43 UTC

    if u give input in command line as follows assumptions for \n in argument

    @ARGV="sample\nnew\nline\ntext\n\nfor\nsample\ntesting"; my @lines = () ; ($msg) = @ARGV ; #print $msg; @lines = split /\\n/,$msg ; print "$_\n"for @lines

    output:

    *********

    sample

    new

    line

    text

    for

    sample

    testing

    regards,

    Senthil kumar.k

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://419920]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-09-20 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (159 votes), past polls