kanka has asked for the wisdom of the Perl Monks concerning the following question:

Thank you very much for your answer, in fact my input read file:
Thomas 60 10 20 85 Samuel 35 90 80 65 Adam 100 90 85 52
and I wanna write and save it as Hash using that formula, here`s sample output:(Hash)
Adam 79.35 Samuel 69 Thomas 45
I tried to do it like this
use strict; use Fcntl ':flock'; my $file = "inputdata.txt"; open(INFILE, $file) or die "File Not Found: $!"; flock(INFILE, LOCK_EX); my @students; while (<INFILE>) { push @students, [split / /, $_]; my %hash; my @keys = keys(%hash); @keys = sort(@keys); foreach my $key(@keys){ print $hash{$key} = $students[0]*0.2 + $students[1]*0.25 + $students[2 +]*0.25 + $students[3]*0.3, "\n"; } } close(INFILE);

Replies are listed 'Best First'.
Re: Create Hash from array structure
by tinita (Parson) on Mar 16, 2004 at 23:13 UTC
    i rewrote your code a bit, but i don't see which format of hash you want. you should provide some (few!) input data and desired output. here's the code:
    #!/usr/local/bin/perl -w use strict; use Fcntl ':flock'; my $file = "inputdata.txt"; open(INFILE, $file) or die "File Not Found: $!"; flock(INFILE, LOCK_EX); my @students; while (<INFILE>) { push @students, [split / /, $_]; } my %hash; # where's $grades coming from? # $hash{$grades}= $students[0]*0.2 + $student[1]*0.25+$students[2]*0.2 +5 + $student +s[3]*0.3; # flock(INFILE, LOCK_UN); # don't use unlock, it's done automagically +by close() close(INFILE);

    update: oops, i forgot 'my @students'

    update2: ok, after you updated your code and added input and output, here's my suggestion:

    use strict; use Fcntl ':flock'; my $file = "inputdata.txt"; open(FH, $file) or die "File Not Found: $!"; flock(FH, LOCK_EX); my @students; my %hash; my @mul = (0.2,0.25,0.25,0.3); while (<FH>) { my ($student, @grades) = (split ' ', $_); # split at one or more whi +tespaces my $grade = 0; for (0..$#mul) { $grade += $mul[$_] * $grades[$_] } $hash{$student} = $grade; # save the sum as a value to the student n +ame } foreach my $key(sort {$hash{$b} <=> $hash{$a}} keys %hash){ print "$key: $hash{$key}\n"; } close(INFILE);
Re: Create Hash from array structure
by BrowserUk (Patriarch) on Mar 17, 2004 at 00:49 UTC

    It's not at all obvious from your question what you are aiming for here. This might be something like what your after, though it is limited to calculating grades from just 4 Students as is, but maybe it will get you started. It would be fairly easy to extend this to deal with any number of students or scores, but I couldn't see any pattern in how you are deriving your constant factors.

    #! perl -slw use strict; # do open and lock stuff my @students = map{ [ split ] } <DATA>; print "Student scores\n------------"; print "@$_" for @students; my %grades; for my $grade ( 0 .. 2 ) { $grades{ $grade } = $students[ 0 ][ $grade ] * 0.2 + $students[ 1 ][ $grade ] * 0.25 + $students[ 2 ][ $grade ] * 0.25 + $students[ 3 ][ $grade ] * 0.3 } print "\nGrades\n--------"; print "$_ => $grades{ $_ }" for sort keys %grades; =Output P:\test>337154 Student scores ------------ 1 2 3 2 1 3 3 1 2 3 2 1 Grades -------- 0 => 2.35 1 => 1.5 2 => 2.15 =cut __DATA__ 1 2 3 2 1 3 3 1 2 3 2 1

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: Create Hash from array structure
by calin (Deacon) on Mar 17, 2004 at 11:26 UTC

    Here's my take. Modifiying it to read input from a file or for pretty printing is left as an exercise.

    #!/usr/bin/perl use Data::Dumper; my @students = qw { Thomas 60 10 20 85 Samuel 35 90 80 65 Adam 100 90 85 52 }; my %h; while ( my ($student, @vals) = splice @students, 0, 5) { $h{$student} += @vals[$_] * (0.2, 0.25, 0.25, 0.3)[$_] for 0..3; } print Dumper \%h;

    Regarding the code provided by you: one hint - split / / is not probably what you want. Try split // (and see perldoc -f split). Another hint - there are at least three more obvious logical mistakes (...hash keys(mental confusion)...two issues with contents of @students (forgot about references, extra element, again confusion)...) .

Re: Create Hash from array structure
by kanka (Initiate) on Mar 17, 2004 at 14:04 UTC
    I`m grateful for your answers, I mean each one of you, God Bless you all, now I`m learning the real deal