I have data in text file. It's only name and points. I need to remove duplicate names and count the average of points. I created a struct and read the file into struct array. Now how can I manipulate data in array? How can I take persons, who is in file more than one time, points and calculate average? Perl is new language for me and I don't know syntax well. My code:
use Class::Struct;
use warnings;
use strict;
struct Person => {
name => '$',
points => '$'};
my $filename = 'data.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file\n";
my @resultArray;
my @name;
my @name2;
my @grade2;
my @grade;
my @nameArray;
my @gradeArray;
my $person = Person->new();
while (my $row = <$fh>) {
chomp $row;
(@name, @grade) = split("\t", $row);
push(@nameArray, @name);
#($person->name, $person->points) = split("\t", $row);
#push(@nameArray, @name);
}
foreach(@nameArray) {
my @seperated = split(' ', $_);
$person->name($seperated[0]);
$person->points($seperated[1]);
}
print($person->points);
(@name, @grade) = split("\t", $row);is wrong, you can't assign to two arrays at the same time - the first one takes all the elements of the RHS.($name, $points)scalars. The data should be specifed in the question and I'd suggest that you edit it and add that.