473,809 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert rows into columns

2 New Member
hi,
I have a text file containing 2 rows of data points ....these data points are separated by 4 spaces in between and there are n number of such points where n can be 200-500.....i want a new text file to be created such that these 2 rows get converted to 2 columns( i need this file as a input file for gnuplot)......c an anybody please help me in writing the code in vb....i have used vba in autocad and am a complete newbie ............ple ase reply asap

eg.

1 2 3 4 5 6
7 8 9 10 11 12

to

1 7
2 8
3 9
4 10
5 11
6 12
Jun 27 '07 #1
3 1631
kadghar
1,295 Recognized Expert Top Contributor
hi,
I have a text file containing 2 rows of data points ....these data points are separated by 4 spaces in between and there are n number of such points where n can be 200-500.....i want a new text file to be created such that these 2 rows get converted to 2 columns( i need this file as a input file for gnuplot)......c an anybody please help me in writing the code in vb....i have used vba in autocad and am a complete newbie ............ple ase reply asap

eg.

1 2 3 4 5 6
7 8 9 10 11 12

to

1 7
2 8
3 9
4 10
5 11
6 12

Sure, just use a cupple of arrays and you are done; something like:

Expand|Select|Wrap|Line Numbers
  1. 'Put your text in TextBox1 and the new text willl appear in TextBox2
  2.  
  3. dim OurText() as string
  4. dim nCols as integer
  5. dim nRows as integer
  6. dim i as integer
  7. dim j as integer
  8. dim k as long
  9. dim Str1 as string
  10.  
  11. nCols = 200
  12. nRows = 2
  13.  
  14. redim ourtext(1 to nrows, 1 to ncols)
  15.  
  16. i=1
  17. j=1
  18. k=1
  19. str1 = textbox1.text
  20.  
  21.     do
  22.         if  mid(str1,k,4)="    " then
  23.             j = j +1
  24.             k = k+4
  25.         elseif mid(str1,k,1) = chr(10) then
  26.             j=1
  27.             i = i+1
  28.             k=k+1
  29.         else
  30.              ourtext(i , j) = ourtext (i , j) & mid(str1,k,1)
  31.              k = k+1
  32.          end if
  33.          if k >= len(str1) then exit do
  34.      loop
  35.  
  36. str1 = ""
  37. for i = 1 to ncols
  38.     for j = 1 to nrows
  39.         str1 = str1 & ourtext( i , j ) & "    "
  40.     next
  41.     str1 = str1 & chr(10)
  42. next
  43.  
  44. textbox2.text=str1
Im afraid im at my job right now so i didnt have time to check it out carefully, it might have many mistakes, but i think the idea is clear.

If you have doubts with the file importation and exportation, im pretty sure that's a question that have been answered many times before, if not PM me and i'll send it back to you as soon as i can (thats not a warranty of speed).

Good Luck
Jun 27 '07 #2
danp129
323 Recognized Expert Contributor
Was working on this before Kadghar's reply so I'll go ahead and post what I got

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private Sub Command1_Click()
  3.     Dim sFileIn As String, sFileOut As String
  4.     sFileIn = "c:\in.txt"
  5.     sFileOut = "c:\out.txt"
  6.     Call Convert2Columns(sFileIn, sFileOut)
  7. End Sub
  8.  
  9.  
  10. Private Function PrepLine(sIn As String) As String
  11.     While InStr(1, sIn, "  "): sIn = Replace(sIn, "  ", " "): Wend
  12.     PrepLine = sIn
  13. End Function
  14.  
  15.  
  16. Sub Convert2Columns(sfIn As String, sfOut As String)
  17.  
  18.     'Set how many lines should be converted to columns
  19.     Const lngExpectedLines As Long = 2
  20.  
  21.     Dim FF_in As Long, FF_Out As Long
  22.     Dim sLine As String
  23.     Dim arLines(), arValues
  24.     ReDim Preserve arLines(lngExpectedLines - 1, 0)
  25.     Dim lngLine As Long
  26.     Dim i As Long
  27.  
  28.     FF_in = FreeFile
  29.  
  30.     'open file and lock it so no other apps can write while we're using it
  31.     Open sfIn For Input Lock Write As #FF_in
  32.  
  33.     'read each line of the file
  34.     Do While Not EOF(FF_in)
  35.  
  36.         'set sLine variable to the line of text VB reads from file
  37.         Line Input #FF_in, sLine
  38.  
  39.         If sLine <> "" Then
  40.             If lngLine > lngExpectedLines - 1 Then
  41.                 MsgBox "More lines then expected, continuing with what's already parsed."
  42.                 Exit Do
  43.             End If
  44.  
  45.             'remove extra spaces
  46.             sLine = PrepLine(sLine)
  47.  
  48.             'create array of numbers
  49.             arValues = Split(sLine)
  50.  
  51.             'add each number to arLines array
  52.             For i = 0 To UBound(arValues)
  53.                 If i > UBound(arLines, 2) Then
  54.                     ReDim Preserve arLines(lngExpectedLines - 1, i)
  55.                 End If
  56.                 arLines(lngLine, i) = arValues(i)
  57.             Next 'i
  58.  
  59.             lngLine = lngLine + 1
  60.         End If
  61.     Loop
  62.     Close #FF_in
  63.  
  64.     FF_Out = FreeFile
  65.     Open sfOut For Output Lock Write As #FF_Out
  66.  
  67.     Dim x As Long
  68.     Dim y As Long
  69.     Dim sLineOut As String
  70.     Dim sDelim As String
  71.  
  72.     For x = 0 To UBound(arLines, 2)
  73.         For y = 0 To UBound(arLines)
  74.             sLineOut = sLineOut & sDelim & arLines(y, x)
  75.             sDelim = " "
  76.         Next 'y
  77.         Print #FF_in, sLineOut
  78.         sLineOut = ""
  79.         sDelim = ""
  80.     Next 'x
  81.     Close #FF_Out
  82. End Sub
Jun 27 '07 #3
pushkaraj
2 New Member
thx guys....it was great help
Jun 28 '07 #4

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

Similar topics

1
3630
by: Stan Sainte-Rose | last post by:
Oopps sorry for the previous post. As I said, I m trying to make a web custom control and I do know how I have to convert this part to for the render section In fact it may be a stupid question, how to "ouput" this section Table1 = CType(S_TABLES(S_IdAnnee), System.Web.UI.WebControls.Table) Table1.Visible = True Stan
0
1802
by: Harry Haller | last post by:
The context is shown below in the getGames() method. I get errors on these lines: dtGames.Rows = (TimeSpan)dtGames.Rows; dtGames.Rows = (DayOfWeek)dtGames.Rows; because the playDate column is a DateTime. Here is my solution but I don't like it. What else can I do?
5
2729
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried my best to convert it, but somehow the app does not work. I am also not getting any errors when I complie thus, letting me know I am on the write track. Basically what I want to do is edit,add,delete, and update an XML file in a DataGrid. Below...
9
101445
by: dotnetguru | last post by:
Hi SMART GUYS, Please help me write a query. Actually I want to convert my rows into columns. Can anyone kindly give me the query to do it? My rows are about employees. There can be any number of employees (in the following table I have 3 employees ABC, EFG, WRI) with any number of records for each employee (there can be 20, 30 records each), but all employees have the same number of records (in the following table each employee has 3...
9
35858
by: myotheraccount | last post by:
Hello, Is there a way to convert a DataRow to a StringArray, without looping through all of the items of the DataRow? Basically, I'm trying to get the results of a query and put them into a listbox. Right now it is done like this:
0
9603
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
10640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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
9200
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
7662
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
6881
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
5550
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...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.