Re: Use String with Scalar Pattern?
- --- In firstname.lastname@example.org, "chcst53" <charley@p...> wrote:
> --- In email@example.com, Mike Dillinger <miked@s...>a
> > Hello,
> > Can I use the split function where my pattern is a scalar
> variable? For
> > instance:
> > my $splitPattern = '|' ;
> > my( $data1, $data1 ) = split( /$splitPattern/, $someData ) ;
> my ($data1, $data2) = split( /\Q$splitPattern\E/, $someData ) ;
> \Q to \E makes the pipe, |, a regular character. Otherwise, it is
> metacharacter. You could also do:the
> my $splitPattern = quotemeta '|' ; # Does the same thing as \Q
> > I can't seem to get it to work. If I hard code the pattern in
> splitOops, gave more than was required for my answer, Mike :-(
> > function call, it works fine.
> > Thanks,
> > -MikeD
You could simply write:
my $splitPattern = '\|' ;
to escape the pipe.
The way you have it, in the code line with 'split( /$splitPattern/',
is actually to perl, 'split /|/' where the pipe
effectively 'says', 'split on nothing *or* nothing' (what is on
either side of the pipe, |) and it ends up splitting on every
character in a line. This question has been asked (many times?)
before, but I can't seem to find it discussed in the faq.
Hope this makes your problem clearer.