Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

sorting AoA
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


Report this thread to moderator Post Follow-up to this message
Old Post
Guruguhan \ N
03-27-04 04:15 AM


Re: sorting AoA
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 statistic
s
> for each of the column data stored in array3. Before getting the statistic
s, 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 'row
s'
is unclear, but by far the simplest case is if you want to sort each 'second
-
level' array - @{$array3[0]}, @{$array3[1]}, @{$array
3[2]} etc.

This code will do it.

@$_ = sort { $a <=> $b } @$_ foreach @array3;

HTH,

Rob




Report this thread to moderator Post Follow-up to this message
Old Post
Rob Dixon
03-27-04 04:15 AM


Re: sorting AoA
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 statistic
s
> for each of the column data stored in array3. Before getting the statistic
s,
> I wanted to sort each column data of the array3 in ascending order. I trie
d
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
03-27-04 04:15 AM


Re: sorting AoA
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 fi
les as
there is no guarantee that the test will be valid by the time you open the f
ile.

>      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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
03-27-04 04:15 AM


Re: sorting AoA
>>     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.[/
color]

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

Report this thread to moderator Post Follow-up to this message
Old Post
Wc -Sx- Jones
03-27-04 04:15 AM


Re: sorting AoA
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


Report this thread to moderator Post Follow-up to this message
Old Post
James Edward Gray II
03-27-04 04:15 AM


Re: sorting AoA
James Edward Gray II wrote: 

:)

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

Report this thread to moderator Post Follow-up to this message
Old Post
Wc -Sx- Jones
03-27-04 04:15 AM


Re: sorting AoA
Wc -Sx- Jones wrote:
>
> James Edward Gray II wrote: 

Why are you attributing to James what Guruguhan wrote?

> 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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
03-28-04 04:53 AM


Re: sorting AoA
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

Report this thread to moderator Post Follow-up to this message
Old Post
Wc -Sx- Jones
03-28-04 04:53 AM


Re: sorting AoA
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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
03-28-04 04:53 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:09 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.