> From: Ian Clark > > Here's my take on a currency conversion table from Björn's URL. > ... > > Who can see how to improve on my code?
Here is a different approach trying to reuse existing code from the base library or JAL where possible: NB.================================================================ NB. Script require 'xml/sax/x2j web/gethttp tables/dsv pack' NB. Source of exchange rate data... URL=: 'http://www.ecb.int/stats/exchange/eurofxref/html/index.en.html' x2jclass 'forex' 'Items' x2jDefn / := table : table=: ,:'Code';'Currency';'Value' tr := table=: table, empty^:(0 = #) row : row=: '' td := row=: row, (''"_)^:( ('aa';'ab') -.@:e.~ <@(atr bind 'headers')) <y td/span := row=: row, (''"_)^:('rate' -...@-: atr bind 'class') <y ) NB.================================================================= NB.================================================================= NB. Example session CurrencyTable=: makenum process_forex_ gethttp URL 'CHF' pget CurrencyTable 1.4144 ('GBP';'CHF') psel CurrencyTable +---+---------------+-------+ |GBP|Pound sterling |0.86805| +---+---------------+-------+ |CHF|Swiss franc |1.4144 | +---+---------------+-------+ NB.================================================================= NB.================================================================= NB. The following is the explicit equivalent of the tacit code in NB. script above x2jclass 'forex' '/' x2jElm (3 : 0) table : table=: ,:'Code';'Currency';'Value' ) 'tr' x2jElm (3 : 0) if. #row do. table=: table,row end. : row=: '' ) 'td' x2jChar (3 : 0) if. (<atr 'headers') e. 'aa';'ab' do. row=: row,<y end. ) 'td/span' x2jChar (3 : 0) if. 'rate' -: atr 'class' do. row=: row,<y end. ) NB.================================================================= ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
