• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Does anyone know a good program to count text

TrukinDave

Diamond Member
Hello

I am looking for a program to count numbers in a text file: I have found a couple but they dont count the way I am needing. The program has about 800,000 numbers that needs to be counted:

Here is a example of what I am needing a program to count:
Take this text:

10 20 30 40 10 60 70 10
20 30 20 20
5 5 5 5 5 5

Results I am needing

5 X 6
10 X 3
20 X 4
30 X 2
40 X 1
60 X 1
70 X 1

Most will count like this: These I cannot use

18 Counts entire text

or

0 X 12
1 X 3
2 X 4
3 X 3
4 X 1
5 X 6
6 X 1
7 X 1
 
I wrote a small script, although you'd have to reformat your text file a little more, for some reason. An example:

numbers.txt (2 lines, single-digit numbers next to two-digit numbers):

Code:
10 20 30 40 10 60 70 10
20 30 20 20 5 5 5 5 5 2 2 2 6 6 6 8 8 8

And then the code:

PHP:
<?php

// Loads file, and separates each number by a space

$numbers = file_get_contents('numbers.txt');
$sep     = explode(' ', $numbers);
$arr = array();

for ($i = 0; $i < count($sep); $i++)
{
	array_push($arr, $sep[$i]);
}

function findNumber($num, $array)
{
	$counter = 0;
    foreach ($array as $key => $value) 
    {
	   if ($value == $num)
	   {
		  $counter++;
	   }
    }

    return $num .' X '. $counter;
}

// The first parameter is the specific number (you want to know how many times it occurs)

echo findNumber(70, $arr);


?>

Not awesomely written, and you could obviously write it in OOP, but that would be a waste of time (in my opinion). Hope that works.
 
you could obviously write it in OOP

That would be like building a tank to kill a mouse. 😉

Here, have a Perl 2-liner:
Code:
map { map { ++$counts{$_} } split(/\s+/) } (<STDIN>);
map { print "$_ x $counts{$_}\n" } (sort { $a <=> $b } keys %counts);
(accepts any number of lines of text, with numbers delimited by any number of whitespace chars)

Sample usage/output:
Code:
C:\Temp>type numbers.txt
10 20 30 40 10 60 70 10
20 30 20 20
5 5 5 5 5 5

C:\Temp>foo.pl < numbers.txt
5 x 6
10 x 3
20 x 4
30 x 2
40 x 1
60 x 1
70 x 1
 
Last edited:
Sorry its taking me this long to reply back I am a truck driver and have been out for a while.

I am not a programmer LOL. I wouldn't know what to do with those codes on my best day 🙂

Is here a program that can be downloaded that can do that, I always wanted to learn programming just never had the time.

Thanks for any help 🙂
 
You might be able to work something out in Excel, but otherwise, not really.

I guess my best guess would be to open it in Word, and manually do a find/replace for each possible number. (replace all occurrences of "20" with "20", make sure you have the "whole words only" checkbox checked.)

It will report the number of replacements made, which is the number of occurrences in the document.
 
Out of curiosity, why did you request that the output be in a specific format, then? That usually is needed only when you need to feed the results into some other script or program...

In any case, the Perl option is still by far the easiest, even for someone who has never touched code in their life.

1) Copy and paste the 2 lines of Perl code into Notepad and save it with a .pl file extension, e.g., counter.pl

2) Install ActivePerl (I assume you're on Windows?)

3) Dump the numbers you want counted into a text file, e.g., numbers.txt; for convenience, save it in the same directory as counter.pl

4) Open a command prompt in the directory containing counter.pl, and type counter.pl < numbers.txt
 
Last edited:
Out of curiosity, why did you request that the output be in a specific format, then? That usually is needed only when you need to feed the results into some other script or program...

In any case, the Perl option is still by far the easiest, even for someone who has never touched code in their life.

1) Copy and paste the 2 lines of Perl code into Notepad and save it with a .pl file extension, e.g., counter.pl

2) Install ActivePerl (I assume you're on Windows?)

3) Dump the numbers you want counted into a text file, e.g., numbers.txt; for convenience, save it in the same directory as counter.pl

4) Open a command prompt in the directory containing counter.pl, and type counter.pl < numbers.txt

That was a neat 2 liner you wrote code65536
installed ActivePerl and worked like you said.

Then installed their 15 day trial Perl Dev Kit to make a standalone executable and that worked.

Are there any free versions (either Linux or Windows)
to convert perl scripts into standalone Windows executeables ?
 
That was a neat 2 liner you wrote code65536
installed ActivePerl and worked like you said.

Then installed their 15 day trial Perl Dev Kit to make a standalone executable and that worked.

Are there any free versions (either Linux or Windows)
to convert perl scripts into standalone Windows executeables ?

The PDK isn't really a compiler--all it does is take your Perl script and bundle it with their Perl interpreter into a single file. If you run a PDK-created executable, you'll notice that it actually extracts some temp files as it is run. So, given that, it should be relatively easy for someone to make their own "compiler" if they really felt such an urge.
 
So, given that, it should be relatively easy for someone to make their own "compiler" if they really felt such an urge.

relatively easy for who ? 😛

(they sure packed a lot into a final 1.2mb file)


edit:
installed PAR😛acker into Activestate and it seems to work just as well
-the smile popped in there on its own
 
Last edited:
relatively easy for who ? 😛

(they sure packed a lot into a final 1.2mb file)


edit:
installed PAR:Packer into Activestate and it seems to work just as well
-the smile popped in there on its own

Yes, the PDK creates quite a large executable because of everything that it has to bundle. It also bundles any external Perl modules that a script references, as well as their dependencies, in which case, it can become even larger. You can mitigate this somewhat by running it through UPX afterwards.

(Also, "relatively easy", as in, compared to the alternatives like making a real compiler, a bundle/wrapper around an existing interpreter is, relatively speaking, very easy.)
 
now I just got to figure out how your 2 lines of code do what they do 🙂
I'm almost able to understand Aldon's code but have no idea how yours works
 
Last edited:
now I just got to figure out how your 2 lines of code do what they do 🙂
I'm almost able to understand Aldon's code but have no idea how yours works

Perl (which was designed by someone with background in linguistics) can be like poetry. There are many ways to express something, and there can be a lot of meaning that is implicit and subtle.

Python is like a technical whitepaper. The style is strict, much of it is explicit, and Python tries to avoid having many different ways of expressing / doing the same thing.

If you have to understand and maintain someone else's code, Python is the best. But it's also a boring, stifling language. Perl can be beautiful (sigils not withstanding), but trying to understand someone's Perl code can sometimes be like trying to parse the meaning of a poem in English class. For better or for worse (and sadly, most people think worse), Perl is the programming language that comes closest to the flexibility of human language. (Perl can also be boring and dry--it depends on how it's written. But at least it offers you that freedom to choose.)

Anyway, this should help you get started.
 
Last edited:
TextPad will do that. You can view properties for your current file and see a word count, edit/insert a line of statistics that includes word count and more.
 
Back
Top