Welcome to WebmasterWorld Guest from 54.156.92.138

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

A command I over looked

     
8:10 pm on Aug 29, 2009 (gmt 0)

Administrator from US 

WebmasterWorld Administrator brett_tabke is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 21, 1999
posts:38071
votes: 16


I stopped dead in my tracks at some code a coworker wrote. I thought for sure it was a typo and that it was my sworn duty to fix it. The bizarre thing, was that the code appeared to work. There are many different ways you can do the following code, but this was certainly the most unique I'd seen.

foreach my $project(@projectdb) {
$color = '#ffffff';
$color = '#e1e1e1e' if $cnt%2; #### nifty cmd
print qq<tr bgcolor="$color"><td>...;

}

...the rest is left as an exercise to the reader. ;-)

9:23 pm on Aug 29, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


What is 'remarkable' here, the modulus operator, or its application to shading alternate rows?

Jim

11:35 pm on Aug 29, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member encyclo is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 31, 2003
posts:9074
votes: 6


an exercise to the reader

foreach my $hopelesscause(@projectdb) {
$class = 'row1';
$class = 'row2' if $cnt%2; #### nifty cmd
print qq<div class="$class">...;
}

;)

2:47 am on Aug 30, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


^ ^ ^ LOL . . . now how do you know his output wasn't supposed to be tabular data? :-)

Seems a modifier with a modulus and "counting" to store in $cnt is the long way around the bush, but in the spirit of TMTOWTDI . . . .

$lightbg = 'row1';
$darkbg = 'row2';

......

foreach my $project(@projectdb) {
$class = ($class eq $lightbg)?$darkbg:$lightbg;
print qq<tr><td class="$class">...</td>....;
}

2:28 pm on Aug 31, 2009 (gmt 0)

Administrator from US 

WebmasterWorld Administrator brett_tabke is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 21, 1999
posts:38071
votes: 16


Well, using the modulus operator as a toggle switch was quit unique. I've never seen anyone use it outside of pure math applications. So clearly, it is an overlooked operator with some unique application potential.

I like it rocknbil. I usually do the above code like this:
I define $altbgcone/two somewhere and then inside of the loop, I do:

($altbgcone,$altbgctwo)=($altbgctwo,$altbgcone);
print qq<tr bgcolor="$caltbgcone"><td>...;

4:54 pm on Aug 31, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


You could also use the single-line if/else construct to make the modulo/remainder method even more compact (or obscure) :)

foreach my $project(@projectdb) {
color = $cnt%2 ? '#e1e1e1e' : '#ffffff';
print qq<tr bgcolor="$color"><td>...;
...
}
5:28 pm on Aug 31, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


color = $cnt%2 ? '#e1e1e1e' : '#ffffff';

can't modify constant item in scalar assignment jdmorgan.pl, line 2 near "$cnt%2;" Execution of jdmorgan.pl aborted due to compilation errors.

:-)

We don't have enough of these threads, life is all so . . . business like. :-)

5:45 pm on Aug 31, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Well, the 'form' and 'intent' is the same as that of your post above, just adapting it to a non-CSS method. I typed it, but didn't test it. oops. :)

I assume it's obvious that the problem --like that of the current state of e-commerce-- is that there are insufficient $s.

Jim

6:03 pm on Aug 31, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member encyclo is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 31, 2003
posts:9074
votes: 6


'#e1e1e1e'

One other thing, shouldn't this read

[b]'#e1e1e1'[/b]
?