Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Numbers

Status
Not open for further replies.
Jun 16, 2005
52
US
Can some help me write a script? The script needs to open a file, read a column( <ID #> ) for ID's and give total count ID's present in that column. The file is a flat text file and is tab delimited. Although I realize this is probably a simple task, I am horrible at Perl. Can some help me write the script or point me in the right direction? Any help of any kind would be great. Thank you.


ex:


<ID #> <Name>
00000 Joe
00000 John
00000 Sam
 
This really really sounds like school/class work. Please post the code you have tried so far if you want assistance.

- Kevin, perl coder unexceptional!
 
I am horrible at Perl
Why don't you write it in a language you are familiar with, then?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
You should listen both Kevin and Steve. People can help you here if you actually show effort in whatever your problem is.

Nevertheless, it amused me to come up with a one liner for this. Which probably won't be of much use to you.

Code:
$ perl -e 'while (<>) {$c += /\d+/g} print $c' nums.txt
3

Enjoy.
 
Well it wasn't class work nor did I have anything to go on before posting this. At least I got some amusing answers. Thanks for at least reading the post.
 
Please don't be offended AquaTeenFryMan,

Your question does read like it's a class assignment. If it's not, then fine. Do you still want some help?

- Kevin, perl coder unexceptional!
 
well, in the spirit of the season:

Code:
my %count = ();
open(FH,'file.txt') or die "$!";
while (my @stuff = split(/\s+/,<FH>)){
   $count{$stuff[0]}++;
}
close(FH);
print map {"$_ = $count{$_}\n"} sort {$a <=> $b} keys %count;




- Kevin, perl coder unexceptional!
 
Thanks. I wasn't offended...I am never easily offended. I do understand how someone would see that as a class project. Thank you for your help. These forums have always been a great help to me. I would have offered up code to start the discussion, but I didn't know where to start. Thank and Happy Holidays!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top