Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Unwanted empty value in array after Splitting a string

by monkfan (Curate)
on Aug 28, 2006 at 02:22 UTC ( [id://569916]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Fellow monks,

I wanted to extract the N segment of a string, and I uses split for that. But why my script below results in the unwanted empty value ('') in the final array (see the Dumper output below).
perl -MData::Dumper -e ' $str = "GTGNNTNNG"; @nseg = split(/[ATCG]+/,$str); print Dumper \@nseg;'
$VAR1 = [ '', # Why this extra??? 'NN', 'NN' ];

Regards,
Edward

Replies are listed 'Best First'.
Re: Unwanted empty value in array after Splitting a string
by explorer (Chaplain) on Aug 28, 2006 at 02:43 UTC
Re: Unwanted empty value in array after Splitting a string
by BrowserUk (Patriarch) on Aug 28, 2006 at 02:53 UTC

    Use grep in conjunction with length to get rid of it.

    print for grep length, split '[ACGT]+', "GTGNNTNNG";; NN NN

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Unwanted empty value in array after Splitting a string
by grep (Monsignor) on Aug 28, 2006 at 02:51 UTC
    Straight from perldoc -f split
    Splits the string EXPR into a list of strings and returns that list. By default, empty leading fields are preserved, and empty trailing ones are deleted.
    The only exemption is the pattern split(' ',$str) so just add a grep
    perl -MData::Dumper -e ' $str = "GTGNNTNNG"; @nseg = split(/[ATCG]+/,$str); @nseg = grep {/./} @nseg; print Dumper \@nseg;'


    grep
    Mynd you, mønk bites Kan be pretti nasti...
Re: Unwanted empty value in array after Splitting a string
by davido (Cardinal) on Aug 28, 2006 at 03:26 UTC

    As the gang has already pointed out, the documentation for split dispells the mystery. If you're trying to eliminate all empty fields, the grep trick that BrowserUk illustrated is a good solution. But if you're only trying to eliminate one or two (or a few) leading empty fields, you can do this:

    shift @nseg until length $nseg[0];

    ...of course that's not going to help if @nseg contains nothing but empty fields. ;)


    Dave

Re: Unwanted empty value in array after Splitting a string
by swampyankee (Parson) on Aug 28, 2006 at 03:08 UTC

    This is documented behavior: see Split

    "Splits the string EXPR into a list of strings and returns that list. By default, empty leading fields are preserved,..."

    There's an empty leading field before the first "G".

    emc

    Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.

    Albert Einstein

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-19 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found