FAQ
Folks,

I am having a problem dereferencing a hash and am hoping somebody can
assist. I am getting the error:

Can't use string ("napMD_ProgressCode") as a HASH ref
while "strict refs" in use

for the code below. I have tried various combinations of brackets and
plus signs in the subroutine to dereference the HoH to no avail.
Thanks in advance for setting me straight on how to do this properly.

Andrew Koebrick


==========
use strict;
my %napMD_ProgressCode = (
1 => {
Code => 'ProgCd',
Name => 'MD_ProgressCode',
Definition => 'status of the dataset or progress of a
review'
},
2 => {
Code => '001',
Name => 'completed',
Definition => 'production of the data has been
completed'
},
3 => {
Code => '002',
Name => 'historicalArchive',
Definition => 'data has been stored in an offline
storage facility'
}
);
sub drawInputRowControlled{
my $ControlList = $_[0];
my %ControlList = %$ControlList; #Problem line

for my $i (sort keys %ControlList) {
print qq{
<input type="checkbox"
name='status'
value="$ControlList{$i}{Code}">$ControlList{$i}{Name}</option><br>
}
}
}

drawInputRowControlled('napMD_ProgressCode');

Search Discussions

  • Chas. Owens at Nov 13, 2007 at 8:02 pm

    On Nov 13, 2007 12:55 PM, wrote:
    Folks,

    I am having a problem dereferencing a hash and am hoping somebody can
    assist. I am getting the error:

    Can't use string ("napMD_ProgressCode") as a HASH ref
    while "strict refs" in use snip
    my %napMD_ProgressCode = ( snip
    drawInputRowControlled('napMD_ProgressCode');
    snip

    That is because you are passing a scalar containing the string
    "napMD_ProgressCode" instead of a reference to the hash. Try

    drawInputRowControlled(\$napMD_ProgressCode)

    Also, it is wasteful to copy the entire hash into a new variable
    (unless you want to make changes that should not be reflected in the
    original hash). Just use the arrow operator:

    sub drawInputRowControlled{
    my $ControlList = shift;

    for my $key (sort keys %$ControlList) {
    print qq(
    <input type="checkbox"
    name='status'

    value="$ControlList->{$key}{Code}">$ControlList->{$key}{Name}</option><br>
    );
    }
    }
  • John W . Krahn at Nov 13, 2007 at 9:04 pm

    On Tuesday 13 November 2007 12:02, Chas. Owens wrote:
    On Nov 13, 2007 12:55 PM, wrote:

    my %napMD_ProgressCode = ( snip
    drawInputRowControlled('napMD_ProgressCode');
    snip

    That is because you are passing a scalar containing the string
    "napMD_ProgressCode" instead of a reference to the hash. Try

    drawInputRowControlled(\$napMD_ProgressCode)
    The variable $napMD_ProgressCode does not appear to exist. Perhaps you
    meant:

    drawInputRowControlled(\%napMD_ProgressCode)



    John
    --
    use Perl;
    program
    fulfillment
  • Dr.Ruud at Nov 13, 2007 at 8:26 pm

    exlibris@visi.com schreef:

    use strict;
    my %napMD_ProgressCode = (
    [...]
    );

    sub drawInputRowControlled{
    my $ControlList = $_[0];
    my %ControlList = %$ControlList; #Problem line

    for my $i (sort keys %ControlList) {
    print qq{
    <input type="checkbox"
    name='status'
    value="$ControlList{$i}{Code}">
    $ControlList{$i}{Name}
    </option>
    <br>
    }
    }
    }
    drawInputRowControlled('napMD_ProgressCode');
    You are crossing an ugly border there, by the way that you are trying
    to convert a string to the name of a data structure.
    Make it: drawInputRowControlled(\%napMD_ProgressCode);

    Then inside the sub you can use $_[0]->{$key}{Code}
    or $ControlList->{$key}{Code}.


    You use the values 1..3 for your first key level, and sort, so why don't
    you use an @array[0..2] for that level?

    --
    Affijn, Ruud

    "Gewoon is een tijger."
  • John W . Krahn at Nov 13, 2007 at 8:47 pm

    On Tuesday 13 November 2007 09:55, exlibris@visi.com wrote:
    Folks, Hello,
    I am having a problem dereferencing a hash and am hoping somebody can
    assist. I am getting the error:
    It appears that you are actually having a problem creating a reference
    to a hash.

    perldoc perldata
    perldoc perldsc
    perldoc perllol
    perldoc perlreftut
    perldoc perlref

    Can't use string ("napMD_ProgressCode") as a HASH ref
    while "strict refs" in use

    for the code below. I have tried various combinations of brackets
    and plus signs in the subroutine to dereference the HoH to no avail.
    Thanks in advance for setting me straight on how to do this properly.


    ==========
    use strict;
    my %napMD_ProgressCode = (
    1 => {
    Code => 'ProgCd',
    Name => 'MD_ProgressCode',
    Definition => 'status of the dataset or progress of a
    review'
    },
    2 => {
    Code => '001',
    Name => 'completed',
    Definition => 'production of the data has been
    completed'
    },
    3 => {
    Code => '002',
    Name => 'historicalArchive',
    Definition => 'data has been stored in an offline
    storage facility'
    }
    );
    sub drawInputRowControlled{
    my $ControlList = $_[0];
    my %ControlList = %$ControlList; #Problem line
    You are creating a copy of the hash. Why not just pass the whole hash
    in the first place:

    my %ControlList = @_;

    for my $i (sort keys %ControlList) {
    print qq{
    <input type="checkbox"
    name='status'
    value="$ControlList{$i}{Code}">$ControlList{$i}{Name}</option><br>
    }
    }
    It you are using a scalar that contains a reference to a hash then you
    don't need to make a copy of it, you can access it through the
    reference:

    for my $i (sort keys %$ControlList) {
    print qq{
    <input type="checkbox"
    name='status'

    value="$ControlList->{$i}{Code}">$ControlList->{$i}{Name}</option><br>
    }
    }

    }

    drawInputRowControlled('napMD_ProgressCode');
    You are passing a string to your subroutine. You need to pass a
    reference to the hash instead:

    drawInputRowControlled( \%napMD_ProgressCode );



    John
    --
    use Perl;
    program
    fulfillment

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedNov 13, '07 at 5:56p
activeNov 13, '07 at 9:04p
posts5
users4
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase