Re: Fields won't add when reading a text file



NevadaSam@xxxxxxxxx <NevadaSam@xxxxxxxxx> wrote:

In learning CGI/Perl


Learning two things at once makes it more than twice as hard.

It will be easier if you learn one at a time.

Start with learning Perl.

Learn your application area after you have at least some
familiarity with the programming language.

Also, please see the Posting Guidelines that are posted here frequently.


I am trying to write a script that will read
records and print the total number of people registered for each
seminar.

use CGI qw(:standard -debug);


There is no point in including a module if you are not going
to make use of any of its features.


use strict;


You should always enable warnings when developing Perl code:

use warnings;


#declare variables
my ($name, $seminar, @records, %sem_count);


You should declare each variable at its first use, rather
than all at the top.


my @name_count = (0);


Why is this an array?


my %sem_count = ("Computer Maintenance", 0,
"Microsoft Office", 0,
"Unix Essentials", 0,
"CGI/Perl", 0);

#calculate people registered and seminar count
open(INFILE, "<", "list.txt")
or die "Error opening list.txt. $!, stopped";
my @records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
chomp($rec);


Reading them all into an array only to process them one-at-a-time
anyway is silly.

If you are going to process them one-at-a-time, then simply
read them one at a time.

while (my $rec = <INFILE> ) {


($name, $seminar) = split(/,/ , $rec);
$name_count [$name] = $name_count[$name] + 1;


You should always enable warnings when developing Perl code.

It would have pointed out what your problem is.

What is in $name? A string.

You are using a string as an array index, so the string
will be converted to a number to use for indexing the array.

All of your strings will evaluate to zero when you use them
where Perl was expecting a number (like in an array index).

So, you keep adding things to $array[0], and never have
any other elements in the array.

Why? A scalar can hold a number (count) just fine, you don't
need an array for that.


$sem_count {$seminar} = $sem_count{$seminar} +1;


What is in $seminar? A number.

So, the hash keys here will all be numbers, while you earlier
put 4 _string_ keys in there.

You should print out your intermediate data structures when
debugging to see if they contain what you think they contain:

print "$_ ==> $sem_count{$_}\n" for sort keys %sem_count;


}

#generate HTML


If you are not having a problem printing the HTML, then we don't
need to see the printing of the HTML.


foreach my $key ("Computer Maintenance", "Microsoft Office", "Unix
Essentials", "CGI/Perl") {


You only examine the stringy keys, you never look a the numbery keys.


print "<tr><td>$key</td> <td>$sem_count{$key}</td></tr>\n";
}


What I haven't figured out how
to do is total the second field to get the number attending each
seminar. The numbers represent which seminar by 1-Computer Maintenance,
2-Microsoft Office, 3-Unix Essentials, and 4-CGI/Perl. I keep coming up
with 0 for each event.


That is because you are adding to $sem_count{1} instead of
to $sem_count{'Computer Maintenance'} when counting.


---------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my %course_codes = ( 1 => 'Computer Maintenance',
2 => 'Microsoft Office',
3 => 'Unix Essentials',
4 => 'CGI/Perl'
);

my $name_count;
my %sem_count;
while ( my $rec = <DATA> ) {
chomp($rec);
my($name, $sem_num) = split(/,/ , $rec);
$name_count++;
$sem_count{ $course_codes{$sem_num} }++;
}

foreach my $key ("Computer Maintenance", "Microsoft Office",
"Unix Essentials", "CGI/Perl") {
print "$key ==> $sem_count{$key}\n";
}
print "Total registered: $name_count\n";

__DATA__
Janice Alto,3
Nancy Perez,1
James Houza,2
Beth Jimminez,2
Michael Hiller,4
Inez Smith,4
Paul Eniudo,3
Tess Bacza,2
Robert Hau,1
Opal Jones,2
---------------------------------


Thanks for any ideas you may have.


Indent your code to help show the structure of your program.

Get your program working from the command line before moving
it to a CGI environment for further testing.


--
Tad McClellan SGML consulting
tadmc@xxxxxxxxxxxxxx Perl programming
Fort Worth, Texas
.



Relevant Pages

  • Re: Nugget - Using control references
    ... until someone decides they want to save a 3D array. ... If the users are likely to add elements to the middle of the enum, you should save and load by the enum text. ... What you can try is this - save both the string and the number. ... Unused keys are not deleted.  When the number of elements in an array is reduced, the key entries still exist in the file. ...
    (comp.lang.labview)
  • Re: Dynamic variable names
    ... It will show 'true' where the host environment has a window object that is identical to the global object. ... As everything in OOP which is a collection of properties, it can be accessed as the KEY of an array: ... They do not have any of the special attributes or properties that might be associated with 'keys' in another language. ... power of the object and you resort to brackets, the variable name must be called in as a key, and as such can be either numerical or a string, If a string, it should gon in between quotes. ...
    (comp.lang.javascript)
  • Re: most efficient collection for..
    ... > I wish to create keys to a very large array of random data in the order ... The unique aspect of this array is it's element's ... I think I should use either a set or a map, ... It's not clear to me whether you want to fast indexing by string name, ...
    (comp.lang.java.help)
  • most efficient collection for..
    ... I wish to create keys to a very large array of random data in the order ... only a single string, if auto-ordered, or a string and a number. ... The retreival code will take a hardcoded label/hardcoded string, ...
    (comp.lang.java.help)
  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)