develooper Front page | perl.beginners | Postings from May 2007

Re: Assign a delimiter variable

Thread Previous
From:
Mike Blezien
Date:
May 15, 2007 09:57
Subject:
Re: Assign a delimiter variable
Message ID:
009701c79711$e28dcad0$04fea8c0@DGGTPQ11
Hello,

----- Original Message ----- 
From: "Xavier Noria" <fxn@hashref.com>
To: "Perl List" <beginners@perl.org>
Sent: Tuesday, May 15, 2007 11:49 AM
Subject: Re: Assign a delimiter variable


> On May 15, 2007, at 6:42 PM, Mike Blezien wrote:
>
>> Hello,
>>
>> this one has been driving me nuts and I'm sure it's something very  simple I 
>> maybe overlooking. I need to assign a delimiter variable  IE: Pipe or Comma 
>> delimiter:
>>
>> my $del         = '|'; # use either a '|' or ','
>> my $dataline  = "0|1|2|3|4|5|6|7|8|9";
>> my @data     = split(/$del/, $dataline);
>>
>> This does not work, it won't split the file line with the '|'  delimiter, and 
>> get no errors. But if I do this:
>>
>> my $dataline  = "0|1|2|3|4|5|6|7|8|9";
>> my @data     = split(/\|/, $dataline);
>>
>> Then it works prefectly, it splits the line as expected. What am I  missing 
>> ??
>
> The actual regexp is what you get _after_ interpolation.
>
> Since the pipe is a metacharacter it is being interpreted as such, as  if you 
> directly wrote
>
>   split /|/, ...
>
> To prevent this there's quotemeta(), which is available in literals  as \E:
>
>   my @data     = split(/\E$del/, $dataline);
>
> -- fxn

Actually it was the /\Q that worked. Your suggestion got me looking back at the 
books and found it, thanks :)

Mike 


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About