Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While loop

Status
Not open for further replies.

sulfericacid

Programmer
Aug 15, 2001
244
US
is it possible to iterate over all elements of a hash EXCEPT for some?

The script below looks for ALL hash keys/values and prints them, right?
while( my ($key, $value) = each(%emails)) {

My db is setup like:
$emails{$emails} = $name;

And that while iterations works great, it loops through all the database keys and emails them. I want to add a $time key but I dont want it read through that while loop. (i want to print when the last time the script was used but if it's looped it will start sending emails to localtime).

Would I need to start a new database or is there a way to do this?

Thanks,

sulfericacid "Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
Is it possible for you to just conditionally print what you want?
Code:
while (my ($key, $value) = each(%emails))
{
    continue if $key == $time;
    ...
}
For multiple values just use another hash instead of a long string of elsif's
Code:
while (my ($key, $value) = each(%emails))
{
    continue if exists $noprint{$key};
    ...
}
 
I decided to go the other way around and you can ignore my first question. I open up two databases/two hashes (never done this before) and I am experiencing problems with the one I'm writing to. I'm only reading from one of them, I'm reading/writing from the other.

My problem is $sig is not saving, well it is essentially but it's not permanent. This is hard to explain, you can test my script at [] . You 'need' to add data to the signature field and CLICK the save checkbox. After the you do that it will do a database print and show you what has been saved on the bottom of the screen. When you reload the screen, however, $sig is not displaying itself as a default value in the Signature form field. Can anyone tell me what's wrong with the database? Is it getting overwritten somehow?

#!/usr/bin/perl -w

use strict;
use warnings;
use POSIX;
use CGI qw/:standard/;
require SDBM_File;

my %sig;
my $sigsave = "sig.dbm";
my %emails;
my $list = "list.dbm";
my $adminmail = "admin\@test.com";
my $sendmail = "/usr/lib/sendmail";
my $signature;

tie %emails, 'SDBM_File', $list, O_CREAT | O_RDWR, 0644;
if ( !tied %emails ) {
print "database unsuccessful $!.\n";
}

tie %sig, 'SDBM_File', $sigsave, O_CREAT | O_RDWR, 0644;
if ( !tied %sig ) {
print "database unsuccessful $!.\n";
}

print header, start_html('Email Management');

print "DB contents: $sig{$signature}\n";

print start_form(), table(
Tr(
td("Subject: "),
td(
textfield(
-name => 'subject',
-size => 40
)
)
),
Tr(
td("Message: "),
td(
textarea(
-name => 'message',
-columns => 40,
-rows => 5
)
)
),
Tr(
td("Signature: "),
td(
textarea(
-name => 'signature',
-default => $sig{$signature},
-columns => 40,
-rows => 5
)
)
),
Tr(
td(
checkbox_group(
-name => 'todo',
-values => [ 'use', 'save' ],
-rows => 2,
-columns => 2
),

),
td(submit)
),
),
end_form(), hr();

# rid ourselves from those nasty params
my $message = param('message');
my $subject = param('subject');
$signature = param('signature');
my $todo = param('todo');

if ( param() ) {
if ( $message && $subject eq "" ) {
print "Your subject or email content was missing.\n";
}
else {
foreach my $key ( keys %emails ) {
print &quot;Things we have: $key <br>\n&quot;;
}
print &quot;<br>\n&quot;;

while ( my ( $key, $value ) = each(%emails) ) {

# Email Subs, special commands
$message =~ s/\[name\]/$value/; #[name] = user name

open( MAIL, &quot;| $sendmail -t&quot; );
print MAIL &quot;To: $key\n&quot;;
print MAIL &quot;From: $adminmail\n&quot;;
print MAIL &quot;Subject: $subject\n\n&quot;;
print MAIL &quot;$message\n&quot;;
if ( $todo eq &quot;use&quot; && $signature ne &quot;&quot; ) {
print MAIL &quot;$signature\n&quot;;
}
print MAIL &quot;.\n&quot;;
close(MAIL);

print &quot;Email was sent to: $key !<br>&quot;;
}
}
if ( $todo eq &quot;save&quot; ) {
print &quot;<br>&quot;;
print &quot;Saving to database<br>\n&quot;;
$sig{$signature} = $signature;
print &quot;\$sig{\$signature}: $sig{$signature}&quot;;
}

}
untie %emails;
untie %sig;

&quot;Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top