Operating System - OpenVMS
cancel
Showing results for 
Search instead for 
Did you mean: 

understanding the perl one line

Abdul khadeer_1
Occasional Advisor

understanding the perl one line

Hi,
in this forum in the thread http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1065208
i have seen the solution for the question as
#perl -ne '($h,$n)=split; if ($old=$o{$h}) {print "$h : ", $n - $old, "\n"} else {$o{$h}=$n}' file1.txt file2.txt
in the if statement iam seeing that the value $o{$h}is being assigned to $old,in the if condition there is no condition as i was expecting i.e like == ,>,< but the result is coming out as expected can please someone help in understanding this
Thanks in advance
3 REPLIES
Hein van den Heuvel
Honored Contributor

Re: understanding the perl one line



if ($variable)

Is true if $variable is defined and not 0.

if ($variable=$other)

First assigns $other to $variable and test the result for true or false.
So it really tests $other, while at the same time picking up its value.

It could be written as:

if ($other) {
$variable = $other;
...
}

hth,
Hein.




Abdul khadeer_1
Occasional Advisor

Re: understanding the perl one line

($h,$n)=split is going to split the file i.e host1-80 10
host2-80 15 according the space .
how is %o association being formed in this case i believe that is what you are refering by saying the $o{$h} . After the condition is true you are trying to substract $n[0]..that would be 10 or 15 and you are refering the $o{$h} is the same value if the association is being formed from spilt.
Thanks for your help in advance
Abdul khadeer_1
Occasional Advisor

Re: understanding the perl one line

thanks for explaining the how the if works .but that raised one more question i will post in a new thread