Counting strings in a txt file

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

fathom274
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Oct 09, 2012 11:17 am

Counting strings in a txt file

Postby fathom274 » Tue Oct 09, 2012 11:25 am

Hi I have a text file that I want to parse and count the strings.
The txt file looks like this;
087654321:987654321:987654321:987654321:1232456789 ........ up to 10M

I am able to read the text file and explode the results;

Code: Select all

$txt_file = file_get_contents('./10M.txt');
$pieces = explode(":", $txt_file);
$passed =1;
$failed= 0;


I would like to assign either 'PASSED' or 'FAILED' to each $pieces and then count the number of passes and fails.

Code: Select all

foreach($pieces as $row)
{
    if ($row == 987654321){
$count = $passed;
}
    else {
$count = $failed;
}
}


I have tried several ideas and methods using count, str_len, str_count etc.. but I can never seem to get the correct count.

Any Ideas?

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Counting strings in a txt file

Postby seandisanti » Tue Oct 09, 2012 1:14 pm

what about...

Code: Select all

    $txt_file = file_get_contents('./10M.txt');
    $pieces = explode(":", $txt_file);
    $pieces = array_map(checkit,$pieces);
   $passed = array_sum($pieces);
   $failed = count($pieces)-$passed;
   

   function checkit($value)
   {
      return ($value==987654321)?1:0;
   }

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Counting strings in a txt file

Postby egami » Tue Oct 09, 2012 1:50 pm

100 ways to skin this cat :)


Code: Select all

<?php

$passed 
0;
$failed 0;

$file get_file_contents('myFile.txt');
$lines explode(':',$file);
foreach(
$lines as $line) {
 ((
$line == '987654321') ? $passed += $failed += 1);
}

echo 
"\r\n
Passed: $passed \r\n
Failed: $failed \r\n
"
;
 

fathom274
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Oct 09, 2012 11:17 am

Re: Counting strings in a txt file

Postby fathom274 » Tue Oct 09, 2012 2:36 pm

Much thanks, I was rather perplexed why my choices weren't working, I assumed there was a '100 way to skin this cat', but I guess my approach was faulty.

Solution 1: Can't get it to work
Solution 2: Count to 44445 MAX (have 10 000 000 results to parse)
Will work on it some more and see if I can tweak it.

PS: Running in Client not Browser

Thanks to you both :)

Alex

fathom274
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Oct 09, 2012 11:17 am

Re: Counting strings in a txt file

Postby fathom274 » Tue Oct 09, 2012 3:03 pm

Count should read
Passed : 9999998
Failed: 2


But; I get a count;
Passed : 1111110
Failed : 2

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Counting strings in a txt file

Postby egami » Tue Oct 09, 2012 3:09 pm

Code: Select all

<?php

$passed 
0;
$failed 0;
$letscount 0;


$file get_file_contents('myFile.txt');
$lines explode(':',$file);

$total_count count($lines);

foreach(
$lines as $line) {
 
$letscount++;
 ((
$line == '987654321') ? $passed += $failed += 1);
}

echo 
"\r\n
Passed: $passed \r\n
Failed: $failed \r\n
Counted: $letscount \r\n
Total Lines: $total_lines\r\n 
"
;
  

fathom274
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Oct 09, 2012 11:17 am

Re: Counting strings in a txt file

Postby fathom274 » Tue Oct 09, 2012 4:50 pm

I solved it, there are 10M numbers but only 1111110 complete string and one broken string and one disimilar string,

So effectively the count was right.

The results concur with the initial calculation and expected results.

I've re-run the original program out to 90M, but Unix kills the process past that, so I escaped it into 190M files at 90M each to give me results out to 1.71×10¹⁶ and the results always return 1 failed (the first which is 087654321) 1.9×10¹⁵ Passed and one incomplete.

Much thanks,

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Counting strings in a txt file

Postby seandisanti » Wed Oct 10, 2012 8:55 am

can you attach the file? i kind of want to test mine and see why it didn't work


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests