Loading ...
Sorry, an error occurred while loading the content.
 

Re: Use String with Scalar Pattern?

Expand Messages
  • chcst53
    ... a ... the ... Oops, gave more than was required for my answer, Mike :-( You could simply write: my $splitPattern = | ; to escape the pipe. The way you
    Message 1 of 2 , Jul 20, 2004
      --- In perl-beginner@yahoogroups.com, "chcst53" <charley@p...> wrote:
      > --- In perl-beginner@yahoogroups.com, Mike Dillinger <miked@s...>
      > wrote:
      > > 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
      a
      > metacharacter. You could also do:
      > 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
      the
      > split
      > > function call, it works fine.
      > >
      > > Thanks,
      > > -MikeD

      Oops, gave more than was required for my answer, Mike :-(

      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.
      Chris
    Your message has been successfully submitted and would be delivered to recipients shortly.