1

Consider:

#! /usr/bin/perl @no = (1 .. 20000); foreach(@no) { print "<div id=\"world@no\" onclick=\"javascript:showDiv_postscreen(); javascript:hideDiv_welcomebuttons()\">&nbsp;</div>\n"; } 

This is my Perl script, but how do I get it to rewrite the sentence with a new variable each time?

I.e., how do I get it to output the following?

<div id="world1" onclick="javascript:showDiv_postscreen(); javascript:hideDiv_welcomebuttons()">&nbsp;</div> . . . <div id="world20000" onclick="javascript:showDiv_postscreen(); javascript:hideDiv_welcomebuttons()">&nbsp;</div> 
1

5 Answers 5

3

Your main problem is interpolating the whole array into your string instead of the loop variable ($_ in your case since you didn't specify one, but I prefer to give it a name).

You can avoid needing to escape the "s by using a different delimiter for your string:

use strict; use warnings; for my $world_no (1..20000) { print qq!<div id="world$world_no" onclick="showDiv_postscreen(); hideDiv_welcomebuttons()">&nbsp;</div>\n!; } 

Also, the "javascript:" is only necessary for things like <a href="..."> where a url is expected and you want to supply javascript code instead. It's not needed for onclick, certainly not twice.

Sign up to request clarification or add additional context in comments.

1 Comment

At least one answer makes like easier. We need those backslashes so don't waste them. :)
1
#!/usr/bin/perl my @no = (1 .. 20000); foreach my $i (@no) { print "<div id=\"world$i\" onclick=\"javascript:showDiv_postscreen(); javascript:hideDiv_welcomebuttons()\">&nbsp;</div>\n"; } 

8 Comments

Why not just use: foreach my $i (1 .. 20_000), otherwise you are loosing memory to prelocate the array size
@Dimi Or for (my $i = 1; $i <= 20_000; $i++) to avoid loading any memory. ;) Or a while loop, like in my answer.
This is the example, in real program array may be created some other way, not every number.
@TLP: That's also a solution, however I just dont like that syntax ;) Alexandr: My point was that you don't need to create the array at all, because you allocate memory for 20_000 elements.
TLP: for(;;) doesn't save any memory over for(..)
|
1
#! /usr/bin/perl @no = (1 .. 20); foreach $x (@no) { print "<div id=\"world$x\" onclick=\"javascript:showDiv_postscreen()\;javascript:hideDiv_welcomebuttons()\">&nbsp;</div>\n"; } 

Comments

0

The @ in the print statement is confusing the interpreter. Also, it isn't what you want, since print "@no" would print out the same thing as join(' ',@no). Instead, you want to interpolate each element of @no into the string that's printed out:

#! /usr/bin/perl use strict; use warnings; #Always use these! my @no = (1 .. 20000); foreach(@no) { print "<div id=\"world" . $_ . "\" onclick=\"javascript:showDiv_postscreen(); javascript:hideDiv_welcomebuttons()\">&nbsp;</div>\n"; } 

Comments

0

I would consider using a variable to count, instead of defining a range. It may feel less intuitive, but it is not.

Also, instead of escaping the double quotes (\"), you can use qq(), which is equivalent.

my $i = 1; print qq(<div id="world$i" onclick="javascript:showDiv_postscreen(); javascript:hideDiv_welcomebuttons()">&nbsp;</div>\n) while ($i++ <= 20_000); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.