Array reference not consistently interpolating

E

evillen

Hi

I am trying to pass an array reference to a subroutine but get the
error:

"Not an ARRAY reference at array_ref_test.txt line 14."

I have slimmed down my code so don't worry about why I'm using an array
reference. The reference is getting correctly interpolated at some
point as line 12 reports:

"$key is: CONN_UMP_3MM_SMT"

Thanks for any ideas!
Len

## Please remove line numbers!
###########
1 #! perl -w
2 use strict;

4 my (%symbols_and_data, $key);
5 my @symbol_data = ("CONN_UMP_3MM_SMT", "sym.req:0554",
"lib.name:conn_ump_3mm_smt", "height:3.00mm", "used:01ATiss1/Whitney
RF", "rev:2,NJH,26/05/04");

7 &hash_sym(@symbol_data, $key);

9 sub hash_sym{
10 $symbols_and_data{$_[0]} = \@_[0]; #create new Hash key "reference"
11 foreach $key (keys %symbols_and_data){
12 print "\$key is: $key\n"
13 };
14 print "Elements in this hash key are: ".join(', ',
@{$symbols_and_data{$_[0]}})."\n";
15 }
 
A

A. Sinan Unur

Hi

I am trying to pass an array reference to a subroutine but get the
error:

"Not an ARRAY reference at array_ref_test.txt line 14."

I have slimmed down my code so don't worry about why I'm using an
array reference. The reference is getting correctly interpolated at
some point as line 12 reports:

"$key is: CONN_UMP_3MM_SMT"

Thanks for any ideas!
Len

## Please remove line numbers!

Yeah, thanks ... And with an exclamation point at the end. There is no
point in posting line numbers. It makes it harder than necessary to copy
and paste your code in an editor.

If you want to draw our attention to a line, you can put a comment
_above_ it (not in the right margin, because those comments have an
annoying tendency to run off the end of the line).
###########
1 #! perl -w

use warnings;

is better because it allows you to selectively disable warnings.
2 use strict;

4 my (%symbols_and_data, $key);

Always declare your vairables in the smallest applicable scope. What is
$key doing here? What is %symbols_and_data doing here? You only use them
in the body of the sub.
5 my @symbol_data = ("CONN_UMP_3MM_SMT", "sym.req:0554",
"lib.name:conn_ump_3mm_smt", "height:3.00mm", "used:01ATiss1/Whitney
RF", "rev:2,NJH,26/05/04");

Properly formatting your code would give you an advantage when you are
asking for others' help. It is like saying "I respect your time". Not
properly formatting your code is then tantamount to spitting in the faces
of the people whose help you are asking. Just thought I would point that
out.
7 &hash_sym(@symbol_data, $key);

9 sub hash_sym{
10 $symbols_and_data{$_[0]} = \@_[0]; #create new Hash key
"reference" 11 foreach $key (keys %symbols_and_data){

See what I meen by comments wrapping?

I do not know what 'create new Hash Key "reference"' means. It seems like
what you are trying to do is to insert a key into %symbols_and_data hash
whose value is a reference to a hash formed using the elements of
@symbol_data. I have come up with something based on that interpretation.
12 print "\$key is: $key\n"
13 };
14 print "Elements in this hash key are: ".join(', ',
@{$symbols_and_data{$_[0]}})."\n";

I am speechless in the face of this join call. Is there any reason why
you don't use Data::Dumper?

C:\Documents> cat s__t.pl
#! perl

use strict;
use warnings;

use Data::Dumper;

my @symbol_data = (
'CONN_UMP_3MM_SMT',
'sym.req:0554',
'lib.name:conn_ump_3mm_smt',
'height:3.00mm',
'used:01ATiss1/WhitneyRF',
'rev:2,NJH,26/05/04',
);


my %symbols_and_data;

print Dumper hash_sym(\%symbols_and_data, @symbol_data);

sub hash_sym {
my $h = shift;
$h->{$_[0]} = { @_ };
}

__END__

C:\Documents> perl s__t.pl
$VAR1 = {
'used:01ATiss1/WhitneyRF' => 'rev:2,NJH,26/05/04',
'CONN_UMP_3MM_SMT' => 'sym.req:0554',
'lib.name:conn_ump_3mm_smt' => 'height:3.00mm'
};

Sinan
 
G

Gunnar Hjalmarsson

I am trying to pass an array reference to a subroutine but get the
error:

"Not an ARRAY reference at array_ref_test.txt line 14."

You did not pass any array reference, to start with.
## Please remove line numbers!

Please don't include line numbers!

Is this what you are trying to do:

my %symbols_and_data;
hash_sym( \@symbol_data );

sub hash_sym {
$symbols_and_data{ shift @{ $_[0] } } = $_[0];
foreach my $key ( keys %symbols_and_data ) {
print "\$key is: $key\n";
print "Elements in this hash key are: " .
join( ', ', @{ $symbols_and_data{$key} } ) . "\n";
};
}
 
E

evillen

A. Sinan Unur said:
Yeah, thanks ... And with an exclamation point at the end. There is no
point in posting line numbers. It makes it harder than necessary to copy
and paste your code in an editor.

If you want to draw our attention to a line, you can put a comment
_above_ it (not in the right margin, because those comments have an
annoying tendency to run off the end of the line).


use warnings;

is better because it allows you to selectively disable warnings.


Always declare your vairables in the smallest applicable scope. What is
$key doing here? What is %symbols_and_data doing here? You only use them
in the body of the sub.


Properly formatting your code would give you an advantage when you are
asking for others' help. It is like saying "I respect your time". Not
properly formatting your code is then tantamount to spitting in the faces
of the people whose help you are asking. Just thought I would point that
out.

7 &hash_sym(@symbol_data, $key);

9 sub hash_sym{
10 $symbols_and_data{$_[0]} = \@_[0]; #create new Hash key
"reference" 11 foreach $key (keys %symbols_and_data){

See what I meen by comments wrapping?

I do not know what 'create new Hash Key "reference"' means. It seems like
what you are trying to do is to insert a key into %symbols_and_data hash
whose value is a reference to a hash formed using the elements of
@symbol_data. I have come up with something based on that interpretation.
12 print "\$key is: $key\n"
13 };
14 print "Elements in this hash key are: ".join(', ',
@{$symbols_and_data{$_[0]}})."\n";

I am speechless in the face of this join call. Is there any reason why
you don't use Data::Dumper?

C:\Documents> cat s__t.pl
#! perl

use strict;
use warnings;

use Data::Dumper;

my @symbol_data = (
'CONN_UMP_3MM_SMT',
'sym.req:0554',
'lib.name:conn_ump_3mm_smt',
'height:3.00mm',
'used:01ATiss1/WhitneyRF',
'rev:2,NJH,26/05/04',
);


my %symbols_and_data;

print Dumper hash_sym(\%symbols_and_data, @symbol_data);

sub hash_sym {
my $h = shift;
$h->{$_[0]} = { @_ };
}

__END__

C:\Documents> perl s__t.pl
$VAR1 = {
'used:01ATiss1/WhitneyRF' => 'rev:2,NJH,26/05/04',
'CONN_UMP_3MM_SMT' => 'sym.req:0554',
'lib.name:conn_ump_3mm_smt' => 'height:3.00mm'
};

Sinan

Hi Sinan

I would like to thank you both for your excellent advice - which I
shall shortly investigate, and also for the great pleasure it gave me
to read you venting your spleen upon me.

I guess I should have added that this is my first Perl prog, indeed it
is my first programming at all in the last five years... be patient
with the newbs ;-)

Thanks
Len
 
E

evillen

Gunnar said:
I am trying to pass an array reference to a subroutine but get the
error:

"Not an ARRAY reference at array_ref_test.txt line 14."

You did not pass any array reference, to start with.
## Please remove line numbers!

Please don't include line numbers!

Is this what you are trying to do:

my %symbols_and_data;
hash_sym( \@symbol_data );

sub hash_sym {
$symbols_and_data{ shift @{ $_[0] } } = $_[0];
foreach my $key ( keys %symbols_and_data ) {
print "\$key is: $key\n";
print "Elements in this hash key are: " .
join( ', ', @{ $symbols_and_data{$key} } ) . "\n";
};
}

Thanks for your help Gunnar

After a bit of tweaking I got this code to work with the rest of my
program:

#! perl
use strict;
use warnings;

my @symbol_data = (
'CONN_UMP_3MM_SMT',
'sym.req:0554',
'lib.name:conn_ump_3mm_smt',
'height:3.00mm',
'used:01ATiss1/WhitneyRF',
'rev:2,NJH,26/05/04',
);

hash_sym( \@symbol_data );

sub hash_sym {
my %symbols_and_data;
$symbols_and_data{ shift @{ $_[0] } } = $_[0];
foreach my $key ( keys %symbols_and_data ) {
print "\$key is: $key\n";
print "Elements in this hash key are: " .
join( ', ', @{ $symbols_and_data {$key} } ) . "\n";
};
}

___END___
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top