Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: two array comparisons

by beable (Friar)
on Jul 16, 2004 at 10:31 UTC ( [id://374959]=note: print w/replies, xml ) Need Help??


in reply to two array comparisons

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://374959]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-19 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found