Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: How to create variables for each hash key that has a value.

by GrandFather (Saint)
on Jun 26, 2019 at 22:45 UTC ( [id://11102004]=note: print w/replies, xml ) Need Help??


in reply to How to create variables for each hash key that has a value.

You still haven't actually said what you are trying to do, but reading between the lines it seems you want to be able to run code to handle an entry in a hash passed to your code from some front end code dealing with fields (sounds a little like handling a web page to me). If that is the case then consider:

use strict; use warnings; my %dispatch = ( 0 => \&HandleParam0, 1 => \&HandleParam1, 2 => \&HandleParam2, 3 => \&HandleParam3, 4 => \&HandleParam4, 5 => \&HandleParam5, ); my %params = ( 'default-0' => ['lakja', 'haljl', 'alka'], 'default-1' => 'abc', 'default-2' => [1, 54, 83, 23], 'default-5' => '2019-06-26 00:00:10', ); for my $key (sort keys %params) { next if $key !~ /^default-(\d+)/ || !exists $dispatch{$1}; $dispatch{$1}->($params{$key}); } sub HandleParam0 { my ($values) = @_; print "default-0: @$values\n"; } sub HandleParam1 { my ($str) = @_; print "default-1: $str\n"; } sub HandleParam2 { my ($values) = @_; print "default-2: @$values\n"; } sub HandleParam3 { my ($values) = @_; print "default-3: @$values\n"; } sub HandleParam4 { my ($str) = @_; print "default-4: $str\n"; } sub HandleParam5 { my ($date) = @_; print "default-5: $date\n"; }

Prints:

default-0: lakja haljl alka default-1: abc default-2: 1 54 83 23 default-5: 2019-06-26 00:00:10
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^2: How to create variables for each hash key that has a value.
by GrandFather (Saint) on Jun 26, 2019 at 23:05 UTC

    An interesting twist is to dispense with the dispatch table:

    use strict; use warnings; my %params = ( 'default-0' => ['lakja', 'haljl', 'alka'], 'default-1' => 'abc', 'default-2' => [1, 54, 83, 23], 'default-5' => '2019-06-26 00:00:10', 'default-10' => 'Nothing to see here', ); for my $key (sort keys %params) { next if $key !~ /^default-(\d+)/; my $sub = main->can("HandleParam$1"); if ($sub) { $sub->($params{$key}); } else { warn "Don't know how to handle 'default-$1'"; } } ...

    which prints:

    default-0: lakja haljl alka Don't know how to handle 'default-10' at noname1.pl line 20. default-1: abc default-2: 1 54 83 23 default-5: 2019-06-26 00:00:10

    where the second line is a warning line. Using this technique means you only need to create correctly named handler subs to add support for new fields and you get told if there is a missing handler. Both features make maintenance much easier!

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?
Username:
Password:

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

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

    No recent polls found