By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

Array won't print

P: 3
Here's the code. When I run it, the array prints fine in the while loop but I get the last person in every array element in the for loop.

Thanks in advance.



File:

Jerry,12
Lon,11
Jon,10
Mike,9

Perl code (on linux)
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. open (FILE, "try.txt");
  3. $x = 0;
  4. while(<FILE>) {
  5.     chomp;    
  6.     ($STUDENT,$GR)= split(/,/);
  7. $hold[$x,1] = $STUDENT;
  8. $hold[$x,2] = $GR;
  9. print "$x: $hold[$x,1],$hold[$x,2]\n";
  10. $x = $x+1;
  11.  
  12. }
  13. close(FILE);
  14. for ($i = 0; $i < 4; $i++){
  15. print "$i --  NAME--$hold[$i,1],GRADE--$hold[$i,2]\n";
  16. ;
  17.  
May 29 '08 #1
Share this Question
Share on Google+
5 Replies


KevinADC
Expert 2.5K+
P: 4,059
Unless you are using Perl 6 and the array syntax is valid for perl 6, your code is not valid for other versions of perl. It looks like you want to create a multi-dimensional array, with perl 5.x you can do it like this:

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
  3.  
  4. open (FILE, "try.txt");
  5. my $x = 0;
  6. my @hold = ();
  7. while(<FILE>) {
  8.    chomp;    
  9.    push @hold, [split(/,/)];
  10. }
  11. close(FILE);
  12. for my $i (0..$#hold){
  13.    print "$i --  NAME -- $hold[$i][0],  GRADE -- $hold[$i][1]\n";

If you are using perl 6, I am not familiar with it so can't answer why the code does not work.
May 30 '08 #2

P: 3
Thanks,

I have been out of the tech game for 3 years so I am not sure what version the school district uses (they dumped the linux platform in lieu of windows). I will try you solution tomorrow and see what happens.
May 30 '08 #3

Ganon11
Expert 2.5K+
P: 3,652
It seems like you would be much better off using a hash instead of a 2D array:
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/env perl
  2. use warnings;
  3. use strict;
  4.  
  5. open (FILE, "try.txt");
  6. my $x = 0;
  7. my %hold = ();
  8. while(<FILE>) {
  9.    chomp;   
  10.    my ($name, $grade) = split(/,/);
  11.    $hold{$name} = $grade;
  12. }
  13. =close(FILE);
  14. for my $name (keys %hold){
  15.    print "NAME: $name, GRADE: $hold{$name}\n";
  16. }
May 30 '08 #4

P: 3
Awesome! It works like a charm. I went with the array (because I am used to it and I need to do other things).

Thanks again. Great forum!
May 30 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
The considerations are:

Hashes do not maintain original order and the last instance of a duplicate key will be the only one left.

The multi-dim array method retains order and duplicates.

Which method to use depends on the requirements of the program.
May 30 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.