in reply to Re: Split a string into items of constant length
in thread Split a string into items of constant length

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); }

Replies are listed 'Best First'.
Re^3: Split a string into items of constant length
by blazar (Canon) on Oct 04, 2005 at 10:44 UTC
    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?