bini has asked for the wisdom of the Perl Monks concerning the following question:

Could anyone help an initiate with his first perl program? It seems to work fine for the first three strings, then stops. Thanks to all monks for any bits of enlightenment they can offer.
#!/usr/bin/perl @skuid=("11021","11022","11023"); @name=("mt. eden","dom perignon","veuve cliquot"); @price=("45.99","135.99","79.99"); print "Content-type:text/html\n\n"; foreach $skuid(@skuid) { print "SKUID:$skuid\n"; } foreach $name(@name) { print "NAME:$name\n"; } foreach $price(@price){ print "PRICE:$price\n"; }

Replies are listed 'Best First'.
Re: Vexed Otra Vez
by athomason (Curate) on Aug 23, 2000 at 10:37 UTC
    It works fine for me both at the command line and browser, and I don't see anything immediately wrong with it. Try turning on warnings and use the strict pragma; those may help describe the problem more closely. Also, be aware if you're only checking the output in a web browser that you haven't actually created an html document per your content-type header. To do that, you need to wrap your output in <html> and preferably <body> tags. Modern browsers won't care, but it wouldn't hurt. A cleaned up version of your code following all that advice might look like this:
    #!/usr/bin/perl -w use strict; my @skuid=("11021","11022","11023"); my @name=("mt. eden","dom perignon","veuve cliquot"); my @price=("45.99","135.99","79.99"); print "Content-type:text/html\n\n"; print "<html><head><title>Test script</title></head><body>\n"; foreach my $skuid (@skuid) { print "SKUID:$skuid\n"; } foreach my $name (@name) { print "NAME:$name\n"; } foreach my $price (@price){ print "PRICE:$price\n"; } print "</body></html>";
    As an aside, be inclined to use the CGI.pm module rather than manual html if you're planning on doing more serious CGI work in the future.

    If none of this helps, more information on your platform, perl version (with perl -v), and browser would be helpful.

    Update:

    I just couldn't help myself... the problem I think you're trying to solve screams for a hash. Here's how I would actually do it (note I'm a big fan of using CGI.pm functionally):

    #!/usr/bin/perl -w use strict; use CGI ":standard"; my %wines = (11021 => ["Mt. Eden", "45.99"], 11022 => ["Dom Perignon", "135.99"], 11023 => ["Veuve Cliquot", "49.99"]); print header, start_html({-title=>'Wines'}), table({-border=>1}, [Tr([th(["SKUID", "Name", "Price" ]), map td([$_, $wines{$_}->[0], $wines{$_}->[1]]), sort keys %wines ]) ]), end_html;
Re: Vexed Otra Vez
by Maclir (Curate) on Aug 23, 2000 at 09:13 UTC
    I may not be on the right track, but you don't need to delimit your strings with double quotes " ". Are you aware of the difference between a single quoted string and a double?
    #!/usr/local/bin/perl $string1 = '$1.99'; $string2 = '$string1'; $string3 = "$string1"; print "$string1 \n$string2 \n$string3 \n";
    results in:
    $1.99 $string1 $1.99
    Maybe because your constants are in double quotes, they are being interpreted?

    However, your script works for me.

    (Updated) Nice to see you have some good Australian wines in your list.

Re: Vexed Otra Vez
by xdb19 (Sexton) on Aug 23, 2000 at 22:37 UTC
    Hi, I belive that your problem lies in a mistake, not of coding, but rather of spelling. I thought that the space between the colon, and the word text in the

    print "Content-type: text/html\n\n"

    was required.
    So the fixed code would look something like this:
    #!/usr/bin/perl @skuid = ( "11021", "11022", "11023" ); @name = ( "mt. eden", "dom perignon", "veuve cliquot" ); @price = ( "45.99", "135.99", "79.99" ); print "Content-type: text/html\n\n"; foreach $skuid( @skuid ) { print "SKUID: $skuid\n"; } foreach $name( @name ) { print "NAME: $name\n"; } foreach $price( @price ){ print "PRICE: $price\n"; }


    - Have Fun. XDB19
Re: Vexed Otra Vez
by wardk (Deacon) on Aug 25, 2000 at 01:55 UTC
    The text/html should have a space between it and the prior colon, here is a different solution than some others, puts the products in a table. I do prefer the HASH solution idea. good luck!
    #!/usr/bin/perl @skuid=("11021","11022","11023"); @name=("mt. eden","dom perignon","veuve cliquot"); @price=("45.99","135.99","79.99"); # put a space after the colon print "Content-type: text/html\n\n"; print "<table>\n"; print "<tr><th>Sku<th>Name<th>Price\n"; foreach $x (0..(scalar(@skuid)-1)) { print "<tr><td>$skuid[$x]<td>$name[$x]<td>$price[$x]\n"; } print "</table>\n";
RE: Vexed Otra Vez
by Adam (Vicar) on Aug 23, 2000 at 21:32 UTC
    I mistakenly responded to a previous version of this question. Please don't repost the same question like that, it confuses matters. My response can be found at: RE: Perplexed Winemaking Monk
      You're absolutely right, Adam.

      I suspect that fresh Monk bini posted the third attempt after I /msg'd him/her suggesting that <code></code> would format the example properly.   Pretty sure I also /msg'd bini suggesting that a post to Editor Requests might remove the first two attempts.

      It was getting late, I was very tired, yet trying to help a Monk who had a reasonable question.   Sorry if I unwittingly added to the confusion.
          cheers,
          ybiC