Perl: Splitting a pipe character. Bug?
2
0
Entering edit mode
7.7 years ago

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 script (I escaped!)

 if( $id =~ m/\|/g){ @tempID = split "\|",$id;
$id = pop(@tempID); } ... unless($id =~ m/FID.IID/ || $id =~ m/arrayId/ ||$id =~ m/sampleId/){
$orphan{$id}=$line; }  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 bug Perl pipe • 2.5k views ADD COMMENT 0 Entering edit mode 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]?

2
Entering edit mode
7.7 years ago
cts ★ 1.7k

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

1
Entering edit mode
7.7 years ago
Neilfws 49k

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.