#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    55
    Rep Power
    14

    Best algorithm to find an entry


    Hello,

    I have a list of MAC addresses and I want to find how many times I see them (it's just a simplistic example).

    My program looks like that

    PHP Code:
    while (<LIST>)
    {
      
    $found 0;
      
    $mac $_;
      foreach 
    my $k (keys(%tab)) { if($k eq $mac) { $tab{$k}++; $found 1; } }
      if(
    $found eq 0) { $tab{$mac} = 1; }
    }
    close LIST; 
    What it does:
    - Check if the mac address is present in the hash table
    - If the mac address is found, increment the counter
    - If the mac address isn't found add the mac address with counter = 1

    My program works but I'm wondering if it's the best way, as my hash table increases it's going to take more and more time, it's not scalable.
    Is there a more efficient way to do that ?

    Thank you
    Last edited by miky; February 7th, 2017 at 08:16 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,232
    Rep Power
    1296
    Code:
    while( <LIST> ) {
        chomp;
        $tab{$_}++;
    }
    close LIST;

    Comments on this post

    • miky agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    55
    Rep Power
    14
    Originally Posted by FishMonger
    Code:
    while( <LIST> ) {
        chomp;
        $tab{$_}++;
    }
    close LIST;
    Thank you FishMonger, now that I see your answer I find my question a bit stupid

IMN logo majestic logo threadwatch logo seochat tools logo