in reply to variation on splitting a string into elements of an array
in thread splitting a sequence using unpack
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@/
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: variation on splitting a string into elements of an array
by bageler (Hermit) on Mar 02, 2005 at 17:49 UTC | |
by fizbin (Chaplain) on Mar 02, 2005 at 18:11 UTC |