Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Internal Sort Program

Posted on 1998-11-09
8
208 Views
Last Modified: 2010-04-30
This program is required to read a text file of country currency rates into array, sort the array, then output the sorted array as a text file. How do you load the text file into array and how do you sort it?
0
Comment
Question by:gongsher
8 Comments
 
LVL 15

Expert Comment

by:ameba
ID: 1443977
Read file, sort, write file...
3 questions. I will add 1 more:
How does your file look like. Is it coma separated?
0
 

Author Comment

by:gongsher
ID: 1443978
Its a sequential file that has a file size of maximum 50 records, record length of fixed 40 bytes and the record layout should be the following:

CountryCode   text    4 bytes
CountryName   text    20 bytes
BuyRate       text    8 bytes
SellRate      text    8 bytes

Example of input file:
05  Australia    0.8656    0.8498
02  Japan        62.54     61.08
Up to about 50 records

Have to load the input file records into the array on User selection of the Sort button.  It should sort the array elements into ascending Country Code order and then output into another text file.



0
 
LVL 1

Expert Comment

by:lawnranger
ID: 1443979
Look at a recent word joust between me and Waty at http://www.experts-exchange.com/topics/comp/lang/visualbasic/Q.10078969!
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:gongsher
ID: 1443980
First, I would just like to know how exactly do you load the text file into array?

What I have done is:

' Opening the input file which contains 40 bytes
Open (App.Path & "\Input.txt") For Random As #1 Len = 40

How do you load the file as array and then output it to another text file? The file contains 50 records and the output should be like the following:

05  Australia    0.8656    0.8498
02  Japan        62.54     61.08
Up to 50 records.

Thanks!



0
 

Author Comment

by:gongsher
ID: 1443981
As I mentioned above, I would like to know firstly how to load a text file into an array.
0
 
LVL 2

Accepted Solution

by:
dabellei earned 100 total points
ID: 1443982
Private Sub load_Click()
    nfic = FreeFile
    Dim texte_share As String
   
    Open "c:\input.txt" For Input As #1
    Do While Not EOF(nfic)
        Line Input #nfic, texte_share
        List1.AddItem texte_share
    Loop
    Close #1
   

End Sub

Private Sub save_Click()
    nfic = FreeFile
    Dim texte_share As String
   
    Open "c:\inputs.txt" For Binary Access Write As #1
    For x = 0 To List1.ListCount - 1
        texte$ = List1.List(x) & Chr(13) & Chr(10)
        Put #1, , texte$
    Next x
    Close #1

End Sub

Put two button (load and save) and a listbox (sorted = true) on
a form
0
 

Author Comment

by:gongsher
ID: 1443983
dabellei

I have written a code here but it is definitely not right. Just wondering if you can have a check and correct. The form should only have one button, (sort button) and a message will come up after sorting, displaying the number of records sorted.
It should open the "ERInput.txt" file, load into array, sort the array and then output the sorted file into ascending Country Code order into the file "EROutput.txt"

Option Explicit
Type ExchangeRate
    strCountryCode as string *4
    strCountryName as string *20
    strBuyRate as string *8
    strSellRate as string *8
End Type
Public gExchangeRate (1 to 50) as ExchangeRate

Option Explicit
Dim intNumber As Integer
Dim intIndex As Integer
Dim intArrayNum As Integer
Dim intPrevArrayNum As Integer
Dim udtCountryRate As ExchangeRate

Private Sub cmdSort_Click()
Open (App.Path & "\ERInput.txt") For Random As #1 Len =_     Len(gExchangeRate(1))

intNumber = LOF(1) / Len(gExchangeRate(1))

If LOF(1) / Len(gExchangeRate(1)) > 0 Then
    For intIndex = 1 To LOF(1) / Len(gExchangeRate(1))
        Get #1, intIndex, gExchangeRate(intIndex)
    Next intIndex
End If

For intArrayNum = 2 To intNumber
    udtCountryRate = gExchangeRate(intArrayNum)
   
    For intPrevArrayNum = intArrayNum - 1 To 1 Step -1
        If Val(udtCountryRate.strCountryCode) >= Val(gExchangeRate(intPrevArrayNum). _
            strCountryCode) Then
                Exit For
        Else
            gExchangeRate(intPrevArrayNum + 1) = gExchangeRate(intPrevArrayNum)
        End If
    Next intPrevArrayNum
    gExchangeRate(intPrevArrayNum + 1) = udtCountryRate
Next intArrayNum

Open (App.Path & "\EROutput.txt") For Output As #2 Len = Len(gExchangeRate(1))

For intArrayNum = 1 To intNumber
    'Put #2, (intArrayNum), gExchangeRate(intArrayNum)
    Print #2, gExchangeRate(intArrayNum).strCountryCode;
    Print #2, gExchangeRate(intArrayNum).strCountryName;
    Print #2, gExchangeRate(intArrayNum).strBuyRate;
    Print #2, gExchangeRate(intArrayNum).strSellRate;
Next intArrayNum
Close #2

lblMessage = intNumber & " exchange rate records sorted."
Exit Sub
End Sub


0
 
LVL 2

Expert Comment

by:dabellei
ID: 1443984
i'll take a look at your code and return with it soon
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MsgBox 2 59
Adding to a VBA? 6 78
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 58
Sorting multiple rows and columns, and count duplicates in Excel 2013 4 87
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question