homepage Welcome to WebmasterWorld Guest from 54.197.19.35
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
A command I over looked
Brett_Tabke




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

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




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

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

Jim

encyclo




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

an exercise to the reader

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

;)

rocknbil




msg:3980702
 2:47 am on Aug 30, 2009 (gmt 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>....;
}

Brett_Tabke




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

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




msg:3981364
 4:54 pm on Aug 31, 2009 (gmt 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>...;
...
}

rocknbil




msg:3981395
 5:28 pm on Aug 31, 2009 (gmt 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. :-)

jdMorgan




msg:3981413
 5:45 pm on Aug 31, 2009 (gmt 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

encyclo




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

'#e1e1e1e'

One other thing, shouldn't this read [b]'#e1e1e1'[/b] ?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved