Printing After A While Loop

B

Bla

Trying to print to a file after a while loop.....


while {
test
}

do this

can I put an else in here if the above is not true????
 
E

Eric Schwartz

Bla said:
Trying to print to a file after a while loop.....


while {
test
}

do this

can I put an else in here if the above is not true????

I quoted you fully only because I cannot for the life of me figure out
what, in fact, you are trying to do. A while loop doesn't look like
that, it looks like this:

while (some_test()) {
do_something();
}

Can you please try this question again, with actual Perl?

-=Eric
 
B

Bla

I quoted you fully only because I cannot for the life of me figure out
what, in fact, you are trying to do. A while loop doesn't look like
that, it looks like this:

while (some_test()) {
do_something();
}

Can you please try this question again, with actual Perl?

-=Eric

#!/usr/bin/perl -w
#
open (SNMPLOG, ">snmp.log") or die "could not open 'snmp.log' $!";
$ARGV[0] = 'url.log';
my %status;
while (<>) {
/ (FAILURE|SUCCESS).+?from (.+)/ and $status{$2} = $1;
}
$status{$_} eq 'FAILURE' and print SNMPLOG " 0 " for sort keys
%status;

#else {
# print SNMPLOG " 1 " for sort keys %status;
#}
 
T

Tad McClellan

Bla said:
I quoted you fully only because I cannot for the life of me figure out
what, in fact, you are trying to do. A while loop doesn't look like
that, it looks like this:

while (some_test()) {
do_something();
}

Can you please try this question again, with actual Perl?

-=Eric

#!/usr/bin/perl -w
#
open (SNMPLOG, ">snmp.log") or die "could not open 'snmp.log' $!";
$ARGV[0] = 'url.log';
my %status;
while (<>) {
/ (FAILURE|SUCCESS).+?from (.+)/ and $status{$2} = $1;
}
$status{$_} eq 'FAILURE' and print SNMPLOG " 0 " for sort keys
%status;

#else {
# print SNMPLOG " 1 " for sort keys %status;
#}


print SNMPLOG $status{$_} eq 'FAILURE' ? ' 0 ' : ' 1 ' for sort keys %status;


but I don't like that much for maintenance, I wouldn't use it in my code.

I'd "unroll" it, as that seems to make it much easier to see what's going on:

for (sort keys %status) {
if ( $status{$_} eq 'FAILURE' )
{ print SNMPLOG ' 0 ' }
else
{ print SNMPLOG ' 1 ' }
}
 
J

Jay Tilton

: #!/usr/bin/perl -w
: #
: open (SNMPLOG, ">snmp.log") or die "could not open 'snmp.log' $!";
: $ARGV[0] = 'url.log';
: my %status;
: while (<>) {
: / (FAILURE|SUCCESS).+?from (.+)/ and $status{$2} = $1;
: }
: $status{$_} eq 'FAILURE' and print SNMPLOG " 0 " for sort keys
: %status;
:
: #else {
: # print SNMPLOG " 1 " for sort keys %status;
: #}

This makes more sense than the original article. Other than setting up the
data structure, the while loop has nothing to do with the problem.

Since there are only two possible values of $status{$_}, each of which maps
directly to one output string, I'd rather ditch the if/else thing.

print SNMPLOG
{ FAILURE => ' 0 ', SUCCESS => ' 1 ' } -> { $status{$_} }
for sort keys %status;
 
J

Jay Tilton

(e-mail address removed) (Jay Tilton) wrote:

: print SNMPLOG
: { FAILURE => ' 0 ', SUCCESS => ' 1 ' } -> { $status{$_} }
: for sort keys %status;

Or using hash slices:

print SNMPLOG
@{{ FAILURE=>' 0 ', SUCCESS=>' 1 ' }}
{@status{ sort keys %status }};

That's probably going one step too far into obfuscation country.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top