Newscounter code help

Discuss news styles, css code, and other Coranto customizations in here...the possibilities are endless, so have at it!

Moderator: Spunkmeyer

Newscounter code help

Postby BJv » Sun Aug 12, 2007 5:18 pm

here's the example of what I'm working on right now:

Code: Select all
<PerlCode>
  $NewsCounter  = ++$countitems{$ProfileName};
</PerlCode>
<If: Field: NewsCounter eq "4">code</If>
<If: Field: NewsCounter eq "8">code</If>
<If: Field: NewsCounter eq "1">code</If>


This style will have 8 items in it, what I want to do is have the code that appears for 1 to appear for 2,3,5,6&7 as well. Is there any way to do that? I don't want to make a seperate code part for all 8 icons.
BJv
 
Posts: 26
Joined: Fri Jan 26, 2007 4:52 pm

Postby Dale Ray » Sun Aug 12, 2007 5:40 pm

You may want to look at Counters in Coranto Styles and the Style Guide by Mark Olson.

Suggestion:
Code: Select all
<PerlCode>
  $NewsCounter  = ++$countitems{$ProfileName};
</PerlCode>
<If: Field: NewsCounter != 8>code for 1-7<If: Else>
code for 8</If>


When comparing numbers use == and !=, when comparing strings use eq and ne
Dale Ray
User avatar
Dale Ray
 
Posts: 1001
Joined: Sun Jan 19, 2003 6:02 pm
Location: NW Indiana

Postby BJv » Sun Aug 12, 2007 7:48 pm

Thank you! That worked however it did not work with the == or != ? but does work the way I originally did it. That might have something to do with this: http://www.gocoranto.com/forum/viewtopic.php?t=210

Since I wanted both 4 and 8 to be different I did:

Code: Select all
<PerlCode>
  $NewsCounter  = ++$countitems{$ProfileName};
</PerlCode>
<If: Field: NewsCounter eq "4">code for 4<If: Else>
<If: Field: NewsCounter eq "8">code for 8<If: Else>
code for 1,2,3,5,6,7</If></If>
BJv
 
Posts: 26
Joined: Fri Jan 26, 2007 4:52 pm

Postby Dale Ray » Mon Aug 13, 2007 2:05 am

Glad you got it working.
Dale Ray
User avatar
Dale Ray
 
Posts: 1001
Joined: Sun Jan 19, 2003 6:02 pm
Location: NW Indiana

Postby SrNupsen » Mon Aug 13, 2007 5:40 pm

Small comment: If you (or anybody else reading this thread) plan to use that code snippet in several styles, make sure that you rename the variable for each style ($NewsCounter1, $NewsCounter2 etc.) - since when you build news, it won't be reset between profiles (so when the first profile had been built, it will continue at 9, which could give some rather unpredictable results).

This goes for any custom perl code in styles.
-----------------------------------------------------------------------------------------------------
Coranto is free software. I am available for custom work or troubleshooting.

http://www.sundaune.no - transkripsjon, webdesign, nettsider, tekstbyrå
http://www.vagbladet.no - satire, politikk, kultur, sport, nettavis
-----------------------------------------------------------------------------------------------------
SrNupsen
 
Posts: 2229
Joined: Tue Jan 09, 2007 6:46 pm
Location: Nesodden, outside Oslo, Norway

Postby Musicvid » Tue Aug 14, 2007 12:19 am

make sure that you rename the variable for each style ($NewsCounter1, $NewsCounter2 etc.) - since when you build news, it won't be reset between profiles
I think you could avoid the behavior entirely by localizing the variables in the PerlCode for each Style (a good idea anyway).
Code: Select all
<PerlCode>
  [color=red]my[/color] $NewsCounter  = ++$countitems{$ProfileName};
</PerlCode>
It works because $ProfileName is unique to each Profile calling the Style.
Musicvid
 
Posts: 138
Joined: Wed Jan 17, 2007 1:05 am
Location: Western America

Postby Dale Ray » Tue Aug 14, 2007 6:42 am

From the section on hashes in Counters in Coranto Styles

Making your perlcode reusable

If two or more profiles use the same style when the items for each profile are built the variables you use will retain their values and cause unexpected results. To keep from having to think up unique names for variables and having multiple styles that are the same except for these names we can access the variable using perl hashes.

The format for this is:

$yourVariable{$ProfileName}

this makes the name of the current profile the key for the hash, so that as each profile is built the variables you use are unique for that profile. By using this technique you can use the same style for more than one profile without getting unexpected results from inherited variables.
Dale Ray
User avatar
Dale Ray
 
Posts: 1001
Joined: Sun Jan 19, 2003 6:02 pm
Location: NW Indiana

Postby SrNupsen » Tue Aug 14, 2007 7:02 am

Ah. Clever indeed :)
-----------------------------------------------------------------------------------------------------
Coranto is free software. I am available for custom work or troubleshooting.

http://www.sundaune.no - transkripsjon, webdesign, nettsider, tekstbyrå
http://www.vagbladet.no - satire, politikk, kultur, sport, nettavis
-----------------------------------------------------------------------------------------------------
SrNupsen
 
Posts: 2229
Joined: Tue Jan 09, 2007 6:46 pm
Location: Nesodden, outside Oslo, Norway

Postby Dale Ray » Tue Aug 14, 2007 7:30 am

SrNupsen wrote:Ah. Clever indeed :)

I wrote it down, but I learned it here from Parahead and others. 8)

Thanks to everyone who has contributed to these forums, we would be lost without all of this shared knowledge.
Dale Ray
User avatar
Dale Ray
 
Posts: 1001
Joined: Sun Jan 19, 2003 6:02 pm
Location: NW Indiana

Postby Musicvid » Wed Aug 15, 2007 12:29 am

I wanted to make the distinction between using the same style with different profiles (addressed by Dale just above), --or-- defining the same variable in multiple styles, which is what I think SrNupsen was referring to. In that case, one must either name the variable uniquely in each style as he suggested, or localize it with a "my" operator in the PerlCode. Otherwise, the code gets run every time any style or routine in Coranto sees the variable.

This can be especially dangerous when using a previously defined global variable such as $Text -- if you write PerlCode that says
Code: Select all
$Text = blahblah($Text);
in a style, for instance, it will run altered by sub blahblah every time the script sees $Text in any style from that point forward, and will really crack up your results if that is not what you intended. However if you say
Code: Select all
[color=red]my[/color] $Text = blahblah($Text);
it will stay plain old $Text everywhere except in the profiles that call that particular style.

I wanted to clarify for those who aren't as experienced in putting Perl in their styles.
Musicvid
 
Posts: 138
Joined: Wed Jan 17, 2007 1:05 am
Location: Western America

Postby Dale Ray » Wed Aug 15, 2007 2:19 am

Musicvid,

Thanks for the clarification. I will add it to the docs.
Dale Ray
User avatar
Dale Ray
 
Posts: 1001
Joined: Sun Jan 19, 2003 6:02 pm
Location: NW Indiana

Postby Dale Ray » Wed Aug 15, 2007 2:43 am

Musicvid - hope you don't mind, I used the content of your previous post here in the counter howto.
Dale Ray
User avatar
Dale Ray
 
Posts: 1001
Joined: Sun Jan 19, 2003 6:02 pm
Location: NW Indiana

Postby Musicvid » Wed Aug 15, 2007 11:54 pm

Thanks for the mention, Dale. And congratulations for maintaining the docs and demo site in such an organized fashion. I learn something new every time I go there.
Musicvid
 
Posts: 138
Joined: Wed Jan 17, 2007 1:05 am
Location: Western America

Postby Musicvid » Sun Sep 02, 2007 3:57 am

I learn something new every time I go there.
Here is some more information on the use of "my" command in Perl, gratitude Elvii, our originator.
http://www.corantodemo.net/wiki/index.php/Addons/AddonGuide#toc6
Another link, also courtesy Elvii.
http://perl.plover.com/FAQs/Namespaces.html
He knows this stuff much better than me...
Musicvid
 
Posts: 138
Joined: Wed Jan 17, 2007 1:05 am
Location: Western America


Return to Customization

Who is online

Users browsing this forum: No registered users and 2 guests

cron