in reply to Split a string into items of constant length

my @chunks = unpack "(A5)*", "a(b)cd(e)f"; print $chunks[0], "\n"; print $chunks[1], "\n"; __END__ a(b)c d(e)f
Perl --((8:>*

Replies are listed 'Best First'.
Re^2: Split a string into items of constant length
by sauoq (Abbot) on Oct 04, 2005 at 10:53 UTC

    This won't play well with UTF8.

    -sauoq
    "My two cents aren't worth a dime.";
    
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: Split a string into items of constant length
by gargle (Chaplain) on Oct 04, 2005 at 10:26 UTC

    Hi,

    Or more dynamic, but note the little 'empty' problem.

    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { my $line = $_; chomp $line; my @chunks = unpack "(A5)*", $line; print @chunks . "\n"; # beware, the last 'entry' in chunks is empty, note the < and >! foreach my $i (@chunks) { print ">" . $i . "<\n"; } } __DATA__ a(b)cd(e)fg(h)i j(k)lm(n)o
    --
    if ( 1 ) { $postman->ring() for (1..2); }
      my $line = $_; chomp $line; my @chunks = unpack "(A5)*", $line;
      Just a side note: what's wrong with
      chomp; my @chunks = unpack "(A5)*", $_;
      ?

      (or else

      while (my $line=<DATA>) { ...
      instead.)

      Also, more on a stylistic personal preference ground:

      foreach my $i (@chunks) { print ">" . $i . "<\n";
      why not
      print ">$_<\n" for @chunks;
      instead?