Array reference not consistently interpolating

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

  1. Guest

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

  2. "" <> wrote in
    news::

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

  3. 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
     
    Gunnar Hjalmarsson, Feb 4, 2005
    #3
  4. Guest

    A. Sinan Unur wrote:
    > "" <> wrote in
    > news::
    >
    > > 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


    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
     
    , Feb 4, 2005
    #4
  5. Guest

    Gunnar Hjalmarsson wrote:
    > 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___
     
    , Feb 8, 2005
    #5
    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. Replies:
    2
    Views:
    543
  2. jitasi
    Replies:
    1
    Views:
    780
    Terry Reedy
    Mar 4, 2007
  3. syockit
    Replies:
    2
    Views:
    282
    Dave Angel
    Jul 2, 2010
  4. Jarmo Pertman
    Replies:
    3
    Views:
    165
    Jarmo Pertman
    Nov 27, 2010
  5. Peter

    js not working consistently

    Peter, Jan 20, 2008, in forum: Javascript
    Replies:
    2
    Views:
    130
    Thomas 'PointedEars' Lahn
    Jan 20, 2008
Loading...

Share This Page