Compara tablas y agrega campo

8 08UTC Diciembre, 2009

Compara dos tablas delimitadas por tabuladores por un campo numerico ordenado, imprime registros de la tabla 1 coincidentes con la tabla 2 y agrega un campo suplementario. Evalua que las lineas no estén vacías, si no se interrumpe el procesamiento. Requiere val() ya que en la cabecera el rótulo del campo índice es una cadena y la evaluación no daría el resultado esperado.


const sArch1 = "archivo1.txt"  ' tabla 1
const sArch2 = "archivo2.txt"  ' tabla 2
const nIX    = 0               ' campo indice
const nCampo = 2               ' campo suplementario (tabla 2)

const ht = chr(9) : nCount = 0 : nMatch = 0
tload sArch1, aArch1
for i in aArch1
  if (len(i) > 0) then
  split i, ht, aLinea1()
  nIndice1 = val(aLinea1(nIX))
  tload sArch2, aArch2
  for j in aArch2
    if (len(j) > 0) then
    split j, ht, aLinea2()
    nIndice2 = val(aLinea2(nIX)) : nCount += 1
      if ( nIndice1 == nIndice2 ) then
        print i, aLinea2(nCampo) : nMatch += 1
        goto leave_for_j
      elif ( nIndice1 < nIndice2 ) then
        goto leave_for_j
      fi
    fi
  next
  label leave_for_j:
  fi
next

'print "Iterations: " + nCount, "Matches: " + nMatch

Compara tablas ordenadas y agrega campo

8 08UTC Diciembre, 2009

Similar a la entrada previa, pero disminuye las repeticiones.


const ht = chr(9)
const sArch1="archivo1.txt"
const sArch2="archivo2.txt"
nCount = 0 : nMatch = 0

open sArch1 for input as #1
repeat
  linput #1, sLinea1 : split sLinea1, ht, aLinea1
  open sArch2 for input as #2
  repeat
    linput #2, sLinea2 : split sLinea2, ht, aLinea2 : nCount += 1
    if ( val(aLinea1(0)) == val(aLinea2(0))) then
      print sLinea1, aLinea2(2) : nMatch += 1
      goto leave_repeat2
    elif ( val(aLinea1(0)) < val(aLinea2(0)) ) then
      goto leave_repeat2
    fi
  until eof(2)==1
  label leave_repeat2:
  close #2
until eof(1) == 1
close #1

'print "Iterations: " + nCount, "Matches: " + nMatch

Compara listas – usa TLOAD

8 08UTC Diciembre, 2009

Compara dos listas leidas con tload. Requiere val() para traducir la cadena “n” al número n.


' Define constantes con caracteres especiales
const ht = chr(9) : const lf = chr(10) : const cr = chr(13)

' Define constantes con nombres de archivos
const sArch1 = "archivo1.txt"
const sArch2 = "archivo2.txt"

' Inicializa contadores
let nCount = 0 : let nMatch = 0

' Procesa el resto
tload sArch1, aArch1
for i in aArch1
  tload sArch2, aArch2
  for j in aArch2
    nCount += 1
    if (val(i) == val(j)) then
      print i," equals ",j : nMatch += 1
      goto leave_for_j
    elif (val(i) < val(j)) then
      goto leave_for_j
    fi
  next
  label leave_for_j:
next

print "Iterations: " + nCount, "Matches: " + nMatch

Compara tablas por índice y agrega campo

8 08UTC Diciembre, 2009

Compara dos tablas de texto delimitado por tabuladores por un campo índice, y agrega a la tabla 1 un campo de la tabla 2. Prestarle atención al manejo de arreglos con split y aArreglo(n)


const ht = chr(9)

open "archivo1.txt" for input as #1
repeat
  linput #1, sLinea1 : split sLinea1, ht, aLinea1
  open "archivo2.txt" for input as #2
  repeat
    linput #2, sLinea2 : split sLinea2, ht, aLinea2
    if (aLinea1(0) == aLinea2(0)) then
      print sLinea1, aLinea2(2)
    fi
  until eof(2)==1
  close #2
until eof(1) == 1
close #1

Lee archivos con REPEAT/UNTIL anidado

8 08UTC Diciembre, 2009

Prestarle atención dónde se usan open y close


open "archivo1.txt" for input as #1
repeat
  linput #1, sLinea1
  open "archivo2.txt" for input as #2
  repeat
    linput #2, sLinea2
    print sLinea1, sLinea2
  until eof(2)==1
  close #2
until eof(1) == 1
close #1

Compara listas ordenadas

6 06UTC Diciembre, 2009

Compara elementos de dos listas ordenadas e imprime los elementos iguales. Sale de “for j” con goto. Si los números a comparar provienen de tload hay que usar val() para que devuelva el número n de la cadena “n”.


let nCount = 0 : let nMatch = 0

for i=1 to 30 step 2
  for j=1 to 50
    nCount += 1
    if ( i == j ) then
      nMatch += 1
      print i," equals ",j
      goto leave_for_j
    elif ( i < j ) then
      goto leave_for_j
    fi
  next
  label leave_for_j:
next

print "Iterations: " + nCount, "Matches: " + nMatch

Compara tablas por indice

5 05UTC Diciembre, 2009

'print "////// INICIO //////"
' Define constantes para characteres especiales
const ht = chr(9)
const nl = chr(10)
'const vt = chr(11)
'const cr = chr(13)

' Usa "cuenta" para verificar luego interaciones de los bucles
let cuenta = 0

' Define constantes con nombres de archivos
const archivo1 = "D:\Temporal\smallbasic\archivo1.txt"
const archivo2 = "D:\Temporal\smallbasic\archivo2.txt"

' Procesa el resto
open archivo1 as #1 : tload #1, aArch1

for i=0 to ubound(aArch1)
  split aArch1(i),ht,aOtro1()   ' Divide elemento de un arreglo en otro
  open archivo2 as #2 : tload #2, aArch2
  for j=0 to ubound(aArch2)
    cuenta += 1
    split aArch2(j),ht,aOtro2() ' Divide elemento de un arreglo en otro
    if aOtro1(0) = aOtro2(0) then
      print aArch1(i),aOtro2(2)
    endif
  next
  close #2
next

close #1

'print "/////// FIN ////////"
print cuenta

Execute

5 05UTC Diciembre, 2009

' execute requiere cmd /c en Windows
' comparar con system

sDir = execute("cmd /c dir /b",-1,PID)

ScriptBasic: lee una tabla

29 29UTC Noviembre, 2009

' define constantes para tabulación y para nueva línea
const HT = "\t"
const NL = "\n"

' open abre un archivo en un puntero
' datos.txt es un ejemplo de texto con tabuladores

open "datos.txt" for input as #1

' repeat ... until me permite barrer cada linea de un archivo
' hasta que alcanza el final del archivo
' line input asigna la linea a una variable a$
' splita asigna una cadena a un vector cuyo primer elemento
' se inicializa a 0. Comparar con split
' Imprime el 1er y 3er elemento de cada registro

repeat
    line input #1, a$
    splita a$ by HT to arrA
    print arrA[0],HT,arrA[2],NL
until eof(#1) = -1

' close cierra el archivo
close #1
' ---------------------------------------------------------------

Memento – Online Bibliography Management

8 08UTC Octubre, 2007

If you manage an important amount of bibliographic references Memento is a very interesting alternative among other online tools. Some of its features are

  • Add articles from URLs —Pubmed, Nature, etc— from files —BibTeX, RIS and more— and manually
  • Organize your collection by Author and by Keywords
  • Write reports linking selected articles
  • Make watchlists
  • Export to BibTeX and MS Word 2007 XML
  • Share, Add to Cart, Add to Reports, Delete and Comment entries
  • Sort articles by Title, Year, etc
  • Explore graphically articles, tags and authors
  • Edit article fields
  • Search article in Citeseer, HighWire and more
  • Last but not least, a competent and responsive developer supporting it. Thank you Ritesh Agrawal!

For those who do academic work —like me— it does definitively deserve a try. You can know more visiting Memento

Enjoy it!