LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2013, 08:18 PM   #1
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Rep: Reputation: 10
unexpected output in perl


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

@var = (0 .. 10);
$value = 1;
$a = 0;
$var[$a++] = $var[$a++] + $value;
foreach (@var) {
    print $_ . " ";
}
print "\n";
I'm expecting this to output:
Code:
2 1 2 3 4 5 6 7 8 9 10
but I'm getting:
Code:
0 1 2 3 4 5 6 7 8 9 10
When I change the line:
Code:
$var[$a++] = $var[$a++] + $value;
to:
Code:
$var[0] = $var[1] + $value;
I get the expected out put:
Code:
2 1 2 3 4 5 6 7 8 9 10
What's the problem?

Last edited by doughyi8u; 12-14-2013 at 08:19 PM.
 
Old 12-15-2013, 04:35 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Using multiple increments on the same variable in one line leads to undefined behaviour. This from the perlop document:
Quote:
Note that just as in C, Perl doesn't define when the variable is incremented or decremented. You just know it will be done sometime before or after the value is returned. This also means that modifying a variable twice in the same statement will lead to undefined behavior. Avoid statements like:

$i = $i ++;
print ++ $i + $i ++;

Perl will not guarantee what the result of the above statements is.
You can change your code to something like this:
Code:
#!/usr/bin/perl -w

@var   = ( 0 .. 10 ) ;
$value = 1 ;
$a     = 0 ;
$b     = 0 ;

$var[ $a ] = $var[ ++$b ] + $value ;

foreach ( @var ) {
  print $_ . " " ;
}

print "\n" ;
Which will result in:
Code:
$ ./foo.pl 
2 1 2 3 4 5 6 7 8 9 10
 
Old 12-15-2013, 02:50 PM   #3
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
By the way, what's likely happening is that “$var[$a++] + $value” is evaluated fully with its side effects before the rest of the assignment. This leads to accessing “$var[0]”, which is zero, adding value of “$value”, which is one, to get the result one, followed by incrementing “$a”. At this point, the rest of the statement is evaluated: “$var[$a++] = 1”. This time, “$a” is one so “$var[1]” is assigned the value of one (which it already has).

Of course, as druuna described, this is not reliable behaviour and may change at any time theoretically even within the same run of perl interpreter.
 
Old 12-22-2013, 05:21 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Any time I see the phrase 'undefined behaviour' it reminds me of a line in the Advanced C book by Peter Van der Linden (http://www.amazon.com/Expert-Program.../dp/0131774298).
It went something along these lines
Quote:
.. undefined behaviour (or implementation defined) means anything can happen as desired by the author (he's talking about the C compiler writer here..).
This may range from doing that calcn all the way to launching nuclear missiles, assuming you have the relevant Hardware module installed...


That last definitely helps me to treat it as a red flag

Last edited by chrism01; 12-30-2013 at 01:19 AM. Reason: typo
 
Old 12-26-2013, 10:02 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by doughyi8u View Post
... What's the problem?
If you have an expectation, you should first verify that the expectation is correct according to the official documentation.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unexpected output getopts in AIX Mark1986 Linux - Newbie 9 04-16-2012 09:32 AM
Unexpected output from my program hida4all Linux - Newbie 1 09-08-2010 04:53 AM
Unexpected output from grep zer0x333 Linux - General 12 12-04-2007 10:42 AM
C: malloc arrays- unexpected output kpachopoulos Programming 3 03-24-2006 09:30 PM
Unexpected output from 'ls' when using glob expressions psiakr3w Linux - General 7 07-22-2004 03:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration