Jump to content

How to make $foo[0] to be number of items in array?


 Share

Recommended Posts

G'day everyone

I know that if I create an array using StringSplit, then $array[1] is the first item in the array and $array[0] is the number of items in that array. This is how I prefer it. I also know that if I create an array by declaring it, then $array[0] is the first item in that array.

Now I have a script in which an array that was created using StringSplit DOES NOT put the number of items in $array[0]. My question is either (a) how can I force the array to behave like it should, or (B) how can I get the number of items in the array, so that I can tell a loop to repeat itself that many times?

The script itself is attached (sorry, it's a work in progress, but the problem line is commented with "WHY??"). What the script does is to compare an input string (grabbed text) to a glossary file (one line per entry, three columns, tab delimited) to output a list of matches from the glossary.

Also, if you can tell me how to force a declared array to behave like a StringSplit-created array (i.e. so that $array[0] is the number of items in the array), then that would also make my life a little simpler... but that is not the subject of this post.

Thanks

Samuel

test files.zip

Link to comment
Share on other sites

Please check the AutoIt help file.

Parameter 3 of function StringSplit sets a flag. Value 2 means: Disable the return the count in the first element

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Developers

What did the Helpfile tell you about using 2 for the third parameter?

$grabbedword = StringSplit ($grabbedtext, " .,!?<>{}[]()@#$&", 2)

Jos :mellow:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I think what the user wants is to always have a 1 based array; which is against programming norms but several people seem to prefer that for some strange reason. Using the flag 2 on StringSplit() returns a 0 based array.

Perhaps the OP would like to clarify that for us, or perhaps (better yet) he wants to rethink his preference and read UBound() in the help file.

Personally I wish all of the arrays in AutoIt were 0 based by default but it seems to me that we have rehashed that on several occasions with no definitive outcome.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I've found that when I tried using 1 based arrays that I have created myself, I tended to forget to always have my code update the 0 element to keep track of the item count in the array, which made them pointless in those instances. Then I'd have to debug my code to find out where I made the noob mistake of doing something like that. For me, 0 based and Ubound takes care of most counting issues.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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...