Kiko has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I have an array that has some characters in it as well as new lines. I want to remove all the new lines and if there is a place where there is more than one newline together (i.e text\ntext2\n\ntext3) i want to remove them as well.

i.e

@test = pram("test"); foreach (@test) { s/(\n)*/,/; }
if the input is text\ntext2\n\ntext3\n, i want the results to be text,text2,text3 so then i can split the array using a comman as the delimeter.

Thanks,
Kiko

Edit: chipmunk 2001-05-31

Replies are listed 'Best First'.
Re: Substitution
by runrig (Abbot) on May 31, 2001 at 23:35 UTC
    One mistake is your use of '*'. '\n*' will match everywhere in the string. Another is that you're only finding the first match. You want:
    s/\n+/,/g for @array # Even better... tr/\n/,/s for @array
      Hi,

      The real problem that i'm having is that when someone enters text and then hits return twice, i get an empty value in the array and that's what i'm really trying to get rid off.

      For example, If i enter the following text:

      text1 text2 text3 text4
      I will get that one empty value in the array and i need to delete it. Is there a way to search the array for any empty values and remove them?

      Thanx,
      Kiko

      Edit: chipmunk 2001-05-31

        then split( /\n+/, $text ) is your friend

        On a second thought, can these parameters contain \r\n? in that case, split( /(?:\r?\n)+/, $text ) should do...

Re: Substitution
by DBX (Pilgrim) on May 31, 2001 at 23:43 UTC
    It sounds like what you probably want is to read the entire file into a scalar, then split it on multiple newlines:
    my @text = split(/\n+/,$file);
    Now @text should have text,text2,text3. You don't need the intermediate step of converting newlines to commas in order to split.
Re: Substitution
by lestrrat (Deacon) on May 31, 2001 at 23:44 UTC

    I don't see why you need to substitute \n with commas to start with, if you're going to split the strings....

    foreach my $text ( @test ) { my @elements = split( /\n/, $text ); # do something with @elements }

    ... would suffice, no?

Re: Substitution
by myocom (Deacon) on May 31, 2001 at 23:49 UTC

    Does this array (@test) actually have more than one item in it? It's hard to tell, since we don't know what the 'pram' function does. It appears that you want to end up with an array like this: [text, text2, text3].

    If 'pram' actually returns a single value (which you happen to be assigning to an array), you should assign it to a scalar, then use a pattern with split to accomplish what you want. There's no sense in replacing the newlines with a comma, just so you can split them again.

    $test = pram("test"); @array = split /\n+/$test/;
    ...and now @array has each item in its own element.
Re: replacing one or more newlines with commas
by larryk (Friar) on Jun 01, 2001 at 14:56 UTC
    A non-regex option:
    #!perl -w use strict; undef $/; my $csv = join(',',split(/\n+/,<DATA>)); print $csv; my @values = split(/,/,$csv); # hmmm, we had this a couple of lines ba +ck __DATA__ 1 2 3 4 5 6
    It does seem a bit strange to create a new string just with a different delimiter purely so you can split it again. I agree with myocom above - you can split the list straight into an array.

    "Argument is futile - you will be ignorralated!"