Perl

Moderators: Jonathan
Number of threads: 1259
Number of posts: 3644

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
count Posted by jgr on 10 Feb 2004 at 10:00 AM
Hi ! :)
For example I've got something like this:

$users = `users`
;
# command users show actually logged users in system, for example
# now show us this:
# jgr root root jgr greg n0c

@data = split (/ /, $users);

# so in @data i've got: "jgr", "root", "root", "jgr", "greg", "n0c"

eh, end now, how can I count how many each elemenets is in @data, I mean, for example how many "jgr", is in @data ?
But contents of @data is not always the same.
Sorry for my poor english :))
Ok, I think thats all :)

jgr
Report
Re: count Posted by Jonathan on 10 Feb 2004 at 12:28 PM
Hi,

: For example I've got something like this:
:
: $users = `users`
: ;
: # command users show actually logged users in system, for example
: # now show us this:
: # jgr root root jgr greg n0c
:
: @data = split (/ /, $users);
:
: # so in @data i've got: "jgr", "root", "root", "jgr", "greg", "n0c"
:
: eh, end now, how can I count how many each elemenets is in @data, I
: mean, for example how many "jgr", is in @data ?
: But contents of @data is not always the same.
print scalar(@data); # Will print number of elements
print $#data; # Will print the highest array index.

Basically, an array evaluated in scalar context will show the number of elements in the array.

: Sorry for my poor english :))
No worries, I understood.

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: count Posted by jgr on 10 Feb 2004 at 1:23 PM
: print scalar(@data); # Will print number of elements
: print $#data; # Will print the highest array index.
:
: Basically, an array evaluated in scalar context will show the number of elements in the array.
:


:)
Ok, supose in @data = ("jgr", "root", "jgr"); is 2 "jgr",
scalar (@data) give number of elements so 3 :)
so how can count how many elements "jgr" is in @data, no all elements of @data, but only "jgr" :)

: : Sorry for my poor english :))
: No worries, I understood.

ufff.. :)))

jgr
Report
Re: count Posted by Jonathan on 10 Feb 2004 at 1:36 PM
: : print scalar(@data); # Will print number of elements
: : print $#data; # Will print the highest array index.
: :
: : Basically, an array evaluated in scalar context will show the number of elements in the array.
: :
:
:
: :)
: Ok, supose in @data = ("jgr", "root", "jgr"); is 2 "jgr",
: scalar (@data) give number of elements so 3 :)
: so how can count how many elements "jgr" is in @data, no all
: elements of @data, but only "jgr" :)
:
Use the grep statement.

@data = grep { $_ eq 'jgr' } @data;

Then @data only contains the two jgr elements. Of course, you can wrap that up a bit:-

print scalar grep { $_ eq 'jgr' } @data;

Will print 2 given the array you gave above as an example.

: : : Sorry for my poor english :))
: : No worries, I understood.
: ufff.. :)))
:
Not so sure what ufff means.

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: count Posted by jgr on 10 Feb 2004 at 3:12 PM
: Not so sure what ufff means.

Heheh, no bother about this :) & Thanks ;]

jgr
Report
Re: count Posted by Jonathan on 10 Feb 2004 at 3:59 PM
: : Not so sure what ufff means.
:
: Heheh, no bother about this :) & Thanks ;]
:
Welcome. :)

Jonathan


###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.