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

16381Re: [PBML] Nother Quick Question

Expand Messages
  • Damien Carbery
    Sep 1, 2003
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, Paul Archer <tigger@i...> wrote:
      > 3:45am, Jeff 'japhy' Pinyan wrote:
      >
      > > On Aug 31, Dustin Smith AKA GIDustin said:
      > >
      > > >I have a string, a large string, and I want to put it in an array
      2 chars
      > > >at a time, so "AABBGGHHSS...." would be transefered into like @Array=
      > > >("AA","BB","GG",....) How would I do that?
      > >
      > > >@squares=split(/.{2}/, $line);
      > >
      > > split() is the wrong tool for the job. Use this instead:
      > >
      > > my @squares = $line =~ /.{2}/g;
      > >
      >
      > To expand on this slightly, the *reason* that split is the wrong
      tool for
      > the job is that the pattern you use with split is the delimiter, and the
      > delimiter is removed from the string. So you're saying every two
      characters
      > (much more simply written as just /../, BTW) are delimiters, so remove
      > them--leaving you with zilch.
      >
      > Paul

      And if you retain the delimiters with:
      @squares=split(/(..)/, $line); # Note brackets.
      you will get:
      'AA', '', 'BB', '', 'CC', etc
      i.e. the delimiters (AA, BB etc) are retained along with the items
      between them (which are all zero length).
    • Show all 5 messages in this topic