Home > Archive > PERL Beginners > March 2004 > sorting AoA
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]
|
|
| Guruguhan \ N 2004-03-26, 11:15 pm |
| Hi ,
I have an array that is build like this
foreach $i ( 0 .. @array1-1) {
foreach $j ( 0 .. @array2-1) {
$array3[$i][$j] =3D $array2[$j];
}
}=20
The array3 has "m" rows and "n" columns of data.
This code is written by some one else and I am trying to get the =
statistics for each of the column data stored in array3. Before getting =
the statistics, I wanted to sort each column data of the array3 in =
ascending order. I tried doing it as below, but could not succeed.
foreach $i ( 0 .. @array1-1) {
foreach $j ( 0 .. @array2-1) {
@array3 =3D sort {$a <=3D> $b } @array3;
}
}=20
=20
Can some one tell me how do I this? =20
Thanks
Regards
Guruguhan
| |
| Rob Dixon 2004-03-26, 11:15 pm |
| Guruguhan N wrote:
>
> I have an array that is build like this
> foreach $i ( 0 .. @array1-1) {
> foreach $j ( 0 .. @array2-1) {
> $array3[$i][$j] = $array2[$j];
> }
> }
> The array3 has "m" rows and "n" columns of data.
>
> This code is written by some one else and I am trying to get the statistics
> for each of the column data stored in array3. Before getting the statistics, I
> wanted to sort each column data of the array3 in ascending order. I tried
> doing it as below, but could not succeed.
>
> foreach $i ( 0 .. @array1-1) {
> foreach $j ( 0 .. @array2-1) {
> @array3 = sort {$a <=> $b } @array3;
> }
> }
>
>
> Can some one tell me how do I this?
Hi Guruguhan.
Each element of @array3 is a /reference/ to an array. That's how Perl builds
its multi-dimensional arrays. Which dimension is 'columns' and which is 'rows'
is unclear, but by far the simplest case is if you want to sort each 'second-
level' array - @{$array3[0]}, @{$array3[1]}, @{$array3[2]} etc.
This code will do it.
@$_ = sort { $a <=> $b } @$_ foreach @array3;
HTH,
Rob
| |
| John W. Krahn 2004-03-26, 11:15 pm |
| Guruguhan N wrote:
>
> Hi ,
Hello,
> I have an array that is build like this
> foreach $i ( 0 .. @array1-1) {
> foreach $j ( 0 .. @array2-1) {
> $array3[$i][$j] = $array2[$j];
> }
> }
You can use an array slice to do that:
@array3[ 0 .. $#array1 ] = map [ @array2 ], 0 .. $#array1;
> The array3 has "m" rows and "n" columns of data.
>
> This code is written by some one else and I am trying to get the statistics
> for each of the column data stored in array3. Before getting the statistics,
> I wanted to sort each column data of the array3 in ascending order. I tried
> doing it as below, but could not succeed.
>
> foreach $i ( 0 .. @array1-1) {
> foreach $j ( 0 .. @array2-1) {
> @array3 = sort {$a <=> $b } @array3;
> }
> }
>
> Can some one tell me how do I this?
@array3 = map [ sort { $a <=> $a } @$_ ], @array3;
John
--
use Perl;
program
fulfillment
| |
| John W. Krahn 2004-03-26, 11:15 pm |
| Guruguhan N wrote:
>
> Hi John,
Hello,
> Thanks for the quick help. I have resolved the problem
> with your suggestions. But now I am facing a new problem. Actually
> the goal is to get the statistics for each of the data column stored
> in a file (FilterMC_3bar_data.out). This file has the following data
> as its content:
>
> RUN a1 a2 a3 weight
> sig1 sig2 sig3 4 0.359575 0.253987
> 0.359575 1.271019 43898.7 19675.6 -24223.1 5 0.359921
> 0.253987 0.359921 1.271995 43861.3 19666.1
> -24195.2 6 0.359575 0.254332 0.359575 1.271364
> 43892.0 19662.2 -24229.8 7 0.587633 0.213477 0.587633
> 1.875554 28791.2 15898.3 -12892.8 8 0.587978
> 0.213477 0.587978 1.876530 28775.9 15892.2 -12883.7 9
> 0.587633 0.213822 0.587633 1.875899 28786.8 15889.6
> -12897.2
>
> This is the code intended do it.
>
> #!/usr/bin/perl -w
>
> $output_file = "FilterMC_3bar_data.out";
>
> if (-s $output_file) {
You shouldn't use file test operators to determine whether or nor to open files as
there is no guarantee that the test will be valid by the time you open the file.
> open ( INTER,$output_file) || die "Cannot open: \nReason: $!\n";
> my @temp = <INTER>;
If the file were empty you could have determined that by seeing if @temp had any data.
@data or warn "$output_file is empty\n";
> chomp @temp;
> my $response = shift(@temp); # Remove the header line
>
> foreach $i ( 0 .. $#temp ) {
> $filter_data = $temp[$i];
> $filter_data =~ s/^\s+(.*)/$1/; #Remove the leading white spaces
> @data2 = split /\s+/, $filter_data;
You can write the previous three lines in one line:
@data2 = split ' ', $temp[ $i ];
> foreach $j ( 0.. $#data2) {
> $data3[$i][$j] = $data2[$j];
> }
> }
> }
> #Till this part of the code is already existing, written by earlier
> colleague.
>
> @data3[0..$#temp] = map[@data2], 0..$#temp;
> @data3 = map[sort {$a <=> $b } @$_],@data3;
>
> foreach $i ( 0 .. $#data2) {
> foreach $j ( 0 .. $#data3) {
> print " $data3[$j][$i]\n";
> }
> }
>
> The above code with your suggestions gives an output like
>
> -24223.1
> -24195.2
>
> [snip]
>
> 28775.9
> 28786.8
>
> I want to sort each and every column separately so as get statistics
> for each. can you please tell me what necessary changes would enable
> me to do the same.
It looks like you need to transpose the rows and columns
#!/usr/bin/perl -w
use strict;
$output_file = 'FilterMC_3bar_data.out';
open INTER, $output_file or die "Cannot open: $output_file\nReason: $!\n";
my @data;
while ( <INTER> ) {
next if /[a-z]/i; # Remove the header line
my @temp = split;
my $index = 0;
push @{ $data[ $index++ ] }, $_ for @temp;
# use the next line instead if you don't want the first "RUN" column
# push @{ $data[ $index++ ] }, $_ for @temp[ 1 .. $#temp ];
}
@data or die "$output_file was empty.\n";
@data = map [ sort { $a <=> $b } @$_ ], @data;
for ( @data ) {
print "@$_\n";
}
__END__
John
--
use Perl;
program
fulfillment
| |
| Wc -Sx- Jones 2004-03-26, 11:15 pm |
| >> if (-s $output_file) {
>
>
> You shouldn't use file test operators to determine whether or nor to open files as
> there is no guarantee that the test will be valid by the time you open the file.
File tests should be done AFTER you obtain a RW lock.
This is *supposed* to prevent other system processes
from over-writing/truncating/changing it.
When you do test - group relevant tests together so
that the whole process is quick -- this way if
there are other processes waiting application
deadlock doesn't occur.
--
_Sx_ http://youve-reached-the.endoftheinternet.org/ _____
perldoc -qa.a | perl -lpe '($_)=m("(.*)")' | grep Martian
| |
| James Edward Gray II 2004-03-26, 11:15 pm |
| On Mar 26, 2004, at 6:06 PM, WC -Sx- Jones wrote:
>
> File tests should be done AFTER you obtain a RW lock.
>
> This is *supposed* to prevent other system processes
> from over-writing/truncating/changing it.
>
> When you do test - group relevant tests together so
> that the whole process is quick -- this way if
> there are other processes waiting application
> deadlock doesn't occur.
I'm coming into this discussion late, but for things like the exists
test above, it would be much better to use sysopen() and make the
operation atomic. If you test and create in one step, this is a
non-issue.
James
| |
| Wc -Sx- Jones 2004-03-26, 11:15 pm |
| James Edward Gray II wrote:[color=darkred]
:)
There are other reasons to test for a file size prior to opening it :)
Why open a data file if it is zero length when you expect data?
--
_Sx_ http://youve-reached-the.endoftheinternet.org/ _____
perldoc -qa.a | perl -lpe '($_)=m("(.*)")' | grep Martian
| |
| John W. Krahn 2004-03-27, 11:53 pm |
| Wc -Sx- Jones wrote:
>
> James Edward Gray II wrote:
Why are you attributing to James what Guruguhan wrote?
[color=darkred]
> There are other reasons to test for a file size prior to opening it :)
>
> Why open a data file if it is zero length when you expect data?
What if "-s $output_file" returns true and then another process
truncates the file before you open it?
What if "-s $output_file" returns false and then another process adds
data to the file which you didn't open?
John
--
use Perl;
program
fulfillment
| |
| Wc -Sx- Jones 2004-03-27, 11:53 pm |
| John W. Krahn wrote:
> Wc -Sx- Jones wrote:
>
>
>
> Why are you attributing to James what Guruguhan wrote?
:)
I'm not -- however that statement was promoted into the
portion of the thread that James asked about (which I was
replying) so James can take ownership of those parts of
this thread.
>
>
>
> What if "-s $output_file" returns true and then another process
> truncates the file before you open it?
>
> What if "-s $output_file" returns false and then another process adds
> data to the file which you didn't open?
>
If you read back in this thread I stated that the OP needed to
use a file lock -- shared read w/o write access -- so that the
tests could be accomplished BEFORE another application thread
destroyed the file.
If the data file was expected to have data at the time I was
planning on opening it and there was no data - an error is
logged -- I care not about what happened after the event in
question -- the file is allowed to expand and fill the entire
Universe when I release the RO lock.
At any rate we all program differently. :)
--
_Sx_ http://youve-reached-the.endoftheinternet.org/ _____
perldoc -qa.a | perl -lpe '($_)=m("(.*)")' | grep Martian
| |
| John W. Krahn 2004-03-27, 11:53 pm |
| Wc -Sx- Jones wrote:
>
> John W. Krahn wrote:
>
> :)
>
> I'm not -- however that statement was promoted into the
> portion of the thread that James asked about (which I was
> replying) so James can take ownership of those parts of
> this thread.
Not under most country's copyright laws he can't. :-(
>
> If you read back in this thread I stated that the OP needed to
> use a file lock -- shared read w/o write access -- so that the
> tests could be accomplished BEFORE another application thread
> destroyed the file.
>
> If the data file was expected to have data at the time I was
> planning on opening it and there was no data - an error is
> logged -- I care not about what happened after the event in
> question -- the file is allowed to expand and fill the entire
> Universe when I release the RO lock.
>
> At any rate we all program differently. :)
Locking or opening or stating the file do not guarantee that the file
contains data. The only guarantee is if read or readline return actual
data to your program.
John
--
use Perl;
program
fulfillment
| |
| Wc -Sx- Jones 2004-03-27, 11:53 pm |
| John W. Krahn wrote:
>
>
> Not under most country's copyright laws he can't. :-(
LOL :)
I'd like to see that erroneous verbage - as words,
expressed as thoughts and ideas in the discourse of
conversation (whether spoken or written), cannot be
copyrighted.
--
_Sx_ http://youve-reached-the.endoftheinternet.org/ _____
perldoc -qa.a | perl -lpe '($_)=m("(.*)")' | grep Martian
| |
| R. Joseph Newton 2004-03-27, 11:53 pm |
| "N, Guruguhan (GEAE, Foreign National, EACOE)" wrote:
>
[Implementation stuff snipped]
>
> Can some one tell me how do I this?
Well, if you tellus what you want as output, we could maybe start. Hint--if you really have production code with variable names like @array1, @array2, etc., the best thing to do is trash it. It is highly unlikely to contain anything of value that cannot
be easily
replaced.
Joseph
|
|
|
|
|