> Yet further question:
>
> my %this = ('A' => 'valueA', 'B' => 'valueB');
> my %is = ('A' => 'othervalueA', 'B' => 'othervalueB');
> my @array = ('this', 'is');
> my $string = "this";#will actually be user input, might also be "is"
> my $data = "A and B";
> foreach my $array (@array) {
> if ($string eq $array) {
> $data =~ s/A/$array{A}/g;#just as an example, there'll be more string
> conversions to follow, probably done with regexps
> }
> }
>
> Is this clear? I want to carry out replacement operations on string $data
> depending on the value of $string. There are different types of
replacement
> routines, stored in hashes %this and %is. I'm looking for a way to
condense
> code that does that for all available hashes, instead of doing single
> conditions like:
>
> if ($string eq "this") { $data =~ s/A/$this{A}/g;}
>
> I don't suppose $array{A} in the above code is what I want it to be: the
> value for the key "A" from that particular hash to which the scalar $array
> refers - it would be the value for the key "A" in %array, which doesn't
> exist in my script.
> But how can I turn it into what I want it to be?
I'm not really sure about what you want to do, but here's what I think could
help you. First, you may want to use a hash of hashes instead of 2 (or more)
separate hashes and an array. (If you can call the camel book your own, have
a look at chapter 9, 'Data Stuctures'. There's a section 'Hashes of
Hashes'.)
my %hash = (
this => {
A => 'valueA',
B => 'valueB',
},
is => {
A => 'othervalueA',
B => 'othervalueB',
},
);
Supposed you have $string and $data as in the code you posted and want to
replace all occurrences of the hash keys in $data by their corresponding
hash values, depending on $string, this does a simple for loop.
my $string = "this";
my $data = "A and B";
$data =~ s/$_/$hash{$string}{$_}/g for keys %{$hash{$string}};
This can of course be improved by checking if $string is a valid key for
%hash.
If you want to play around with the code, here's a complete script:
----------------------------------------------------------------
#!/bin/perl -w
use strict;
my %hash = (
this => {
A => 'valueA',
B => 'valueB',
},
is => {
A => 'othervalueA',
B => 'othervalueB',
},
);
my $string = "this";
my $data = "A and B";
$data =~ s/$_/$hash{$string}{$_}/g for keys %{$hash{$string}};
print $data, "\n";
----------------------------------------------------------------
Currently, this will print
valueA and valueB
Hope this helps.
-- Marcus
----- Original Message -----
From: "Birgit Kellner" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, August 25, 2001 4:05 AM
Subject: hash key with regexp - thanks & further question
> --On Freitag, 24. August 2001 09:54 -0700 "Randal L. Schwartz"
> <[EMAIL PROTECTED]> wrote:
> > That's too much there...
> >
> > /add_\w+/ and print("Yippie!\n"), last while $_ = each %hash;
> >
> > "scalar each" is underutilized. :)
> >
> I'm impressed, thanks a lot!
>
> Further question: Someone mentioned a module that allows for regexps as
> hash keys. Could you please repost that, I inadvertently deleted the
> message ...
>
> Yet further question:
>
> my %this = ('A' => 'valueA', 'B' => 'valueB');
> my %is = ('A' => 'othervalueA', 'B' => 'othervalueB');
> my @array = ('this', 'is');
> my $string = "this";#will actually be user input, might also be "is"
> my $data = "A and B";
> foreach my $array (@array) {
> if ($string eq $array) {
> $data =~ s/A/$array{A}/g;#just as an example, there'll be more string
> conversions to follow, probably done with regexps
> }
> }
>
> Is this clear? I want to carry out replacement operations on string $data
> depending on the value of $string. There are different types of
replacement
> routines, stored in hashes %this and %is. I'm looking for a way to
condense
> code that does that for all available hashes, instead of doing single
> conditions like:
>
> if ($string eq "this") { $data =~ s/A/$this{A}/g;}
>
> I don't suppose $array{A} in the above code is what I want it to be: the
> value for the key "A" from that particular hash to which the scalar $array
> refers - it would be the value for the key "A" in %array, which doesn't
> exist in my script.
> But how can I turn it into what I want it to be?
>
>
> Birgit Kellner
>
>
>
>
>
>
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]