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

References: a little Help 1

Status
Not open for further replies.

liviux76

Technical User
Jul 3, 2008
3
CA
Hi all!
I'm just beginning to learn Perl.
I'm doing some exercise and I found a problem with subroutines and references.

My question is:

Why in the following simple code I'm not able to obtain the value of the numbers increased of 1?

Code:
#!/usr/bin/perl

# I send the array’s reference to the subroutine 
sub incr_ref {
	my $aref = shift;
	my @rnum = @$aref;
	for $i(0..2) {
	$$rnum[$i]++;
	$i++;
	}
}

print "insert 3 numbers to increment of 1: ";
for $i(0..2) {
$mynumber[$i] = <STDIN>;
$i++;
}
incr_ref (\@mynumber);
for $i(0..2) {
print "well, the number increased of 1 now values: $mynumber[$i]\n";
}

I did the same sending only a value (a scalar instead of an array) and it worked... ?!?
Thanks in advance, any help will be really appreciated!
Livio.
 
first of all you should learn to place your subroutines at the end of your script (it looks and reads easier)
try the following instead
Code:
#!/usr/bin/perl

# I send the array's reference to the subroutine
print "insert 3 numbers to increment of 1: ";
for $i(0..2) {
	$mynumber[$i] = <STDIN>;
	$i++;
}
[red]my @originalnumbers = @mynumber;[/red]

incr_ref (\@mynumber);

for $i(0..2) {
	print "well, the number [red]$originalnumbers[$i][/red] increased of 1 now values: $mynumber[$i]\n";
}

sub incr_ref {
    my $aref = shift;
    for $i(0..2) {
    	$$aref[$i]++;
    	$i++;
    }
}

what exactly were you trying to do with this my @rnum = @$aref;....you assign a array dereference reference to an array and you dereference that array and then your perl tries to jump out of the window but there is no window and it can not yell and it opens the door and it runs too....sorry I got lost... :)



``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
or if you want to make it work for both scalars and arrays then you can check your ref to see if you deal with array or not and act accordingly...like this
Code:
#!/usr/bin/perl

# I send the array's reference to the subroutine
print "insert 3 numbers to increment of 1: \n";
for $i(0..2) {
	$mynumber[$i] = <STDIN>;
	$i++;
}
[red]my @originalnumbers = @mynumber;[/red]

incr_ref (\@mynumber);

for $i(0..2) {
	print "well, the number [red]$originalnumbers[$i][/red] increased of 1 now values: $mynumber[$i]\n\n";
}

[red]print "insert 1 number to increment of 1: \n";
my $number = <STDIN>;
my $originalnumber = $number;
incr_ref (\$number);
print "well, the number $originalnumber  increased of 1 now values: $number\n";[/red]

sub incr_ref {
    my $aref = shift;
	[red]if (ref($aref) ne 'ARRAY') {
		$$aref++;
		return;
	}
	else {[/red]
		for $i(0..2) {
			$$aref[$i]++;
			$i++;
		}
		return;
	}
}


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
WOW THANKS! :)
Now It works!

Actually I don't know why I did this my @rnum = @$aref; ... but for that I got lost too!!!
Probably I read something in some website and I got confused more than I was before...

I'm improving my skills, I'm quite rusty in programming but in my new job probably I would be asked to use it...

Thanks a lot again!!!
I'm sure that I'll look often at this website in the future!
Livio.
 
perluserpengo - Tried the loop
Code:
for $i(0..2) {
   print ">$i<\n";
}
and noticed that $i incremented as I would have expected. I see you're adding a $i++ at the bottom of your loop. Is there a reason you added this or just a coding habit? I'm asking from this perspective because I often will do the following
Code:
CODEBLOCK:
{
   $result = <insert some formula here> ;
   $result += 0 ;    # <--- see below
}
I'll add the += 0 as one final calculation because from time to time I'll get "-0" (neg 0) showing up in the output. Instead of trying to figure out why I'm getting the -0, I just toss in a += 0 and it tends to clean up the negative flag.
 
these are probably just carried over from the original code:

Code:
        for $i(0..2) {
            $$aref[$i]++;
            [b]$i++;[/b]
        }

In the above case $i++ has no affect because the value of $i is reset when the next loop iteration starts, but if you did something like this:

Code:
        for $i(0..2) {
            $i++;
            $$aref[$i]++;
        }

$i will be incremented before the rest of the code is evaluated and will be 1-2-3 instead of 0-1-2 and the code will probably not work as expected. The unwary perl programmer may think perl has gone bonkers.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
KevinADC is right...I didn't quite pay any attention to it...
I tried only to clear the reference query...

it doesn't really have to be there...
its fine like this...
Code:
for ( 0..2 ) {
  $$aref[$_]++;
}


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Hi!
I was adding $i++ at the bottom of my loop just for a coding habit...
Years ago I was used to do it writing C programs but now, as Kevin said, I noticed that in Perl it doesn't matter...
Good to known!
Thanks again to all! ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top