Jump to content

Recommended Posts

Posted

G'day everyone

I'm trying to read the first column of a tab-delimited text file into an array. For some reason, the first line of the text file is not read into the array (or: it is read a a blank). I might add that I don't usually use "UBound ($xyz) -1" in my For/Next loops, but "$xyz[0]", so I wonder if that is where my problem lies.

My glossary file is this:

the[tab]asdf[tab]asdf
quick[tab]asdf[tab]asdf
brown[tab]asdf[tab]asdf
fox[tab]asdf[tab]asdf

The script is:

#Include <Array.au3>

$glossaryfileopendialog = FileOpenDialog ("Select glossary", @ScriptDir, "Text files (*.txt)|All files (*.*)")
$glossaryfileopen = FileOpen ($glossaryfileopendialog, 128)
$glossaryfileread = FileRead ($glossaryfileopen)

$glossaryfilelines = StringSplit ($glossaryfileread, @CRLF, 3)

Global $glossaryfilefirstcolumn[UBound ($glossaryfilelines)]

For $i = 1 to UBound ($glossaryfilelines) - 1

$glossaryfilecolumns = StringSplit ($glossaryfilelines[$i], @TAB, 3)

If UBound ($glossaryfilecolumns) > 1 Then ; If there are at least 2 columns in the line
$glossaryfilefirstcolumn[$i] = $glossaryfilecolumns[0]
EndIf

Next

MsgBox (0, "", _ArrayToString($glossaryfilefirstcolumn, " | "), 0)

What I expect to get as a message is:

the | quick | brown | fox

But what I get instead is:

| quick | brown | fox

I guess I'm missing something really obvious... but what?

Thanks

Samuel

Posted

the flag 3 you set to StringSplit, ( 1+2 ) flag = 2, disable the return the count in the first element - effectively makes the array 0-based (must use UBound() to get the size in this case).

So you need to Start with 0

For $i = 0 to UBound ($glossaryfilelines) - 1 ! Posted Image

AutoIt 3.3.18.0 X86 - SciTE 5.5.7WIN 11 24H2 X64 - Other Examples Scripts

Posted

So you need to Start with 0 ... For $i = 0 to UBound ($glossaryfilelines) - 1 !

Aah, of course, yes. I mistakenly thought (although it is a silly thought, now that I think about it) that the "- 1" would work on both what comes before and after the "to".

Thanks

Samuel

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
×
×
  • Create New...