LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-14-2011, 08:21 PM   #1
monkeyorhunter
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Rep: Reputation: 0
array issues in PERL


Hi

I have a 2-D array:


16 14 458
17 14 489
17 16 494
17 15 494

what I'm trying to do is compare the value of the 2nd row, 2nd column (14) to that of the 1st row, 2nd column (14) and then the 3rd row 2nd column(16) to the 2nd row 2nd column (14) and I want this to be done for all the rows in the array.

Can anyone help with this?

Thanks!

P.S. By compare I am thinking of a if ($a >=$b) {

where $a is the value in the second row and $b is the value in the first row.
 
Old 03-14-2011, 09:28 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by monkeyorhunter View Post
Hi

I have a 2-D array:


16 14 458
17 14 489
17 16 494
17 15 494

what I'm trying to do is compare the value of the 2nd row, 2nd column (14) to that of the 1st row, 2nd column (14) and then the 3rd row 2nd column(16) to the 2nd row 2nd column (14) and I want this to be done for all the rows in the array.

Can anyone help with this?

Thanks!

P.S. By compare I am thinking of a if ($a >=$b) {

where $a is the value in the second row and $b is the value in the first row.
What is the exact question ? I.e., for example, do you know how to write multidimensional arrays in Perl ?
 
Old 03-14-2011, 10:07 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
To the OP - read the first 20-40 lines of

man perl

paying attention to the "array" word, This will give you a clue what to read next.
 
Old 03-15-2011, 07:45 AM   #4
monkeyorhunter
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Hi

Sorry, I forgot to include this in my original post:

this is the code I have so far. I know it's wrong, but I'm not sure what to fix.

Thanks!


#!/usr/bin/perl
my $INPUT = "Data_1.txt";
my $OUTPUT = "Data_2.txt";
open (DATA, "<$INPUT");
open (DATAN, ">$OUTPUT");
my @matrix = <DATA>;
for(my $i = 0; $i <= $#matrix; ++$i){
for(my $j = 0; $j <= $#matrix ; ++$j){
if ($matrix[$i+1][2] >= $matrix[$i][2]){
print DATAN ($matrix[$i+1][3]);
}
}
}
close DATA;
close DATAN;
 
Old 03-15-2011, 08:26 AM   #5
raymor
Member
 
Registered: Nov 2005
Posts: 59

Rep: Reputation: 20
Quote:
Originally Posted by monkeyorhunter View Post
I have a 2-D array:
...
Code:
#!/usr/bin/perl
open (DATA, "<$INPUT");
my @matrix = <DATA>;
You don't have a 2-D array, or a matrix or an array of references to arrays or anything like that.
You just have an array of lines of text. $matrix[0] is literally the single value "16 14 458".

You want something like:
Code:
#!/usr/bin/perl

my $matrix = ();

open (DATA, "<$INPUT");
while (my $line = <DATA>) {
    chomp $line;
    my @row = split(/ /, $line);
    push (@{$matrix}, \@row);
}
close DATA;
print $matrix->[0]->[2], "\n";

See also:
http://search.cpan.org/search?query=matrix&mode=all
 
Old 03-15-2011, 10:36 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by monkeyorhunter View Post
Hi

Sorry, I forgot to include this in my original post:

this is the code I have so far. I know it's wrong, but I'm not sure what to fix.

Thanks!


#!/usr/bin/perl
my $INPUT = "Data_1.txt";
my $OUTPUT = "Data_2.txt";
open (DATA, "<$INPUT");
open (DATAN, ">$OUTPUT");
my @matrix = <DATA>;
for(my $i = 0; $i <= $#matrix; ++$i){
for(my $j = 0; $j <= $#matrix ; ++$j){
if ($matrix[$i+1][2] >= $matrix[$i][2]){
print DATAN ($matrix[$i+1][3]);
}
}
}
close DATA;
close DATAN;
Have followed the suggestion in my http://www.linuxquestions.org/questi...2/#post4291006 post ?

And please use CODE (rather than QUOTE) tags for code - can be applied to selected text using then '#' button.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
RHEL4u2 perl localtime() call issues (perl & glibc) Brad.Scalio@noaa.gov Linux - Enterprise 3 01-23-2007 09:27 PM
perl + @INC array Ateo Programming 0 01-22-2007 01:38 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM


All times are GMT -5. The time now is 04:55 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration