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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user852974
  • 2,242
  • 10
  • 41
  • 65
  • Note: Not to be confused with *[How can I repeat a string N times in Perl?](https://stackoverflow.com/questions/277485/)* (the search engines can't tell the difference). – Peter Mortensen Aug 23 '23 at 19:53

5 Answers5

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.

ysth
  • 96,171
  • 6
  • 121
  • 214
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"; 
}
Alexandr Ciornii
  • 7,346
  • 1
  • 25
  • 29
  • Why not just use: `foreach my $i (1 .. 20_000)`, otherwise you are loosing memory to prelocate the array size – Dimitar Petrov Jul 21 '11 at 10:28
  • @Dimi Or `for (my $i = 1; $i <= 20_000; $i++)` to avoid loading any memory. ;) Or a while loop, like in my answer. – TLP Jul 21 '11 at 12:27
  • This is the example, in real program array may be created some other way, not every number. – Alexandr Ciornii Jul 21 '11 at 12:35
  • @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. – Dimitar Petrov Jul 21 '11 at 12:39
  • TLP: for(;;) doesn't save any memory over for(..) – ysth Jul 21 '11 at 19:30
  • @ysth You mean that it still extrapolates into a list before the loop begins? – TLP Jul 22 '11 at 12:29
  • @ysth You may wish to have a chat with Chas. He seems to think that it does. Do you mean to say that `for (...)` does not expand its arguments into a list before the loop begins? – TLP Jul 22 '11 at 20:56
  • @TLP: specifically for over a range operator – ysth Jul 22 '11 at 21:01
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";
}
cppcoder
  • 22,227
  • 6
  • 56
  • 81
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"; 
} 
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);
TLP
  • 66,756
  • 10
  • 92
  • 149