Question: Perl: Splitting a pipe character. Bug?
4.8 years ago by

I'm working on making a BED file but I'm having a problem splitting a variable to get the individual id.


Here are the data

   fam_scz_uktr_eur_omni*UK1090_0_pca|PT-BHLS    chr15    20301669 ...

This is my scrip (I escaped!) 

    if( $id =~ m/\|/g){
        @tempID = split "\|", $id;
        $id = pop(@tempID);


    unless($id =~ m/FID.IID/ || $id =~ m/arrayId/ || $id =~ m/sampleId/){


This is the output of the orphan hash

    KEY    VALUE
    S    fam_scz_uktr_eur_omni*UK1090_0_pca|PT-BHLS    chr15    20301669   ....

It's returning the last character. What gives!?

Am I missing something here? Any help is appreciated :D

pipe bug perl • 1.4k views
ADD COMMENTlink modified 4.8 years ago by Neilfws48k • written 4.8 years ago by QVINTVS_FABIVS_MAXIMVS2.2k

Quick question: What are you expecting out of the pop operation? If you're sure the '|' is used only once in a line, why not just take $tempId[1]?

ADD REPLYlink written 4.8 years ago by RamRS20k
gravatar for cts
4.8 years ago by
cts1.6k wrote:

In the split function use /\|/ rather than the quotes. It worked for me

ADD COMMENTlink written 4.8 years ago by cts1.6k
gravatar for Neilfws
4.8 years ago by
Sydney, Australia
Neilfws48k wrote:

Just to expand a little on the answer by cts. Debug this problem by inspecting the variable @tempID.

print "@tempID\n";

Here it is when split uses double quotes:

f a m _ s c z _ u k t r _ e u r _ o m n i * U K 1 0 9 0 _ 0 _ p c a | P T - B H L S

And when you use single quotes, or the regular expression suggested by cts:

fam_scz_uktr_eur_omni*UK1090_0_pca PT-BHLS

What's happening is that when you use double-quotes, split looks for the literal string "\|", not the escaped pipe. It does not find it and so returns the result "no character" which results in splitting between the characters.

ADD COMMENTlink written 4.8 years ago by Neilfws48k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1025 users visited in the last hour