473,379 Members | 1,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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:

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
1 2355
RonB
589 Expert Mod 512MB
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

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

Similar topics

1
by: TheOne | last post by:
I have two classes: class OntologyParser { ... protected: virtual void startElement(void *userData, const char *name, const char **atts); virtual void endElement(void *userData, const char...
2
by: enio | last post by:
Hi, I intend to get some answer from postgres concerning if an update command has altered some records in the database. To do this I coded this function: create or replace function...
0
by: Crystal | last post by:
I have created a small program that prints out an organization chart in PHP using the GD lib's imagecreate, imageline and imagestring functions. Basically, a user selects an employee from a...
6
by: mforema | last post by:
Hi Everybody, I have a function that worked perfectly when it was used in a query. I received help in creating this function from this discussion: Help with Query and Subform - post #18 ...
8
by: cpptutor2000 | last post by:
Could some C guru please help me? I have a function that takes as an argument a pointer to an array of unsigned chars (basically a hex representation of a dotted decimal IP address). When I print...
4
jenkinsloveschicken
by: jenkinsloveschicken | last post by:
I imagine this is a n00b mistake, but the problem I am having is that the first function(stratQual) is evaluating to "No" despite the argument being passed to it being greater than 80.00. The second...
3
by: viki1967 | last post by:
Hi there. I have a problem with function updateSum(id). The code is this: <script language="javascript" type="text/javascript"> <!--
4
by: StephQ | last post by:
I need to know if it is possible to solve the following problem (I am really stuck at it). Consider the CRTP pattern: struct Derived { ....// May contain void operate( double x ) , may not....
2
by: presencia | last post by:
Hi everyone, I have written a small program running fine until a Segmentation fault. Using a debugger I have localized the function the fault happens in. The Signal appears during a call to delete...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.