IMPERATOR
IMPERATOR

Reputation: 287

Perl: Splitting a Pipe Character BUG?

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/){
    $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

Upvotes: 0

Views: 629

Answers (2)

Matthew Lundberg
Matthew Lundberg

Reputation: 42639

While you can escape with backslashes, most regex special characters lose their meaning when used in a character class. I find this more readable, and its meaning is consistent no matter how many levels of interpretation are present; there's no need to escape the escape for a string delimited with " or even if it is passed to a shell prior to use.

This expression splits the string using such a character class:

@tempID = split ("[|]", $id);

Upvotes: 1

happydave
happydave

Reputation: 7187

Try @tempID = split /\|/, $id;. I think the quotes cause \| to just be interpreted as the pipe character, so the regex engine actually sees only the pipe (i.e. alternation), and not the fact that it's escaped. (I.e. it's matching on empty string or empty string.)

(Alternatively, you could use "\\|", but that just seems unnecessarily complicated.)

Upvotes: 4

Related Questions