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

Re: Assign a delimiter variable

Thread Previous | Thread Next
From:
Xavier Noria
Date:
May 15, 2007 09:49
Subject:
Re: Assign a delimiter variable
Message ID:
8E3E9DFB-E946-4109-90B1-45E4A88A3A2F@hashref.com
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


Thread Previous | Thread Next


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