Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can hash key be a variable/regex ?

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
IL
HI,

I can not figure out if a hash key can be a variable/regex ?

Is the following valid ?

%myHash{$param . "string"}

or

if ( %myHash{$param\s*\D})

Long live king Moshiach !
 
This is possible:
Code:
[COLOR=#008080]$param[/color] = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]abc[/color][COLOR=#ff00ff]'[/color];
[COLOR=#008080]$my_hash[/color]{[COLOR=#008080]$param[/color] . [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]string[/color][COLOR=#ff00ff]"[/color]} = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]foo[/color][COLOR=#ff00ff]'[/color];
[COLOR=#008080]$param[/color] = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]xyz[/color][COLOR=#ff00ff]'[/color];
[COLOR=#008080]$my_hash[/color]{[COLOR=#008080]$param[/color] . [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]string[/color][COLOR=#ff00ff]"[/color]} = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]bar[/color][COLOR=#ff00ff]'[/color];

[COLOR=#0000ff]# process all keys -> values[/color]
[COLOR=#804040][b]foreach[/b][/color] [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$key[/color] ([COLOR=#804040][b]sort[/b][/color] [COLOR=#804040][b]keys[/b][/color]([COLOR=#008080]%my_hash[/color])){
  [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$key[/color][COLOR=#ff00ff] -> [/color][COLOR=#008080]$my_hash[/color][COLOR=#ff00ff]{[/color][COLOR=#008080]$key[/color][COLOR=#ff00ff]}[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
}

[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];

[COLOR=#0000ff]# find specific key and process key -> value[/color]
[COLOR=#804040][b]foreach[/b][/color] [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$key[/color] ([COLOR=#804040][b]sort[/b][/color] [COLOR=#804040][b]keys[/b][/color]([COLOR=#008080]%my_hash[/color])){
  [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]$key[/color] =~[COLOR=#804040][b] /[/b][/color][COLOR=#ff00ff]^xyz[/color][COLOR=#6a5acd].+[/color][COLOR=#ff00ff]$[/color][COLOR=#804040][b]/[/b][/color]) {
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]do something with this '[/color][COLOR=#008080]$key[/color][COLOR=#ff00ff] -> [/color][COLOR=#008080]$my_hash[/color][COLOR=#ff00ff]{[/color][COLOR=#008080]$key[/color][COLOR=#ff00ff]}'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
  }
}
Output:
Code:
abcstring -> foo
xyzstring -> bar

do something with this 'xyzstring -> bar'
 
This
Code:
#
$param = 'abc';
if (exists $my_hash{$param . 'string'}) {
  print "I found '$my_hash{$param . 'string'}'";
}
works:
Code:
I found 'foo'
 
thanks.

will also this work ?

$param = 'abc';
if (exists $my_hash{$param\s{3}\D*string}) {
print "I found $my_hash{$param\s{3}\D*string}";
}

Long live king Moshiach !
 
Didn't try it, but I think it won't.
A hash key is simply an [tt]EXPR[/tt] (as it is referred to in the doc), so it can contain a regex operation with the [tt]~[/tt] operator, and the result (whatever it is and interpreted in scalar context) becomes the key value.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
As I showed you above you can try if the hash key matches the regex and then ask if the hash value exists and process it:
Code:
if ($key =~ /^xyz.+$/) {
  if (exists $my_hash{$key} {
    ...
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top