Small Currency Converter

By -XperTeeZ- on Mar 26, 2010

So, this was the code made for code competition, the task was to code the smallest currency convertor possible. I won the competition, with the following code:

(Don't look the design, the functionality matters)

dialog c {
  size -1 -1 60 80
  option dbu
  edit "", 1, 5 12 17 10
  edit "", 3, 33 12 17 10
  text "From:", 2, 6 2 25 8
  text "To:", 4, 34 2 25 8
  button "Convert", 5, 9 41 37 12
  edit "", 6, 20 30 17 10, autohs
  text "Amount:", 7, 19 22 25 8
  edit "", 8, 0 59 60 10, autohs center
}
alias g return $gettok(%x,$1,32)
on *:DIALOG:c:sclick:5:{
  sockclose c
  sockopen c www.xe.com 80
  %x = $did(c,1) $did(c,3) $did(c,6)
}
on *:SOCKOPEN:c:{
  sockwrite -n c GET $+(/ucc/convert.cgi?Amount=,$g(3),&From=,$g(1),&To=,$g(2)) HTTP/1.1
  sockwrite -n c Host: www.xe.com
  sockwrite -n c $crlf
}
on *:SOCKREAD:c:{
  sockread %tmp
  if !%z {
    if proh isin %tmp && $g(2) isin %tmp {
      did -ra c 8 $g(3) $g(1) = $left($gettok(%tmp,3,62),5) $g(2)
      set -u3 %z 1
    }
  }
}

Comments

Sign in to comment.
Outkax   -  Jul 25, 2014

The script doesn't work, how make it worked?

 Respond  
-XperTeeZ-   -  Mar 27, 2010

So you meant to post this to show the success of your work with the shortest bytes possible in this script?
Yes, that's exactly what I went for.

 Respond  
Jethro   -  Mar 27, 2010

So you meant to post this to show the success of your work with the shortest bytes possible in this script?

I mean, when you post a snippet, that's beside the point. You should try making it perfect in any way regardless of its size, along with an instruction on how to operate it.

That's the purpose of a successful script.

P.S. Don't steer me wrong, -Xper. You did a good job, but the script itself didn't seem "efficient" at its current state.

 Respond  
-XperTeeZ-   -  Mar 27, 2010

This wasn't about functionality, it was about 1 time script run, and efficiency of that 1 time run :)

Hah, I just can't stop reading this xD xD xD God, how stupid can I be... Anyway. I meant to say that the purpose was to create shortest code possible, that will convert currencies successfully :) Thank you for your comments.

 Respond  
WorldDMT   -  Mar 26, 2010

good job

but i think is better to put "read" to the ID 8

and like napa's said about money index u can replace if u want the edit ID 1 and ID 3 by a combo "drop" and add in init /didtok c 1,3 46 USD AUD CAD ... THB ZAR then no errors

about %z is unless and dont complicate the task you can get the result from title like this

on *:sockread:c:{
  var %tmp | sockread %tmp
  if $left(%tmp,7) == <title> {
    tokenize 58 %tmp
    did -ra c 8 $left($3,-8)
    sockclose c
    unset %x
  }
}

e.g: the value of 1 USD EUR return 1.00 USD = 0.745740 EUR

the code can be like this if something help u as u like

alias currency dialog $iif($dialog(c),-v,-m c) c
dialog c {
  size -1 -1 100 55
  option dbu
  combo 1, 3 10 26 20, drop
  combo 3, 30 10 26 20, drop
  text From:, 2, 6 2 25 8
  text To:, 4, 34 2 25 8
  button Convert, 5, 3 22 90 12
  edit , 6, 60 10 32 10, autohs
  text Amount:, 7, 60 2 25 8
  text , 8, 2 36 96 40, center
}
on *:dialog:c:*:*:{
  if ($devent == init) didtok c 1,3 32 USD AUD CAD CHF CNY DKK EUR GBP HKD HUF INR JPY MXN MYR NOK NZD RUB SEK SGD THB ZAR
  elseif $devent == sclick && $did == 5 {
    if ($sock(c)) sockclose c
    sockopen c www.xe.com 80
    sockmark c $did(1) $did(3) $did(6) www.xe.com
  }
}
on *:sockopen:c:{
  tokenize 32 $sock(c).mark
  sockwrite -n c GET $+(/ucc/convert.cgi?Amount=,$3,&From=,$1,&To=,$2) HTTP/1.1
  sockwrite -n c Host: $4
  sockwrite -n c $crlf
}
on *:sockread:c:{
  var %tmp | sockread %tmp
  if $left(%tmp,7) == <title> {
    tokenize 58 %tmp
    did -ra c 8 $left($3,-8)
    sockclose c
    unset %x
  }
}

use /currency to open dialog

 Respond  
Jethro   -  Mar 26, 2010

Your statement is contradictory to what you've claimed in your introduction:> (Don't look the design, the functionality matters)> This wasn't about functionality, it was about 1 time script run, and efficiency of that 1 time run :)Still, one of the efficiencies is to at least clean up the vars the script leaves behind.

 Respond  
-XperTeeZ-   -  Mar 26, 2010

This wasn't about functionality, it was about 1 time script run, and efficiency of that 1 time run :)

 Respond  
napa182   -  Mar 26, 2010

also maybe some error checks along with the USD AUD CAD CHF CNY DKK EUR GBP HKD HUF INR JPY MXN MYR NOK NZD RUB SEK SGD THB ZAR index.

 Respond  
Jethro   -  Mar 26, 2010

Perhaps you need to provide a brief instruction. Because some people who are new to MSL may not know how to use this script. Also, you can combines these two if statements:> if !%z {
if proh isin %tmp && $g(2) isin %tmp {to one line:

if !%z && proh isin %tmp && $g(2) isin %tmp {

, which will make your script one step smaller.

P.S. I think you should include a sockclose event to unset a couple of vars:

on *:SOCKCLOSE:c:unset %x %tmp
 Respond  
napa182   -  Mar 26, 2010

well making it the smallest you can tends to make it lose it's efficiency imo.

 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.