Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

For statement 2 html table

by senik148 (Beadle)
on Jan 03, 2006 at 22:27 UTC ( [id://520748]=perlquestion: print w/replies, xml ) Need Help??

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

for my $i (@$times) { print qq{ <div class=\"forecast\"> <img src="$icons->{$i}" alt="" /> <br /> $i $conditions->{$i} <br /> $hilo->{$i}: $temperatures->{$i}\&deg;F <br /> PoP: $precip->{$i}\% </div> }; }


OUTPUT IS SOME WHAT SIMILAR TO THIS BUT IS USING DIV tags:

img
Today
Chance Rain
High: 58°F
PoP: 22%
img
Tomorrow
Chance Rain
High: 58°F
PoP: 22%
img
Friday
Chance Rain
High: 58°F
PoP: 22%


NOW, HOW CAN IT BE TURNED INTO THIS EXACT TABLE FORMAT? :

img img img
Today Tomorrow Friday
Chance Rain
Chance Rain
Chance Rain
High: 58°F
PoP: 22%
High: 58°F
PoP: 22%
High: 58°F
PoP: 22%


using the same for statement code... sorry *POST UPDATED* for below REPLY.

Replies are listed 'Best First'.
Re: For statement 2 html table
by esskar (Deacon) on Jan 03, 2006 at 22:37 UTC
    what IF statement ?
    print "<table><tr>"; for my $i (@$times) { print qq{ <td> <img src="$icons->{$i}" alt="" /> <br /> $i $conditions->{$i} <br /> $hilo->{$i}: $temperatures->{$i}\&deg;F <br /> PoP: $precip->{$i}\% </td> }; print "</tr></table>";
      update: re Esskar's post
      I think you're putting the values associated with $i next to each other, instead of on top of each other in columns...
      I just tried your code and this is what i get, you can view html source on the script if you like..

      here is the link for all of you: http://www.fremontvirtualoffice.com/forecast.pl

      can anybody help? do you want me to post all the code, its just that FOR { statement } that i need to get working to output a horizontal looking table. not vertical.

      the rest is just the XML parsing, using modules.
Re: For statement 2 html table
by SamCG (Hermit) on Jan 03, 2006 at 22:59 UTC
    Forgive me for asking, but what does "using the same IF statement... " actually mean? Do you mean you want to use only one "for" loop starting "for my $i (@$times)"? For that matter, I'm also not sure what you mean by "OUTPUT IS SOME WHAT SIMILAR TO THIS BUT IS USING tags: " -- though I think I understand what output you're getting from the code.

    Forgive me for asking, but are you familiar with html? Enforcing the "IF" restriction doesn't seem wise, since if you want a table you'll be iterating through rows, and would need to access the images successively, then the $i successively, etc. The easiest (definitely far from best) way to do this might be to set up a loop for the four rows of your table, and to print a different element into each td element depending on which row you were in.
    untested semi-code follows
    use Switch; for my ($x=0;$x<4;$x++){ switch ($x) case 0 for my $i (@$times) { print qq{<td><img src="$icons->{$i}" alt="" /></td>}; } case 1 for my $i (@$times){ print qq{<td>"$i"</td>}; } etc., etc.
    Note you will still need to insert the table and tr tags. Someone will probably (maybe already) suggest you do something else entirely, like use CGI and a templating system, or give a better method of doing something like the above. They're probably right (and will probably lambast me for suggesting such a clumsy solution). I just wanted to give you something that might work, and not require complete overhauls.

    I might have misunderstood your question. I tend to have some difficulty reading sentences in all caps.
Re: For statement 2 html table
by jhourcle (Prior) on Jan 04, 2006 at 10:22 UTC

    This actually is a perl question, although it involves HTML, if I'm understanding the question correctly.

    The problem is that the data is in a rather odd format (multiple hashes), and there's a need to get it out of those structures, and into an HTML table -- which can be a pain, given the order that it's necessary to emit an HTML table.

    This can be done quite a few ways:

    • a series of loops for each row.
    • Build an list of the columns (Array of Arrays) and then walk the structure to emit it back in the right order.
    • Build a list of the rows (Array of Array), and then it's basically in the right order to emit

    (all code untested as I don't have the original data structures, and I'm only writing this all because it's 5am, and I can't sleep)

    Which would I use? Well, the first one's fastest to write, and it's easy to modify so you're emitting <th> as a replacement for <td> in the correct place ... you could loop over a list of strings to be 'eval'd for each iteration, if you wanted to remove redundancies ... but I'd probably look into what put the data in those original structures, and just modify it so it was placed into either an array of hashes, or hash of arrays to start with.

Re: For statement 2 html table
by john_oshea (Priest) on Jan 03, 2006 at 22:44 UTC

    Errh, I may be missing something, but this looks more like a HTML/CSS question than a Perl one.

    Either style the <div> with display:inline, or float: left. One of those should work (subject to any other page formatting you may have), and you won't have to change the Perl at all.

    Hope that helps.

      Errh, I may be missing something, but this looks more like a HTML/CSS question than a Perl one.

      It looks to me more a question of extracting data from a number of hash references using keys stored in an array sorting first by hash and then by key rather then first by key then by hash.

      I'm just confused by the "IF statement" comment, and the odd markup in the sample output which doesn't seem to bare much relation to the code. I'm also having some trouble coming up with an answer that isn't simply spoonfed code.

      style the <div>

      It is better to alter the Perl then to represent tabular data without tabular markup.

        Ah yes, a very good point...

        <cough> Can you guess which of us has been reading too much about CSS and Javascript lately?  ;-)

Re: For statement 2 html table
by graff (Chancellor) on Jan 04, 2006 at 06:20 UTC
    The idea of using something like HTML::Template is definitely worth looking into -- it can simplify your perl code a lot, and make it easier to tweak/fix the HTML display without having to change the perl code.

    Based on the code you've posted, it's not clear to me (because I lack CSS experience) how the div and br tags are being transformed into a table layout. In any case, if I were to try to do this without HTML::Template, it would probably look something like this (note that I'm not using div or br tags, either, but maybe you can work that out yourself, if it matters):

    # create a reference to a hash with @$times as keys and values: my $times_href = { map { $_ => $_ } @$times }; # add the image tagging to the $icon elements: $icons->{$_} = "<img src=\"$icons->{$_}\" alt=\"\" />" for ( keys %$ic +ons ); print "<table>\n"; for my $dref ( $icons, $times_href, $conditions, $hilo, $temperatures +) { print "<tr>"; print "<td>$dref->{$_}</td>" for ( @$times ); print "</tr>\n"; } print "</table>\n";
    (not tested)

    Since most of your data is in hash refs already, putting the @$times data into a hash ref as well means that you can treat all the table rows the same way, and you still use @$times to run the loop that prints the columns on each row in the proper order.

    (updated to add (and fix) the for loop that modifies the contents of the $icons hash ref, so that they can also be used the same way as the other hash refs when printing the table)

    (another update: I realize that I've left out some of the details in the OP code, like adding the "°F" on the temperatures, the "abbr" tags, etc. But these would be handled the same way as adding the "img" tagging on the "%$icons" data.)

      Thanks, your code did the job!


      i was looking at html::template before, tryng to look for a solution. thanx to all of you!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://520748]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-25 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found