473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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)
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
5 1433
KevinADC
4,059 Expert 2GB
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
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
3,652 Expert 2GB
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
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
4,059 Expert 2GB
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

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

Similar topics

15
by: Bob | last post by:
I've tried everything; and I can't seem to get past this VERY (seemingly) simply problem. I want to work with an array variable within a function(s). I can't get it to work; if I: 1) global...
6
by: supercomputer | last post by:
I am using this function to parse data I have stored in an array. This is what the array looks like: , , , , , , , , , , , , , , , , , , , , , , , ] This is the code to parse the array:
5
by: buda | last post by:
Hi, given the following code, .... int main( void ) { char *a = { "abc", "def", "ghijkl", "o", "prs" }; // for example .... }
4
by: Carramba | last post by:
hi! Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is found this is done if funktion serach_char. so far all good what I want do...
9
by: steph_de_marseille | last post by:
I would like to write a x array in a file. If the array has a small numbers of columns I know I can use a loop like: int i,j; double array; FILE *file1; file1=fopen("data.dat","w");...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
9
by: JackpipE | last post by:
I need to create multidimensional array with arrays inside of it. database name | value1 | value2 john | red | 45 john | red | 56 john | yellow | 11 mike | blue | 23 mike | black | 41
11
by: whodgson | last post by:
I`m trying to write a shuffle function to shuffle a sorted array with an even number of elements but it won`t print past the first 2 elements as shown below. When i print the temp array it only...
3
by: Brian Smither | last post by:
$result = $class->method(); //returns an array of associative arrays if($result==TRUE) $cat=$result; I've been converting a class from an original and the original class *apparently* returned an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.