(no subject)

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…

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.

On Wed, Sep 16, 2009 at 11:06 PM, prashanth hiremath
[email protected] 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? :slight_smile:

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. ------------------------ [email protected]
twitter: @hassan