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
yucatan
FRANCE
etc

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

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..??

VirtualWeb


In reply to Problem with push() Function by virtualweb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.