Problem with push() Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • virtualweb
    New Member
    • Aug 2007
    • 30

    Problem with push() Function

    Hello:

    Im trying to do a web directory similar to Yellow Pages based in a flat file database.

    My script saves info in a folder named COUNTRIES in the following manner.
    $Country-$State.txt

    OK, now I need to use this data to present it on the browser. I want to make a one column table so each row in the column will be a different Country. Inserted between countries, in the same column I want to display the respective States registered.

    Imagine for USA and Mexico would be:


    USA
    california
    new york
    florida
    MEXICO
    cuernavaca
    veracruz
    baja california
    FRANCE
    etc

    I went OK as far as the first step (Countries) and ran into trouble when trying to display States as follows:

    Code:
     opendir LOGDIR, "COUNTRIES"; 
     @logfiles = readdir (LOGDIR);
     closedir (LOGDIR);
    
     if (@logfiles) {
        foreach $filename (@logfiles) {
        $filename =~ s/.txt/ /;
        push (@countries, "$filename");
        }
      }
     
    
    
    foreach $country_data (@countries) {
    @splitted_country_name = split(/\-/,$country_data );
    push (@single_countries, "$splitted_country_name[0]");
    }
      
    undef %saw;
    @unique_countries = grep(!$saw{$_}++, @single_countries);
      
    
    print"<table>"
    
    foreach $final_country (@unique_countries) {
    
    #### Countries like EL_Salvador or Costa_Rica need 
    ####  to have underscores removed
    
    $no_line_country = "$final_country";
    $no_line_country =~ s/_/ /g; 
    
    foreach $Country_File (@countries) {
    @splitted_country_name = split(/\-/,$Country_File );
    
    if($final_country =~ /$splitted_country_name[0]/){#1
    $final_state = "$splitted_country_name[1]";
    $no_line_state = "$final_state";
    $no_line_state =~ s/_/ /g;
    $State_Row ="<tr><td>$no_line_state</td></tr>";
    push (@single_states, "$State_Row"); 
    }
    }
    
    print"<tr><td>$no_line_country</td></tr>"
    
      print "@single_states";
    }
    
    
    print"</table>"
    OK the above code displays a single column table with each country in a row and inserts the states of the first country right below it. Then displays the states of the first and second country right below the second country. Then all the states of the first three countries below the third country,etc. I only need to display the states corresponding to each country right below the country name.

    What am I doing wrong..??
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    You're using the wrong data structure.

    If the list needs to be ordered by strings, then you should use a hash, not one or more arrays. In this case, you should be using a HoH ( hash of hashes ). The 1st level key will be the countries and the second level key will be the states.

    Comment

    Working...