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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Perplexed Winemaking Monk
by chromatic (Archbishop) on Aug 23, 2000 at 08:43 UTC
    @combo holds three (probably undefined) values, not the contents of the arrays. Something like the following would work better for you:
    for (0 .. $#skuid) { print "SKUID: ", $skuid[$_], "\n"; print "NAME: ", $name[$_], "\n"; print "PRICE: ", $price[$_], "\n"; }
    You might want a different datastructure, though, so take a look at perlref and perldsc. A list of lists would do the trick nicely.
Re: Perplexed Winemaking Monk
by Russ (Deacon) on Aug 24, 2000 at 02:56 UTC
    Since you described this as your first Perl program, let me comment on your loop. The others have given good advice for the data structures, so I'll just make a quick suggestion about your actual code.

    You have:

    $n == 0; for (loop condition){ $n++ }
    • Use = for assignment, == for testing. This line doesn't actually do anything, except cause a warning. It tests whether $n (an undefined value) is equal to zero.
    • On the subject of warnings, you are running with warnings enabled, right? <nobr>#!/usr/bin/perl -w</nobr> at the top of your script will enable many useful warnings. Don't leave home without it. :-)
    • On the subject of global variables ($n is a global, in your code), don't use them. Adam has <nobr>use strict; #Always</nobr> in his code. This is a good idea. It will help you avoid problems.
    • Looping: Here is a slightly more idiomatic way to do your loop:
    for my $Idx (0 .. $#combo){ print "SKUID:$skuid[$Idx]\n"; print "NAME:$name[$Idx]\n"; print "PRICE:$price[$Idx]\n"; }
    In other words, for each index in @combo (the 0 .. $#combo part), set $Idx to that index and run the body of the block. This accomplishes the same thing as your code, but doesn't leave an unused variable ($n) lying around. It specifically uses the index of each element in @combo, which makes the code easier to follow and maintain. Besides, it looks so much cooler... ;-)

    Welcome to Perl in general and Perl Monks in specific. We're glad to have you.

    Russ
    Brainbench 'Most Valuable Professional' for Perl

RE: Perplexed Winemaking Monk
by Adam (Vicar) on Aug 23, 2000 at 20:36 UTC
    Please use <code> tags around your code, thusly:
    #!/usr/bin/perl @skuid=("11021","11022","11023","92523002", "02196005","92196019","92176184","92305069", "92305070","92305054","14316002","92620206", "92503001"); @name=("mt. eden","dom perignon","veuve cliquot", "Arcadian Pinot Noir Sl Holl","Au Bon Climat Pinot Noir La Baug +e", "Au Bon Climat Pinot Noir Laetitia", "Bannister Pinot Noir Seghesio Vineyard", "Broadley Pinot Noir Claudias Choice", "Broadley Pinot Noir Marcille Loraine", "Broadley Pinot Noir Reserve","Castalia Pinot Noir Rochioli Vyd +", "Coldstream Hills Pinot Noir","Costa De Oro Pinot Noir"); @price=("45.99","135.99","79.99","24.99","24.99", "39.99","26.99","39.99","29.99","23.99", "29.99","17.99","19.99"); @combo=($skuid,$name,$price); print "Content-type:text/html\n\n"; $n==0; foreach $item(@combo) { print "SKUID:$skuid[$n]\n"; print "NAME:$name[$n]\n"; print "PRICE:$price[$n]\n"; $n++; }
    That said, I'm not entirely sure of your goal here. I'm going to guess that you want to print out the three data points for each wine. As chromatic suggested, you probably want a better data structure. May I suggest a hash? Since the skuid is probably unique, I would make that the key. Each value would then be an array containing the vinyard and the price. Then you just dump the contents of the hash. (crudely using data::dumper, or nicely as I do below.) Here is my version:
    #!/usr/bin/perl use strict; # Always. use CGI ':standard'; my %winelist = ( "11021" => ["mt. eden", "45.99"], "11022" => ["dom perignon", "135.99"], "11023" => ["veuve cliquot", "79.99"], "92523002" => ["Arcadian Pinot Noir Sl Holl", "24.99"], "02196005" => ["Au Bon Climat Pinot Noir La Bauge", "24.99"], "92196019" => ["Au Bon Climat Pinot Noir Laetitia", "39.99"], "92176184" => ["Bannister Pinot Noir Seghesio Vineyard", "26.99"], "92305069" => ["Broadley Pinot Noir Claudias Choice", "39.99"], "92305070" => ["Broadley Pinot Noir Marcille Loraine", "29.99"], "92305054" => ["Broadley Pinot Noir Reserve", "23.99"], "14316002" => ["Castalia Pinot Noir Rochioli Vyd", "29.99"], "92620206" => ["Coldstream Hills Pinot Noir", "17.99"], "92503001" => ["Costa De Oro Pinot Noir", "19.99"], ); print header(), start_html(); print "\n\n<!-- This page generated by a script written by Adam at Per +lMonks.org -->\n\n"; print h1( "My Wine Collection" ), "\n\n"; print table( {-width, '100%'}, TR( td( {-width, '20%'}, "SKUID" ), td( {-width, '60%'}, "WINE" ), td( {-width, '20%'}, "PRICE" ) )), "\n"; my $wine; foreach $wine (keys %winelist) { print table( {-width, '100%'}, TR( td( {-width, '20%' }, "$wine" ),"\n", td( {-width, '60%' }, "$winelist{$wine}[0]" ), td( {-width, '20%' }, "$winelist{$wine}[1]" ) )), "\n"; } print end_html();
    Note that I assumed from your print content text/html line that this was generating html, so I did it using CGI.pm's procedural methods. You can also use CGI.pm oo methods, but I didn't feel like it.