--- Wagner Jeff Civ Logicon/TTMS <[EMAIL PROTECTED]> wrote:
> . . . I would guess that, behind the scenes, the
> hash implementation is doing a similar amount of work (perhaps an
> insertion sort).
Actually, it's called a "hash" because it uses a hashing algorithm to
place values. It's pretty efficient -- looks at the value, does a
little calculation, and knows where to get/put it in the hash.
> . . . I started to use the C-style "for" format you mentioned,
> but I changed it to the "(0..$n+1)" format after looking at some
> other code examples.
> Which of the two is more commonly used?
the C-style for(;;){} idiom works in Perl, but in most cases the
foreach structure is more efficient. Instead of
for(my $c=0;$c<10;$c++) { print $ray[$c] }
you can say
foreach my $c (@ray) { print $c }
or even (using $_ and a postfix loop)
print foreach @ray; # you can even say for instead of foreach
These are also a little more readable once you get used to them -- not
so much noise.
If you *need* the index, try
foreach my $c (0..$#ray) { print $ray[$c]; }
> Again as a newbie, I have not yet adopted the use of "@_" and "$_",
> because it seems to be a very mild form of obfuscation.
> I'm sure it just takes getting used to.
I have a coworker who foams at the mouth about this, but there are
times when the code is *easier* to read by using the assumed variables.
They cut down on the line-noise a lot, and aren't so freaky once you
start *thinking* in Perl. =o)
> I am also in favor of the possible future adoption of
> named formal parameters for subroutines, which I read about in the
> "perlsub" Perl Doc.
If you want to emulate that sort of thing now, you can code it.
Try this:
sub func {
my %arg = @_; # assign all incoming args to a hash
print "$_: $arg{$_}\n" foreach keys %arg;
}
Now call it with something like
func( this => 1, that => 2, theOther => 3 );
it should print
this: 1
that: 2
theOther: 3
(although it's just coincidence that they come out in the same order
they went in. =o)
In the function, you can get $arg{this} when you want, which is kind of
like a named parm. =o) You *have* to make sure you send and receive
the args correctly, though; shop an odd number in and let it drop into
the hash, and you might have a real mess......
you could say
sub func {
my($odd, %hash) = @_;
# . . .
if the odd arg comes at the beginning of the list, or even
sub func {
my $odd = pop @_;
my %hash = @_;
# . . .
if it's at the end.....
But be careful that you understand what you're doing.
While these are great exercises for beginners to play with, don't try
this in production code until you've got it...and if this is confusing
you too much, just ignore me. I'm rambling again. ;o]
Paul
__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]