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);

In reply to Re: Create Hash from array structure by tinita
in thread Create Hash from array structure by kanka

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.