giany has asked for the wisdom of the Perl Monks concerning the following question:

Hy,

1).I`m trying to write a code that will show smth like this(It seems HTML has the same problem) :

User1 [Ok] USer100 [Ok] User1000 [Ok]


The problem is that when I print using this :

print color("white"), "\n $elt ", color("reset") . color("green") , "\ +t\t\t [Ok]", color("reset");

I get this :

User1 [Ok] USer100 [Ok] User1000 [Ok] ............... [Ok] ............ [Ok]

I want all to be in a corect order.[Ok] to be under an other [Ok].

Any hints?

2).The second issue is this : I`m tring to count smth and at the end to show only the things i`m interesed.. the code looks like this:

foreach $i ($elt) { $check = system($check . $elt); if ( $check > 0 ) { } elsif ( $check = 1 ) { chomp($elt); print color("white"), "\n $elt ", colo +r("reset") . color("green") , "\t\t\t [Ok]", color("reset"); } }

I want to count only the things i get on the elsif loop.

How do I do that?


Thank you!

Edit: g0n - added code tags

Replies are listed 'Best First'.
Re: Counter && print issue
by SamCG (Hermit) on Oct 07, 2005 at 16:23 UTC
    I'm not entirely sure what you're asking, so I'm afraid I can't help very much, though I can point out a few things you might want to be careful of:

    print color("white"), "\n $elt ", color("reset") . color("green") , "\ +t\t\t Ok", color("reset");
    I'm sure this is in a loop of some type. If you want to format your output you should probably use printf, the print format command (though I'm unsure of how to mix the colors in there).
    foreach $i ($elt) { $check = system($check . $elt); if ( $check > 0 ) { } elsif ( $check = 1 )
    this statement will assign a value of 1 to $check, so what you're saying here is "if $check is 0 or less, assign a value of 1 to it and continue with the block". This may not be what you want.

    Hope this helps...update: and you may want to
    use warnings;
    at the top of your script.
Re: Counter && print issue
by sgifford (Prior) on Oct 07, 2005 at 17:00 UTC
    For the first part of your question, I think you want printf, which allows specifying column widths. Something like this (not tested):
    printf "%s\n%25s%s%s%4s%s",color('white'),$elt, color('reset'),color('green'),'[Ok]',color('reset');

    For the second part of your question, to count the number of times system returned 1, just declare a variable before the loop starts, and inside that elsif put $your_variable++. As others have mentioned, $? is probably closer to what you want; you have to muck about with the bits returned by system to get the program's exit status; see the documentation for details

      Thx for your input..i`ve tried what you said and now it gets like this :
      WAA[Ok] Longname.......[Ok] Onotherlognname...[Ok] Smalln[Ok]
      And I wanted to look like :
      WAA [Ok] Longname....... [Ok] Onotherlognname... [Ok] Smalln [Ok]
        Use a negative field width to get it left-aligned (yes, it's non-intuitive, but printf has been that way for a long time):
        printf "-26s %4s\n","WAA","[Ok]";
Re: Counter && print issue
by Hue-Bond (Priest) on Oct 07, 2005 at 18:04 UTC
    foreach $i ($elt)

    You have to declare a counter outside of the loop and increment it when needed. Also, that code will run only once since foreach expects a list and $elt isn't one. Thus, there's no need to count anything. What about (untested):

    system($check . $elt); if (1 == $? >> 8) { chomp $elt; print blah blah; }

    If, on the other hand, $elt is a reference to an array, you may be wanting something along this lines:

    my $count; foreach $i (@$elt) { # @$elt is an abbreviation of @{$elt} -- see + perlreftut system($check . $i); # using $i instead of $elt if (1 == $? >> 8) { chomp $i; print blah blah; # use $i here $count++; } } ## use $count here

    UPDATE: Typo fixed "$@" -> "@$". Thanks to Errto.

    --
    David Serrano

Re: Counter && print issue
by pg (Canon) on Oct 07, 2005 at 16:53 UTC

    To answer your second question. Checking the return code from system() is useless towards what you wanted to do. What you want to check is $?. That perlvar has an English name $CHILD_ERROR, which clearly explains itself ;-)

Re: Counter && print issue
by marto (Cardinal) on Oct 07, 2005 at 16:14 UTC
Re: Counter && print issue
by giany (Acolyte) on Oct 08, 2005 at 18:25 UTC
    I managed to make it work.. here is the complete code:
    use warnings; my $count = 0; foreach $i ($elt) { $execute = system($command . $i); if ( $execute > 0 ) { $counter++; chomp($i); printf "%s\n%-45s%s%s%4s%s",color('white +'),$elt, color('reset'),color('red'),'[DEAD]',color('reset'); } elsif ( $execute == 1 >> 8) { chomp($i); printf "%s\n%-45s%s%s%4s%s",color('whit +e'),$elt, color('reset'),color('green'),'[ Ok ]',color('reset'); $count++; } } } } print "$count";
    Everything works fine now..but I have an other question : The foreach loop executes that  system($command . $i); command until it reaches the end of  $elt and this thing takes a few minutes.. is there a way to execute that command in a second for every $elt? Thx