How to delete array element and add to previous element

Discussion in 'Ruby' started by Al Cholic, Jul 27, 2007.

  1. Al Cholic

    Al Cholic Guest

    Hello,

    I have an array that has elements that are arrays.

    It looks like this(I'll call it array), it has:
    [
    [POS1, POS2a\, POS2b, POS3, POS4], # this array is in position
    array[0]
    [POS2c\, POS2d\, POS2e], # this array is in position
    array[1]
    [POS2f\, POS2g\, POS2h], # this array is in position
    array[2]
    [POS1, POS2a\, POS2b, POS3, POS4] # this array is in position
    array[3]

    ]

    Notice that Im trying to escape the commas with the backslash in the
    array. Is this the proper ways to escape commas in arrays? The POS2's
    are separated with commas(not to confuse them with the commas that
    separate the array elements).

    So, what Im trying to do is have arrays array[1] and array[2] added to
    position array[0][1] and then have array[1] and array [2] deleted and
    the have the next element from array moved downward in into the position
    where array[1] and array[2] use to be.

    So the final array should look like this:
    [
    [POS1, POS2a\, POS2b\, POS2c\, POS2d\, POS2f\, POS2g\, POS2h\, POS2e,
    POS3, POS4],
    [POS1, POS2a\, POS2b, POS3, POS4]
    ]

    Does it make sense to store all of this information inside arrays or
    would hashes be better? Im shuffling this data around to have ir ready
    to be placed inside of a database. Is this the best setup for that?

    This is the first time I've had to move arrays around like this and it
    got me really confused. Also, should I split the POS2a\, POS2b\, POS2c\
    up into another array and have a three leveled array?

    Could someone please write a preliminary loop for this type of operation
    to get me on the right track or give advice on what I should be doing.

    Thanks in advance.
    --
    Posted via http://www.ruby-forum.com/.
     
    Al Cholic, Jul 27, 2007
    #1
    1. Advertising

  2. Al Cholic

    Guest

    Hi --

    On Fri, 27 Jul 2007, Al Cholic wrote:

    > Hello,
    >
    > I have an array that has elements that are arrays.
    >
    > It looks like this(I'll call it array), it has:
    > [
    > [POS1, POS2a\, POS2b, POS3, POS4], # this array is in position
    > array[0]
    > [POS2c\, POS2d\, POS2e], # this array is in position
    > array[1]
    > [POS2f\, POS2g\, POS2h], # this array is in position
    > array[2]
    > [POS1, POS2a\, POS2b, POS3, POS4] # this array is in position
    > array[3]
    >
    > ]
    >
    > Notice that Im trying to escape the commas with the backslash in the
    > array. Is this the proper ways to escape commas in arrays? The POS2's
    > are separated with commas(not to confuse them with the commas that
    > separate the array elements).


    I'm afraid I don't know what you mean. What exactly are you trying to
    do with those commas?


    David

    --
    * Books:
    RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
    RUBY FOR RAILS (http://www.manning.com/black)
    * Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
     
    , Jul 27, 2007
    #2
    1. Advertising

  3. Al Cholic

    Al Cholic Guest

    unknown wrote:
    > Hi --
    >
    > On Fri, 27 Jul 2007, Al Cholic wrote:
    >
    >> [POS2f\, POS2g\, POS2h], # this array is in position
    >> array[2]
    >> [POS1, POS2a\, POS2b, POS3, POS4] # this array is in position
    >> array[3]
    >>
    >> ]
    >>
    >> Notice that Im trying to escape the commas with the backslash in the
    >> array. Is this the proper ways to escape commas in arrays? The POS2's
    >> are separated with commas(not to confuse them with the commas that
    >> separate the array elements).

    >
    > I'm afraid I don't know what you mean. What exactly are you trying to
    > do with those commas?
    >
    >
    > David


    Dont worry about the commas. they could just as well be spaces. Its
    just he way Im reasing in a file, in the file they are just separated by
    commas.
    --
    Posted via http://www.ruby-forum.com/.
     
    Al Cholic, Jul 27, 2007
    #3
  4. On Jul 26, 2007, at 8:15 PM, Al Cholic wrote:

    > I have an array that has elements that are arrays.
    >
    > It looks like this(I'll call it array), it has:
    > [
    > [POS1, POS2a\, POS2b, POS3, POS4], # this array is in
    > position array[0]
    > [POS2c\, POS2d\, POS2e], # this array is in
    > position array[1]
    > [POS2f\, POS2g\, POS2h], # this array is in
    > position array[2]
    > [POS1, POS2a\, POS2b, POS3, POS4] # this array is in
    > position array[3]
    > ]
    >
    > Notice that Im trying to escape the commas with the backslash in the
    > array. Is this the proper ways to escape commas in arrays? The
    > POS2's
    > are separated with commas(not to confuse them with the commas that
    > separate the array elements).


    If you are showing here is intended to be Ruby code, it is simply bad
    syntax. AFAIK, backslashes have no escape function outside of strings
    [*] and regular expressions. Also, as written, those POS thingies are
    going to be treated as identifiers naming references to constant
    objects. Is that what you intended? Then your sub-arrays should be
    written something like

    [POS1, [POS2a, POS2b], POS3, POS4]
    [nil, [POS2c, POS2d, POS2e], nil, nil]

    and so forth.

    On the hand, if what you are showing is intended to represent data
    stored as text in a file, I think you would be better off with a
    different data format. Look at YAML or CSV. There are libraries for
    handling those well-established formats.

    Regards, Morton

    [*] I'm lumping back-tick expressions with strings here.
     
    Morton Goldberg, Jul 27, 2007
    #4
  5. Al Cholic

    Al Cholic Guest


    > [POS1, [POS2a, POS2b], POS3, POS4]
    > [nil, [POS2c, POS2d, POS2e], nil, nil]
    >


    Sorry about the syntax. I was just trying to show the array, it was not
    intended to be ruby code. What Im trying to do is read a text file and
    then make sense of the data in that text file and store that info in a
    database. I have managed to sort out into the arrays but need to get
    the POS2c etc elements into the same array (im guessing thats the best
    way to do it).

    For example here is code that im shuffling:

    49 7 RP13,RP15,RP17,RP19,RP24, 12X2 33XD Wireless, Independent, 5%
    235003410229 Toshiba 7024022000
    RP32,RP33
    50 4 RP27,RP28,RP30,RP31 10XC4 3X567 Network, Isolated, 5%
    2353454310103 Philips 7024010300
    51 31 R1,R2,R8,R30,R32,R33,R35, 0603,R10K,1% 0603 3%,1/12W
    S1345310KFJ2 Bonshui 7563010300
    R37,R39,R41,R42,R43,R49,
    R50,R51,R52,R58,R68,R69,
    R71,R72,R74,R85,R95,R117,
    R129,R130,R155,R156,R158,
    R160

    What I've done is split the text up into lines stored all the lines in
    an array with text.strip.split("\n") But as you can see the R71, R129,
    the elements in the third column continue on the next line. I need a
    way to get them into one place. Because those elements that are on the
    next line are a continuation of the third column element.

    --
    Posted via http://www.ruby-forum.com/.
     
    Al Cholic, Jul 27, 2007
    #5
  6. Al Cholic

    Al Cholic Guest

    I reposting my last response since there is no edit on this forum.

    Sorry about the syntax. I was just trying to show the array, it was not
    intended to be ruby code. What Im trying to do is read a text file and
    then make sense of the data in that text file and store that info in a
    database. I have managed to sort out into the arrays but need to get
    the POS2c etc elements into the same array (im guessing thats the best
    way to do it).

    For example here is code that im shuffling:

    49 7 RP13,RP15,RP17,RP19,RP24, 12X2 33XD Wireless, Independent, 5%
    RP32,RP33
    50 4 RP27,RP28,RP30,RP31 10XC4 3X567 Network, Isolated, 5%
    51 31 R1,R2,R8,R30,R32,R33,R35, 0603,R10K,1% 0603 3%,1/12W
    R37,R39,R41,R42,R43,R49,
    R50,R51,R52,R58,R68,R69,
    R71,R72,R74,R85,R95,R117,
    R129,R130,R155,R156,R158,
    R160

    What I've done is split the text up into lines stored all the lines in
    an array with text.strip.split("\n") But as you can see the R71, R129,
    the elements in the third column continue on the next line. I need a
    way to get them into one place. Because those elements that are on the
    next line are a continuation of the third column element. The elements
    on the line are separated by tabs.

    --
    Posted via http://www.ruby-forum.com/.
     
    Al Cholic, Jul 27, 2007
    #6
  7. On Jul 26, 2007, at 9:59 PM, Al Cholic wrote:
    > I reposting my last response since there is no edit on this forum.
    >
    > Sorry about the syntax. I was just trying to show the array, it
    > was not
    > intended to be ruby code. What Im trying to do is read a text file
    > and
    > then make sense of the data in that text file and store that info in a
    > database. I have managed to sort out into the arrays but need to get
    > the POS2c etc elements into the same array (im guessing thats the best
    > way to do it).
    >
    > For example here is code that im shuffling:
    >
    > 49 7 RP13,RP15,RP17,RP19,RP24, 12X2 33XD Wireless,
    > Independent, 5%
    > RP32,RP33
    > 50 4 RP27,RP28,RP30,RP31 10XC4 3X567 Network, Isolated, 5%
    > 51 31 R1,R2,R8,R30,R32,R33,R35, 0603,R10K,1% 0603 3%,1/12W
    > R37,R39,R41,R42,R43,R49,
    > R50,R51,R52,R58,R68,R69,
    > R71,R72,R74,R85,R95,R117,
    > R129,R130,R155,R156,R158,
    > R160
    >
    > What I've done is split the text up into lines stored all the lines in
    > an array with text.strip.split("\n") But as you can see the R71,
    > R129,
    > the elements in the third column continue on the next line. I need a
    > way to get them into one place. Because those elements that are on
    > the
    > next line are a continuation of the third column element. The
    > elements
    > on the line are separated by tabs.


    I'm guessing that "51\t31\tR1,R2,..." means that this is the 51st
    "thing" and there are 31 of the Rnn's in the third column. The
    following lines begin "\t\tR37,R39,..." and "\t\tR50,R51,..."

    In that case, this fragment might help jump-start you toward a solution:

    text = gets # or however you're getting the next line

    data = text.split("\t") # or .split("\t", 6) to limit to 6 parts
    expected = data[1].to_i # here's where you get 31
    data[2] = data[2].split(',').compact # the .compact removes a
    possible nil from a trailing ','
    found = data[2].length
    while found < expected
    moretext = gets
    break unless moretext =~ /\A\t\t/ # \A anchors to start, then 2 TABs
    more_elements = moretext.split("\t",4)[2].split(',').compact
    data[2].concat(more_elements)
    found += more_elements.length
    end

    unless found == expected
    raise "found #{found}, but expected #{expected} on number #{data[0]}"
    end


    Yes, it's rough (but so is your problem statment ;-) and you might do
    better checking for the trailing comma on the line as an added
    indication that there are more elements to come.

    Good Luck,

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Jul 27, 2007
    #7
  8. On Jul 26, 2007, at 9:59 PM, Al Cholic wrote:

    > I reposting my last response since there is no edit on this forum.
    >
    > Sorry about the syntax. I was just trying to show the array, it
    > was not
    > intended to be ruby code. What Im trying to do is read a text file
    > and
    > then make sense of the data in that text file and store that info in a
    > database. I have managed to sort out into the arrays but need to get
    > the POS2c etc elements into the same array (im guessing thats the best
    > way to do it).
    >
    > For example here is code that im shuffling:
    >
    > 49 7 RP13,RP15,RP17,RP19,RP24, 12X2 33XD Wireless,
    > Independent, 5%
    > RP32,RP33
    > 50 4 RP27,RP28,RP30,RP31 10XC4 3X567 Network, Isolated, 5%
    > 51 31 R1,R2,R8,R30,R32,R33,R35, 0603,R10K,1% 0603 3%,1/12W
    > R37,R39,R41,R42,R43,R49,
    > R50,R51,R52,R58,R68,R69,
    > R71,R72,R74,R85,R95,R117,
    > R129,R130,R155,R156,R158,
    > R160


    Now I understand. That's a rather nasty data file. How I would go
    about reformatting to something more rational would depend on what
    the field separators really are. I mean is

    49 7 RP13,RP15,RP17,RP19,RP24, 12X2 33XD Wireless, Independent, 5%

    really

    49\t7\tRP13,RP15,RP17,RP19,RP24,\t12X2\t33XD\tWireless, Independent, 5%

    which I construe as 6 tab-delimited fields, or am I misreading it?
    Also, can any field other than the second one spill over onto
    following lines? And is the white space after the commas in what I
    think is last field significant? That is, does 'Wireless,
    Independent, 5%' need to be handled differently than
    'RP13,RP15,RP17,RP19,RP24,'?

    Regards, Morton
     
    Morton Goldberg, Jul 27, 2007
    #8
  9. Al Cholic

    Al Cholic Guest

    Morton Goldberg wrote:
    > On Jul 26, 2007, at 9:59 PM, Al Cholic wrote:
    >
    >>
    >> R129,R130,R155,R156,R158,
    >> R160

    >
    > Now I understand. That's a rather nasty data file. How I would go
    > about reformatting to something more rational would depend on what
    > the field separators really are. I mean is
    >
    > 49 7 RP13,RP15,RP17,RP19,RP24, 12X2 33XD Wireless, Independent, 5%
    >
    > really
    >
    > 49\t7\tRP13,RP15,RP17,RP19,RP24,\t12X2\t33XD\tWireless, Independent, 5%
    >
    > which I construe as 6 tab-delimited fields, or am I misreading it?
    > Also, can any field other than the second one spill over onto
    > following lines? And is the white space after the commas in what I
    > think is last field significant? That is, does 'Wireless,
    > Independent, 5%' need to be handled differently than
    > 'RP13,RP15,RP17,RP19,RP24,'?
    >
    > Regards, Morton


    Only the third column with the R37,R39,R41,R42,R43,R49 can spill over to
    the next line. Yes, the elements are separated by tabs like you said.
    I have managed to get the lines into an array by just using
    inputed_text.strip.split("\n")
    So now I have an array that contains each line as its elements. But the
    problem is that I need to get the spilled over lines into a subarray of
    the right line. I think this is the easiest way to organize the data.
    After i get the spilled over elements to the right location i can go a
    head and put the data into a database because I know which element in
    the array represents what. I hope this helped in clarifying the issue.

    Thanks for you help so far.
    --
    Posted via http://www.ruby-forum.com/.
     
    Al Cholic, Jul 27, 2007
    #9
  10. 2007/7/27, Morton Goldberg <>:
    > If you are showing here is intended to be Ruby code, it is simply bad
    > syntax. AFAIK, backslashes have no escape function outside of strings
    > [*] and regular expressions.


    There are also line continuations:

    RKlemme@padrklemme1 ~
    $ ruby -e 'puts 1
    > + 2'

    1

    RKlemme@padrklemme1 ~
    $ ruby -e 'puts 1\
    + 2'
    3

    Kind regards

    robert
     
    Robert Klemme, Jul 27, 2007
    #10
  11. On Jul 27, 2007, at 2:12 AM, Al Cholic wrote:

    > Only the third column with the R37,R39,R41,R42,R43,R49 can spill
    > over to
    > the next line. Yes, the elements are separated by tabs like you said.
    > I have managed to get the lines into an array by just using
    > inputed_text.strip.split("\n")
    > So now I have an array that contains each line as its elements.
    > But the
    > problem is that I need to get the spilled over lines into a
    > subarray of
    > the right line. I think this is the easiest way to organize the data.
    > After i get the spilled over elements to the right location i can go a
    > head and put the data into a database because I know which element in
    > the array represents what. I hope this helped in clarifying the
    > issue.


    The following code isn't a full solution to your problem, but I think
    it might suggest a strategy you can use.

    <code>
    raw_data = DATA.read
    data = []
    raw_data.each do |line|
    data << line.chomp.split(/\t/)
    end
    primary = nil
    data.each_with_index do |row, i|
    if row[0].empty?
    primary[2] << row[2]
    data = nil
    else
    primary = row
    end
    end
    data.compact!
    p data

    __END__
    49 7 RP13,RP15,RP17,RP19,RP24, 12X2 33XD Wireless, Independent, 5%
    RP32,RP33
    50 4 RP27,RP28,RP30,RP31 10XC4 3X567 Network, Isolated, 5%
    51 31 R1,R2,R8,R30,R32,R33,R35, 0603,R10K,1% 0603 3%,1/12W
    R37,R39,R41,R42,R43,R49,
    R50,R51,R52,R58,R68,R69,
    R71,R72,R74,R85,R95,R117,
    R129,R130,R155,R156,R158,
    R160
    </code>

    <result>
    [["49", "7", "RP13,RP15,RP17,RP19,RP24,RP32,RP33", "12X2", "33XD",
    "Wireless, Independent, 5%"],
    ["50", "4", "RP27,RP28,RP30,RP31 ", "10XC4", "3X567", "Network,
    Isolated, 5%"],
    ["51", "31",
    "R1,R2,R8,R30,R32,R33,R35,R37,R39,R41,R42,R43,R49,R50,R51,R52,R58,R68,R6
    9,R71,R72,R74,R85,R95,R117,R129,R130,R155,R156,R158,R160", "0603,R10K,
    1%", "0603", "3%,1/12W"]]
    </result>

    Regards, Morton
     
    Morton Goldberg, Jul 27, 2007
    #11
  12. On Jul 27, 2007, at 12:55 PM, Morton Goldberg wrote:

    > The following code isn't a full solution to your problem, but I
    > think it might suggest a strategy you can use.
    >
    > <code>
    > raw_data = DATA.read
    > data = []
    > raw_data.each do |line|
    > data << line.chomp.split(/\t/)
    > end
    > recd = nil
    > data.each_with_index do |row, i|
    > if row[0].empty?
    > recd[2] << row[2]
    > data = nil
    > else
    > recd = row
    > end
    > end
    > data.compact!
    > p data
    >
    > __END__
    > 49 7 RP13,RP15,RP17,RP19,RP24, 12X2 33XD Wireless, Independent, 5%
    > RP32,RP33
    > 50 4 RP27,RP28,RP30,RP31 10XC4 3X567 Network, Isolated, 5%
    > 51 31 R1,R2,R8,R30,R32,R33,R35, 0603,R10K,1% 0603 3%,1/12W
    > R37,R39,R41,R42,R43,R49,
    > R50,R51,R52,R58,R68,R69,
    > R71,R72,R74,R85,R95,R117,
    > R129,R130,R155,R156,R158,
    > R160
    > </code>
    >
    > <result>
    > [["49", "7", "RP13,RP15,RP17,RP19,RP24,RP32,RP33", "12X2", "33XD",
    > "Wireless, Independent, 5%"],
    > ["50", "4", "RP27,RP28,RP30,RP31 ", "10XC4", "3X567", "Network,
    > Isolated, 5%"],
    > ["51", "31",
    > "R1,R2,R8,R30,R32,R33,R35,R37,R39,R41,R42,R43,R49,R50,R51,R52,R58,R68,
    > R69,R71,R72,R74,R85,R95,R117,R129,R130,R155,R156,R158,R160",
    > "0603,R10K,1%", "0603", "3%,1/12W"]]
    > </result>


    The following does the job in one pass, rather than three:

    <code>
    raw_data = DATA.read
    data = []
    recd = nil
    raw_data.each do |line|
    row = line.chomp.split("\t")
    if row[0].empty?
    recd[2] << row[2]
    else
    data << (recd = row)
    end
    end
    p data
    </code>

    Regards, Morton
     
    Morton Goldberg, Jul 28, 2007
    #12
  13. Al Cholic

    Al Cholic Guest


    > <code>
    > raw_data = DATA.read
    > data = []
    > recd = nil
    > raw_data.each do |line|
    > row = line.chomp.split("\t")
    > if row[0].empty?
    > recd[2] << row[2]
    > else
    > data << (recd = row)
    > end
    > end
    > p data
    > </code>
    >
    > Regards, Morton


    Hi Morton,

    Thank you very much for putting effort into this. I was able to get it
    working, but could you explain how it works. Here is the way I
    implemented:

    def get_component_info(bom) #gettting the bom array (each
    element=line)
    data = [] #setting up a temp variable
    recd = nil
    bom.each do |line| #starting to loop though each line
    row = line.chomp.split("\t") #created new array row by splitting
    with tab
    if row[0].empty?
    recd[2] << row[2]
    else
    data << (recd = row)
    end
    end
    data
    end

    Questions:
    at this line:
    if row[0].empty?

    Why are you checking the first element of the array? Would this always
    return false because there are no empty lines in the raw text file.

    at this line:
    recd[2] << row[2]

    Does recd now become a an array? And you are storing the third element
    of row in the third position of recd? Why?

    at this line:
    data << (recd = row)

    Is this the way to read it: assign row value to recd and store it in
    data?



    I hope you can clarify these points. It would be big help in
    understanding ruby better for me.

    Thank you,

    Chris

    --
    Posted via http://www.ruby-forum.com/.
     
    Al Cholic, Jul 28, 2007
    #13
  14. On Jul 27, 2007, at 8:08 PM, Al Cholic wrote:

    > def get_component_info(bom) #getting the bom array (each
    > element=line)
    > data = [] #setting up a temp variable
    > recd = nil
    > bom.each do |line| #starting to loop though each line
    > row = line.chomp.split("\t") #created new array row by
    > splitting with tab
    > if row[0].empty?
    > recd[2] << row[2]
    > else
    > data << (recd = row)
    > end
    > end
    > data
    > end
    >
    > Questions:
    > at this line:
    > if row[0].empty?
    >
    > Why are you checking the first element of the array? Would this
    > always
    > return false because there are no empty lines in the raw text file.


    row = line.chomp.split("\t")

    transforms one line of bom from a tab delimited record into an array
    of fields. row[0] holds the contents of the first field. If that
    content is an empty string, code will assume the line being processed
    is a spill-over line. If it is not empty, code will assume the
    beginning of a new record.

    > at this line:
    > recd[2] << row[2]
    >
    > Does recd now become a an array? And you are storing the third
    > element
    > of row in the third position of recd? Why?


    At this point, code assumes row is a spill-over and that the third
    field, row[2], is where the spill-over data lives. This line of code
    appends the spill-over data to the third field, recd[2], of the
    current record. Here the << operator is a string append.

    > at this line:
    > data << (recd = row)
    >
    > Is this the way to read it: assign row value to recd and store it in
    > data?


    At this point, code assumes it has a new record. The code here is
    shorthand for

    recd = row
    data << recd

    The first code line above provides a way to reference this record
    later, should there be spill-over lines following it. The second code
    line adds (appends) the new record to the array of records the method
    is building. Here the << operator is an array append.

    > I hope you can clarify these points. It would be big help in
    > understanding ruby better for me.


    I hope I have clarified things sufficiently, but if I haven't, feel
    free to ask more.

    Regards, Morton
     
    Morton Goldberg, Jul 28, 2007
    #14
  15. Al Cholic

    Al Cholic Guest

    Morton Goldberg wrote:
    > On Jul 27, 2007, at 8:08 PM, Al Cholic wrote:
    >
    >> data << (recd = row)
    >> always
    >> return false because there are no empty lines in the raw text file.

    >
    > row = line.chomp.split("\t")
    >
    > transforms one line of bom from a tab delimited record into an array
    > of fields. row[0] holds the contents of the first field. If that
    > content is an empty string, code will assume the line being processed
    > is a spill-over line. If it is not empty, code will assume the
    > beginning of a new record.


    The only line that I have trouble understanding is this one:
    if row[0].empty?

    I try to logically think about it. Lets say it hits a spill-over line
    and it has to process the string "C22,C23,C26,C27". So it chomps it at
    divides it up by tabs, which there are none because the chomp removed
    them if there were any. So, now row is a one element array. At this
    point I think row[0]="C22,C23,C26,C27" and row[1]=nil, row[2]=nil.
    Where is my logic flawed?

    Whats the difference between chomp and strip. I've only been using
    strip.

    Thanks again.
    --
    Posted via http://www.ruby-forum.com/.
     
    Al Cholic, Jul 28, 2007
    #15
  16. On Jul 27, 2007, at 11:39 PM, Al Cholic wrote:

    > The only line that I have trouble understanding is this one:
    > if row[0].empty?
    >
    > I try to logically think about it.


    Programming is more of an experimental science than a deductive one.
    If you have a theory like this, convert it into a code snippet and
    evaluate it (in irb, for example -- although I would do it in
    TextMate, but not everybody has that). One of Ruby's strengths is,
    being an interpreted language, it is very easy to test theories.

    > Lets say it hits a spill-over line
    > and it has to process the string "C22,C23,C26,C27". So it chomps
    > it at
    > divides it up by tabs, which there are none because the chomp removed
    > them if there were any. So, now row is a one element array. At this
    > point I think row[0]="C22,C23,C26,C27" and row[1]=nil, row[2]=nil.
    > Where is my logic flawed?


    You are wrong in thinking chomp removes any of the tabs. It only
    removes trailing end-of-line characters. Try

    "\t\ta,b,c\n".chomp.split("\t')

    in irb. You will see it produces ["", "", "a,b,c"]

    I hope you won't mind if I give you a bit of advice. I think you have
    reached the point in your Ruby journey where you will greatly benefit
    from two resources:

    1. Programming Ruby, The Pragmatic Programmers Guide, 2nd Edition
    (aka the pickaxe book) by Dave Thomas

    2. The Ruby Way, 2nd Edition, by Hal Fulton

    > Whats the difference between chomp and strip. I've only been using
    > strip.


    I think you will find it very helpful to have the PDF version of the
    pickaxe book on your computer to consult while you are writing code.
    It can instantly answer questions like this. I have my copy open on-
    screen all the time while I'm coding.

    Regards, Morton
     
    Morton Goldberg, Jul 28, 2007
    #16
  17. On Jul 27, 2007, at 11:39 PM, Al Cholic wrote:

    > Whats the difference between chomp and strip. I've only been using
    > strip.


    Oh, yes, there's always ri. I tend to forget about it because I rely
    on my pickaxe PDF.

    ~ mg: ri String#chomp
    ----------------------------------------------------------- String#chomp
    str.chomp(separator=$/) => new_str
    ------------------------------------------------------------------------
    Returns a new +String+ with the given record separator removed
    from
    the end of _str_ (if present). If +$/+ has not been changed from
    the default Ruby record separator, then +chomp+ also removes
    carriage return characters (that is it will remove +\n+, +\r+, and
    +\r\n+).

    "hello".chomp #=> "hello"
    "hello\n".chomp #=> "hello"
    "hello\r\n".chomp #=> "hello"
    "hello\n\r".chomp #=> "hello\n"
    "hello\r".chomp #=> "hello"
    "hello \n there".chomp #=> "hello \n there"
    "hello".chomp("llo") #=> "he"

    ~ mg: ri String#strip
    ----------------------------------------------------------- String#strip
    str.strip => new_str
    ------------------------------------------------------------------------
    Returns a copy of _str_ with leading and trailing whitespace
    removed.

    " hello ".strip #=> "hello"
    "\tgoodbye\r\n".strip #=> "goodbye"

    Regards, Morton
     
    Morton Goldberg, Jul 28, 2007
    #17
  18. Al Cholic

    Ian Whitlock Guest

    Morton Goldberg wrote:
    > On Jul 27, 2007, at 12:55 PM, Morton Goldberg wrote:
    >
    > <code>
    > raw_data = DATA.read
    > data = []
    > recd = nil
    > raw_data.each do |line|
    > row = line.chomp.split("\t")
    > if row[0].empty?
    > recd[2] << row[2]
    > else
    > data << (recd = row)
    > end
    > end
    > p data
    > </code>


    Great lesson for a Ruby Newbie! I had to
    struggle with why

    recd[2] << row[2]

    changed data, but once one understands,
    it is an eye opener. Page 41 of Programming
    Ruby explains, and yells gotcha at me. Your
    code turns it into a tool.

    In a more traditional (less ruby) form:

    > raw_data = DATA.read
    > data = []

    i = -1
    > raw_data.each do |line|
    > row = line.chomp.split("\t")

    if not row[0].empty? # new logical line
    i += 1
    data = row
    > else # continuation

    data[2] << row[2]
    > end
    > end
    > p data


    Thanks, again.
    Ian
    --
    Posted via http://www.ruby-forum.com/.
     
    Ian Whitlock, Jul 28, 2007
    #18
  19. Al Cholic

    Al Cholic Guest

    > I think you will find it very helpful to have the PDF version of the
    > pickaxe book on your computer to consult while you are writing code.
    > It can instantly answer questions like this. I have my copy open on-
    > screen all the time while I'm coding.
    >
    > Regards, Morton


    Thank you Morton. You've been a big help. I got the books now.

    --
    Posted via http://www.ruby-forum.com/.
     
    Al Cholic, Jul 28, 2007
    #19
    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. HeroOfSpielburg
    Replies:
    1
    Views:
    391
    Alf P. Steinbach
    Aug 6, 2003
  2. Peter
    Replies:
    5
    Views:
    344
    Jan Engelhardt
    Dec 10, 2003
  3. Replies:
    0
    Views:
    176
  4. Replies:
    2
    Views:
    224
  5. Greg Ma
    Replies:
    7
    Views:
    174
    Robert Klemme
    Feb 15, 2010
Loading...

Share This Page