in reply to adding the missing sequence numbers

You never say what values you want in columns 7 and 8 of your lines. Always 2,2 ?

The example input you give at the end has only 7 columns, not 8

Also you seem to have duplicate keys in your hash, i.e. the key '1' happens to be 3 times in your input. If you store that in a hash, only the last line with a specific key will survive. So either you have to append additional data to the arrays in your hash or use a ArrayofArrays structure to store the data. Or, if the data is already sorted, don't store anything (Ikegamis solution works with that assumption)

  • Comment on Re: adding the missing sequence numbers

Replies are listed 'Best First'.
Re^2: adding the missing sequence numbers
by ikegami (Patriarch) on Jan 19, 2011 at 18:33 UTC

    Also you seem to have duplicate keys in your hash

    His hash never has more than one element, so I find that hard to believe.

      Let me rephrase that: Also you seem to try to store duplicate keys in your hash ;-)

      Naturally the 'my %hash' inside the loop prevents any data to accumulate in the hash (this as an explanation to the poster who started the thread), but I'm sure he had something else in mind for that hash. Not to say that I did spot that bug, I didn't

        hello this is my first post. I think third column has some special meaning which I don't know. If treat rows with each third column value, how about like this?
        use strict; use warnings; my @key=("key", "start", "end"); my @data=(); my $key_pre=0; while(<DATA>) { my %hash; @hash{@key}=(split(/,/,$_))[2 .. 4]; #(key=>1, start=>23, end=> +45) if ($key_pre != $hash{key} ){ &proc_eachkey( \@data ) if $#data > 0; @data=(); } $key_pre=$hash{key}; push @data, {%hash}; #copy hash value } &proc_eachkey( \@data ) if $#data > 0; #for last key sub proc_eachkey { my ($data)= @_; my ($start_pre, $end_pre)=(0,0); foreach my $h (@$data){ if ($end_pre != $h->{start} + 1) { printf "%d,%d,%d\n", $h->{key}, ($start_pre + 1), ($h->{s +tart} -1); } printf "%d,%d,%d\n", $h->{key}, $h->{start}, $h->{end}; ($start_pre, $end_pre)=($h->{start}, $h->{end}); } } __DATA__ 1,34,1,23,45,2,2 35,45,1,56,78,1,1 46,56,1,88,101,2,2 57,68,2,13,34,4,3 69,78,2,45,56,1,3
        nothing better than ikegami's one. regards.