Professional Web Applications Themes

prepend file line number to array - Ruby

using ruby, i am trying to read a file of a couple hundred lines into an array and prepend (if there is such a word) the line number (starting at 1) to the beginning of each line. a simple example from the file follows : 01/02/2004 bought 100 widgets 19.95 company_1 01/02/2004 sold 5 widgets 22.95 company_2 01/02/2004 bought 50 widgets 19.95 company_1 what i would like to end up with is : 1 01/02/2004 bought 100 widgets 19.95 company_1 2 01/02/2004 sold 5 widgets 22.95 company_2 3 01/02/2004 bought 50 widgets 19.95 company_1 is there an array method that ...

  1. #1

    Default prepend file line number to array

    using ruby, i am trying to read a file of a couple hundred lines into an
    array and prepend (if there is such a word) the line number (starting at 1)
    to the beginning of each line.

    a simple example from the file follows :
    01/02/2004 bought 100 widgets 19.95 company_1
    01/02/2004 sold 5 widgets 22.95 company_2
    01/02/2004 bought 50 widgets 19.95 company_1

    what i would like to end up with is :

    1 01/02/2004 bought 100 widgets 19.95 company_1
    2 01/02/2004 sold 5 widgets 22.95 company_2
    3 01/02/2004 bought 50 widgets 19.95 company_1


    is there an array method that i can use on each element to prepend the line
    number after reading the file into an array using IO.readlines?

    thanks

    joe




    Joseph Guest

  2. #2

    Default Re: prepend file line number to array

    How about

    ary = []
    file.each_line {|line| ary << "#{file.lineno} #{line}" }

    /kent

    Joseph Paish wrote: 
    Kent Guest

  3. #3

    Default Re: prepend file line number to array


    "Joseph Paish" <edmonton.ab.ca> schrieb im Newsbeitrag
    news:localdomain... 
    1) 
    line 

    Either use "cat -n" or insert the number during writing of each line -
    this is more efficient:

    while ( line = ARGF.gets )
    printf "%5d %s", ARGF.lineno, line
    end

    If you need to keep the lines in the array something like this might do

    arr.each_with_index do |line, index|
    print index, " ", line, "\n"
    end

    Regards

    robert

    Robert Guest

  4. #4

    Default Re: prepend file line number to array

    On Saturday, February 7, 2004, 1:51:56 AM, Joseph wrote:
     
     
     

     


    Sure there is!

    require 'extensions/all' # extensions.rubyforge.org
    lines = lines.map_with_index { |line, i| "#{i+1} #{line}" }

    Although this may be more efficient:

    require 'extensions/all'
    File.open(path) do |f|
    lines = f.map_with_index { |line, i| "#{i+1} #{line}" }
    end

    Cheers,
    Gavin




    Gavin Guest

Similar Threads

  1. [PHP-DEV] [PATCH] Prepend and append strings for zend_highlight
    By Evan Nemerson in forum PHP Development
    Replies: 0
    Last Post: October 29th, 07:00 PM
  2. prepend file to all scripts...
    By Jonathan in forum PHP Development
    Replies: 3
    Last Post: October 24th, 09:57 PM
  3. Error line number
    By Sune Hansen in forum ASP.NET General
    Replies: 2
    Last Post: July 1st, 09:23 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139