So long as you're sure that $line has a length that is a multiple of 3. If you don't necessarily have that, you'll get trailing crud in the last element of @triplets:@triplets = unpack ('(a3)*', $line);
This leads to this answer for your original question (very similar to what's already been posted)# throw away trailing crud pop @triplets if $triplets[-1] !~ /.../;
If you want to start reading at some arbitrary point, you can do:my $line = 'atccatccctttaat'; my @triplets = unpack( '(a3)*', $line); my @triplets2 = unpack( 'x(a3)*', $line); my @triplets3 = unpack( 'xx(a3)*', $line); # throw away trailing crud pop @triplets if $triplets[-1] !~ /.../; pop @triplets2 if $triplets2[-1] !~ /.../; pop @triplets3 if $triplets3[-1] !~ /.../;
Of course, it might just be easier to replace $line with substr($line,$skip).my @triplets = unpack("x${skip}(a3)*", $line); pop @triplets if $triplets[-1] !~ /.../;
-- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
In reply to Re: variation on splitting a string into elements of an array
by fizbin
in thread splitting a sequence using unpack
by Rashmun
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |