Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Printing After A While Loop

Reply
Thread Tools

Printing After A While Loop

 
 
Bla
Guest
Posts: n/a
 
      01-25-2005
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????
 
Reply With Quote
 
 
 
 
Eric Schwartz
Guest
Posts: n/a
 
      01-25-2005
Bla <(E-Mail Removed)> writes:
> 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
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-25-2005
Bla <(E-Mail Removed)> wrote:

> while {
> test
> }



This is the Perl newsgroup. We discuss Perl here.

Did you have some _Perl_ code that you wanted to ask about?


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Bla
Guest
Posts: n/a
 
      01-25-2005
On Mon, 24 Jan 2005 17:35:34 -0700, Eric Schwartz <(E-Mail Removed)>
wrote:

>Bla <(E-Mail Removed)> writes:
>> 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


#!/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;
#}



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-25-2005
Bla <(E-Mail Removed)> wrote:
> On Mon, 24 Jan 2005 17:35:34 -0700, Eric Schwartz <(E-Mail Removed)>
> wrote:
>
>>Bla <(E-Mail Removed)> writes:
>>> 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

>
> #!/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 ' }
}



--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      01-25-2005
Bla <(E-Mail Removed)> wrote:

: #!/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;

 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      01-26-2005
(E-Mail 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
Opinion poll: for loop vs while loop with Iterators. Daniel Pitts Java 14 12-23-2006 05:22 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM



Advertisments