Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Array reference not consistently interpolating

Reply
Thread Tools

Array reference not consistently interpolating

 
 
evillen@innocent.com
Guest
Posts: n/a
 
      02-04-2005
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 }

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-04-2005
"(E-Mail Removed)" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> 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:umper?

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

use strict;
use warnings;

use Data:umper;

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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-04-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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";
};
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
evillen@innocent.com
Guest
Posts: n/a
 
      02-04-2005

A. Sinan Unur wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed) oups.com:
>
> > 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:umper?
>
> C:\Documents> cat s__t.pl
> #! perl
>
> use strict;
> use warnings;
>
> use Data:umper;
>
> 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

 
Reply With Quote
 
evillen@innocent.com
Guest
Posts: n/a
 
      02-08-2005
Gunnar Hjalmarsson wrote:
> (E-Mail Removed) wrote:
> > 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";
> };
> }
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl


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___

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pool Module: iterator does not yield consistently with differentchunksizes syockit Python 2 07-02-2010 11:55 AM
PC does not consistently hibernate =?Utf-8?B?RGVuemlsIEZlcm5hbmRlcw==?= Windows 64bit 7 07-21-2006 07:44 AM
Clipboard? Cut & Paste not working consistently... DWACON Computer Support 1 03-03-2006 11:33 AM
Page_Load not firing first consistently mollyf@hotmail.com ASP .Net 2 05-31-2005 01:58 PM
XP system tray program not showing up consistently Cloud Burst Computer Support 6 06-16-2004 03:46 PM



Advertisments