Minichan

Topic: Does anyone here know Perl?

Anonymous A started this discussion 12 years ago #34,022

I need some help with something.

Anonymous B joined in and replied with this 12 years ago, 1 minute later[^] [v] #469,874

Perl? She's a good sheila, but cut her off after three pints.

Anonymous A (OP) replied with this 12 years ago, 1 minute later, 2 minutes after the original post[^] [v] #469,875

@previous (B)
No, the scripting language.

Anonymous B replied with this 12 years ago, 12 minutes later, 14 minutes after the original post[^] [v] #469,877

@previous (A)
Oh, right. The scripting language. I'm sure a few here have dabbled in it. What are you trying to do?

Anonymous C joined in and replied with this 12 years ago, 42 seconds later, 15 minutes after the original post[^] [v] #469,878

Yes. I've maintained some Perl scripts. What's up?

Anonymous A (OP) replied with this 12 years ago, 6 minutes later, 22 minutes after the original post[^] [v] #469,883

@469,877 (B)
I am suppose to make a checkbox list that will display only the options selected, but I'm having trouble with the loop.

foreach $use (@primary_uses) {
print "$use<BR>\n";
}

I declared the $use as a scalar variable at the top and I assigned @primary_uses to param('Use'); as well as declare four variables to it.

Now it works as intended, but it just displays the selected ID numbers and not the name.

advi !!3gpAcg/hF joined in and replied with this 12 years ago, 7 minutes later, 29 minutes after the original post[^] [v] #469,887

@previous (A)
Full code?

Meowth joined in and replied with this 12 years ago, 40 seconds later, 30 minutes after the original post[^] [v] #469,888

@previous (advi !!3gpAcg/hF)
It's patented troll.

advi !!3gpAcg/hF replied with this 12 years ago, 1 minute later, 31 minutes after the original post[^] [v] #469,890

@previous (Meowth)
Who patents Perl code? It's impossible to copy by nature.

Anonymous A (OP) replied with this 12 years ago, 1 minute later, 33 minutes after the original post[^] [v] #469,891

@469,887 (advi !!3gpAcg/hF)
Sure, why not.
I left off the first part of it though as it's just the use CGI and use strict stuff.

#declare variables
my ($name, $serial, $modnum, $sysletter, $use);
my @models = ("Laser JX", "Laser PL", "ColorPrint XL");
my %systems = ("W", "Windows",
"M", "Macintosh",
"U", "UNIX");
my @primary_uses = ("Home", "Business", "Educational", "Other");

#assign input items to variables
$name = param('Name');
$serial = param('Serial');
$modnum = param('Model');
$sysletter = param('System');
@primary_uses = param('Use');

#create Web page
print "<HTML><HEAD><TITLE>Juniper Printers</TITLE></HEAD>\n";
print "<BODY><H2>\n";
print "Thank you, $name, for completing \n";
print "the registration form.<BR><BR>\n";
print "We have registered your Juniper $models[$modnum] printer, \n";
print "serial number $serial.\n";
print "You indicated that the printer will be used on a \n";
print "$systems{$sysletter} system.<BR><BR>\n";
print "The primary use for this printer is:<BR><BR>\n";
foreach $use (@primary_uses) {
print "$use<BR>\n";
}
print "</H2></BODY></HTML>\n";

Anonymous C replied with this 12 years ago, 2 minutes later, 36 minutes after the original post[^] [v] #469,892

@469,883 (A)
It sounds like you need a hash or array that functions as a dictionary structure. Do you intend for the ID-to-label relationship to be exactly one in both directions? If so, you could use this pseudo code…


declare each ID as mapping to a text label
loop over each ID
if the selected options includes this ID
output the text value from the list that is stored at the current ID index
else
debug log that this option was not selected
end


Note that this only outputs predefined, known safe content and does not pass through user submitted content. Also note that this discards and does not loop over user submitted content that might not match the defined criteria. Both of those ate for security and resource efficiency purposes.

(Edited 23 seconds later.)

Meowth replied with this 12 years ago, 1 minute later, 37 minutes after the original post[^] [v] #469,895

@469,890 (advi !!3gpAcg/hF)

Anonymous C replied with this 12 years ago, 2 minutes later, 39 minutes after the original post[^] [v] #469,898

@469,891 (A)
Please use formatted string interpolation as a basic sanitization method:

sprintf "thank you %s", $name;

Anonymous A (OP) replied with this 12 years ago, 1 minute later, 41 minutes after the original post[^] [v] #469,899

@469,892 (C)
Well, the project in my book says it needs an array not a hash. I was thinking I might have needed a hash too.

This is the checkbox list I have to connect the script to.

Anonymous A (OP) double-posted this 12 years ago, 1 minute later, 42 minutes after the original post[^] [v] #469,900

@previous (A)
As you can see, the desired effect gets done, but it doesn't display the names Home, Business, Educational, or Other, it just displays the numbers assigned to the array.

Anonymous C replied with this 12 years ago, 32 seconds later, 43 minutes after the original post[^] [v] #469,902

@469,891 (A)
It appears you are overloading your
@primary_uses
array as the user-submitted content. If the param values are the selected IDs, output those labels by referencing the value in the array at the selected index; e.g.
@primary_uses[0];

Anonymous A (OP) replied with this 12 years ago, 3 minutes later, 47 minutes after the original post[^] [v] #469,903

@previous (C)
So my @primary_uses[0] = or @primary_uses[0] = param('Use');?

(Edited 45 seconds later.)

Anonymous C replied with this 12 years ago, 1 minute later, 49 minutes after the original post[^] [v] #469,904

@previous (A)
I'm providing you with explanations of how to make changes and not literal examples of how your code should read. That example is to tell you that if the user has selected value zero of the list of options, then you should output value zero from your array of options.

Anonymous B replied with this 12 years ago, 3 minutes later, 53 minutes after the original post[^] [v] #469,905

@469,903 (A)
Does it work any differently if you ditch the line @primary_uses = param('Use'); altogether?

Anonymous A (OP) replied with this 12 years ago, 14 minutes later, 1 hour after the original post[^] [v] #469,912

@previous (B)
Yes, it just displays all four options. I assume this is because the script isn't connected to the html form tags, right?

Anonymous B replied with this 12 years ago, 13 minutes later, 1 hour after the original post[^] [v] #469,913

@previous (A)
> Yes, it just displays all four options.
But it' displaying the names you put in the array now, not numbers? What's changing when you assign the user submitted content to that array?

(Edited 17 seconds later.)

Anonymous C replied with this 12 years ago, 11 minutes later, 1 hour after the original post[^] [v] #469,924

@previous (B)
The user submitted values appear to be the array indices of the selected checkboxes.

(Edited 9 seconds later.)

Anonymous A (OP) replied with this 12 years ago, 1 minute later, 1 hour after the original post[^] [v] #469,926

I figured out. I just used a $key command and assigned array @use to param('Use');

My revised code:

#declare variables
my ($name, $serial, $modnum, $sysletter, $key, @use);
my @models = ("Laser JX", "Laser PL", "ColorPrint XL");
my %systems = ("W", "Windows",
"M", "Macintosh",
"U", "UNIX");
my @uses = ("Home", "Business", "Educational", "Other");

#assign input items to variables
$name = param('Name');
$serial = param('Serial');
$modnum = param('Model');
$sysletter = param('System');
@use = param('Use');

#create Web page
print "<HTML><HEAD><TITLE>Juniper Printers</TITLE></HEAD>\n";
print "<BODY><H2>\n";
print "Thank you, $name, for completing \n";
print "the registration form.<BR><BR>\n";
print "We have registered your Juniper $models[$modnum] printer, \n";
print "serial number $serial.\n";
print "You indicated that the printer will be used on a \n";
print "$systems{$sysletter} system.<BR><BR>\n";
print "The primary use for this printer is:<BR><BR>\n";
foreach $key (@use) {
print "$uses[$key]<BR>\n";
}
print "</H2></BODY></HTML>\n";

Anonymous B replied with this 12 years ago, 5 minutes later, 1 hour after the original post[^] [v] #469,928

@previous (A)
> $uses[$key]

Yay!

Anonymous F joined in and replied with this 12 years ago, 16 seconds later, 1 hour after the original post[^] [v] #469,929

@469,926 (A)
It's beautiful. :')

Anonymous C replied with this 12 years ago, 2 hours later, 4 hours after the original post[^] [v] #469,966

@469,926 (A)
sprintf with HTML entity escaping, plz

Anonymous G joined in and replied with this 12 years ago, 2 hours later, 6 hours after the original post[^] [v] #470,002

Homeworks grate
:

Please familiarise yourself with the rules and markup syntax before posting.