2

I have a text file named data.txt having various parameters such as number, status and so on. Each line contains a different set of data.

Now, in my GUI, I have submit button. On click of that button, I want 'few' of these data to be displayed as a table on the GUI screen

One thing is I do know which position it exists in the text file :

Ex: Status appears at position [70..78] in each line of text file Number appears at position [85..90] in each line of text file and so on

I want only those particular parts of data to be displayed in respective columns of the table

Any suggestions would be welcome. I am using Qt and Ruby for my GUI design

Following images show what exactly i am looking for: 

Image 1: My text file containing data
Image 2 : My final expected outcome should look like that.

EDIT: Using the solution mentioned by Stephen:

Using the line position worked for me. But one query here, when i try to use that using puts statement, it does print properly. However when i try to use the same for displaying it inside TextEdit widget, each line does not get displayed..

 @text_var = "#{line[70..78]}\t#{line[85..90]}

  @text = Qt::TextEdit.new(self)
  @text.setText (@text_var) 
  puts  @text_var

Puts statement gives correct output, however i am not able to send the same inside the widget. If i use break statement , then first line is getting displayed in widget correctly. So error is happening when it is trying to read line by line on to the widget. Ultimately, it is getting overwritten and last line where just blank spaces are there is getting printed in the widget i feel.

Any solutions for this ?

topjay
  • 65
  • 6
  • better split the conversion part and the GUI part into separate processes and questions if necessary, convert into eg array and check if everything ok by a simple puts of the array then try tio display the array into the table, ask separate question for that and show us the code you used and a sample of your data – peter Mar 09 '16 at 12:11

1 Answers1

3

It looks like the Status and Number you want always appear in the same column. You can split each line of data.txt on whitespace to only display the column you want.

File.open('data.txt').readlines.each do |line|
  columns = line.split(/\s+/)
  puts "#{columns[4]}\t#{columns[5]}"  
end

This prints the 5th and 6th columns, separated by a tab.

You could also take advantage of knowing the position, as you mention:

File.open('data.txt').readlines.each do |line|
  puts "#{line[70..78]}\t#{line[85..90]}"  
end
Stephen Grimes
  • 398
  • 1
  • 7
  • Thanks for the solution. I will try using this method once and get back. However since i know status and number appear at exact same position in each line, like line[70..78] wil b status , cant i make use of that instead of columns ? – topjay Mar 09 '16 at 04:37
  • Yeah, you can do that too. I'll add it to the solution – Stephen Grimes Mar 09 '16 at 04:39
  • Thanks. That part would be more suitable for me I guess since I have tried to use the positions in other parts of the code – topjay Mar 09 '16 at 04:42
  • I will try them out as soon as possible and get back with the outcome !! – topjay Mar 09 '16 at 04:42
  • Please do check the edit part in the code below and clear that query ! I was unable to add code part in comments, so edited in question – topjay Mar 09 '16 at 05:23