Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Problem with push() Function

by GrandFather (Sage)
on Jul 23, 2009 at 21:09 UTC ( #782788=note: print w/replies, xml ) Need Help??


in reply to Problem with push() Function

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://782788]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2018-04-24 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?