How to count different lines

Discussion in 'PHP' started by Voimis, Oct 4, 2007.

  1. #1
    How to count different lines?

    So I mean if I have "something.txt" and there is 10 lines where is four lines printed "X" and six lines printed "B" it should count "2".


    Understand?

    Thanks!
     
    Voimis, Oct 4, 2007 IP
  2. intoex

    intoex Peon

    Messages:
    414
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if file not big, then
    <?
    $lines = file('something.txt');
    sort($lines);
    $cnt = count($lines);
    $res=($cnt)?1:0;
    for($i=1; $i<$cnt; $i++) {
    if ($lines[$i]!=$lines[$i-1]) $res++;
    }

    echo $res;
    ?>

    it's idea, didn't tested it though, but must work
     
    intoex, Oct 4, 2007 IP
  3. ravish_83

    ravish_83 Peon

    Messages:
    221
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    just an idea

    read the file with file command
    $lines = file('something.txt');
    PHP:
    now the all line are in the $lines array

    let's say the value of array look like this

    lines=array("Mango","Banana","Orange","Banana"); 
    $new_array=array_unique($lines); 
    PHP:
    Remove the duplicate lines with unique_array function
    Now our $new_array will contain values ("Mango","Banana","Orange")

    $unique_lines = count($new_array);
    PHP:
    $unique_lines is What you require
     
    ravish_83, Oct 4, 2007 IP
  4. Voimis

    Voimis Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank to you guys!

    Ravish´s idea will work.



    I have also another question which will make my script a lot easier..

    Is there possibility to count only something from lines in "something.txt"?
    I mean I have all of the lines first "X" or "B" or "C" and then comes " - " and the line continues.
    So one line looks like:

    X - something...

    Can I count everything what comes before " - " ?


    Thanks!
     
    Voimis, Oct 4, 2007 IP
  5. Voimis

    Voimis Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I made a code but it doesn´t work.

    <?php
    $file = "recordlist.txt";

    $i = 0;
    while ($i <= count($file) - 1 ) {

    $pum = explode(" - ", $file[$i]);

    $bands = file($pum[1]);
    $bands_array = array_unique($bands);
    $unique_bands = count($bands_array);


    $lines = count(file($file));
    print "CDs: $lines<br /><br />";
    echo "Different bands: $unique_bands<br /<br />";

    $i++;
    }

    $list = file('recordlist.txt');
    sort($list);

    for ($i=0; $i <= $lines; $i++)
    echo $list[$i]."<br \>";
    ?>

    It prints just "1" for $unique_bands :(:(:(

    Can you help me guys? Thanks a lot!
     
    Voimis, Oct 4, 2007 IP
  6. ravish_83

    ravish_83 Peon

    Messages:
    221
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    this seems confusing to me
    Please put the file contents and let me know what the script to do.

    I can write one quick for you :) and will be explaining what it is doing at every step

    Ravish
     
    ravish_83, Oct 4, 2007 IP
  7. Voimis

    Voimis Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I have a code with my CDlist. (It's not the one above)
    It makes strings to recordlist.txt file like this:

    Iron Maiden - 1981 - Killers
    Iron Maiden - 1982 - The Number of the Beast
    Judas Priest - 1990 - Painkiller
    Motörhead - 1979 - Overkill

    etc etc

    So. Counting how much CDs I have (lines in the recordlist.txt) is easy, but also I need to count how many different artist there is!
    It works if I $_POST["artist"] also to other .txt file (bands.txt) and count unique lines from there.

    But I think there should be easier way which counts unique lines before first " - " in recordlist.txt.


    Thanks to you Ravish, hope you understand what I mean :)
     
    Voimis, Oct 4, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    It would probably be easier to load the file contents into a string and use regular expression to get all artist names.

    
    preg_match_all('/(?:^|\n)([^-]+)/', $file_contents, $artists);
    
    // Different artists
    echo sizeof(array_unique($artists[1]));
    
    PHP:
     
    nico_swd, Oct 5, 2007 IP