in reply to Split Function - Positions

One way to do this is to capture the junk along with the substrings:
#! /usr/bin/perl -w use strict; my (@genes, @positions); my $screen = "ATCGATCGXXXXXATCGATXXXACTGCTACGGTACXXXAATTATXGCGCGXXT"; if ($screen =~ /X/) { my @parts = split /(X+)/, $screen; my $pos = 0; foreach my $part (@parts) { if ($part !~ /X/) { push @genes, $part; push @positions, $pos; print "Gene $part is at position $pos\n"; } $pos += length $part; } my $genecounter = @genes; print "Number of components = $genecounter\n\n"; }

-Mark

Replies are listed 'Best First'.
Re^2: Split Function - Positions
by tkil (Monk) on Jun 02, 2004 at 04:51 UTC
    One way to do this is to capture the junk along with the substrings

    This is a fine use for the special treatment of capturing parentheses in the first (regex) argument to split:

    my @chunks; my @start_pos; my $pos = 0; foreach my $chunk ( split /(X+)/, $string ) { unless ( $chunk =~ /^X/ ) { push @start_pos, $pos; push @chunks, $chunk; } $pos += length $chunk; }