http://www.perlmonks.org?node_id=1050905

ccelt09 has asked for the wisdom of the Perl Monks concerning the following question:

If one has an array can you assign the length of its current index value from the beginning to a scalar variable?

Essentially I am reprinting a text file made of one long string of 0's and N's with no spaces. At the positions along this string that lie outside of certain intervals, I want all the elements reprinted as N's.

input file looks like this: 0000000NNNN00NNN0NNN0N0N0NN0N #------------------------------------------------- my $population = "all28_males"; my $count = 0; my $position = 0; my ($n, $data, @info, $info, $buf); my $filtered_sites = "/Users/logancurtis-whitchurch/Dropbox/thesis_fol +der/galaxy_chrX_data/filtered_chrX_rawdata.interval"; open (INTERVAL, "<$filtered_sites") or die "can't open $filtered_sites +"; open(MASK, "/Users/logancurtis-whitchurch/Dropbox/thesis_folder/consen +sus_files/filtered_mask_files/filtered.mask."."$population".".txt") o +r die "can't open masked file\n"; my $mask = <MASK>; chomp ($mask); for each (my @interval = <INTERVAL>); { chomp (@interval); my @positions = split(/\t/, $interval[$count]); my $start = $positions[1]; my $end = $positions[2]; while (($n = read MASK, $data, 100000) != 0) { my @info = split(//, $data); my $length = 1; foreach $info (@info){ if ( $length < $start]) { substr($mask,($info),1,'N'); $length++; } if (( $length >= $start) && ($length <= $end)) { next; $length++ } if ($length > $end) { $count++; } } $buf .= $data; print OUT "$mask"; close OUT; close MASK; } }