in reply to Stuck with manipulating an array

#!/usr/bin/perl # http://perlmonks.org/?node_id=1198153 use strict; use warnings; use Data::Dumper; my $start; my @answer; for ( sort {$a <=> $b} map tr/\n//dr, <DATA> ) { if( not defined $start or $_ > $start + 1000 ) { push @answer, [ $_ ]; $start = $_; } else { push @{ $answer[-1] }, $_; } } print Dumper \@answer; __DATA__ 141326478 103194415 86004442 86004438 86004437 86004434 86004431 85280835 85280834 85280832 53250112 50137387 50137382 50137380 29223108 25694155 17916134

Replies are listed 'Best First'.
Re^2: Stuck with manipulating an array
by Anonymous Monk on Aug 28, 2017 at 13:40 UTC
    seems to do the trick, thank you so much!
    Is this an array of arrays what you are creating, correct?

      Yes

        Ok, this is very frustrating...
        I tried to use your code inside my script like this:
        open IN2, $first_tmp; while(<IN2>) { if($_=~/^(chr.*?)REC:(.*)/) { $respective_chrom=$1; $all_entries=$2; @split_entries=(); @split_entries = split(/\#/, $all_entries); @split_sep_entries=(); %collapsed_loci_HoA=(); print ">".$respective_chrom."\n"; foreach $sep_entry(@split_entries) { @split_sep_entries = split(/\t/, $sep_entry); $locus_to_use = $split_sep_entries[1]; $rest_entry=$split_sep_entries[0]."\t".$split_sep_entries[ +2]."\t". $split_sep_entries[3]."\t".$split_sep_entries[ +4]."\t". $split_sep_entries[5]."\t".$split_sep_entries[ +6]."\t". $split_sep_entries[7]; push @{ $collapsed_loci_HoA{$locus_to_use} }, $rest_entry; } $start; @answer; @array_of_loci = keys %collapsed_loci_HoA; for (sort { $b <=> $a } @array_of_loci) { if( not defined $start or $_ > $start + 1000 ) { push @answer, [ $_ ]; $start = $_; } else { push @{ $answer[-1] }, $_; } } for my $i ( 0 .. $#answer ) { print "$i\t [ @{$answer[$i]} ]\n"; } print "//\n"; } } close IN2;

        and, although, it does create an array with all the elements sorted, it does not create the bins as the standalone snippet does...Any ideas why?