Array won't print

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpaterso
    New Member
    • May 2008
    • 3

    Array won't print

    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)
    [code=perl]
    #!/usr/bin/perl
    open (FILE, "try.txt");
    $x = 0;
    while(<FILE>) {
    chomp;
    ($STUDENT,$GR)= split(/,/);
    $hold[$x,1] = $STUDENT;
    $hold[$x,2] = $GR;
    print "$x: $hold[$x,1],$hold[$x,2]\n";
    $x = $x+1;

    }
    close(FILE);
    for ($i = 0; $i < 4; $i++){
    print "$i -- NAME--$hold[$i,1],GRADE--$hold[$i,2]\n";
    }
    ;
    [/code]
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    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:

    Code:
    use warnings;
    use strict;
    
    open (FILE, "try.txt");
    my $x = 0;
    my @hold = ();
    while(<FILE>) {
       chomp;	
       push @hold, [split(/,/)];
    }
    close(FILE);
    for my $i (0..$#hold){
       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.

    Comment

    • jpaterso
      New Member
      • May 2008
      • 3

      #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.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        It seems like you would be much better off using a hash instead of a 2D array:
        [CODE=perl]#!usr/bin/env perl
        use warnings;
        use strict;

        open (FILE, "try.txt");
        my $x = 0;
        my %hold = ();
        while(<FILE>) {
        chomp;
        my ($name, $grade) = split(/,/);
        $hold{$name} = $grade;
        }
        =close(FILE);
        for my $name (keys %hold){
        print "NAME: $name, GRADE: $hold{$name}\n" ;
        }[/CODE]

        Comment

        • jpaterso
          New Member
          • May 2008
          • 3

          #5
          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!

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            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.

            Comment

            Working...