# (no subject)

**URL:** <https://www.ruby-forum.com/t/no-subject/175659>\
**Category:** Rails\
**Created:** [September 17, 2009, 8:08am UTC](https://www.ruby-forum.com/t/no-subject/175659 "2009-09-17T08:08:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![prashanth\_hiremath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/df705f/32.png) [@prashanth\_hiremath](https://www.ruby-forum.com/u/prashanth_hiremath)\
**Post date:** [September 17, 2009, 8:08am UTC](https://www.ruby-forum.com/t/no-subject/175659/1 "2009-09-17T08:08:15Z")

</div>

HiDestinationID^Name^City^StateProvince^Country^Latitude^Longitude^Type  
{B75F49C9-7B62-44E7-92FE-0A7F0CCEDF14}^ Sardinia de Galdar Beach^Gran  
Canary^ES^28.151890^-15.696225^2

## {F920BB01-7E65-4E7F-B312-826A299CB154}^10 Yul Park^Ashkhabad,Ashgabat^TKM^37.927452^58.374588^2

* * *

So that my code is

dataArray= Array.new  
n = 0  
File.new(“data.csv”).each\_line do |line|  
line.chomp!  
dataArray[n] = line.split(’,’)  
n += 1  
end

i=0  
while(i \< dataArray.length)

puts “First: #{dataArray[i][0]}”

i=i+1

end

But this code wotks to display all the fields

My requirement is i have to put .csv file into elements of array so  
that i  
can increment longitude and latitude values for compraion if it matches

then i have to pull the entire row from the file this is the problem am  
facing

Regards  
prash…

---

<div class="post-metadata">

**Author:** ![prashanth\_hiremath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/df705f/32.png) [@prashanth\_hiremath](https://www.ruby-forum.com/u/prashanth_hiremath)\
**Post date:** [September 17, 2009, 2:29pm UTC](https://www.ruby-forum.com/t/no-subject/175659/2 "2009-09-17T14:29:26Z")

</div>

prashanth hiremath wrote:

> My requirement is i have to put .csv file into elements of array so  
> that i  
> can increment longitude and latitude values for compraion if it matches
> 
> then i have to pull the entire row from the file this is the problem am  
> facing

Let me see if I understand what you’re saying:

1. You have to read each row of file to get the lat / long to compare
2. Then compare the lat / long to something else to see if its the  
record you want
3. If so, you need to read the whole record

Well, to get to the lat / long (fields 6 and 7 out of 8), you have to  
have read the row anyway, so it seems that your solution should be to

Read the file and for each line  
parse the line  
test the lat / long  
if its what you’re looking for, keep that row (you’ve already read  
it)  
otherwise throw it away

Are you expecting a single match for a lat / long, or could there be  
multiple rows that meet your “match” criteria? This serves just as an  
efficiency measure. If you’re only supposed to find a single match,  
once you’ve found it, you’re done.

---

<div class="post-metadata">

**Author:** ![prashanth\_hiremath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/df705f/32.png) [@prashanth\_hiremath](https://www.ruby-forum.com/u/prashanth_hiremath)\
**Post date:** [September 17, 2009, 3:02pm UTC](https://www.ruby-forum.com/t/no-subject/175659/3 "2009-09-17T15:02:08Z")

</div>

On Wed, Sep 16, 2009 at 11:06 PM, prashanth hiremath  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> HiDestinationID^Name^City^StateProvince^Country^Latitude^Longitude^Type  
> {B75F49C9-7B62-44E7-92FE-0A7F0CCEDF14}^ Sardinia de Galdar Beach^Gran  
> Canary^ES^28.151890^-15.696225^2

> So that my code is

> i=i+1  
> end

> But this code wotks to display all the fields

No kidding. Could that have anything to do with splitting your input  
on ‘,’ when there are no commas in the line at all? 🙂

Your code is also very un-rubyish. A more idiomatic approach:

lines = []  
File.new(“data.csv”).each\_line { |line| lines \<\< line.chomp!.split(“^”)  
}  
lines.each { |line| puts “First: #{line[0]}” }

## FWIW,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan
