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) -
#!/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";
-
}
-
;
-
5 1433
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: - 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.
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.
It seems like you would be much better off using a hash instead of a 2D array: - #!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";
-
}
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!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:
|
by: buda |
last post by:
Hi,
given the following code,
....
int main( void ) {
char *a = { "abc", "def", "ghijkl", "o", "prs" }; // for example
....
}
|
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...
|
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");...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
| |