Results 1 to 7 of 7

Thread: How to check the frequency of an array in a txt?

  1. #1
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default How to check the frequency of an array in a txt?

    Hello ,

    Can anyone tell me what I should use to check the frequency of an array that contains the alphabet in a txt file that contains words? Thanks a lot

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    An array is not the same format as a txt file. It is a list of items, each of which may (or may not) be in the same format as the contents of the txt file. This format is called a string (basically "text" data).

    If you can give us the exact formats you're talking about we can help.

    Does the array represent a list of items that you want to track individually or as a whole?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    I want to count how many times the letters of the english alphabet are repeated in a .txt file which contains words in these format :

    apple
    add
    banana
    bird
    coach
    etc..

    and output them like this:

    a->2 times
    b->15 times
    etc

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    This should do it:
    PHP Code:
    <?php
    function reoccurences($file){
        if(!
    is_file($file)){ return false; }
        
    $file file_get_contents($file);
        
    $reoccurences = array();
        for(
    $i 0$i strlen($file); $i++){
            
    $current strtolower($file[$i]);
            if(
    preg_match('/[a-zA-Z]/i'$current)){
                if(isset(
    $reoccurences[$current])){
                    
    $reoccurences[$current]++;
                } else {
                    
    $reoccurences[$current] = 1;
                }
            }
        }
        
    ksort($reoccurences);
        return 
    $reoccurences;
    }

    print_r(reoccurences('file.txt'));
    ?>
    Jeremy | jfein.net

  5. The Following User Says Thank You to Nile For This Useful Post:

    ntin0s (03-06-2011)

  6. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    PHP Code:
    <?php
    $string 
    file_get_contents('file.txt');
    $alphabet = array('a''b''c''d''e''f''g''h''i''j''k''l''m''n''o''p''q''r''s''t''u''v''w''x''y''z');
    for (
    $x0$x26$x++) {
        
    $how_many explode($alphabet[$x], strtolower($string));
        echo 
    $alphabet[$x] . "->" . (count($how_many) - 1) . "<br />";
        unset(
    $how_many);
    }
    ?>
    Oh, I'm a little late...
    Corrections to my coding/thoughts welcome.

  7. The Following User Says Thank You to bluewalrus For This Useful Post:

    ntin0s (03-06-2011)

  8. #6
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks both! Both works perfectly!

  9. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    No problem, I'm glad to help

    Here on DD, we like to keep things organized. In an effort to do so, you have the option to set a thread to resolved when an issue is fixed. To make the status of the thread resolved:
    1. Go to your first post
    2. Edit your first post
    3. Click "Go Advanced"
    4. In the dropdown next to the title, select "RESOLVED"
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •