Transfer content of flat file to an array

Discussion in 'Ruby' started by Surjit Nameirakpam, Nov 14, 2007.

  1. Scenario ..

    I have a text file

    23
    24
    56
    78
    90
    89

    I want to transfer this content into an array,
    numArr=[23,24,56,78,90,89]

    How to do it
    --
    Posted via http://www.ruby-forum.com/.
    Surjit Nameirakpam, Nov 14, 2007
    #1
    1. Advertising

  2. Surjit Nameirakpam

    Alex Young Guest

    Surjit Nameirakpam wrote:
    > Scenario ..
    >
    > I have a text file
    >
    > 23
    > 24
    > 56
    > 78
    > 90
    > 89
    >
    > I want to transfer this content into an array,
    > numArr=[23,24,56,78,90,89]
    >
    > How to do it

    File.readlines, map, to_i.

    --
    Alex
    Alex Young, Nov 14, 2007
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    > I have a text file
    > 23
    > 24
    > 56
    > 78
    > 90
    > 89
    > I want to transfer this content into an array,
    > numArr=[23,24,56,78,90,89]


    >> File.readlines("surjit.txt").map {|item| item.chomp}

    => ["23", "24", "56", "78", "90", "89"]
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2.2 (GNU/Linux)

    iD8DBQFHO1iTb6UiZnhJiLsRApK1AKCNW3uah81gxjhPRfbycRpidF9ORwCeMY6z
    Z8ZmbQqMMG2vUAfkRSzvDb0=
    =pKJi
    -----END PGP SIGNATURE-----
    Eustáquio 'TaQ' Rangel, Nov 14, 2007
    #3
  4. Surjit Nameirakpam

    Phrogz Guest

    Re: Transfer content of flat file to an array

    On Nov 14, 1:15 pm, Surjit Nameirakpam <>
    wrote:
    > I have a text file
    >
    > 23
    > 24
    > 56
    > 78
    > 90
    > 89
    >
    > I want to transfer this content into an array,
    > numArr=[23,24,56,78,90,89]


    IO.read( 'myfile.txt' ).scan( /\d+/ ).map{ |s| s.to_i }

    ....assuming you don't have any floating point numbers in there.
    Phrogz, Nov 14, 2007
    #4
  5. On Nov 14, 2007, at 3:15 PM, Surjit Nameirakpam wrote:

    > Scenario ..
    >
    > I have a text file
    >
    > 23
    > 24
    > 56
    > 78
    > 90
    > 89
    >
    > I want to transfer this content into an array,
    > numArr=[23,24,56,78,90,89]
    >
    > How to do it


    ruby -e 'p File.readlines("/tmp/numbers.txt").map{|line| line.to_i}'

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Nov 14, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hazz
    Replies:
    6
    Views:
    49,570
    SkyUCHC
    Jun 9, 2010
  2. Alex
    Replies:
    2
    Views:
    993
    John Hunter
    Jul 22, 2003
  3. Delaney, Timothy C (Timothy)

    RE: Flat file to associative array.

    Delaney, Timothy C (Timothy), Jul 22, 2003, in forum: Python
    Replies:
    0
    Views:
    359
    Delaney, Timothy C (Timothy)
    Jul 22, 2003
  4. Peter Steiner
    Replies:
    2
    Views:
    454
    Peter Steiner
    Nov 3, 2005
  5. blnukem
    Replies:
    7
    Views:
    117
    Joe Smith
    Sep 6, 2004
Loading...

Share This Page