By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,712 Members | 1,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,712 IT Pros & Developers. It's quick & easy.

Problem with push() Function

P: 30
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  opendir LOGDIR, "COUNTRIES"; 
  3.  @logfiles = readdir (LOGDIR);
  4.  closedir (LOGDIR);
  5.  
  6.  if (@logfiles) {
  7.     foreach $filename (@logfiles) {
  8.     $filename =~ s/.txt/ /;
  9.     push (@countries, "$filename");
  10.     }
  11.   }
  12.  
  13.  
  14.  
  15. foreach $country_data (@countries) {
  16. @splitted_country_name = split(/\-/,$country_data );
  17. push (@single_countries, "$splitted_country_name[0]");
  18. }
  19.  
  20. undef %saw;
  21. @unique_countries = grep(!$saw{$_}++, @single_countries);
  22.  
  23.  
  24. print"<table>"
  25.  
  26. foreach $final_country (@unique_countries) {
  27.  
  28. #### Countries like EL_Salvador or Costa_Rica need 
  29. ####  to have underscores removed
  30.  
  31. $no_line_country = "$final_country";
  32. $no_line_country =~ s/_/ /g; 
  33.  
  34. foreach $Country_File (@countries) {
  35. @splitted_country_name = split(/\-/,$Country_File );
  36.  
  37. if($final_country =~ /$splitted_country_name[0]/){#1
  38. $final_state = "$splitted_country_name[1]";
  39. $no_line_state = "$final_state";
  40. $no_line_state =~ s/_/ /g;
  41. $State_Row ="<tr><td>$no_line_state</td></tr>";
  42. push (@single_states, "$State_Row"); 
  43. }
  44. }
  45.  
  46. print"<tr><td>$no_line_country</td></tr>"
  47.  
  48.   print "@single_states";
  49. }
  50.  
  51.  
  52. 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..??
Jul 15 '09 #1
Share this Question
Share on Google+
1 Reply


Expert Mod 100+
P: 589
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.
Jul 15 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.