virtualweb has asked for the wisdom of the Perl Monks concerning the following question:


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.


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:

new york

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


Replies are listed 'Best First'.
Re: Problem with push() Function
by ikegami (Pope) on Jul 22, 2009 at 23:12 UTC
    To common methods.

    Group and peek at previous:

    my @country_states; for (@logfiles) { my $country_state = $_; # Avoid clobbering @logfiles $country_state =~ s/.txt//; $country_state =~ s/_/ /g; push @country_states, $country_state; } @country_states = sort @country_states; my $last_country; for my $country_state (@country_states) { my ($country, $state) = split(/-/, $country_state, 2); if (!defined($last_country) || $last_country ne $country) { $last_country = $country; print(uc($country), "\n"); } print($state, "\n"); }

    Group into a multi-dimensional structure:

    my %states_by_country; for (@logfiles) { my $country_state = $_; # Avoid clobbering @logfiles $country_state =~ s/.txt//; $country_state =~ s/_/ /g; my ($country, $state) = split(/-/, $country_state, 2); push @{ $states_by_country{$country} }, $state; } for my $country (sort keys %states_by_country) { print(uc($country), "\n"); my $states = $states_by_country{$country}; for my $state (sort @$states) { print($state, "\n"); } }
Re: Problem with push() Function
by ssandv (Hermit) on Jul 23, 2009 at 16:53 UTC
    I suspect you mean
    on the offchance that a state ever changes their name to contain the letters "txt". The . happens to match a real . in this case, but that regex probably doesn't exactly say what you mean it to.
Re: Problem with push() Function
by GrandFather (Sage) on Jul 23, 2009 at 21:09 UTC
    What am I doing wrong..??

    1/ Using a faulty keyboard that repeats punctuation.

    2/ Not using strictures (use strict; use warnings;).

    3/ Using inconsistent indentation

    4/ Either not using copy and paste or showing us code that you haven't run (several print statements are missing semicolons).

    5/ Using redundant interpolation ("$final_country", "$splitted_country_name1", ...).

    6/ You don't perform sanity checking (especially following the country name split).

    7/ Hand rolling HTML rather than using one of the many HTML writing modules (HTML).

    8/ not using lexical file or dir handles.

    However, the immediate problem is related to 2/. Because you are not forced to consider where variables need to be declared and thus need to think about their lifetime, you haven't noticed that @single_states is global to the nested for loops and thus has successive groups of states concatenated on to the end of the list. The following cleaned up code may help:

    use warnings; use strict; use HTML::EasyTags; opendir my $LogDirScan, "."; my @logfiles = readdir $LogDirScan; closedir $LogDirScan; my @countries; my @single_countries; if (@logfiles) { foreach my $filename (@logfiles) { $filename =~ s/\.txt/ /; push (@countries, "$filename"); } } foreach my $country_data (@countries) { my @splitted_country_name = split (/\-/, $country_data); push @single_countries, $splitted_country_name[0]; } my %saw; my @unique_countries = grep (!$saw{$_}++, @single_countries); my $html = HTML::EasyTags->new(); print $html->start_html (); print $html->table_start (); foreach my $final_country (@unique_countries) { next if ! length $final_country; my $no_line_country = $final_country; my @single_states; # Countries like EL_Salvador or Costa_Rica need to have underscore +s removed $no_line_country =~ s/_+/ /g; foreach my $Country_File (@countries) { my @splitted_country_name = split (/\-/, $Country_File); next if ! $#splitted_country_name; next if $final_country !~ /$splitted_country_name[0]/; my $final_state = $splitted_country_name[1]; my $no_line_state = $final_state; $no_line_state =~ s/_/ /g; my $State_Row = $html->tr ($html->td ($no_line_state)); push (@single_states, $State_Row); } print $html->tr ($html->td ($no_line_country)); print "@single_states"; } print $html->table_end (); print $html->end_html ();

    True laziness is hard work