Printing After A While Loop

Discussion in 'Perl Misc' started by Bla, Jan 25, 2005.

  1. Bla

    Bla Guest

    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????
    Bla, Jan 25, 2005
    #1
    1. Advertising

  2. Bla <> 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.
    Eric Schwartz, Jan 25, 2005
    #2
    1. Advertising

  3. Bla <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 25, 2005
    #3
  4. Bla

    Bla Guest

    On Mon, 24 Jan 2005 17:35:34 -0700, Eric Schwartz <>
    wrote:

    >Bla <> 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;
    #}
    Bla, Jan 25, 2005
    #4
  5. Bla <> wrote:
    > On Mon, 24 Jan 2005 17:35:34 -0700, Eric Schwartz <>
    > wrote:
    >
    >>Bla <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 25, 2005
    #5
  6. Bla

    Jay Tilton Guest

    Bla <> 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;
    Jay Tilton, Jan 25, 2005
    #6
  7. Bla

    Jay Tilton Guest

    (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.
    Jay Tilton, Jan 26, 2005
    #7
    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. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,217
    Tim Slattery
    Mar 30, 2005
  2. Daniel Pitts
    Replies:
    14
    Views:
    1,215
    Patricia Shanahan
    Dec 23, 2006
  3. Uday Bidkar
    Replies:
    4
    Views:
    476
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. gaga
    Replies:
    2
    Views:
    345
    Marcus Kwok
    Mar 5, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    354
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page