Home > Archive > PERL Beginners > October 2006 > Problem dynamically sign array a name
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem dynamically sign array a name
|
|
| Shiping Wang 2006-09-29, 6:57 pm |
| Hi, I have a big array, I need re-arrange it then put into sub array, after
that do something on each sub array. I have a problem to dynamically give
sub array a name. Any help? Maybe I should use anonymous array?
Thanks,
Shiping
Here is my code:
use strict;
use warnings;
my @big_arr = (
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);
for (my $i = 0; $i < 10; $i++){
my @q_$i = ($P_array[$i], @P_array[8*$i+10 .. 8*$i+17]);
for (@q_$i){
print join "\t", @_$i, "\n";
}
}
| |
| Paul Lalli 2006-09-29, 6:57 pm |
| 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. I have a problem to dynamically give
> sub array a name. Any help?
What you are trying to do is known as "symbolic references" and they
are a really bad idea. The question you're asking is spelled out in
the FAQ:
perldoc -q "variable name"
> Maybe I should use anonymous array?
You're on the right track, but the important part isn't whether or not
your sub arrays have names, but that your sub arrays are references, so
that you can store one big overall array of array references.
> use strict;
> use warnings;
>
> my @big_arr = (
> 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);
>
my @q;
> for (my $i = 0; $i < 10; $i++){
>
> my @q_$i = ($P_array[$i], @P_array[8*$i+10 .. 8*$i+17]);
$q[$i] = [ $P_array[$i], @P_array[8*$i+10 .. 8*$i+17] ];
> for (@q_$i){
> print join "\t", @_$i, "\n";
> }
Don't know what you're trying to do there. To print out all your
arrays:
for my $i (0..$#q) {
print "$i: ", join("\t", @{$q[$i]}), "\n";
}
Hope this helps,
Paul Lalli
| |
| John W. Krahn 2006-09-29, 6:57 pm |
| Shiping Wang wrote:
> Hi,
Hello,
> I have a big array, I need re-arrange it then put into sub array,
> after that do something on each sub array. I have a problem to
> dynamically give sub array a name. Any help? Maybe I should use
> anonymous array?
It looks like you may need a Hash of Arrays.
> Here is my code:
>
> use strict;
> use warnings;
>
> my @big_arr = (
> 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);
>
> for (my $i = 0; $i < 10; $i++){
>
> my @q_$i = ($P_array[$i], @P_array[8*$i+10 .. 8*$i+17]);
> for (@q_$i){
> print join "\t", @_$i, "\n";
> }
> }
for my $i ( 0 .. 9 ) {
my %q = ( $i => [ $P_array[ $i ], @P_array[ 8 * $i + 10 .. 8 * $i + 17 ] ] );
print join( "\t", @{ $q{ $i } } ), "\n";
}
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Charles K. Clarkson 2006-09-29, 6:57 pm |
| 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.
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer
254 968-8328
Don't tread on my bandwidth. Trim your posts.
| |
| Mumia W. 2006-09-29, 6:57 pm |
| On 09/29/2006 11:49 AM, 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. I have a problem to
> dynamically give sub array a name. Any help? Maybe I should use
> anonymous array?
>
> Thanks,
>
> Shiping
>
> Here is my code:
>
> use strict;
> use warnings;
>
> my @big_arr = (
> 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);
>
> for (my $i = 0; $i < 10; $i++){
>
> my @q_$i = ($P_array[$i], @P_array[8*$i+10 .. 8*$i+17]);
> for (@q_$i){
> print join "\t", @_$i, "\n";
> }
> }
>
>
Create an anonymous array and put it into a hash. Read "perldoc perldsc"
E.g.
my %q;
....
$q{$i} = [ ($P_array[$i], @P_array[8*$i+10 .. 8*$i+17]) ];
....
print join("\t", @{$q{$i}}), "\n";
....
WARNING: UNTESTED CODE
| |
| Shiping Wang 2006-09-29, 6:57 pm |
| 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
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 $p_cutoff = 0.01;
my $qtl_flag;
my @result;
for my $i ( 0 .. 5 ) {
for ($P_array[$i], @P_array[8*$i+10 .. 8*$i+17]){
$qtl_flag = 0;
if ($_ <= $p_cutoff){
$qtl_flag = 1;
last;
}
}
push @result, $qtl_flag;
}
print join "\t", @result, "\n";
__END__
Basically, I need re-arrange @P_array and put into sub array, to each sub
array, I exam if at least one element pass a threshold, then I sign the
flag = 1, otherwise flag = 0 in that sub array.
Instead of signing dynamic name for a sub array, I change to use anonymous
array, it works fine. Alternatively, John and Mumia
approach are also works well. Any suggestion for better coding?
Thanks all,
Shiping
>HTH,
>
>Charles K. Clarkson
>--
>Mobile Homes Specialist
>Free Market Advocate
>Web Programmer
>
>254 968-8328
>
>Don't tread on my bandwidth. Trim your posts.
>
>
>--
>To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>For additional commands, e-mail: beginners-help@perl.org
><http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| Charles K. Clarkson 2006-09-29, 6:57 pm |
| Shiping Wang wrote:
According to the code you provided, you are breaking up the
large array into smaller arrays like this:
print "Large array values become these subarrays:\n\n";
my @P = 1 .. 90;
foreach my $i ( 0 .. 9 ) {
print "\t[";
foreach my $value ( $P[$i], @P[8*$i+10 .. 8*$i+17] ) {
printf '%3d,', $value;
}
print " ],\n";
}
__END__
Large array values become these subarrays:
[ 1, 11, 12, 13, 14, 15, 16, 17, 18, ],
[ 2, 19, 20, 21, 22, 23, 24, 25, 26, ],
[ 3, 27, 28, 29, 30, 31, 32, 33, 34, ],
[ 4, 35, 36, 37, 38, 39, 40, 41, 42, ],
[ 5, 43, 44, 45, 46, 47, 48, 49, 50, ],
[ 6, 51, 52, 53, 54, 55, 56, 57, 58, ],
[ 7, 59, 60, 61, 62, 63, 64, 65, 66, ],
[ 8, 67, 68, 69, 70, 71, 72, 73, 74, ],
[ 9, 75, 76, 77, 78, 79, 80, 81, 82, ],
[ 10, 83, 84, 85, 86, 87, 88, 89, 90, ],
Is that what you really want for each sub array?
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer
254 968-8328
Don't tread on my bandwidth. Trim your posts.
| |
| Shiping Wang 2006-09-29, 6:57 pm |
| At 16:22 2006-9-29, Charles K. Clarkson wrote:
>Shiping Wang wrote:
>
> According to the code you provided, you are breaking up the
>large array into smaller arrays like this:
>
>print "Large array values become these subarrays:\n\n";
>
>my @P = 1 .. 90;
>foreach my $i ( 0 .. 9 ) {
> print "\t[";
>
> foreach my $value ( $P[$i], @P[8*$i+10 .. 8*$i+17] ) {
> printf '%3d,', $value;
> }
>
> print " ],\n";
>}
>
>__END__
>
>Large array values become these subarrays:
>
> [ 1, 11, 12, 13, 14, 15, 16, 17, 18, ],
> [ 2, 19, 20, 21, 22, 23, 24, 25, 26, ],
> [ 3, 27, 28, 29, 30, 31, 32, 33, 34, ],
> [ 4, 35, 36, 37, 38, 39, 40, 41, 42, ],
> [ 5, 43, 44, 45, 46, 47, 48, 49, 50, ],
> [ 6, 51, 52, 53, 54, 55, 56, 57, 58, ],
> [ 7, 59, 60, 61, 62, 63, 64, 65, 66, ],
> [ 8, 67, 68, 69, 70, 71, 72, 73, 74, ],
> [ 9, 75, 76, 77, 78, 79, 80, 81, 82, ],
> [ 10, 83, 84, 85, 86, 87, 88, 89, 90, ],
>
>
> Is that what you really want for each sub array?
Yes, but it start @P = 0 .. 89;
Thanks,
Shiping
>HTH,
>
>Charles K. Clarkson
>--
>Mobile Homes Specialist
>Free Market Advocate
>Web Programmer
>
>254 968-8328
>
>Don't tread on my bandwidth. Trim your posts.
>
>
>--
>To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>For additional commands, e-mail: beginners-help@perl.org
><http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| Mumia W. 2006-09-29, 6:57 pm |
| On 09/29/2006 02:24 PM, Shiping Wang wrote:
> Hi Charles,
>
> At 13:08 2006-9-29, Charles K. Clarkson wrote:
>
> 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: [@{$q{$_}}]\n", $_ for (sort $numeric keys %q);
__HTH__
| |
| Charles K. Clarkson 2006-09-29, 6:57 pm |
| Shiping Wang wrote:
: Yes, but it start @P = 0 .. 89;
I might use the any() function available in List::MoreUtils.
I try to avoid flag like the plague.
use List::MoreUtils 'any';
my @P = (
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 $cutoff = 0.01;
my @result;
foreach my $i ( 0 .. 5 ) {
if ( any { $_ <= $cutoff } $P[$i], @P[8*$i+10 .. 8*$i+17] ) {
push @result, 1;
} else {
push @result, 0;
}
}
print join( "\t", @result ), "\n";
__END__
I might also try a subroutine to process each array.
my @result;
foreach my $i ( 0 .. 5 ) {
push @result, cutoff( [ $P[$i], @P[8*$i+10 .. 8*$i+17] ] );
}
print join( "\t", @result ), "\n";
sub cutoff {
my $array_ref = shift;
return any { $_ <= 0.01 } @$array_ref ? 0 : 1;
}
__END__
Or even this (if you can understand it in six months):
my @result =
map
cutoff( [ $P[$_], @P[8*$_+10 .. 8*$_+17] ] ),
0 .. 5;
print join( "\t", @result ), "\n";
sub cutoff {
my $array_ref = shift;
return any { $_ <= 0.01 } @$array_ref ? 0 : 1;
}
__END__
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer
254 968-8328
Don't tread on my bandwidth. Trim your posts.
| |
| Shiping Wang 2006-10-02, 6:59 pm |
| Hi Charles,
At 17:18 2006-9-29, Charles K. Clarkson wrote:
>Shiping Wang wrote:
>
>: Yes, but it start @P = 0 .. 89;
>
> I might use the any() function available in List::MoreUtils.
>I try to avoid flag like the plague.
>
>use List::MoreUtils 'any';
>
>my @P = (
> 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 $cutoff = 0.01;
>
>my @result;
>foreach my $i ( 0 .. 5 ) {
>
> if ( any { $_ <= $cutoff } $P[$i], @P[8*$i+10 .. 8*$i+17] ) {
> push @result, 1;
>
> } else {
> push @result, 0;
> }
>}
>
>print join( "\t", @result ), "\n";
>
>__END__
>
>
> I might also try a subroutine to process each array.
>
>
>my @result;
>foreach my $i ( 0 .. 5 ) {
> push @result, cutoff( [ $P[$i], @P[8*$i+10 .. 8*$i+17] ] );
>}
>
>print join( "\t", @result ), "\n";
>
>sub cutoff {
> my $array_ref = shift;
>
> return any { $_ <= 0.01 } @$array_ref ? 0 : 1;
>}
>
>__END__
>
>
> Or even this (if you can understand it in six months):
>
>my @result =
> map
> cutoff( [ $P[$_], @P[8*$_+10 .. 8*$_+17] ] ),
> 0 .. 5;
>
>print join( "\t", @result ), "\n";
>
>sub cutoff {
> my $array_ref = shift;
>
> return any { $_ <= 0.01 } @$array_ref ? 0 : 1;
>}
>
>__END__
Thank you very much for your three approaches in coding. I personally like
#2 for the reason you have mentioned.
Shiping
>HTH,
>
>Charles K. Clarkson
>--
>Mobile Homes Specialist
>Free Market Advocate
>Web Programmer
>
>254 968-8328
>
>Don't tread on my bandwidth. Trim your posts.
>
>
>--
>To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>For additional commands, e-mail: beginners-help@perl.org
><http://learn.perl.org/> <http://learn.perl.org/first-response>
|
|
|
|
|