Podes leer el archivo y usar el método Split de la clase String para cada
linea.

Doc:


 str.split(pattern=$;, [limit]) => anArray
 <http://www.ruby-doc.org/core/classes/String.src/M000818.html>

Divides *str* into substrings based on a delimiter, returning an array of
these substrings.

If *pattern* is a String <http://www.ruby-doc.org/core/classes/String.html>,
then its contents are used as the delimiter when splitting *str*. If *
pattern* is a single space, *str* is
split<http://www.ruby-doc.org/core/classes/String.html#M000818>on
whitespace, with leading whitespace and runs of contiguous whitespace
characters ignored.

If *pattern* is a Regexp <http://www.ruby-doc.org/core/classes/Regexp.html>,
*str* is divided where the pattern matches. Whenever the pattern matches a
zero-length <http://www.ruby-doc.org/core/classes/String.html#M000789>string,
*str* is split <http://www.ruby-doc.org/core/classes/String.html#M000818>into
individual characters.

If *pattern* is omitted, the value of $; is used. If $; is nil (which is the
default), *str* is
split<http://www.ruby-doc.org/core/classes/String.html#M000818>on
whitespace as if ` ' were specified.

If the *limit* parameter is omitted, trailing null fields are suppressed. If
*limit* is a positive number, at most that number of fields will be returned
(if *limit* is 1, the entire string is returned as the only entry in an
array). If negative, there is no limit to the number of fields returned, and
trailing null fields are not suppressed.

   " now's  the time".split        #=> ["now's", "the", "time"]
   " now's  the time".split(' ')   #=> ["now's", "the", "time"]
   " now's  the time".split(/ /)   #=> ["", "now's", "", "the", "time"]
   "1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
   "hello".split(//)               #=> ["h", "e", "l", "l", "o"]
   "hello".split(//, 3)            #=> ["h", "e", "llo"]
   "hi mom".split(%r{\s*})         #=> ["h", "i", "m", "o", "m"]

   "mellow yellow".split("ello")   #=> ["m", "w y", "w"]
   "1,2,,3,4,,".split(',')         #=> ["1", "2", "", "3", "4"]
   "1,2,,3,4,,".split(',', 4)      #=> ["1", "2", "", "3,4,,"]
   "1,2,,3,4,,".split(',', -4)     #=> ["1", "2", "", "3", "4", "", ""]







On 10/31/07, Damian Janowski <[EMAIL PROTECTED]> wrote:
>
> On 10/30/07, Martin Aceto <[EMAIL PROTECTED]> wrote:
> > Hola gente, hace poco me inscribí en la lista, ya que comencé a
> desarrollar
> > en RoR, y ahora se me planteo una inquietud.
> >
> > Necesito importar un archivo .txt que esta separado por tab del tipo de
> >
> > Pos.    PEC    N°    Nombre
> > 1    1    56    Nahuel Tromtti
> > 2    2    4    Amado So
> > 3    3    56    Lucas Cno
> > 4    4    23    Patricio Rs
> > 5    5    9    Carlos Aldo
> > 6    6    6    Maximiliano Sa
> >
> > y conseguir algo como ['1','1','19', 'Nahuel Trombetti'] o mejor aun
> algo
> > como ['Pos.' => '1','PEC' => '1', 'N'  => '19', 'nombre' => 'Nahuel
> > Tromtti'].
> >
> > Alguien me podría tirar alguna idea de como encararlo
>
> Hola Martín,
>
> Probablemente te sirva FasterCSV para parsear este tipo de archivos.
>
> http://fastercsv.rubyforge.org
>
> Ejemplos: http://fastercsv.rubyforge.org/classes/FasterCSV.html
> _______________________________________________
> Ruby mailing list
> [email protected]
> http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar
>



-- 
Martín Sagastume http://msagastume.blogspot.com
098 66.82.06
094 57.58.46
Montevideo - Uruguay
_______________________________________________
Ruby mailing list
[email protected]
http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar

Responder a