in reply to Re: variation on splitting a string into elements of an array
in thread splitting a sequence using unpack

Since you had repeating code, my preference is to put it in a loop. I'd also use length instead of /.../, it's computationally less expensive which I assume is important if you're parsing lots of sequences.
my $line = 'atccatccctttaat'; my %triplets; for (0 .. 2) { @{$triplets{$_}} = unpack(('x' x $_).'(a3)*',$line); pop @{$triplets{$_}} if length($triplets{$_}->[-1]) != 3; print "Offset $_: @{$triplets{$_}}\n"; }

Replies are listed 'Best First'.
Re^3: variation on splitting a string into elements of an array
by fizbin (Chaplain) on Mar 02, 2005 at 18:11 UTC
    Yeah, I thought about a loop, but I was afraid it would obscure what the code is doing - I'd certainly switch to a loop if doing more than 3 or four repetitions, and if I were doing more than two statements per offset, but at less than that the loop syntax just clutters things up. (I realize that this is a matter of personal taste, and I might change my answer depending on my mood).

    Good point about using length - again, I think my way is clearer, but I'm not sure whether that's the use of length or the ->. (which you could drop)

    By the way, I would have written the first line of your loop as:

    $triplets{$_} = [unpack("x$_ (a3)*", $line)];
    But that's only because I don't like using @{$unusedvarref} to auto-vivify.
    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/