On 09/29/2006 02:24 PM, Shiping Wang wrote:
Hi Charles,

At 13:08 2006-9-29, Charles K. Clarkson wrote:
Shiping Wang wrote:

: Hi, I have a big array, I need re-arrange it then put into sub
: array, after that do something on each sub array.

    How do you want to split it into sub arrays? Like items,
number of items, random items, ...?

: Here is my code:
[snip]

    It is really hard to tell what you are attempting. We don't
know what is in @P_array, for example, because it is never defined
and @q_$i isn't a valid perl variable name. Working code would be
better.

    Tell us what you want to do to the big array.

Here is my real working code:

use strict;
use warnings;

# actually this array has 90 elements, I cut it for short. I have several thousands of this kind of arrays.
=pod
the way I need re-arrange in this way:
array0: 0, 10..17
array1: 1, 18..25
array2: 2, 26..33
.......
.......
array9: 9, 82..89
=cut [...]

Use splice() to suck off eight elements at a time. Put the elements into an anonymous array and put that array into a hash.

use strict;
use warnings;
my @P_array = ( 0.06,0.04,0.98,0.12,0.02,0.98,0.11,0.25,0.36,0.01,0.01,0.01,0.02,0.01,0.05,0.056, 0.046, 0.98, 0.12, 0.002,0.06,0.04,0.98,0.12,0.02,0.98,0.11,0.25,0.36,0.01,0.01,0.01,0.02,0.01,0.05,0.056, 0.046, 0.98, 0.12, 0.002,0.06,0.04,0.98,0.12,0.02,0.98,0.11,0.25,0.36,0.01,0.01,0.01,0.02,0.01,0.05,0.056, 0.046, 0.90);

my %q;

my @pcopy = @P_array;
my $count = 0;
while (my @part = splice(@pcopy, 0, 8)) {
    $q{$count++} = [ @part ];
}
my $numeric = sub { $a <=> $b };
printf "%2d: [EMAIL PROTECTED]", $_ for (sort $numeric keys %q);


__HTH__



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to