Welcome to WebmasterWorld Guest from 54.227.110.209

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

A command I over looked

     

Brett_Tabke

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

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



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. ;-)

jdMorgan

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

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



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

Jim

encyclo

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

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



an exercise to the reader

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

;)

rocknbil

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

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



^ ^ ^ 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>....;
}

Brett_Tabke

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

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



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>...;

jdMorgan

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

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



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>...;
...
}

rocknbil

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

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



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. :-)

jdMorgan

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

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



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

encyclo

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

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



'#e1e1e1e'

One other thing, shouldn't this read

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

Featured Threads

Hot Threads This Week

Hot Threads This Month