Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to delete array element and add to previous element

Reply
Thread Tools

How to delete array element and add to previous element

 
 
Al Cholic
Guest
Posts: n/a
 
      07-27-2007
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/.

 
Reply With Quote
 
 
 
 
dblack@rubypal.com
Guest
Posts: n/a
 
      07-27-2007
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)

 
Reply With Quote
 
 
 
 
Al Cholic
Guest
Posts: n/a
 
      07-27-2007
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/.

 
Reply With Quote
 
Morton Goldberg
Guest
Posts: n/a
 
      07-27-2007
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.

 
Reply With Quote
 
Al Cholic
Guest
Posts: n/a
 
      07-27-2007

> [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/.

 
Reply With Quote
 
Al Cholic
Guest
Posts: n/a
 
      07-27-2007
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/.

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      07-27-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
Morton Goldberg
Guest
Posts: n/a
 
      07-27-2007
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\tWire less, 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

 
Reply With Quote
 
Al Cholic
Guest
Posts: n/a
 
      07-27-2007
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\tWire less, 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/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-27-2007
2007/7/27, Morton Goldberg <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Compare and delete element from an array Greg Ma Ruby 7 02-15-2010 08:25 AM
How to add a row to the table web control dynamically in asp .net 2.0 on a button click retaining the user input in previous rows? loga123 ASP .Net 1 10-03-2006 04:48 AM
How to add a button to Modify, delete or add a new record to this code karenmiddleol@yahoo.com ASP General 2 10-04-2005 11:55 AM
How to add a button to Modify, delete or add a new record to this code karenmiddleol@yahoo.com ASP General 0 10-01-2005 07:21 AM



Advertisments