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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hash initialization 1

Status
Not open for further replies.

gnubie

Programmer
Apr 16, 2002
103
US
I want to initialize a hash using a value declared immediately before the current one.

For example (below), I declared title and initialized it to 'The Root Cause'. Then I want to use the value of title in the declaration of headline.

[tt][blue]%article=(
title => 'The Root Cause',
headline => "$article{title}",
);
[/blue][/tt]

This doesn't produce a syntax error, but $article{headline} is empty.

Is there a way to do this?

 
That exact way, I would guess not, as the Hash doesnt exist yet. If you did

Code:
$article{'title'} = 'The Root Cause';,

then

Code:
$article{'headline'} = $article{'title'}

Would work, but if you need to do it as you described, I would guess that would be impossible, as that hash doesn't exists yet at that point.

Maybe someone else has a better solution.

Scott Prelewicz
Web Developer
COMAND Solutions
 
Thanks for the tip. That will work just fine.
 
I'm sure you'll figure it out, but theres some typos in my message. Remove the , after the first line, and the second line needs a semi-colon :)

Scott Prelewicz
Web Developer
COMAND Solutions
 
Since title and headline will have the same value you just do this:

Code:
%article=(
  title => 'The Root Cause',
  headline => 'The Root Cause',
);

Not sure why you are trying to complicate this.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I simplified the example. The actual code is more complex. I want to be able to reuse parts of identical code without having to retype it at every instance. I want to change just the title value and have it replicated where needed. The list is long and there other values to be replicated.

Code:
%article {
  title => 'The Root Cause',
  .
  .
  .
  headline => "$article{'title'}",
  outFile => "../financial/$article{'title'}.html",
}

Scott's suggestion is working perfectly.
 
You're going about it wrong. As has been said, you can't use a hash element inside the hash when you are first initializing the hash. You need to think about what you are doing and do something different.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
As Scott suggested, this is the short version of what I am doing. It works well:

Code:
%article {
  title => 'The Root Cause',
  headline => '',
  outFile => '',
}

$article{headline} => "$article{'title'}",
$article{outFile} => "../financial/$article{'title'}.html",

I only have to initialize the title and any title derivative is created automatically, which was my goal.
 
OK.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top