split-like thing

Discussion in 'Perl Misc' started by john, Apr 21, 2009.

  1. john

    john Guest

    Hi,

    If I have a string "abcdefgh", what's the best way to get an array with
    the characters in pairs? ("ab", "cd", "ef", "gh")

    Thanks!
    john, Apr 21, 2009
    #1
    1. Advertising

  2. john

    Frank Seitz Guest

    john wrote:
    >
    > If I have a string "abcdefgh", what's the best way to get an array with
    > the characters in pairs? ("ab", "cd", "ef", "gh")


    (@arr) = $str =~ /(..)/g;

    Frank
    --
    Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
    Anwendungen für Ihr Internet und Intranet
    Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
    Frank Seitz, Apr 21, 2009
    #2
    1. Advertising

  3. john

    Frank Seitz Guest

    john wrote:
    >
    > If I have a string "abcdefgh", what's the best way to get an array with
    > the characters in pairs? ("ab", "cd", "ef", "gh")


    @arr = $str =~ /(..)/g;

    Frank
    --
    Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
    Anwendungen für Ihr Internet und Intranet
    Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
    Frank Seitz, Apr 21, 2009
    #3
  4. john

    Guest

    On Tue, 21 Apr 2009 23:51:47 +0200, Frank Seitz <> wrote:

    >john wrote:
    >>
    >> If I have a string "abcdefgh", what's the best way to get an array with
    >> the characters in pairs? ("ab", "cd", "ef", "gh")

    >
    >@arr = $str =~ /(..)/g;

    ^ ^
    Don't need to capture the whole pattern, that is the default in list context,
    otherwise subpatterns as such will get it: @arr = $str =~ /.(.)/g;

    This also works as well:
    @arr = $str =~ /.{2}/g;

    -sln
    , Apr 21, 2009
    #4
  5. john

    Frank Seitz Guest

    wrote:
    > On Tue, 21 Apr 2009 23:51:47 +0200, Frank Seitz <> wrote:
    >> john wrote:
    >>> If I have a string "abcdefgh", what's the best way to get an array with
    >>> the characters in pairs? ("ab", "cd", "ef", "gh")

    >> @arr = $str =~ /(..)/g;

    > ^ ^
    > Don't need to capture the whole pattern, that is the default in list context,
    > otherwise subpatterns as such will get it: @arr = $str =~ /.(.)/g;


    Thank you for the improvement.

    > This also works as well:
    > @arr = $str =~ /.{2}/g;


    TMTOWTDI

    Frank
    --
    Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
    Anwendungen für Ihr Internet und Intranet
    Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
    Frank Seitz, Apr 21, 2009
    #5
  6. john

    Lars Eighner Guest

    In our last episode, <>, the lovely and
    talented john broadcast on comp.lang.perl.misc:

    > Hi,


    > If I have a string "abcdefgh", what's the best way to get an array with
    > the characters in pairs? ("ab", "cd", "ef", "gh")


    sed


    --
    Lars Eighner <http://larseighner.com/>
    91 days since Rick Warren prayed over Bush's third term.
    Obama: No hope, no change, more of the same. Yes, he can, but no, he won't.
    Lars Eighner, Apr 21, 2009
    #6
  7. john

    News123 Guest

    Lars Eighner wrote:
    > In our last episode, <>, the lovely and
    > talented john broadcast on comp.lang.perl.misc:
    >
    >> Hi,

    >
    >> If I have a string "abcdefgh", what's the best way to get an array with
    >> the characters in pairs? ("ab", "cd", "ef", "gh")

    >
    > sed
    >
    >


    Neither
    echo "abcdefgh" | sed
    nor
    echo "abcdefgh" | perl -e 'sed'

    returns the expected result. ;-)

    Just for fun" Could you show me how to do it with sed?
    News123, Apr 22, 2009
    #7
  8. john

    smallpond Guest

    On Apr 21, 6:14 pm, Lars Eighner <> wrote:
    > In our last episode, <>, the lovely and
    > talented john broadcast on comp.lang.perl.misc:
    >
    > > Hi,
    > > If I have a string "abcdefgh", what's the best way to get an array with
    > > the characters in pairs? ("ab", "cd", "ef", "gh")

    >
    > sed
    >


    sed does not have an array data type. awk would be better,
    although regexp are not as powerful as perl's.
    smallpond, Apr 22, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Blaine

    "Rails"-like thing for Python?

    Jeff Blaine, Nov 10, 2004, in forum: Python
    Replies:
    2
    Views:
    383
    Istvan Albert
    Nov 10, 2004
  2. Replies:
    0
    Views:
    1,351
  3. Patrick Kowalzick
    Replies:
    5
    Views:
    461
    Patrick Kowalzick
    Mar 14, 2006
  4. www
    Replies:
    9
    Views:
    598
    Stefan Ram
    Jul 12, 2007
  5. Zam
    Replies:
    1
    Views:
    223
    Mark Schupp
    Mar 14, 2005
Loading...

Share This Page