Using strict references

T

Todd Bair

Hi,

I am trying to write all of my code using strict to improve my
code. However, the correct use of references for the following problem
eludes me. I have a work around, but I would like to know the correct
way to code this.

#!/usr/bin/perl -w
######################################

use strict;
#no strict 'refs'; ## work around ##

######################################

my %hash = qw(foo one bar two);
my $key;

foreach $key (keys %hash)
{
@{$key} = (0..5);
}

######################################
# later on in the program
######################################

foreach $key (keys %hash)
{
print "array $hash{$key} = ";

print join("\t",@{$key});

print "\n";
}

I would appreciate any help that you can offer.

Thanks,

Todd
 
J

John Strauss

Hi,

I am trying to write all of my code using strict to improve my
code. However, the correct use of references for the following problem
eludes me. I have a work around, but I would like to know the correct
way to code this.

#!/usr/bin/perl -w
######################################

use strict;
#no strict 'refs'; ## work around ##

######################################

my %hash = qw(foo one bar two);
my $key;

foreach $key (keys %hash)
{
@{$key} = (0..5); $hash{$key} = [0..5];
}

######################################
# later on in the program
######################################

foreach $key (keys %hash)
{
print "array $hash{$key} = ";

print join("\t",@{$key});
print join("\t",@{$hash{$key}});
print "\n";
}

I would appreciate any help that you can offer.

Thanks,

Todd



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail
 
T

Todd Bair

@{$key} = (0..5);
$hash{$key} = [0..5];

John,

I the goal is to use the $key as the name of the array not the value of
the key.

Also, your suggestion produces the following error:

Use of uninitialized value in range (or flip) at strict_refs.pl line 15.
Use of uninitialized value in range (or flop) at strict_refs.pl line 15.
Use of uninitialized value in range (or flop) at strict_refs.pl line 15.
Can't use string ("1") as an ARRAY ref while "strict refs" in use at
strict_refs.pl line 26.
array 1 =

Thanks,

Todd




John said:
Hi,

I am trying to write all of my code using strict to improve my
code. However, the correct use of references for the following problem
eludes me. I have a work around, but I would like to know the correct
way to code this.

#!/usr/bin/perl -w
######################################

use strict;
#no strict 'refs'; ## work around ##

######################################

my %hash = qw(foo one bar two);
my $key;

foreach $key (keys %hash)
{
@{$key} = (0..5); $hash{$key} = [0..5];
}

######################################
# later on in the program
######################################

foreach $key (keys %hash)
{
print "array $hash{$key} = ";

print join("\t",@{$key});
print join("\t",@{$hash{$key}});
print "\n";
}

I would appreciate any help that you can offer.

Thanks,

Todd

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail
 
T

Tad McClellan

M

Mina Naguib

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Todd said:
#!/usr/bin/perl -w
######################################

use strict;
#no strict 'refs'; ## work around ##

######################################

my %hash = qw(foo one bar two);

For clarity, hashes should be populated like so instead of the above array-style:

%hash = (
"foo" => "one",
"bar" => "two",
);
my $key;

foreach $key (keys %hash)
{
@{$key} = (0..5);
}

The above loops over the hash's keys ("foo" and "bar") and creates brand new variables out of those
names, @foo and @bar.

Do NOT do this. If you're a beginner, do NOT do it no matter how tempting it might seem, and if
you're not a beginner, you already know not to do it.

Instead of creating brand-new variables, you should save the new stuff either in a new hash or in
your existing hash.


######################################
# later on in the program
######################################

foreach $key (keys %hash)
{
print "array $hash{$key} = ";

print join("\t",@{$key});

print "\n";
}

It appears to me that you don't know that you can store arrays (actually, array _references_) as
values to a hash's keys. Please see "perldoc perlref".

Your code could be re-written in several approaches. One is to create the values in the hash
definition (since you're completely ignoring "one" and "two" in your current code):

%hash = (
"foo" => [0..5],
"bar" => [0..5],
);

Or if you want to do it your way:

%hash = (
"foo" => "one",
"bar" => "two",
);

foreach $key (keys %hash) {
$hash{$key} = [0..5];
}

Then later on during the iteration:

foreach $key (keys %hash) {
print "arrayref in hash key $key dereferences to = ";
print join("\t", @{$hash{$key}});
print "\n";
}

Or:

while (($key, $value) = each %hash) {
print "arrayref in hash key $key dereferences to = ";
print join("\t", @$value);
print "\n";
}


Best of luck.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/RSoFeS99pGMif6wRAjN+AJ9sh4BVeHLUggtYvJma1m9nt/hNTQCg96Lh
gJYTF3PkHE+lpGpaSaJz5rI=
=cK7E
-----END PGP SIGNATURE-----
 
T

Todd Bair

T

Todd Bair

Steve,

Thanks, that is the more graceful/correct method I was looking for.

Todd

Steven said:
Hi,

I am trying to write all of my code using strict to improve my
code. However, the correct use of references for the following problem
eludes me. I have a work around, but I would like to know the correct
way to code this.

#!/usr/bin/perl -w
######################################

use strict;
#no strict 'refs'; ## work around ##

######################################

my %hash = qw(foo one bar two);
my $key;

foreach $key (keys %hash)
{
@{$key} = (0..5);
}

Just say 'no' to symbolic references.

######################################
# later on in the program
######################################

foreach $key (keys %hash)
{
print "array $hash{$key} = ";

print join("\t",@{$key});

print "\n";
}

Try a hash instead:

#!/usr/local/bin/perl

use strict;
use warnings;

my %hash = qw(foo one bar two);
my %array_lookup;
my $key;

foreach $key (keys %hash)
{
$array_lookup{$key} = [ 0 .. 5 ];

# is preferred to
# @{$array_lookup{$key}} = (0 .. 5);
}

foreach $key (keys %hash)
{
print "array $hash{$key} = ";

print join("\t",@{$array_lookup{$key}});

print "\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top