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
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: - 'Put your text in TextBox1 and the new text willl appear in TextBox2
-
-
dim OurText() as string
-
dim nCols as integer
-
dim nRows as integer
-
dim i as integer
-
dim j as integer
-
dim k as long
-
dim Str1 as string
-
-
nCols = 200
-
nRows = 2
-
-
redim ourtext(1 to nrows, 1 to ncols)
-
-
i=1
-
j=1
-
k=1
-
str1 = textbox1.text
-
-
do
-
if mid(str1,k,4)=" " then
-
j = j +1
-
k = k+4
-
elseif mid(str1,k,1) = chr(10) then
-
j=1
-
i = i+1
-
k=k+1
-
else
-
ourtext(i , j) = ourtext (i , j) & mid(str1,k,1)
-
k = k+1
-
end if
-
if k >= len(str1) then exit do
-
loop
-
-
str1 = ""
-
for i = 1 to ncols
-
for j = 1 to nrows
-
str1 = str1 & ourtext( i , j ) & " "
-
next
-
str1 = str1 & chr(10)
-
next
-
-
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
danp129 323
Recognized Expert Contributor
Was working on this before Kadghar's reply so I'll go ahead and post what I got - Option Explicit
-
Private Sub Command1_Click()
-
Dim sFileIn As String, sFileOut As String
-
sFileIn = "c:\in.txt"
-
sFileOut = "c:\out.txt"
-
Call Convert2Columns(sFileIn, sFileOut)
-
End Sub
-
-
-
Private Function PrepLine(sIn As String) As String
-
While InStr(1, sIn, " "): sIn = Replace(sIn, " ", " "): Wend
-
PrepLine = sIn
-
End Function
-
-
-
Sub Convert2Columns(sfIn As String, sfOut As String)
-
-
'Set how many lines should be converted to columns
-
Const lngExpectedLines As Long = 2
-
-
Dim FF_in As Long, FF_Out As Long
-
Dim sLine As String
-
Dim arLines(), arValues
-
ReDim Preserve arLines(lngExpectedLines - 1, 0)
-
Dim lngLine As Long
-
Dim i As Long
-
-
FF_in = FreeFile
-
-
'open file and lock it so no other apps can write while we're using it
-
Open sfIn For Input Lock Write As #FF_in
-
-
'read each line of the file
-
Do While Not EOF(FF_in)
-
-
'set sLine variable to the line of text VB reads from file
-
Line Input #FF_in, sLine
-
-
If sLine <> "" Then
-
If lngLine > lngExpectedLines - 1 Then
-
MsgBox "More lines then expected, continuing with what's already parsed."
-
Exit Do
-
End If
-
-
'remove extra spaces
-
sLine = PrepLine(sLine)
-
-
'create array of numbers
-
arValues = Split(sLine)
-
-
'add each number to arLines array
-
For i = 0 To UBound(arValues)
-
If i > UBound(arLines, 2) Then
-
ReDim Preserve arLines(lngExpectedLines - 1, i)
-
End If
-
arLines(lngLine, i) = arValues(i)
-
Next 'i
-
-
lngLine = lngLine + 1
-
End If
-
Loop
-
Close #FF_in
-
-
FF_Out = FreeFile
-
Open sfOut For Output Lock Write As #FF_Out
-
-
Dim x As Long
-
Dim y As Long
-
Dim sLineOut As String
-
Dim sDelim As String
-
-
For x = 0 To UBound(arLines, 2)
-
For y = 0 To UBound(arLines)
-
sLineOut = sLineOut & sDelim & arLines(y, x)
-
sDelim = " "
-
Next 'y
-
Print #FF_in, sLineOut
-
sLineOut = ""
-
sDelim = ""
-
Next 'x
-
Close #FF_Out
-
End Sub
thx guys....it was great help
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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?
|
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...
|
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...
|
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:
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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();...
| |
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |