Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org array issues in PERL
 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

 03-14-2011, 08:21 PM #1 monkeyorhunter LQ Newbie   Registered: Feb 2011 Posts: 10 Rep: 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.
03-14-2011, 09:28 PM   #2
Sergei Steshenko
Senior Member

Registered: May 2005
Posts: 4,481

Rep:
Quote:
 Originally Posted by monkeyorhunter 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 ?

 03-14-2011, 10:07 PM #3 Sergei Steshenko Senior Member   Registered: May 2005 Posts: 4,481 Rep: 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.
 03-15-2011, 07:45 AM #4 monkeyorhunter LQ Newbie   Registered: Feb 2011 Posts: 10 Original Poster Rep: 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 = ; 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;
03-15-2011, 08:26 AM   #5
raymor
Member

Registered: Nov 2005
Posts: 59

Rep:
Quote:
 Originally Posted by monkeyorhunter I have a 2-D array: ... Code: ```#!/usr/bin/perl open (DATA, "<\$INPUT"); my @matrix = ;```
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";```

http://search.cpan.org/search?query=matrix&mode=all

03-15-2011, 10:36 AM   #6
Sergei Steshenko
Senior Member

Registered: May 2005
Posts: 4,481

Rep:
Quote:
 Originally Posted by monkeyorhunter 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 = ; 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.

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM Brad.Scalio@noaa.gov Linux - Enterprise 3 01-23-2007 09:27 PM Ateo Programming 0 01-22-2007 01:38 PM inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org

All times are GMT -5. The time now is 08:50 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -