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

Dear monks, I am a newbie and am getting stuck! All i want to do is given the two arrays below, i want to concatenate all the elements in @seqs which have the same value in @labels and put each newly formed sequence into a new array with each sequence as a new element. Can someone show me what i'm doing wrong?
my @labels = ('1', '1', '1', '2', '3', '4', '5', '6', '6', '7'); my @seqs = ('a', 'ctgc', 'tggattgactgtg', 'atgcatg' , 'ctgctgcatgtgatg +actgtg', 'tgatg', 'gtgt', 'gcgccggactatgattgagctagcgtatgctgcatgctgat' +, 'gggtttttttttttccccccccccc', 'aaaaaagggggg'); # where the $labels[0] .. [2] all correspond to number 1, so $seqs[0] +.. [2] are concatenated to make actgctggattgactgtg etc. I have tried the following code but the problem is that if there are t +hree or more consecutive elements in @labels that have the same value +, my code only gets the sequences of two in a row, not all of them on +ce only (meaning that sequences will be redundant). for (my $i=0; $i< @labels; $i++) { if ($labels[$i] == $labels[$i+1]) { # print "$labels[$i] == $labels[$i+1]\n"; push @window_int_seqs, "$seqs[$i]$seqs[$i+1] "; } }

Replies are listed 'Best First'.
Re: two array comparisons
by beable (Friar) on Jul 16, 2004 at 10:31 UTC
    I think the best way to do this is, "USE A HASH":
    #!/usr/bin/perl use strict; use warnings; my @labels = ('1', '1', '1', '2', '3', '4', '5', '6', '6', '7'); my @seqs = ('a', 'ctgc', 'tggattgactgtg', 'atgcatg' , 'ctgctgcatgtgatgactgtg', 'tgatg', 'gtgt', 'gcgccggactatgattgagctagcgtatgctgcatgctgat', 'gggtttttttttttccccccccccc', 'aaaaaagggggg'); # a useful rule of thumb to remember when programming in Perl: # can I use a hash to solve this problem? my %output = (); if (@labels != @seqs) { die "arrays are different length"; } for (my $i = 0; $i <= $#labels; $i++) { # concatenate the sequence onto what's already there for this labe +l $output{$labels[$i]} .= $seqs[$i]; } foreach my $label (sort keys %output) { print "label: $label = $output{$label}\n"; } __END__
    Output:
    label: 1 = actgctggattgactgtg label: 2 = atgcatg label: 3 = ctgctgcatgtgatgactgtg label: 4 = tgatg label: 5 = gtgt label: 6 = gcgccggactatgattgagctagcgtatgctgcatgctgatgggtttttttttttcccc +ccccccc label: 7 = aaaaaagggggg
Re: two array comparisons
by murugu (Curate) on Jul 16, 2004 at 10:39 UTC
    Use Hashes,
    my @labels = ('1', '1', '1', '2', '3', '4', '5', '6', '6', '7'); my @seqs = ('a', 'ctgc', 'tggattgactgtg', 'atgcatg' , 'ctgctgcatgtgatg +actgtg', 'tgatg', 'gtgt', 'gcgccggactatgattgagctagcgtatgctgcatgctgat' +, 'gggtttttttttttccccccccccc', 'aaaaaagggggg'); for $i (0..$#seqs) { $hash{$labels[$i]}.=$seqs[$i]; } print "$hash{$_}\n" for sort keys %hash;