Looking at this "more complete"(?) version of your code, it's actually easier for me to say how it can be done without trying to use "dynamic array names", and instead using an appropriate data structure (an array of arrays, or AoA). So I'll try to do that by showing how I would rewrite this.

But in trying to revise your code, I found a number of other problems, which will probably lead to unintended results. So, in addition to recasting the code to use an AoA, I made other changes to improve legibility: moved things around a bit, applied proper indentation (which does matter), used the simpler form of loop control, removed some unnecessary code, and removed your original comments.

Now, the only comments are the ones I put in, mostly (the "um..." ones) to mark things that make no sense or problems that need to be solved (but I don't know how you should solve them, because I don't know enough about your data or your real goals). This version compiles with no errors or warnings, but when you run it, there will certainly be a run-time error.

#!/usr/bin/perl -w use strict; my $dinu_r; # um... nothing is ever assigned to this variable my @energyGrid; # this will be the AoA my $i; for my $x ( 1 .. 5 ) { open( AA, "shuffle$x" ) or die "Can't open shuffle$x ($!)\n"; # LI +NE A my $Sequence; $i=length($Sequence); # um... this is always zero while (<AA>) { $Sequence=$_; # um... $Sequence will only be the last line of + "shuffle$x" } close(AA); my %Winenergy = Energy( $Sequence, $dinu_r ); open (OUT, ">shuffle$x.stb"); # LINE B foreach my $el1 (sort{$a <=> $b} keys %Winenergy) { push( @{$energyGrid[$x]} , $Winenergy{$el1} ); #LINE C } close (OUT); } open(FILE,">total_shuffle.stb"); my $m=8; for my $j ( 0 .. $i ) # um... $i is still zero here { my $avg = 0; for my $x ( 1 .. 5 ) { $avg += $energyGrid[$x][$j]; } $avg /= 5; print FILE "$m\t$avg\n"; $m++; } close (FILE); sub Energy { my ($sequence,$dinu_r) = @_; my %dinu = %$dinu_r; # um... $dinu_r was never a hash ref # (and is undef), so this line will cause a run-time error my $win=15; my $limit1 = length( $sequence ) - $win + 1; my $wincenpos = 1 + ($win-1)/2; my %winenergy = (); for my $j ( 0 .. $limit1-1 ) { my $winseq = substr($sequence,$j,$win); my $energy = 0; for my $k ( 0 .. $win-2 ) { my $dinucle_temp = substr($winseq,$k,2); my $dinucle = uc($dinucle_temp); $energy = $energy + $dinu{$dinucle}; } my $wincentre = $j+$wincenpos; $winenergy{$wincentre} = $energy; } return %winenergy; }

In reply to Re^3: Changing name of ARRAY in each iteration by graff
in thread Changing name of ARRAY in each iteration by cool

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.