Wow, that is a lot of information.  Thank you very much for taking the time
to explain this. 

-----Original Message-----
From: Dan Korn [mailto:[EMAIL PROTECTED] 
Sent: Thursday, December 13, 2007 6:59 PM
To: FusionPro Users Forum
Subject: [fusionpro] RE: Loop question

Hi David,

There are several issues here:

1. When you're putting multiple statements inside a block with a control
flow statement, whether a loop such as "for" or a conditional such as "if",
you need to use curly braces.  See:
<<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Block_S
tatement>>
(Sorry about URL wrapping in the forum.)

So, if you want to do more than one thing inside a loop, you need to use
curly braces, like so:
 
  for (c = 1; c <= text.length; c++)
  {
    DoSomething();
    DoSomethingElse();
  }

The same goes for "if" statements: if there's more than one statement to be
executed when the condition is met, then you need curly braces there as
well; for example:

  if (CurrentChar == "<br>")
  {
    DoSomething();
    DoSomethingElse();
  }

2. You need to include a valid condition in the "for" statement.  See:
<<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Loop_St
atements:for_Statement>>

So instead of this:

  for (c = 1;  text.length ; c++)
    while (c  != text.length)

You want to do something like this:

  for (c = 1; c <= text.length; c++)

(Your code as written will be in an infinite loop because, unless the string
is empty, text.length will always have a non-zero value, which the
JavaScript interpreter equates to "true" as a condition.  Acrobat will
appear to be hung if you validate the rule in the editor.
Actually, the rule will eventually time out with an "Execution limit
exceeded" error, but it might take a while.)

3. The ReplaceSubstring function doesn't modify the string passed into it;
it returns a modified copy of the string.  So if you want to replace
something and get the modified string, you need to assign the return value
back to the string variable, like so:

  text = ReplaceSubstring(text, "<br>", "<br" + counter + ">");

4. If you want to increment a counter variable using the "++" operator, you
don't need to do any additional assignment.  So that line can just
be:

  counter++;

You could also be a bit tricky and do the incrementing right in the other
place where you're using the variable, like so:

  text = ReplaceSubstring(text, "<br>", "<br" + (counter++) + ">");

5. Keep in mind that tag names in FusionPro are (generally) not
case-sensitive, so you should also look for "<BR>" tags.  Thus, your
function should do some kind of case-insensitive comparison, such as:

  if (ToLower(CurrentChar) == "<br>")        

6. Trying to modify a string while you're iterating through it is
problematic.  The loop counter might not point to the same place in the
string after it's modified.  So you can easily lose your place, and possibly
end up looping infinitely because you're replacing the same thing over and
over again.  It's like you're trying to hit a moving target.  (In developer
geek-speak we would call this a flaw in your state machine, or a Heisenbug.)
It's certainly possible to fix the code to avoid this problem, but you have
to take some care here.  A fix in the code as written would be fairly
complex; you'd probably have to make a copy of the original string and then
keep track of two separate offsets, or counters, into both the original and
the copy.  Which brings me to...

7. There's a much more efficient way to accomplish all of this with
JavaScript using regular expressions and native String functions.  Try
this:

  var text = Field("Headline Copy Text vehicle 1");
  var counter = 1;
  return text.replace(/<br/gi,
                 function(w){return w + counter++});

I believe this will work for your stated purpose, without any of the
problems with modifying the string while you're iterating it.  I'll skip the
full explanation of everything that's happening here, but it's all standard
JavaScript (other than the call to FusionPro's Field function).

8. Finally, I'm not sure what the ultimate purpose of this is.  You can
certainly replace <br> tags with other tags, but tags such as <br1> won't
mean anything to FusionPro's tagged markup parser.  What exactly are you
trying to accomplish here?  If I can understand that, I might be able to
help you find a simpler way to do whatever it is.

If you're just trying to find out how many times a substring appears in
another string, you can do something like this:

  function NumberOfSubStrings(str, sub, case_insens)
  {
    var regexp = new RegExp(sub, "g" + (case_insens ? "i" : ""));
    var result = str.match(regexp);
    return result ? result.length : 0;
  }

  var text = Field("Headline Copy Text vehicle 1");
  return NumberOfSubStrings(text, "<br>", true);    

Note that in the call, we're setting the optional "case_insens"
parameter to true to match all cases, that is, to count <BR> as well as
<br>.  (See number 5 above.)

Dan


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
FusionPro 5.0 Now Available!


Variable text on a curve and soft drop-shadows for variable text


LIMITED TIME upgrade offer of $299 per license for current customers:
http://fusionpro.printable.com/store/upgrade

New licenses available for $599 each at:
http://fusionpro.printable.com/store/

All FusionPro 5.0 customers to receive FusionPro 5.1 with Adobe Acrobat 8
and InDesign CS3 support when released for FREE.
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
--

Users of FusionPro Desktop have unlimited free email support. Contact
Printable Support at [EMAIL PROTECTED] 
--

View FusionPro Knowledge Base, FusionPro Samples at
www.printable.com/vdp/desktop.htm

--

To unsubscribe send a blank email to
[EMAIL PROTECTED]
--


--

--




+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
FusionPro 5.0 Now Available!


Variable text on a curve and soft drop-shadows for variable text


LIMITED TIME upgrade offer of $299 per license for current customers:
http://fusionpro.printable.com/store/upgrade

New licenses available for $599 each at:
http://fusionpro.printable.com/store/

All FusionPro 5.0 customers to receive FusionPro 5.1 with
Adobe Acrobat 8 and InDesign CS3 support when released for FREE.
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
--
Users of FusionPro Desktop have unlimited free email support. Contact Printable 
Support at [EMAIL PROTECTED] 
--
View FusionPro Knowledge Base, FusionPro Samples at
www.printable.com/vdp/desktop.htm

--
You are currently subscribed to fusionpro as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]
--


--
Note:  All e-mail sent to or from this address will be received or otherwise 
recorded by the e-mail recipients of this forum. It is subject to archival, 
monitoring or review by, and/or disclosure to someone other than the recipient. 
Our privacy policy is posted on www.printplanet.com
--

Reply via email to