473,666 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading variables in a file

1 New Member
I'm thinking of migrating from Fortran to Ruby. Do you think i should go ahead??
First problem i have.
How to read a file with several fields? This means:
Imagine a file:
Peter4 1990
Sam 3 1980
Grac6 1991

I would like to read for each line:name, order and year (3 variables per line). How can i do this? and how can i write them into a file??
Thank you in advance.
Oct 27 '06 #1
1 6199
jslaroch
2 New Member
I'm thinking of migrating from Fortran to Ruby. Do you think i should go ahead??
First problem i have.
How to read a file with several fields? This means:
Imagine a file:
Peter4 1990
Sam 3 1980
Grac6 1991

I would like to read for each line:name, order and year (3 variables per line). How can i do this? and how can i write them into a file??
Thank you in advance.
Well if you are programming in Fortran you will have to first learn about object oriented programming because unless Fortran as evolve since I programmed in it (in the days of punch cards) you will have a culture shock if you try this with no OOP training.

One way to solve your problem in Ruby would start with something like (hold on to your seat!):

File.open("D:\\ TEMP\\DATA.TXT" ).each() do |line|
stringArray = line.split()
puts "Name = #{stringArray[0]} and Year = #{stringArray[1]}"
# Add your code here (remove the puts above)
end


So assuming your data is in a text file D:\\TEMP\\DATA. TXT the code above would output:

Name = Peter4 and Year = 1990
Name = Sam and Year = 3
Name = Grac6 and Year = 1991


Now you can see that on line 2 the year for Sam is wrong. THis is because on the second line the data in your post contained a blank between Sam and 3.
Removing this typo you would get:

Name = Peter4 and Year = 1990
Name = Sam3 and Year = 1980
Name = Grac6 and Year = 1991


Of course you could add error checking for the data.

Get a copy of "Programmin g Ruby" by Dave Thomas and good luck.

JS
Nov 14 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3056
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
6
12726
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
7
14648
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
7
2554
by: Daniel Moree | last post by:
I'm working on a program that must first establish if the file exists in the program directory then it must open if for reading, read each line and set the variables then the program goes on about it's buisness. My problem is all the resources I have found aren't very clear on these things. All of them open the file then check to see if the stream is open. Well, the problem with using file.open("filename.dat", ios::in | ios::binary) is...
7
5548
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files, with different file names each time. So i'm writing into the code to ask the user how many files, and what their names are. From each we'll read in 2 lines, then do some math using all of those lines. Then do it again on another set of lines. ...
1
5668
by: Andrea Gavana | last post by:
Hello NG, that may sound a silly question, but I didn't find anything really clear about the issue of reading unformatted big endian files with Python. What I was doing till now, was using Fortran to read those files and compile this Fortran extension using F2PY. Now that it seems that no possible combinations of Fortran/C compilers actually *work* with Python 2.4 on Windows XP, I was trying to translate the Fortran subroutine to...
4
20727
waynetheengineer
by: waynetheengineer | last post by:
Hello everyone :) I am trying to write VB code for reading filenames and file property values in a specific directory. For example, I have a directory called C:/Bears and in that directory are three files: Brown Bears.txt, Black Bears.txt, and Orange Bears.txt. Each of these files have text descriptions in both the Title property field and the Comments property field of the each file (when you right click the file and select Properties,...
4
1857
by: isaac2004 | last post by:
hello, what i am trying to do is set up a loop that grabs multiple variables from an input file. my question is, is there a built in function in the istream class that can grab multiple values on one line. such as 60 40 10 10 all getting put into different variables. the problem i am having is
6
3524
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
5
5039
by: imailz | last post by:
Hi all, since I'm forced to switch from Fortran to C I wonder if there is posibility in C: 1) to use implicit loops 2) to parse several variables which number is determined at runtime. Following example: The output contains n columns which have to be read in. The number of
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.