1

After working with this code, I am stuck at what I think is a simple error, yet I need outside eyes to see what is wrong.

I used unpack function to divide an array into the following.

@extract = 

------MMMMMMMMMMMMMMMMMMMMMMMMMM-M-MMMMMMMM  
------SSSSSSSSSSSSSSSSSSSSSSSSSS-S-SSSSSDTA
------TIIIIIIIIIIIIITIIIVVIIIIII-I-IIIIITTT  

Apparently, after unpacking into the array, when I try to go into the while loop, @extract shows up completely empty. Any idea as to why this is happening? print @extract; #<-----------Prints input

my $sum = 0;
my %counter = ();
while (my $column = @extract) {
print @extract; #<------- This extract is completely empty. Should be input

for (my $aa = (split ('', $column))){
    $counter{$aa}++;
    delete $counter{'-'}; # Don't count -
    }

    # Sort keys by count descending  

    my @keys = (sort {$counter{$b} <=> $counter{$a}} keys %counter) [0]; #gives highest letter
    for my $key (@keys) {
        $sum += $counter{$key};
        print OUTPUT "$key   $counter{$key}   ";
2
  • start by writing code that gets the highest count for a single line; then wrap a loop over your lines around that; you seem to be confusing the two tasks here. Commented Apr 13, 2014 at 23:32
  • You are right ysth, got to add another loop element so as to count the individuals. Thank you Commented Apr 14, 2014 at 0:19

2 Answers 2

1

Each line is an array element correct? I don't see in your code where you are checking the individual characters.

Assuming the input that you have shown is a 3 element array containing the line as a string:

#!/usr/bin/perl

use strict;
use warnings;

my @entries;

while(my $line = shift(@extract)){
    my %hash;
    for my $char(split('', $line)){
        if($char =~ /[a-zA-Z]/) { $hash{$char}++ }
    }

    my $high;
    for my $key (keys %hash) { 
        if(!defined($high)){ $high = $key }
        elsif($hash{$high} < $hash{$key}){ 
            $high = $key
        }
    }

    push @entries, {$high => $hash{$high}};
}

Note this empties @extract, if you don't want to do that you'd have to use a for loop like below

for my $i (0 .. $#extract){
    #my %hash etc...
}

EDIT: Changed it so that only the highest number is actually kept

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Gabs00, will give this a try ASAP. Appreciate the time and effort.
You don't have to use array indexes with for, you can just use for my $line (@extract).
@TLP, that is correct, for me its a form of habit since I usually need to know the index.
@Gabs00 Your post is logical, I tried to run it with my given input but it is not getting any results. Will keep you updated.
@Gyler does it give any results?
|
0

An approach using reduce from List::Util.

#!/usr/bin/perl
use strict;
use warnings;
use List::Util 'reduce';

my @extract = qw/
------MMMMMMMMMMMMMMMMMMMMMMMMMM-M-MMMMMMMM
------SSSSSSSSSSSSSSSSSSSSSSSSSS-S-SSSSSDTA
------TIIIIIIIIIIIIITIIIVVIIIIII-I-IIIIITTT
/;


for (@extract) {
    my %count;
    tr/a-zA-Z//cd;
    for (split //) {
        $count{$_}++;   
    }
    my $max = reduce { $count{$a} > $count{$b} ? $a : $b } keys %count;
    print "$max $count{$max}\n";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.