Plz help a newbie add up elements of an array 
Author Message
 Plz help a newbie add up elements of an array

Hi All,
I have a file (survey.out) that looks like this:

shawn|3|shawn test
bruce|5|test for bruce
tom|10|Tom Test
tom|4|
jason|4|jasontest
shawn|1|shawn point test

What I'd like to do is add up the total points (2nd column) for each person.
I'd like to get the following output (I'll worry about the comment field
later):

shawn has 4 points.
bruce has 5 points.
tom has 14 points.
jason has 4 points.
Total points = 27

Here's what I've written so far:

#!/usr/bin/perl


close(INF);

# Loop thru the array

    chop($i);
    ($name,$points,$comments) = split(/\|/,$i);

# Count number of points in survey.out
    $totalpts = $points + $totalpts;

Quote:
}

How do I add up the points for each person?

Thanks for any help!

Shawn



Mon, 20 Oct 2003 00:22:23 GMT  
 Plz help a newbie add up elements of an array

Quote:

> Hi All,
> I have a file (survey.out) that looks like this:
> shawn|3|shawn test
> bruce|5|test for bruce
> tom|10|Tom Test
> tom|4|
> jason|4|jasontest
> shawn|1|shawn point test
> What I'd like to do is add up the total points (2nd column) for each person.
> I'd like to get the following output (I'll worry about the comment field
> later):
> shawn has 4 points.
> bruce has 5 points.
> tom has 14 points.
> jason has 4 points.
> Total points = 27
> Here's what I've written so far:
> #!/usr/bin/perl
> open(INF,"survey.out")

> close(INF);
> # Loop thru the array


Try to avoid the "false idiom" of reading a file into an array and then
looping over each row.  Instead, read the file one line at a time:

open(INF,"survey.out") or die "Can't open survey.out: $!";
while (<INF>) {

Quote:
>     chop($i);

chomp;

Quote:
>     ($name,$points,$comments) = split(/\|/,$i);

Since your input is now in $_, which is the default place that split()
expects, drop the ",$i".

Quote:
> # Count number of points in survey.out
>     $totalpts = $points + $totalpts;
> }
> How do I add up the points for each person?

Use a hash keyed by $name where the values are the points.


Mon, 20 Oct 2003 00:42:06 GMT  
 Plz help a newbie add up elements of an array

Quote:

> Subject: Plz help a newbie add up elements of an array

Please think about your subject lines.

"Plz help a newbie" - contains no usefull information, do not waste
space in the subject line.

"add up elements of an array" - describes the bit you've already
worked out how to do, not the bit you haven't.

Quote:
> shawn|3|shawn test
> bruce|5|test for bruce
> tom|10|Tom Test
> tom|4|
> jason|4|jasontest
> shawn|1|shawn point test

> What I'd like to do is add up the total points (2nd column) for each person.
> I'd like to get the following output (I'll worry about the comment field
> later):

> shawn has 4 points.
> bruce has 5 points.
> tom has 14 points.
> jason has 4 points.
> Total points = 27

Sounds a bit like homework to me.

Quote:
> Here's what I've written so far:

> #!/usr/bin/perl

Get into the habit of using "strict" and "warnings" sooner rather than
latter.

Quote:
> open(INF,"survey.out")

Always check return value of open.

Quote:

> close(INF);

> # Loop thru the array

Why?  Why not simply loop though the file?

Missing "my" - use strict would have caught this.

Quote:
>     chop($i);
>     ($name,$points,$comments) = split(/\|/,$i);

Missing "my" - use strict would have caught this.

Quote:

> # Count number of points in survey.out
>     $totalpts = $points + $totalpts;

There is a += operator in Perl.

Quote:
> }

> How do I add up the points for each person?

Use an associative array ("hash") keyed on $name.

$personpts{$name} += $points;

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Mon, 20 Oct 2003 00:40:06 GMT  
 Plz help a newbie add up elements of an array

[tip: don't use Plz and newbie in your subject]


Quote:
> shawn|3|shawn test
> bruce|5|test for bruce
> tom|10|Tom Test
> tom|4|
> jason|4|jasontest
> shawn|1|shawn point test

[...]

Quote:
> Here's what I've written so far:

> #!/usr/bin/perl

                  ^^^
                  -w
  use strict;

=pod

=for trashcan

Quote:
> open(INF,"survey.out")

> close(INF);

> # Loop thru the array

>     chop($i);
>     ($name,$points,$comments) = split(/\|/,$i);

Don't read the file into an array if you only need to parse it
one line at a time:

=cut

  open INF, "survey.out" or die "Can't open survey.out: $!";
  my %score;    # see below

  while (<INF>) {                               # work with line in $_
    chomp;                                      # smart version of chop
    my ($name, $points, $comments) = split /[|]/;

Quote:
> How do I add up the points for each person?

Use a hash in your loop:

    $score{$name}{points}   += $points;
    $score{$name}{comments} .= $comments . "\n";
  }
  close INF;

then %score will wind up looking like this:

  %score = %{ shawn => { points => 4,
                         comments => "shawn test\nshawn point test\n",
                       }
             ...
            };

ISTR a recent golf game in clp.misc that had a similar objective.

% perl -wpF'\|' -ale'$F{$F[0]}+=$F[1]}for(map"$_=$F{$_}",sort keys%F){' file

--
Joe Schaefer    "The important thing is not to stop questioning. Curiosity has
                                its own reason for existing."
                                               --Albert Einstein



Mon, 20 Oct 2003 02:01:13 GMT  
 Plz help a newbie add up elements of an array


Wed, 18 Jun 1902 08:00:00 GMT  
 Plz help a newbie add up elements of an array

Quote:


> > Subject: Plz help a newbie add up elements of an array

> Please think about your subject lines.

> "Plz help a newbie" - contains no usefull information, do not waste
> space in the subject line.

Bleah. It does contain useful information.
As a matter of fact, you seemed quite plzed to try tp 'help' (or berate) the
newbie.

Quote:
> "add up elements of an array" - describes the bit you've already
> worked out how to do, not the bit you haven't.

Not the way they _wanted to_ though.

Take it easy, mate.

--
Dodger
www.dodger.org
www.necrosoft.net
www.gothic-classifieds.com



Mon, 20 Oct 2003 20:21:53 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Newbie matching problem- plz help

2. Newbie help plz

3. adding array elements

4. adding character to end of each element in array

5. Adding elements to anonymous array.

6. Simple Question: how to add another element to an array

7. adding a constant to elements in an array!

8. adding to the end of each element in an array

9. PERLFUNC: splice - add or remove elements anywhere in an array

10. How to add elements to a 2d array

11. Adding elements to array

 

 
Powered by phpBB® Forum Software