develooper Front page | perl.perl6.users | Postings from January 2019

Re: I need hash string help

Thread Previous | Thread Next
Bruce Gray
January 12, 2019 02:35
Re: I need hash string help
Message ID:

> On Jan 11, 2019, at 7:08 PM, ToddAndMargo via perl6-users <> wrote:
> Hi All,
> Now what am I doing wrong?  I need to convert the value in a
> hash to a string:
> $ p6 'my $x = "acme"; my Str $y; my %Vendors = ( acme => ContactName => "Larry" ); $y= %Vendors<ContactName>; say $y;'
> $ p6 'my $x = "acme"; my Str $y; my %Vendors = ( acme => ContactName => "Larry" ); $y= %Vendors<ContactName>.Str; say $y;’

This has nothing to do with string conversion.

Compared to the previous email thread, two problems have been introduced:

1. The curly braces from around the second-level hash are now missing, changing from 
    CompanyName => { SomeKey => "foo" ... }
    CompanyName =>   SomeKey => "foo" ...
So, you no longer have a HashOfHashes, you have a HashOfPair (singular Pair, not plural Pairs), so while this syntax can technically be made to work, it would only work with a single key, which is pointless.

2. The first level (company name?) of your HashOfHashes is no longer dereferenced. I see that you populated `$x`, but did not ever use it.
	%Vendors{"acme"}<ContactName>;      		# Correct
	%Vendors{$company}<ContactName>;    	# Correct, if $company contains `acme`
	%Vendors<acme><ContactName>;        	# Correct and most Perlish, if company is constant
	%Vendors<ContactName>;              		# Bad; will never work unless you happen to have a company really named "ContactName”. (Even then, you would get the whole first-level hash)

UPDATE: Just before sending this, I peeked ahead at the exchange between Tom Browder and yourself. I think that you hit a snag, and constructed an example that showed what you thought was the snag, but was really a new set of problems that exists only in your example. FYI, every time that I have made this mistake, it was always due to my creating an example from scratch, instead of slowly massaging the real problem code down into a minimal form for public discussion. YMMV.

I am glad you and Tom resolved your problem before I could `send`, but I did not want this post to go to waste.

Hope this helps,
Bruce Gray (Util of PerlMonks)

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About