Hash value not being interpolated

Discussion in 'Perl Misc' started by evillen@innocent.com, Feb 3, 2005.

  1. Guest

    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
     
    , Feb 3, 2005
    #1
    1. Advertising

  2. mjl69 Guest

    wrote:

    > 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
     
    mjl69, Feb 3, 2005
    #2
    1. Advertising

  3. Guest

    "" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Feb 3, 2005
    #3
  4. <> wrote:

    > 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.

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 3, 2005
    #4
  5. Guest

    wrote:
    > "" <> wrote:
    > > 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
    >
    > --
    > -------------------- http://NewsReader.Com/ --------------------
    > Usenet Newsgroup Service $9.95/Month 30GB


    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
     
    , Feb 4, 2005
    #5
  6. On Fri, 04 Feb 2005 02:32:11 -0800, wrote:
    > 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
     
    Leendert Bottelberghs, Feb 4, 2005
    #6
  7. Guest

    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
     
    , Feb 4, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bjorn Sagbakken

    Ajax animation: Interpolated, question

    Bjorn Sagbakken, Nov 18, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    428
    Bjorn Sagbakken
    Nov 18, 2007
  2. rp
    Replies:
    1
    Views:
    595
    red floyd
    Nov 10, 2011
  3. Richard Kilmer
    Replies:
    12
    Views:
    239
    Richard Kilmer
    Oct 5, 2004
  4. Replies:
    9
    Views:
    122
  5. Tom
    Replies:
    6
    Views:
    134
Loading...

Share This Page