Jump to content

Reading Text file and Finding strings


deef99
 Share

Recommended Posts

Ok...trying to do what I thought was fairly simple...

I have a file - example below of some lines. I need to find 2 strings of data, and write them out to a file. I can find the first one...but having trouble gettin the second.

My thought was: find the "Heat List for:" ...then first the very next one. Then go back 2 lines and get the HEAT line. First person done...write to file, move to next person. The goal is to create a file with Dancer's Name and Last Heat Number.

FILE EXAMPLE:

Heat list for:Vance Allen [The Tango], 27 entries

Practice [saturday September 17, 2011, 10:00AM]

American Smooth & International Standard Single Dance Events [saturday September 17, 2011, 11:00AM]

With Stu Rozier, number 12

11:19AM Heat 14 L-B Bronze I Closed Waltz

11:21AM Heat 15 L-B Bronze I Closed Tango

11:22AM Heat 16 L-B Bronze I Closed Foxtrot

11:33AM Heat 22 L-B Bronze II Closed Waltz

11:35AM Heat 23 L-B Bronze II Closed Tango

11:36AM Heat 24 L-B Bronze II Closed Foxtrot

11:38AM Heat 25 L-B Bronze II Closed V. Waltz

12:05PM Heat 43 L-B Bronze I Closed V. Waltz

-----------------------------------PAGE BREAK-----------------------------------

Heat list for:Lenn Also, number 17 [Dance City Ballroom], 4 entries

Practice [saturday September 17, 2011, 10:00AM]

American Smooth & International Standard Single Dance Events [saturday September 17, 2011, 11:00AM]

With Margie

11:07AM Heat 6 L-D Bronze II Closed Foxtrot

11:53AM Heat 35 L-D Bronze II Closed Waltz

11:54AM Heat 36 L-D Bronze II Closed Tango

American Smooth 3 Dance Challenge Events [saturday September 17, 2011, 12:25PM]

01:13PM Heat 65 L-D Pro/Am Tango

American Rhythm & International Latin Single Dance Events [saturday September 17, 2011, 02:00PM]

Rhythm 3 Dance Challenge Events [saturday September 17, 2011, 04:33PM]

-----------------------------------PAGE BREAK-----------------------------------

Heat list for:Margie Doe [Dance City Ballroom], 4 entries

Here's what I have left of my code that didn't work:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_Outfile=C:\cmpmgr\Top.exe

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <File.au3>

$file = FileOpen("c:\cmpmgr\top\lastheat.txt", 0)

$result = ""

$vnum = ""

$d1 = ""

$d2 = ""

$logfile = "c:\cmpmgr\top\LAST.txt"

FileDelete($logfile)

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

$result = StringInStr($line, "Heat List for:")

If $result > 0 Then

$d1 = $line

EndIf

$result = StringInStr($line, "Heat List for:")

MsgBox(0, "vnum:", $result)

$result = $result - 2

$result = StringInStr($line, "M HEAT")

$vnum = $line

MsgBox(0, "vnum:", $vnum)

FileWrite($logfile, $d1 & " - " & $vnum & @CRLF)

WEnd

FileClose($logfile)

Link to comment
Share on other sites

  • Moderators

deef99,

If I use this file:

Heat list for:Vance Allen [The Tango], 27 entries
Practice [Saturday September 17, 2011, 10:00AM]
American Smooth & International Standard Single Dance Events [Saturday September 17, 2011, 11:00AM]
With Stu Rozier, number 12
11:19AM Heat 14 L-B Bronze I Closed Waltz
11:21AM Heat 15 L-B Bronze I Closed Tango
11:22AM Heat 16 L-B Bronze I Closed Foxtrot
11:33AM Heat 22 L-B Bronze II Closed Waltz
11:35AM Heat 23 L-B Bronze II Closed Tango
11:36AM Heat 24 L-B Bronze II Closed Foxtrot
11:38AM Heat 25 L-B Bronze II Closed V. Waltz
12:05PM Heat 43 L-B Bronze I Closed V. Waltz
-----------------------------------PAGE BREAK-----------------------------------
Heat list for:Lenn Also, number 17 [Dance City Ballroom], 4 entries
Practice [Saturday September 17, 2011, 10:00AM]
American Smooth & International Standard Single Dance Events [Saturday September 17, 2011, 11:00AM]
With Margie 
11:07AM Heat 6 L-D Bronze II Closed Foxtrot
11:53AM Heat 35 L-D Bronze II Closed Waltz
11:54AM Heat 36 L-D Bronze II Closed Tango
American Smooth 3 Dance Challenge Events [Saturday September 17, 2011, 12:25PM]
01:13PM Heat 65 L-D Pro/Am Tango
American Rhythm & International Latin Single Dance Events [Saturday September 17, 2011, 02:00PM]
Rhythm 3 Dance Challenge Events [Saturday September 17, 2011, 04:33PM]
-----------------------------------PAGE BREAK-----------------------------------
Heat list for:Margie Doe [Dance City Ballroom], 4 entries
Practice [Saturday September 17, 2011, 10:00AM]
American Smooth & International Standard Single Dance Events [Saturday September 17, 2011, 11:00AM]
With Fred
03:10PM Heat 97 L-D Pro/Am Tango

Then this script works:

#include <File.au3>
#include <Array.au3>

Global $aLines

_FileReadToArray("mylist.txt", $aLines)

$iStart = 0

$sSections = ""

While 1

    $iIndex = _ArraySearch($aLines, "Heat list for:", $iStart, 0, 0, 1)

    If $iIndex = -1 Then ExitLoop

    $sSections &= $iIndex & "|"

    $iStart = $iIndex + 1

WEnd

$sSections &= $aLines[0] + 2

$aSections = StringSplit($sSections, "|")

For $i = 1 To $aSections[0] - 1

    $sName = StringRegExpReplace($aLines[$aSections[$i]], ".*:(.*),.*", "$1")

    ConsoleWrite($sName & " - Last Heat: ")

    $iStart = $aSections[$i + 1] - 1
    If $iStart > $aLines[0] Then
        $iStart = $aLines[0]
    EndIf

    For $j = $iStart To $aSections[$i] Step -1

        If StringInStr($aLines[$j], "Heat") Then
            $iHeatNumber = StringRegExpReplace($aLines[$j], ".*Heat\x20(\d+)\x20.*", "$1")
            ConsoleWrite($iHeatNumber & @CRLF)
            ExitLoop
        EndIf

    Next

Next

Assumption: The last Heat is always the last listed - if this is not the case the problem become much more complex, although not impossible.

Any use? :graduated:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...