Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Perplexed Winemaking Monk

by Russ (Deacon)
on Aug 24, 2000 at 02:56 UTC ( [id://29351]=note: print w/replies, xml ) Need Help??


in reply to Perplexed Winemaking Monk

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://29351]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found