# Array problem with sorting - maybe easy but not for me

**URL:** <https://www.ruby-forum.com/t/array-problem-with-sorting-maybe-easy-but-not-for-me/157478>\
**Category:** Ruby\
**Created:** [January 12, 2009, 4:57am UTC](https://www.ruby-forum.com/t/array-problem-with-sorting-maybe-easy-but-not-for-me/157478 "2009-01-12T04:57:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [January 12, 2009, 4:57am UTC](https://www.ruby-forum.com/t/array-problem-with-sorting-maybe-easy-but-not-for-me/157478/1 "2009-01-12T04:57:47Z")

</div>

This code reads in a text file with comma separated values. Problem is,  
some values are missing and it causing other values to ride up the list.  
I want to replace those values with string ‘no value’ then adjust there  
others if the next value is not listed on the second iteration of the  
array. I am new to learning programming.

patientrecords.txt

1,PersonID,PersonID  
2,ID,ID  
3,LastName,Social Security #\<-insert ‘no value’  
4,Social Security, Address #\<-change array[2] Social Security  
5,Address,UniqueID #\<-change array[2] to Address  
6,UniqueID,Potassium #\<-change array[2] to UniqueID

* * *

File.open(‘patientrecords.txt’, ‘r’) do |temp|  
array = []  
temp.each\_line do |line|  
array = line.chomp.split(",",0)  
nextVal = array[0].to\_i + 1  
line.each\_line do |n1|  
if array[1] != array[2] then  
if array[0] != nextVal then  
array[2] = “no value”  
end  
#array[2] = array[1] #populates the array[2]  
end  
end  
puts array[0]+","+array[1]+","+array[2]  
end  
end

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [January 12, 2009, 9:45pm UTC](https://www.ruby-forum.com/t/array-problem-with-sorting-maybe-easy-but-not-for-me/157478/2 "2009-01-12T21:45:59Z")

</div>

Do you think there is another way to do this? I have tried taking out  
the numbers array[0] and reading in as hash but I can’t get the next  
value in Hash. I have to have the next value to compare like this.

psuedo snippet:

if the array[1] != array[2] then  
array[2]= ‘no value’ if ‘next’ array[1] == array[2] #\<where i am stuck  
end

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [January 12, 2009, 10:21pm UTC](https://www.ruby-forum.com/t/array-problem-with-sorting-maybe-easy-but-not-for-me/157478/3 "2009-01-12T22:21:27Z")

</div>

Mmcolli00 Mom wrote:

> Do you think there is another way to do this? I have tried taking out  
> the numbers array[0] and reading in as hash but I can’t get the next  
> value in Hash. I have to have the next value to compare like this.
> 
> psuedo snippet:
> 
> if the array[1] != array[2] then  
> array[2]= ‘no value’ if ‘next’ array[1] == array[2] #\<where i am stuck  
> end

take out “line.each\_line do|n1|” statement and it’s associated end and I  
think you will get what you want…

ilan
