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

if ($onoma eq 'showlog') { $st = $db->prepare( 'SELECT * FROM logger' ); $st->execute(); while ( $row = $st->fetchrow_hashref ) { print font( {-size=>4, -color=>'Lime'}, $row->{host}, " " +x(40-length($row->{$host})) ); print font( {-size=>4, -color=>'Cyan'}, "-> ", $ro +w->{xronos} ); print font( {-size=>4, -color=>'Yellow'}, " -> ", $ro +w->{game} ); print br(); } exit 0; }
Hello Monks, Is there any other better way to write the above code? Especially the print statements, because i noticed that allthough i try to leave spaces between the vars when i run the code they are only leave one space. Why? Is printf an alternative for that?

Replies are listed 'Best First'.
Re: spaces wont be displayed
by NetWallah (Canon) on May 24, 2004 at 22:43 UTC
    It looks like you are printing to a HTML page - that will eat all whitespace.

    To preserve space in HTML, use " " for each space.

    UPDATE:Ack ! The PerMonks web server ate up my Ampersand-n-b-s-p-semicolon that was supposed to show within quotes. Here is what that line was supposed to look like:

    To preserve space in HTML, use " " for each space.

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.
      A reply falls below the community's threshold of quality. You may see it by logging in.

      You could also use <pre>. Browsers aren't required to preserve whitespace but I haven't found one that doesn't.

      Owl looked at him, and wondered whether to push him off the tree; but, feeling that he could always do it afterwards, he tried once more to find out what they were talking about.

Re: spaces wont be displayed
by cLive ;-) (Prior) on May 25, 2004 at 00:31 UTC
    In answer to your first and second questions combined:
    # amend use CGI statement use CGI qw/:standard *pre *font/; # ... print start_font({-size=>4}). start_pre; while ( $row = $st->fetchrow_hashref ) { printf("%-40s-> %s -> %s", font({-color=>'Lime'},$row->{host}), font({-color=>'Cyan'},$row->{xronos}), font({-color=>'Yellow'},$row->{game}) ). "\n"; } print end_pre. end_font; # ...

    cLive ;-)

    update: Actually, that won't *quite work as intended as the font tag is included when populating the %-40s. You'd need to change it to %-66s. Ouch, Hacky. Any reason you're not using a table?

    my @Tr=(); while ( $row = $st->fetchrow_hashref ) push @Tr, td([ font({-color=>'Lime'},$row->{host}), '->', font({-color=>'Cyan'},$row->{xronos}), '->', font({-color=>'Yellow'},$row->{game}) ]); } print table( Tr(\@Tr) );
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: spaces wont be displayed
by pbeckingham (Parson) on May 25, 2004 at 00:14 UTC

    Use &nbsp; for a non-breaking space, or &#160;.

Re: spaces wont be displayed
by eclark (Scribe) on May 24, 2004 at 23:15 UTC

    In your anonymous hashref's for the font attributes, add in -style=>'white-space: pre'. I have not tested this, but it should work.

Re: spaces wont be displayed
by Abigail-II (Bishop) on May 25, 2004 at 09:53 UTC
    Use a style sheet. Either to make it display spaces, or to create "boxes" of a desired width, which seems to be what you are after.

    Abigail