Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

array newbie...(sigh)

by ddrumguy (Acolyte)
on Mar 15, 2002 at 17:52 UTC ( #152033=perlquestion: print w/ replies, xml ) Need Help??
ddrumguy has asked for the wisdom of the Perl Monks concerning the following question:

help ! my array spits out numbers in a vertical list - i need it to be one long number on a line so i can test it later. As you can see by the commented code I have tried a few different iterations of this and all give the same thing!! Please,, what am i doing wrong? tankx bob
@h = ($netman= `ps -ef|grep -i netman|grep -v grep|wc -l`, $jobman= `ps -ef|grep -i jobman|grep -v grep|wc -l`, $mailman= `ps -ef|grep -i mailman|grep -v grep|wc -l`, $batchman= `ps -ef|grep -i batchman|grep -v grep|wc -l`, $writer= `ps -ef|grep -i writer|grep -v grep|wc -l` ); #%h = ("a", "$netman", "b", "$jobman", "c", "$mailman", "d", "$batchma +n", "e", "$writer"); @h = ("$netman","$jobman","$mailman","$batchman","$writer"); @bob = @h; print @bob; #print "[0],[1],[2],[3],[4]" #print $h {$netman} $h {$jobman} $h {$mailman} $h {$batchman} $h {$wri +ter}; #print "$netman$jobman$mailman$batchman$writer";

Comment on array newbie...(sigh)
Download Code
Re: array newbie...(sigh)
by theguvnor (Chaplain) on Mar 15, 2002 at 18:01 UTC

    I'm guessing that your input is coming to you with an extra newline (\n) character that you need to get rid of. Try

    chomp(@h); @bob = @h; #though not sure why you copy the array ;)

    ..Guv

(jeffa) Re: array newbie...(sigh)
by jeffa (Chancellor) on Mar 15, 2002 at 18:06 UTC
    Hmmm ... i _think_ i see what you are trying to do ...

    Would this be better for you?

    my @occupation = qw(netman jobman mailman batchman writer); foreach my $pation (@occupation) { `ps -ef|grep -i $pation|grep -v grep|wc -l`; }
    Each call to ps is exactly the same except who you are grepping for, so you only need to store what is different in your array.

    Also, there may be a CPAN module for interacting with ps...

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Well i tried it but it just gives me a list like this: netmanjobmanmailmanbatchmanwriter (acts like the grep isn't even working?) also how do is store the output from the array into a variable ( I need the final number to read: 1113112
      my @arraylist = qw(netman jobman mailman batchman writer); foreach my $process (@arraylist) { `ps -ef|grep -i $process|grep -v grep|wc -l`; } chomp @arraylist; #\s+/ /g for @h; print @arraylist; #@h = ($netman= `ps -ef|grep -i netman|grep -v grep|wc -l`, # $jobman= `ps -ef|grep -i jobman|grep -v grep|wc -l`, # $mailman= `ps -ef|grep -i mailman|grep -v grep|wc -l`, # $batchman= `ps -ef|grep -i batchman|grep -v grep|wc -l`, # $writer= `ps -ef|grep -i writer|grep -v grep|wc -l` # ); #%h = ("a", "$netman", "b", "$jobman", "c", "$mailman", "d", "$batchma +n", "e", "$writer"); #@h = ("$netman","$jobman","$mailman","$batchman","$writer"); #chomp @h; #\s+/ /g for @h; #@h > ;
        Wow. I can honestly say that you have completely lost me! :D

        I recommed you do what PrakashK offered for you. He is absolutely correct in his method, and i think you should really study that. The snippet that i gave you is unfortunately not very practical, i just wanted to demonstrate how to iterate through an array.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
      foreach my $pation (@occupation) { `ps -ef|grep -i $pation|grep -v grep|wc -l`; }
      Why throw away the results of ps? (What's wrong with using backticks in a void context?)

      If the intention is to count the number of instances of interesting programs from the process list, there is no need to repeat the execution of the ps command.

      my @programs = qw(netman jobman mailman batchman writer); my @all_procs = `ps -ef`; my %counts; for (@programs) { $counts{$_} = scalar grep {/$_/i} @all_procs; } print "$_ $counts{$_}," for keys %counts; print "\n";
      /prakash
Re: array newbie...(sigh)
by BeernuT (Pilgrim) on Mar 15, 2002 at 18:07 UTC
    might try doing
    chomp @h;
    as from those system calls will have extra \n on the end. That is if your wanting your output to be something like
    0     0     0     0
    If your wanting your output to be
    0 0 0 0
    then you might also try applying a substitute to the array
    s/\s+/ /g for @h;
    let me know if you need anymore help

    -bn
      as from those system calls will have extra \n on the end. That is if your wanting your output to be something like 0 0 0 0 If your wanting your output to be 0 0 0 0 then you might also try applying a substitute to the array
      s/\s+/ /g for @h;
      how do i implement the above code - will i have to use an if statement or?? let me know if you need anymore help

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://152033]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-08-28 01:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (254 votes), past polls