Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

array in for loop

by Anonymous Monk
on Feb 27, 2003 at 18:02 UTC ( [id://239177]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: array in for loop
by Ovid (Cardinal) on Feb 27, 2003 at 18:10 UTC

    Can you provide us with more information about what you're trying to accomplish? While there is nothing in your code that is syntactically incorrect, there are some strange things in it and they're probably not doing what you expect. What we need to know is:

    • What are you trying to accomplish?
    • How are you trying to accomplish it? (that's apparently what we have above)
    • What results are you expecting?
    • What results are you getting?

    If you answer those questions, I'm sure we'll have no problem helping you. As it stands, I can't tell what the various variables in your code stand for.

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

      What are you trying to accomplish?

      working on an assignment where I have to:

      • get data from the input file
      • calculate average of the test scores
      • lowest test score
      • highest test score
      • then sort by test score ascending order

      How are you trying to accomplish it? (that's apparently what we have above)

      see above

      What results are you expecting?

      the average of the scores is the average of the numeric input from file

      What results are you getting?

      the average of the scores is 0

Re: array in for loop
by diotalevi (Canon) on Feb 27, 2003 at 18:09 UTC

    I can only guess at what you intended but here's what you probably meant to say. (in two different versions). You'll probably also want to use a different variable name than $a because that is special and will be clobbered if you use sort(). ($b is also special, skip that one too).

    # Better for (@score) { print "$_\n"; $a += $_; } $dev += $#score; print $dev; # Less nice for (my $ctr = 0; $ctr < @score; $ctr++ ) { print "$score[$ctr]\n"; $a += $score[$ctr]; } $dev += $#score; print $dev;

    Seeking Green geeks in Minnesota

Re: array in for loop
by fruiture (Curate) on Feb 27, 2003 at 18:10 UTC

    It works perfectly, but perhaps it doesn't do waht you want.

    To find out, you'll have to tell us what this is supposed to do. "It doesn't work" is no description of a problem, it is a senseless statement without context.

    --
    http://fruiture.de
Re: array in for loop
by jasonk (Parson) on Feb 27, 2003 at 18:06 UTC

    It looks to me like it does exactly what you told it to do, if you want help with it you will have to explain what it is you were attempting to do (and it would help to know what is in @score).

      perhaps if I send in the entire code and the data file.

      open scores, "a:/scores.txt" || die "what did you idiots do with the s +cores file."; while(<scores>){ chomp; my (@names, @score) = split /:/; $dev=1; for($ctr=0; $ctr<@score; $ctr++){ print "@score\n"; $a = $a + @score; $dev++; print "$dev"; } $b = $a / $dev; print "the average of the scores is $b "; }

      zon moy:100:jack douglass:99:abe douglass:98:thomas severson:97:bobby beavers:96:carla steinke:95:bona hayes:94:mike fedie:93:norma sanders:92:jamie maddox:91:scott summers:90:alex summers:89:kitty pryde:88:doug ramsey:87:clarence thomas:86:loki moy:85

      Will this info help

        @score is empty, because my(@names,@score) = list will put all the list elements in @names. If you want average scores try this instead:

        open SCORES, "a:/scores.txt" or die "no scores!"; while(<SCORES>) { chomp; # turn the data into a hash, with names as keys # and scores as values my %scores = split(':',$input); # add up all the values my $total = 0; map { $total += $_; } values %scores; # divide the total by the number of names in the hash # to get the average. my $average = $total / (keys %scores); print "the average of the scores is $average\n"; } close(SCORES);

        This assumes you only want an average of the scores on each line, and that the same name never appears twice on one line of your data file.

Re: array in for loop
by thelenm (Vicar) on Feb 27, 2003 at 18:08 UTC
    Are you just trying to total the scores in @score? Maybe something like this:
    my $total = 0; $total += $_ for @score; print "$total\n";
    Inside this for loop, each element of @score is aliased to $_, so you can just keep a running total as you loop through the array. If you need to use a C-style for loop as you have, then you'll need to dereference the array using the loop index, maybe like this:
    my $total = 0; for (my $ctr=0; $ctr<@score; $ctr++) { $total += $score[$ctr]; # maybe use $ctr for some other purpose as well... } print "$total\n";

    -- Mike

    --
    just,my${.02}

Re: array in for loop
by LAI (Hermit) on Feb 27, 2003 at 18:15 UTC

    Well, that depends on what you are trying to accomplish. To me, it looks like you actually want to do something like:

    @score = qw( 1 2 3 5 8 13); for($ctr=0; $ctr<@score; $ctr++){ print "$score[$ctr]\n"; $a = $a + @score[$ctr]; $dev++; print "$dev"; }

    Right? However, a better way to do this would be:

    @score = qw( 1 2 3 5 8 13); foreach $score (@score){ print "score is $score\n"; $a += $score; print "dev is " , ++$dev , "\n"; } print "a is $a \n";

    LAI
    :eof

Re: array in for loop
by Anonymous Monk on Feb 27, 2003 at 18:47 UTC
    my @score=(12,14,156,453,232,12); my $sum=0; for(my $i=0;$i<scalar(@score);$i++){ $sum=$sum+$score[$i]; } print "sum: $sum\n"; print "avrage" . int($sum/scalar(@score));

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://239177]
Approved by Ovid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found