On Sat, 2002-03-02 at 13:02, Tor Hildrum wrote:
> <snip>
>
> > #!/usr/bin/perl
> >
> > use strict;
> >
> > my %hash = (
> > key1 => 'value 1',
> > key2 => 'value 2',
> > key3 => 'value 3',
> > );
> >
> > print_hash(\%hash, '=>');
> >
> > sub print_hash {
> > my ($hash, $connector) = @_;
> >
> > foreach my $key (sort keys %$hash) {
> > print "$key $connector $$hash{$key}\n";
> > }
> > }
>
> I'm new to perl, but isn't this easier to understand:
> #!/usr/bin/perl
>
> use strict;
>
> my %hash = (
> key1 => 'value 1',
> key2 => 'value 2',
> key3 => 'value 3',
> );
>
> print_hash(\%hash);
>
> sub print_hash {
> my ($hash) = @_;
> foreach my $key (sort keys %$hash) {
> print "$key => $hash->{$key}\n";
> }
> }
>
> I took out the $connector and dereferenced with ->.
> I am pretty new to Perl, and it took me awhile to understand why you passed
> => to the sub, and what $connector was..
> I'm just sayin :)
>
> --
> T.
>
Always ask questions. $connector was just an example of a second
variable that need to be passed in. I probably should have called it
$foo, $trash, $temp, or something else to indicate its unimportance.
Try changing that argument that gets passed into $connector to "points
to " instead of "=>". As for using -> verses adding an extra $, I
believe this is a stylistic issue. I generally use -> when I am dealing
with something that I want to treat like an object and I use an extra $
when I want the reader to understand that this is just a dereferenced
reference. Here is the second example done in a slightly different
style:
<example>
#!/usr/bin/perl
use strict;
my %hash = (
key1 => 'value 1',
key2 => 'value 2',
key3 => 'value 3',
);
print_hash(\%hash, 'points to');
sub print_hash {
my ($hash, $phrase) = @_;
foreach my $key (sort keys %$hash) {
print "$key $phrase $$hash{$key}\n";
}
}
</example>
--
Today is Sweetmorn the 61st day of Chaos in the YOLD 3168
P'tang!
Missile Address: 33:48:3.521N 84:23:34.786W
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]