All pastes #996349 Raw Edit

KIA / Mini-Tutorial - $v1 $v1

public text v1 · immutable
#996349 ·published 2008-04-24 21:40 UTC
rendered paste body
alias iftest {
  ;Next line echos "hello world" because the left and right sides
  ;of the if statement are assigned to $v1 and $v2, respectively
  if (hello != world) echo -a $v1 $v2

  ;Next few lines echo "1", "hello pie" and "hello cheesecake"
  ;This shows three things: 
  ;First how $v1 and $v2 are replaced even when there is only one
  ;Side of the comparison
  ;Second, that $v1 and $v2 work with $iif
  ;Third, it shows that they keep their value after the $iif, and
  ;do not go revert to the previous if statement they are in.
  if (1) {
    echo -a $v1 $v2
    echo -a hello $iif(cheesecake > pie,$v1,$v2)
    echo -a hello $v1
  }

  ;$v1 and $v2 also work with while loops.  This echoes "1"
  var %i = 1
  while (%i < 2) {
    echo -a $v1
    inc %i
  }

  ;This single line emphasizes a couple points and shows a couple more: 
  ;First that they keep their values outside of the comparison itself.
  ;Second that $v1 and $v2 can be used inside if statements themselves.
  ;Third that $ifmatch is the same exact thing as $v1
  ;Fourth that they are still assigned, even when conditional fails:
  ;$v1 uses the failed attempt in the while loop when %i increments to 2
  ;And lastly, that $v2 is assigned to $null when there is only one side to it
  echo -a and that concludes the test of v1 and v $+ $iif($v1,$ifmatch) $v2
}