echoangel911 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a file that contains hash data and within perl, I load it using $hashvalue = do "$filename". I was wondering within the file containing the hash, am I able to reference sames values at different locations? As an example, my file has the following:
$data = { 'CATNUMBER' => '6', 'STATEMENT' => 'THERE EXIST 6 CATS', }
How can I replace the 6 with the CATNUMBER value?

Replies are listed 'Best First'.
Re: Loading Hashes Into Perl
by Fletch (Bishop) on Jul 27, 2006 at 14:39 UTC

    You can't as you have it. However since you're loading the code with do you can write any valid Perl and it'll work fine.

    $data{ 'CATNUMBER' } = 6; $data{ 'STATEMENT' } = "THERE EXIST $data{ 'CATNUMBER' } CATS";

    (Minor style point: note that the quotes on the keys aren't strictly needed since you're using the fat comma => operator, nor are they needed in my sample; however I've started to use them myself after getting into the habit in Ruby where you can't omit them :)

Re: Loading Hashes Into Perl
by swampyankee (Parson) on Jul 27, 2006 at 14:44 UTC

    $data{STATEMENT} =~ s/\b\d+\b/$data{CATNUMBER}/;
    would be one way, but it would only work if you don't have a string of the form "There is 1 grey and 4 ginger cats". If you do, a bit more sophistication is involved.

    If (a big condition, I realize) you've got control over how the file is set up, you could consider some distinct markers for the values you want to replace.

    You've really got to be a bit more forthcoming with your requirements and expectations. Giving one case, which may very well be special, is really not a good way to ask a question

    emc

    Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.

    Groucho Marx
Re: Loading Hashes Into Perl
by sh1tn (Priest) on Jul 27, 2006 at 14:36 UTC
    $data->{'STATEMENT'} =~ s/6/$data->{'CATNUMBER'}/;