Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: syntax for hashes with variable in name

by jethro (Monsignor)
on Nov 22, 2010 at 12:47 UTC ( [id://872961]=note: print w/replies, xml ) Need Help??


in reply to syntax for hashes with variable in name

Your immediate problem is that perl interprets your %{ as a hash slice (see http://docstore.mik.ua/orelly/perl/learn/ch05_05.htm for a small example)

Your larger problem is that you want to use a technique that is a relict from perl4 days and only (if at all) suitable for quick hacks

Instead of using

my %{"platform_$site"}; ${"platform_$site"}{"thingy"}= 5; foreach my $x ( keys %{"platform_$site"}) { ...
use multidimensional hashes
my %platform; $platform{$site}{"thingy"}= 5; foreach my $x ( keys %{$platform{$site}}) { ...

You can use the same technique for your arrays as well, i.e. use a so called HashOfArrays:

push @{$platform{$site}}, 1,2; $platform{$site}[5]= 5; foreach my $x ( @{$platform{$site}}) { ..

PS: I also notice you don't use 'use warnings;' and 'use strict;' at the beginning of your script. It is highly advisable to include those two lines. Avoids many bug finding sessions later.

Replies are listed 'Best First'.
Re^2: syntax for hashes with variable in name
by ikegami (Patriarch) on Nov 22, 2010 at 17:18 UTC

    Hash (and array) slices always start with @. No slices start with %.

    my %{"$name"};

    is the same as

    my %{$name}

    which is the same as

    my %$name; # Symbolic dereference

    which no more works than

    my %$ref; # Generic deference
Re^2: syntax for hashes with variable in name
by ambrus (Abbot) on Nov 22, 2010 at 13:55 UTC

    Your immediate problem is that perl interprets your %{ as a hash slice

    Is this for real?

      is it?

      Let's rule out the fact that this deals with symbolic references, strict tells us ...

      use strict; use warnings; { no strict "refs"; my $site = "foo"; my @{"platforms_$site"} = qw( foo bar ); my %{"platforms_$site"} = map { $_ => 1 } @{"platforms_$site"}; } => Can't declare array dereference in "my" at xx.pl line 8, near "} =" Execution of xx.pl aborted due to compilation errors.

      The difference is in the assignment part. You cannot use my there. When you drop those, you get:

      use strict; use warnings; { no strict "refs"; my $site = "foo"; @{"platforms_$site"} = qw( foo bar ); %{"platforms_$site"} = map { $_ => 1 } @{"platforms_$site"}; }

      Which is still bad coding style, but this works.


      Enjoy, Have FUN! H.Merijn

      It was the best explanation I could find at the moment. But I didn't check with deparse or something.

Re^2: syntax for hashes with variable in name
by equick (Acolyte) on Nov 22, 2010 at 13:06 UTC

    Thanks for your reply.

    Yes usually I include strict, but I had just taken an extract of an old script I'm patching to work on that particular function.

    I also agree multidimensional arrays are probably the way to go, but if possible I'd rather keep the current variables as they are used throughout the other script.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://872961]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-23 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found