Tanton,

Thanks, that's alot better now!  Out of curiosity.  At the moment the
script will read results in from a file that looks similar to the
following...

[Poll]
Newspaper = 43
Word Of Mouth = 10
Friend = 15

What if I was to change the config file manually to add the following
....

[Poll]
Newspaper = 43
Word Of Mouth = 10
Friend = 15
Other = 0

Now because I am so 'slack' I don't really want to have to change the
code.  Is it possible for the Perl code to read that new data in. Hence
me not have to change any of the code...

I think the code would look similar to the following...

while($poll) {
  read_results()
  #Check and see if $poll mactches anything from the config file...
  #Newspaper, Word Of Mouth, Friend or Other...
  #If it does then extract it and add 1 to it

}

At the moment read_results looks like this...

sub read_results {
  open FILE, "$poll_file" or warn "cannot open '$poll_file' for reading:
$!\n";
  flock(FILE, 2) || warn "Can't lock $poll_file exclusively: $!";
  while( <FILE> ) {
    $title = $1 and next if /^\s*\[\s*(.*?)\s*\]\s*$/;
    next unless defined $title && /^\s*(.*?)\s*=\s*(.*?)\s*$/;
    $filehash{$title}{$1} = $2;
  }
  close FILE;
  return %filehash;
}

Does this look stupid to you or is this actually possible?

Regards,

Da


-----Original Message-----
From: Tanton Gibbs [mailto:[EMAIL PROTECTED]]
Sent: Friday, 15 March 2002 12:48 PM
To: Daniel Falkenberg; [EMAIL PROTECTED]
Subject: Re: Writing this chuck of code a little smaller...


Well, I would think you could rewrite it like:
for( $poll ) {
  $filehash{$title}{"Newspaper"}++, last if( /newspaper/ ); 
  $filehash{$title}{"Word of Mouth"}++, last if( /wordofmouth/ );
    $filehash{$title}{"Friend"}++, last if( /friend/ );
   die "Should not get here!";
  }
  write_results( $poll_file, $filehash );
----- Original Message ----- 
From: "Daniel Falkenberg" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, March 14, 2002 8:51 PM
Subject: Writing this chuck of code a little smaller...


> D'day all,
> 
> I have the following chunk of code that I was wondering If I can write
a
> little smaller.  I was thinking a while loop.  Could some one give me
a
> little advice on this one :)...
> 
> results_read();
>     if ($poll =~ /newspaper/) {
>       print "Thank you for picking news paper!";
>       $nonewspaper = $filehash{$title}{"Newspaper"};
>       $new_newspaper_total = $nonewspaper + 1;
>       $filehash{$title}{'Newspaper'} = $new_newspaper_total;
>       write_results($poll_file, $filehash);
>     } elsif ($poll =~ /wordofmouth/) {
>       $no_wordofmouth = $filehash{$title}{'Word of mouth'};
>       $new_no_wordofmouth = $no_wordofmouth + 1;
>       $filehash{$title}{'Word of mouth'} = $new_no_wordofmouth;
>       write_results($poll_file, $filehash);
>     } elsif ($poll =~ /friend/) {
>       $no_friend = $filehash{$title}{'Friend'};
>       $new_no_friend = $no_friend + 1;
>       $filehash{$title}{'Friend'} = $new_no_friend;
>       write_results($poll_file, $filehash);
>     } else {
>       # Theoretically there shold never be a non match as we are using
> radio
>       # buttons!
>       print "No Match!";
>     }
>     results_read();
> 
> It's pretty straight foward... Basically if $poll matches what I have
> told it to match then we it goes and writes some results to a file.  I
> have a tingling feeling that I would be better off using a whilt loop.
> I just don't know where to start.
> 
> Kind regards,
> 
> Dan
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to