SPUG: derefencing hash reference / parsing config file (con't)

Sean Chittenden sean at chittenden.org
Sat Jan 8 15:25:09 CST 2000


	Below:

On Sat, 8 Jan 2000, Todd Wells wrote:

> In the following code, assume that 
> $config{models} = "{CCS = { name = CCS; pool = 1; max = 1; }; };" (without
> the quotes)
> 
> Excerpted Code:
> 
>         my $line = $config{models};
>         $line =~ s/=/=>/g;      # replace "=" with "=>"
>         $line =~ s#\;#\,#g;     # change ";" to ","

	Toss in an 'o' modifier to the end of your regexps for a speed
boost, but I don't know if that's an issue here

>         $line .= ";";           # add a ; to the end so it's Perl
>         $models = eval $line;   # create a models hash
> 
>         print "\nmodels\n=======\n";

	I think your problem is in the following line:

>         foreach $var (keys %models)

	Depends on how your eval is setup, but if my guess is right,
you're getting a hash ref from the eval, so you need to dereference the
models scalar, which is a hash ref.

	foreach $var (keys %$models) {

>         {
>                 my $value = $models{$var};

	Then you'd change the previous line to:
	my $value = $models->{$var};

>                 print "$var is $value\n";
>         }
> 
>         print "models is $models\n";

	I'm not sure what you'd achieve by printing out the hash ref, but
oh well... if you' enjoy looking at that kinda stuff....  ;~)

> My output was only:
> 
> models
> =======
> models is HASH(0x15ba60)

	Which is correct and tells you, the programmer, that the value of
$model is a hash ref.  That being the case, just stick a % in front of your
hashrefs and you're golden... well... that and you have to dereference
your hash ref data members using the -> operator (why don't they make a
key that displays '->'?  I know both C and perl programmers would live
the guy/gal who came out with such a keyboard...)

> Thanks for any help you can give,

	No problem....  a distraction while working OT is a good
thing...  ::grin::

> -Todd

-- 
Sean Chittenden                              <sean at chittenden.org>
fingerprint = 6988 8952 0030 D640 3138  C82F 0E9A DEF1 8F45 0466

There once was an old man from Esser,
Who's knowledge grew lesser and lesser.
	It at last grew so small,
	He knew nothing at all,
And now he's a College Professor.



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list