Hash value not being interpolated

E

evillen

Hi

Please help, I can't figure out why my hash value not being
interpolated correctly?

code snippet:
begin
-------
@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));
$symbols_and_data{$symbol_data[0]} = @symbol_data;
print "\$symbol_data[0] is: $symbol_data[0]\n";
print "Hash value is currently: $symbols_and_data{$symbol_data[0]}\n";
-------
end

description of problem:

I have the following array which has six elements:
@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))


So far so good, I then dynamically declare and assign a hash key:
$symbols_and_data{$symbol_data[0]} = @symbol_data; #Create a Hash with
symbol name as key and data as value


Then I want to check a value:
print "\$symbol_data[0] is: $symbol_data[0]\n";

that produces this:
"$symbol_data[0] is: CONN_UMP_3MM_SMT"


Finally I want to see my hash key's values:
print "Hash value is currently: $symbols_and_data{$symbol_data[0]}\n";


that produces this:
"Hash value is currently: 6"
this appears to be the number of elements, rather than the values of
the elements.


I am expecting to see this:
"Hash value is currently: 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"

Sorry about the horrible Google formatting - I am looking into a proper
Usenet service...

Many thanks
Len
 
M

mjl69

Hi

Please help, I can't figure out why my hash value not being
interpolated correctly?
print "Hash value is currently: $symbols_and_data{$symbol_data[0]}\n";

try

print "Hash value is currently: '
.$symbols_and_data{$symbol_data[0]}.
"\n";

Try News.Individual.NET
 
X

xhoster

Hi

Please help, I can't figure out why my hash value not being
interpolated correctly?

The problem is that your hash value is not being assigned correctly.
$symbols_and_data{$symbol_data[0]} = @symbol_data;

The left hand side is a scalar. Which means the right hand side
is interpreted in scalar context. Using an array in scalar context
yields the length of the array.

$symbols_and_data{$symbol_data[0]} = \@symbol_data;
or
$symbols_and_data{$symbol_data[0]} = [@symbol_data];
or
@{$symbols_and_data{$symbol_data[0]}} = @symbol_data;

Read up on perl references and data structures (perldoc perlref,
perldoc perldata, etc.).

BTW, it doesn't look like you are using warnings or strict. Do so.

Xho
 
T

Tad McClellan

Please help,


Please show actual Perl code.

I can't figure out why my hash value not being
interpolated correctly?


The code does not even compile, so speaking of its output
is premature.

@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));

Bareword found where operator expected at temp line 6, near "3.00mm"
(Missing operator before mm?)
Bareword found where operator expected at temp line 6, near "01ATiss1"
(Missing operator before ATiss1?)
syntax error at temp line 5, near "req:"
temp had compilation errors.


You should put quotes around your strings.
 
E

evillen

Hi

Please help, I can't figure out why my hash value not being
interpolated correctly?

The problem is that your hash value is not being assigned correctly.
$symbols_and_data{$symbol_data[0]} = @symbol_data;

The left hand side is a scalar. Which means the right hand side
is interpreted in scalar context. Using an array in scalar context
yields the length of the array.

$symbols_and_data{$symbol_data[0]} = \@symbol_data;
or
$symbols_and_data{$symbol_data[0]} = [@symbol_data];
or
@{$symbols_and_data{$symbol_data[0]}} = @symbol_data;

Read up on perl references and data structures (perldoc perlref,
perldoc perldata, etc.).

BTW, it doesn't look like you are using warnings or strict. Do so.

Xho

Hi Xho

Unfortunately when I use any of the assignments that you suggest I get
the following:

"Hash value is currently: ARRAY(0x1837f84)"

Any ideas?

Thanks for your help
Len
 
L

Leendert Bottelberghs

Unfortunately when I use any of the assignments that you suggest I get
the following:

"Hash value is currently: ARRAY(0x1837f84)"

That is the correct output for that line.
If you used:
$symbols_and_data{$symbol_data[0]} = \@symbol_data;

then the value of $symbols_and_data{$symbol_data[0]} is a reference to the
@symbol_data array. In order to print all elements of this array, first
"dereference" it (sort of type-casting), and then convert the array into a
string of elements. E.g.

print "Elements in this hash key are: ".join(', ',
@{$symbols_and_data{$symbol_data[0]}})."\n";

Notice that in the join function [syntax = join($string, @array)], the
array reference is "dereferenced" as follows:

@{$array_ref}
e.g.
@{$symbols_and_data{$symbol_data[0]}}

This array can then be used in the join.

-leendert bottelberghs
 
E

evillen

Thanks Leendert

I realised what I was doing and produced:

###################
#! perl -w
use strict;

my $aref;
my %symbols_and_data;
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");

$aref = \@symbol_data;
$symbols_and_data{$symbol_data[0]} = $aref;

print @{$symbols_and_data{$symbol_data[0]}};
####################

Which works, but your suggestion looks more suitable - I'll try it out.

Many thanks
Len
 

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