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!

Problem with adding variables 1

Status
Not open for further replies.

N3wb1e

Programmer
Sep 25, 2011
4
US
I'm very new to perl, and I'm having trouble with the following script:
Code:
#!perl
$firstnum = 3;
$secondnum = 2;

$total = $firstnum + $secondnum;

print $total; ##comes out to 5

$firstnum = 6;

print $total; ##comes out to 5 (I know why) - but I want to come out to 8


How do I write this so that when I change the value of "$firstnum" or "$secondnum", "$total" will also change - without me re-writing "$total = $firstnum + $secondnum;"?
 
you could try this

Code:
#!/usr/bin/perl
$firstnum = 3;
$secondnum = 2;

$total = sub { $firstnum + $secondnum};

print &$total ."\n"; 

$firstnum = 6;

print &$total ."\n";

but really, I would not even consider it as anything other than a curiosity. If you plan to use this kind of approach for something even remotely important, I'd consider an OO based alternative if you really must avoid the repeat add.

Cheers,
Scott
 
I suppose I could take that approach...

Do you know if there is any way I could do this with dereferencing? The following works:
Code:
$number = 2;
$something = /$number;
print $$something;
$number = 3;
print $$something

I can't seem to get it to work when $something is equal to two variables added.
 
My initial example is the dereferencing you are alluding to. You cannot just dereference the variable, you need to dereference the actual calculation.

You might want to explain the reason for wanting to do this, as there could well be an alternative to your design. I am thinking that OO might be the way to go. This way you can re-do the calculations after an assignment.


Cheers,
Scott
 
Wow, I feel stupid. I tried using:
Code:
$total = sub{$var + $var2}
But was having trouble printing and referencing $total elsewhere in the program, and just realized I was making syntax errors.

This is for a D&D style RPG I've been writing in my spare time. I have a large list of variables (around 50) which follow the form:
Code:
$var = $varcomponent1 + $varcomponent2;
$varcomponent1 $varcomponent2 are prone to change often, and so I didn't want to repeat that line of code everytime one of them changed.

Forgive my ignorance, but to what do you refer when you say OO?

Cheers,
 
Ah! Nice project!

Ok - OO is definitely the way to go. Forgive my terseness - it's shorthand for Object Orientation.

You want to create Objects for things such as the Hero or Monsters etc. Then you can perform actions on them, such as

$hero->attack($monster)

or

$hero->heal(20);

or

if($hero->health() < 20) {
...
}

etc. makes it really neat a tidy. Then you can start going with inheritance, so maybe things like

Take a look at this for a primer:
Good luck!

Scott
 
Thanks for the link, that will definitely help.

I am now having the same trouble as I did before...

Code:
#!/usr/bin/perl

$str = 12;

$rec = {
name => "Anonymous",
age => 25,
strmod => statmod($str),
};

print $rec->{name}."\n";
print $rec->{strmod}."\n";##comes out to 1

$str = 14;

print $rec->{name}."\n";
print $rec->{strmod};##comes out to 1, but I need 2

sub statmod {
	$stat = $_[0];
	$mm = (-5);
	while ($stat>1) {
		$mm += 1;
		$stat -= 2;
	}
	return ($mm);
}

The subroutine statmod() is a replica of the D&D method for converting attributes like strength into a modifier for your roll. (12/13 would be 1, and 14/15 would come out to 2)
 
Hi Newbie,

You want to actually create an object for the character, not just use a hash. Try the following:

Create a file called Character.pm and put the following in it:

Code:
package Character;


#-------------------------------------------------------

sub new {

	my($class, %args) = @_;
	my $self = {};
	# map all the arguments to ourself
	# little bit naughty
	my $key;
	foreach $key (keys %args) {
		$self->{$key} = $args{$key};
	}

	bless ($self, $class);
	return $self;

}

#-------------------------------------------------------
sub statMod {
	my $self = shift;
	my $stat = shift;
	if(!exists $self->{$stat}) {
		return(undef);
	}	
	my $statVal = $self->{$stat};
	my $mm = (-5);
	while ($statVal>1) {
		$mm += 1;
		$statVal -= 2;
	}
	return ($mm);
}


#-------------------------------------------------------
1;

The create a perl script with the following:

Code:
#!/usr/bin/perl -w

use strict;
use Character;

my $hero = new Character(
	name => 'Hrun',
	age => 25,
	str => 12,
);

print $hero->{name}."\n";
print $hero->statMod('str')."\n";

$hero->{str} = 14;


print $hero->{name}."\n";
print $hero->statMod('str')."\n";

There are *many* other ways to do this. You might want to create functions for each stat rather than send it as a parameter i.e. $hero->strMod();

Cheers,
Scott

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top