in reply to Re: Re: Complex Data Structures
in thread Complex Data Structures

Thanks for the replies. I will have a look at it but I think I am going to use a different data structure instead. I promise I will use strict and warnings in future. Just for the record the variables were
$curr_struct_pos = "string"; %$curr_struct_pos;
and I wanted the new hash to have the same name as the string in the array - not the keys of the hash. Also
$path = "Global${type}Def:$$curr_struct_pos{$type}[$a].def";
does give me the correct paths. I have only been using Perl for less than two weeks so forgive my Cish Perl.

Replies are listed 'Best First'.
Re: Re: Re: Re: Complex Data Structures
by demerphq (Chancellor) on Aug 31, 2001 at 20:15 UTC
    Just for the record the variables were
    $curr_struct_pos = "string"; %$curr_struct_pos;
    Ok. So it was deliberate. My apologies for getting a little agro about it then... OTOH, why are you doing this? Why do you want to get a reference to a hash this way? Why not use a hard ref?
    Most perlmongers would avoid doing this type of thing except in very unusual circumstances, for many reasons. (You will see many threads this subject in CLPM if you do a search for 'symrefs')
    Normally the same result can be achieved in a much more readable and safer way by using a hash. Foinstance our toplevel of the data structure could be
    #assume strict and warnings! my %hash=(string=>{});
    and you would access it like
    $hash{string}->{$key}->[$index]=$whatever;

    and I wanted the new hash to have the same name as the string in the array - not the keys of the hash
    One of us is a bit confused here. What do you mean by the hash having the same name? My understanding is that you will be using anonymous hshes, which have no name. Unless you are evaling named hashes (or symrefs i suppose) into scope you cant do what I think you are saying.
    $path = "Global${type}Def:$$curr_struct_pos{$type}[$a].def"; does give me the correct paths.
    I can believe (now that you have explained that are using symrefs deliberately) that it _works_. But its in a style that IMHO will give you trouble.
    I gave you one interpretation in the earlier post, but now that I understand that you have some idea of what you are doing (even if it is not to be advised) I could give you another way to write it:
    $path = "Global".$type. "Def:".$curr_struct_pos->{$type}[$a]. ".def";
    For me this is easier to understand at a glance, and therefore more maintainable, also it gets rid of that confusing ugly and confusing $$ and replaces it with $var_name-> which should produce the same result.
    Anyways, I suppose Ill stop lecturing, but I will repeat that probably have no good reason to use symrefs, that you can accomplish what you want without them, and have much easier to understand and maintain code.

    If you are going to play around with symrefs a lot then you should have a look at this (after you have read perlreftut):
    sub dump_symbol_table { print "SYMBOL TABLE\n--------------------------------------------- +----------\n"; foreach my $key (sort keys %::) { next unless $key=~/^[\x20-\xff]/; (my $value=$::{$key})=~s/([\x00-\x1f])/sprintf("\\x%02x",ord($ +1))/ge; printf "%20s = %-20s\n",$key,"'$value'"; } print "\n"; } my $symref="hash"; ${$symref}{key}="value"; print "Keys in 'our %hash':".join(",",keys %$symref)."\n"; BEGIN { dump_symbol_table } END { dump_symbol_table }
    Oh and I had the tagline before writing this...

    Yves
    --
    You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)