Perl Sort Question

Discussion in 'Programming' started by pc_user, Sep 4, 2007.

  1. #1
    I have a list of directories:

    /1-30/
    /31-44/
    /45-55/
    /56-91/
    /92-201/

    How do I identify which directory is 41 being which directory has 41 in it? i.e. 31-44

    Additionally, when I get into that directory, I have these files:

    1000-93020.txt
    93020-294384.txt
    294385-399483.txt
    399483-994945.txt

    How do I find 94094, same concept with 94094 in between the data sets, i.e. 93020-294384.txt

    What is the fastest way to do this without having to list out everything or search through everything?
     
    pc_user, Sep 4, 2007 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2
    Still need help?
     
    NetStar, Dec 13, 2007 IP
  3. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #3
    Assuming there is no set pattern for directory names (ie. 1-10, 11-20, 21-30, 31-40, ..., etc.) you can't mathematically figure out which directory contains 41 without fetching a list of directories and using some clever algorithm.

    I'll give you two methods that I personally would do.

    #1 the lightning fast method.

    Create a mySQL table with 3 columns. bottom_range, top range, and directory. Place an index on bottom_range and top_range. Looking at the directory "/31-44/" the bottom_range would be 31, top_range 44, and directory obviously /31-44/.

    If you have a value of 41 and need to figure out which directory 41 is... then fire off this SQL.

    SELECT directory FROM SOME_TABLE WHERE bottom_range >= '41' AND top_range <= '41';

    Then boom you have your directory. This is idea if you have MANY directories... ie. thousands


    #2 would require you grabbing a list of directories. Looping through them and splitting the directory names by using the '-' as a delimiter. Then checking if the first slice is greater then or equal to, and the second slice is less than or equal to... once statement is true end the loop because the search is over.. you've found your directory (or file).


    I would go with #1. It seems more productive and neater. What are you currently doing?
     
    NetStar, Dec 13, 2007 IP
  4. pc_user

    pc_user Notable Member

    Messages:
    1,891
    Likes Received:
    94
    Best Answers:
    0
    Trophy Points:
    235
    #4
    Actually, the purpose of this exercise was a text based system, mysql has several options available beyond the mentioned but due to the nature of this project, that's not an option.

    Standard loop / finds are inefficient for very large data sets. There are formulas that provide virtual index capabilities to directory listings based on pre set formulas. Just thought someone may have some insight.

    Thanks for the help.
     
    pc_user, Dec 13, 2007 IP