Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 715331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:10:29+00:00 2026-05-14T05:10:29+00:00

I am trying to perform some composition-based filtering on a large collection of strings

  • 0

I am trying to perform some composition-based filtering on a large collection of strings (protein sequences).
I wrote a group of three subroutines in order to take care of it, but I’m running into trouble in two ways – one minor, one major. The minor trouble is that when I use List::MoreUtils ‘pairwise’ I get warnings about using $a and $b only once and them being uninitialized. But I believe I’m calling this method properly (based on CPAN’s entry for it and some examples from the web).
The major trouble is an error "Can't use string ("17/32") as HASH ref while "strict refs" in use..."

It seems like this can only happen if the foreach loop in &comp is giving the hash values as a string instead of evaluating the division operation. I’m sure I’ve made a rookie mistake, but can’t find the answer on the web. The first time I even looked at perl code was last Wednesday…

use List::Util;
use List::MoreUtils;
my @alphabet = (
 'A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I',
 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V'
);
my $gapchr = '-';
# Takes a sequence and returns letter => occurrence count pairs as hash.
sub getcounts {
 my %counts = ();
 foreach my $chr (@alphabet) {
  $counts{$chr} = ( $_[0] =~ tr/$chr/$chr/ );
 }
 $counts{'gap'} = ( $_[0] =~ tr/$gapchr/$gapchr/ );
 return %counts;
}

# Takes a sequence and returns letter => fractional composition pairs as a hash.
sub comp {
 my %comp = getcounts( $_[0] );
 foreach my $chr (@alphabet) {
  $comp{$chr} = $comp{$chr} / ( length( $_[0] ) - $comp{'gap'} );
 }
 return %comp;
}

# Takes two sequences and returns a measure of the composition difference between them, as a scalar.
# Originally all on one line but it was unreadable.

sub dcomp {
 my @dcomp = pairwise { $a - $b } @{ values( %{ comp( $_[0] ) } ) }, @{ values( %{ comp( $_[1] ) } ) };
 @dcomp = apply { $_ ** 2 } @dcomp;
 my $dcomp = sqrt( sum( 0, @dcomp ) ) / 20;
 return $dcomp;
}

Much appreciation for any answers or advice!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:10:29+00:00Added an answer on May 14, 2026 at 5:10 am

    %{ $foo } will treat $foo as a hash reference and dereference it; similarly, @{} will dereference array references. Since comp returns a hash as a list (hashes becomes lists when passed to and from functions) and not a hash reference, the %{} is wrong. You could potentially leave off the %{}, but values is a special form and needs a hash, not a hash passed as a list. To pass the result of comp to values, comp needs to return a hash ref that then gets dereferenced.

    There’s another problem with your dcomp, namely that the order of values (as the documentation says) “are returned in an apparently random order”, so the values passed to the pairwise block aren’t necessarily for the same character. Instead of values, you can use hash slices. We’re now back to comp returning a hash (as a list).

    sub dcomp {
        my %ahisto = comp($_[0]);
        my %bhisto = comp($_[1]);
        my @keys = uniq keys %ahisto, keys %bhisto;
        my @dcomp = pairwise { $a - $b } , @ahisto{@keys}, @bhisto{@keys};
        @dcomp = apply { $_ ** 2 } @dcomp;
        my $dcomp = sqrt( sum( 0, @dcomp ) ) / 20;
        return $dcomp;
    }
    

    This doesn’t address what happens if a character appears in only one of $_[0] and $_[1].

    uniq left as an exercise for the reader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to perform some unit testing on the iphone but for some
I'm trying to perform some calculations on a non-directed, cyclic, weighted graph, and I'm
I'm trying to perform a group by action on an aliased column (example below)
I'm trying to perform some offline maintenance (dev database restore from live backup) on
I am trying to perform some generic number manipulation independent of the number type.
I have been trying to perform some OpenGL ES performance optimizations in an attempt
I'm trying to perform some simple animations using CA layers on transparent window and
I'm trying to perform some basic AD User managment tasks in C# using .Net
I am trying to perform some computations on a server. For this, the client
I am trying to perform some calculations with a form but every time i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.