snippet packet

By _Dean_ on Feb 07, 2011

This a snippet packet containing 4 snippets

• $dices

echo -a $dices(C,N)
where C is the number of combinations, and N is the random number 1 to N

like

echo -a $dices(5,10)
will choose 5 random numbers between 1 to 10

• $char_it

echo -a $char_it(text)
the properties for $char_it is en,de

convert all text into characters number

like

//echo -a $char_it(test).en

will return

116 101 115 116

//echo -a $char_it(116 101 115 116).de

will return test

• /type_back

/type_back text

will write all text in backwards

/type_back test

will return tset

• /convert
/convert C | ?N

convert all characters into Octal, Decimal, Hexadecimal and Binary

/convert +
will return

Converted values for + is Octal: 53 | Decimal: 43 | Binary: 101011 | Hexadecimal: 2B

if you want, you can choose character numbers, like, the value for space character is 32
you need to put a ? before the character number

/convert ?32

Converted values for Character 32 is Octal: 40 | Decimal: 32 ( ) | Binary: 100000 | Hexadecimal: 20

;; The original idea for the $dices was not mine, but i realized it (original idea, made by request)

alias dices {
  var %x = $$1, %y = $$2, %z
  %z = $regsubex($str($chr(44),$calc(%x - 1)),//g,$r(1,%y)) 
  return %z
}

;; $char_it

alias char_it {
  var %x = $1-, %y
  %y = $regsubex($str($chr(32),$iif($prop == en,$mid(%x,1,\n),$gettok(%x,1,32))),//g,$iif($prop == en,$asc($mid(%x,\n,1)),$iif($chr($gettok(%x,\n,32)) == $chr(32),$replace($chr($gettok(%x,\n,32)), $chr(32), $chr(0160)),$chr($gettok(%x,\n,32)))))
  return $iif($prop == de,$remove(%y,$chr(32)),%y)
}

;; /type_back 

alias type_back {
  var %x = $1-
  echo -a $regsubex($str(.,$mid(%x,1,\n)),/./g,$mid(%x,$+(-,\n),1)) 
}

;; /convert 

alias convert {
  if ($$1 isnum 0-9) || ($$1 !isnum) { 
    var %x = $iif($regex($$1,/\?(\d+)/), $regml(1), $asc($left($$1,1)))
    echo -a Converted values for $iif($regml(1), Character) $iif($regml(1), $+(,%x,), $(,$left($$1,1),)) is Octal: $base(%x,10,8) $chr(124) Decimal: $iif($regml(1), %x $+($chr(40),$chr(%x),$chr(41)), %x)  $chr(124) Binary: $base(%x,10,2) $chr(124) Hexadecimal: $base(%x,10,16)
  }
  else { echo -a 4ERROR: cannot find the value for $$1 }
}

Comments

Sign in to comment.
sunslayer   -  Feb 08, 2011

touché jaytea

 Respond  
jaytea   -  Feb 08, 2011

sunslayer, that is not necessarily true within aliases called as an identifier eg. $dices( , 10).

also, $$identifier isn't always suitable for error detection, and certainly not in a couple of these snippets. utility snippets such as $dices() are often used as part of larger scripts, called from other aliases, events etc. $$identifier, if $null, performs an implicit /halt (without additional implicit /haltdef) which can be very undesirable if the calling routine expects and requires that control returns to it.

so, try to think about what each part of your code is meant for, and decide if it's actually appropriate given its intended purpose.

 Respond  
Jethro   -  Feb 08, 2011

Certainly sunslayer, but the way you put it, it was as if you were talking about the $1 not $2. :P Yes, only the first identifier is required as per your demonstration. ^^

 Respond  
sunslayer   -  Feb 08, 2011

Jethro_, if you use $$2 then there is no need for $$1 because if $2 then there must be $1.

if ($$1 isnum 0-9) || ($$1 !isnum) {

can be rewritten as

if ($$1 isnum 0-9 || $1 !isnum) {

because if !$1 then it would have been halted by $v1

 Respond  
Jethro   -  Feb 07, 2011

Thing thing is, sunslayer, if only one $ is used, the alias may result an error of insufficient parameter if the $1 is not filled with a value. For the first two aliases, one $ is enough since they're called as an identifier, but not for the last two.

 Respond  
sunslayer   -  Feb 07, 2011

you only need one $$N per alias

 Respond  
Jethro   -  Feb 07, 2011

I don't know why you have to go the extra mile for something that can be done shorter with the same outcome. For the type_back alias, you can do it as such:

alias type_back {
  echo -a $regsubex($1-,/(.)/g,$mid(\A,-\n,1)))
}

The rest of aliases can be be rid of some redundancies here and there, but I'm not gonna get into that because you might think I'm trying to pick on your code. Please understand that this is an innocent comment I'm making here, not trying to be critical of your coding style or anything.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.