[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to delete array element and add to previous element

Kristen

7/27/2007 12:16:00 AM

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\, POS2cup 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-....

18 Answers

David A. Black

7/27/2007 12:49:00 AM

0

Kristen

7/27/2007 1:19:00 AM

0

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

Morton Goldberg

7/27/2007 1:33:00 AM

0

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.

Kristen

7/27/2007 1:57:00 AM

0


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

Kristen

7/27/2007 2:00:00 AM

0

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

Rob Biedenharn

7/27/2007 2:24:00 AM

0

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://agileconsult...
Rob@AgileConsultingLLC.com



Morton Goldberg

7/27/2007 3:45:00 AM

0

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

Kristen

7/27/2007 6:12:00 AM

0

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

Robert Klemme

7/27/2007 6:47:00 AM

0

2007/7/27, Morton Goldberg <m_goldberg@ameritech.net>:
> 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

Morton Goldberg

7/27/2007 4:55:00 PM

0

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[i] = 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