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

Basically this script takes the 4 sequences in the @data array and aligns them and gives a final output that looks like this..
$VAR1 = { 'A' => [ 2, 1, 1 ], 'T' => [ 1, 3, 1 ], 'C' => [ 0, 0, 1 ], 'G' => [ 1, 0, 1 ] };
.. whichs is nothing but the occurence of that particualr letter ( A T G or C) in postion 1 2 and 3..... the code is as follows
#!usr/bin/perl use strict; use warnings; use Data::Dumper; local our @deep; local $; = ','; # A vestige of a previous version my @data = qw(AAA ATG TTT GTC); my @d2 = map [ split // ], @data; my (%hash); for my $entry (@d2) { *deep = $entry; for my $nx (0..$#deep) { $hash{$deep[$nx]}[$nx]++; } } foreach my $entry (values %hash) { $entry = [ map defined $_ ? $_ : 0, @$entry ]; } print Dumper(\%hash);

Replies are listed 'Best First'.
Re: can this perl script be explained how and what it is going on...????
by GrandFather (Saint) on Dec 03, 2009 at 08:23 UTC

    Have you run it? If not, do so. It is self contained and you can see the input data and fairly quickly infer what the result of running the program is.

    Personally I'd be inclined to refactor the code a little to remove some of the magic and make to workings a little clearer. Consider:

    #!usr/bin/perl use strict; use warnings; use Data::Dumper; my @data = qw(AAA ATG TTT GTC); my %hash; for my $entry (@data) { my @codes = split //, $entry; ++$hash{$codes[$_]}[$_] for 0 .. $#codes; } print Dumper (\%hash); foreach my $entry (values %hash) { $entry = [map {defined $_ ? $_ : 0} @$entry]; } print Dumper (\%hash);

    Note that I've removed the unused variables, renamed @deep to @codes to better express its use, made @codes local to the loop and got rid of the interesting (but unusual) glob construction, and moved the split inside the loop rather than using the slightly more magical construct with the map. I also changed from using a post increment to a pre-increment so the operator is more visible.

    I also added a dump before the second loop so the effect of that loop is obvious. If you are still having trouble with the code and what it is doing I suggest you run it and try changing the input data to see what the effect is, then look carefully at the code to see how it is doing its trick.


    True laziness is hard work
Re: can this perl script be explained how and what it is going on...????
by Utilitarian (Vicar) on Dec 03, 2009 at 08:33 UTC
    This script counts the number of times a specifc character occurs in each position in a list of fixed length words.

    As for how it does it, well it splits each of the fixed length words into single character arrays and iterates over them incrementing a hash value for that character in that position. (Perl expresses this much better ;) )

    It then assigns the value 0 to undefined elements.

    Stick a few print statements in, use Dumper to examine variables and see what changes are being made and it will become clearer

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: can this perl script be explained how and what it is going on...????
by Anonymous Monk on Dec 03, 2009 at 08:06 UTC
    Without context that script is meaningless. Also local our @deep is nonsensical :)
      It's not nonsense, but it is wrong.
      local our @deep; ... *deep = $entry;
      should be
      our @deep; local *deep; ... *deep = $entry;
      or
      our @deep; ... local *deep = $entry;

      Te code assigns to *deep, so it's *deep that needs to be localised.