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

Mighty perl monks, do you have anty suggestions on how to make the following subroutine, and the following part of my code, case insensitive. I know that it sounds trivial but I had problems doing it.
sub get_Hx{ my $site = $_[0]; my $no_of_sequences =length($site); my $sum = 0; foreach(qw/A C D E F G H I J K L M N P Q R S T V W Y _ \./){ my $count = () = $site =~ /$_/g; $sum += -($count/$no_of_sequences)*log_2($count/$no_of_sequences) +if($count > 0); } return $sum; }
also I nedd the same thing for the next block of code.
my $site1; my $site2; my $l=0; foreach $site1 (@site1) { my @site1_parts = split(//, $site1); $l++; my $j=0; my $mutual; my $Hx= get_Hx($site1); #print "$Hx\n"; foreach $site2 (@site2) {#begining of loop2 for each site 2 $j++; my $Hy = get_Hx($site2); my $Hxy; my %counts; my @count_collection; my @site2_parts = split(//, $site2); my $i = @site1_parts; $counts{get_index($site1_parts[$i], $site2_parts[$i])}++ while + ($i--); push(@count_collection, \%counts); foreach my $counts (@count_collection) { my $total; foreach (sort keys %$counts) { #print "$counts{$_}\n"; my $freq = $counts->{$_}/length($site1); my $MI = -$freq * log_2$freq; #$total +=$freq; $Hxy +=$MI; } #print "$Hxy \ $Hx\ $Hy \n"; $mutual = $Hx + $Hy - $Hxy; } print " $l,$j,$mutual\n"; #print " $l,$j\n"; #print($_, ': ', $counts{$_}, $/) foreach (sort keys %counts); #print($/); }#end of loop2 for each site2 }
By case insensitive I mean that whenever my program finds an A or an a it should do countA +2 and not two different counters. Thanks for anyone's help

Replies are listed 'Best First'.
Re: making my suroutine and parts of my program case insensitive.
by The Mad Hatter (Priest) on Oct 31, 2004 at 12:21 UTC

    Change your first regex to /$_/ig. Note the i-modifier (which tells it to match case insensitively). Another option is to uc() or lc() all input data and then compare.

Re: making my suroutine and parts of my program case insensitive.
by Happy-the-monk (Canon) on Oct 31, 2004 at 12:19 UTC

    my $count = () = $site =~ /$_/g;

    One way would be:

    my ( $upper, $lower ) = ( $_, lc $_ );
    my $count = () = $site =~ m/$upper|$lower/g;

    Edit: Or even simpler:

    my $lower = lc $_;
    my $count = () = $site =~ m/$_|$lower/g;

    Or see The Mad Hatter's suggestion below ;-)
    which is the simplest yet.

    Cheers, Sören