Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: Problem with push() Function by GrandFather
in thread 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    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: (8)
    As of 2017-10-21 21:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My fridge is mostly full of:

















      Results (270 votes). Check out past polls.

      Notices?