Why not either a hash of hash or array of hash for the commands?
Use a hash of hash if each command is unique where the command name (eg 'addTargetServer') is the top level key. Use an array of commands if the order is important or there may be multiple commands with the same name. Add a 'cmdName' key to the command hashes in the AoH case.
Something like the following code may be what you are after. Given the command line -u USER -p PASSWORD cmdName=addTargetServer hostname=host123 ipAddress=1.1.1.1 cmdName=subTargetServer hostname=host321 ipAddress=1.1.1.2, the following:
use warnings; use strict; use Getopt::Long; use Data::Dump::Streamer; my %options; Getopt::Long::GetOptions (\%options, "u=s", "p=s"); print "Option $_: value %options{$_}\n" for keys %options; my @commands; my $entry; for (@ARGV) { next unless /(\w+)=(.*)/; $entry = \%{$commands[scalar @commands]} if $1 eq 'cmdName'; $entry->{$1} = $2; } print "\n"; for my $command (@commands) { print join "\n", (map {"$_ = $command->{$_}"} sort keys %$command) +, "\n"; }
prints:
Option p: value %options{p} Option u: value %options{u} cmdName = addTargetServer hostname = host123 ipAddress = 1.1.1.1 cmdName = subTargetServer hostname = host321 ipAddress = 1.1.1.2
In reply to Re: how to get name of hash value itself?
by GrandFather
in thread how to get name of hash value itself?
by Binford
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |