Fields won't add when reading a text file

Discussion in 'Perl Misc' started by NevadaSam@gmail.com, Apr 30, 2006.

  1. Guest

    In learning CGI/Perl I am trying to write a script that will read
    records and print the total number of people registered for each
    seminar.
    This is the script I have been working on all day and the text file
    which it reads follows it.


    #!/usr/bin/perl
    #list.cgi - reads form data to a file, and creates a dynamic
    #Web page that displays people registering for different seminars.
    print "Content-type: text/html\n\n";
    use CGI qw:)standard -debug);
    use strict;

    #declare variables
    my ($name, $seminar, @records, %sem_count);
    my @name_count = (0);
    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);
    ($name, $seminar) = split(/,/ , $rec);
    $name_count [$name] = $name_count[$name] + 1;
    $sem_count {$seminar} = $sem_count{$seminar} +1;
    }

    #generate HTML
    print "<html><head><title>Seminar Workshop</title></head>\n";
    print "<body>\n";
    print "<table>\n";
    print "<tr align='left'><th>Seminar</th> <th>Registered</th></tr>\n";
    foreach my $key ("Computer Maintenance", "Microsoft Office", "Unix
    Essentials", "CGI/Perl") {
    print "<tr><td>$key</td> <td>$sem_count{$key}</td></tr>\n";
    }
    print "</table>\n";
    print "<br /><br />Total registered: $name_count[$name]\n";
    print "</body></html>\n";



    Text file:

    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



    The first field in the text file is a name which I have been able to
    add all together to get a total of 10. 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.

    Thanks for any ideas you may have.
     
    , Apr 30, 2006
    #1
    1. Advertising

  2. wrote:
    > This is the script I have been working on all day and the text file
    > which it reads follows it.


    <script and data snipped>

    These are a few hints:

    > The numbers represent which seminar by 1-Computer Maintenance,
    > 2-Microsoft Office, 3-Unix Essentials, and 4-CGI/Perl.


    You need to tell Perl that, too. This is one way to do so:

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

    And in the loop:

    $sem_count{ $translate{$seminar} } ++;

    As regards the number of names/records, you can just use the @records
    array in scalar context:

    print "<br /><br />Total registered: " . @records . "\n";

    Your use of a @name_count array is incorrect, and generates a bunch of
    warnings if warnings are enabled (which should be the case...).

    Warnings may give you a couple of other hints.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 30, 2006
    #2
    1. Advertising

  3. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 30, 2006
    #3
  4. Guest

    Thanks everyone. The information I have received has been very helpful.
    I am new at learning this and the text I am using is not the best. My
    problem with this is solved. Thanks again.
     
    , Apr 30, 2006
    #4
  5. <> wrote:

    > I am new at learning this and the text I am using is not the best.



    What text are you using?

    Helping to save others from poor books is something you can
    contribute even when learning...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 30, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. blackjack

    add text fields together...

    blackjack, Nov 28, 2006, in forum: HTML
    Replies:
    1
    Views:
    350
    Toby Inkster
    Nov 28, 2006
  2. Cyril.Liu
    Replies:
    0
    Views:
    456
    Cyril.Liu
    Dec 2, 2008
  3. chief african brewer

    How to create a collection and add text fields

    chief african brewer, Jul 21, 2009, in forum: Java
    Replies:
    6
    Views:
    347
    John B. Matthews
    Aug 8, 2009
  4. AMT2K5
    Replies:
    1
    Views:
    201
    Eric Schwartz
    Nov 8, 2005
  5. Replies:
    23
    Views:
    534
    Tim Streater
    Dec 18, 2013
Loading...

Share This Page