Sign in to follow this  
Followers 0
leuce

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

6 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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

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

Jos :mellow:


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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!"

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

As usual, the answer is VERY SIMPLE, thanks. The 2 should be a 0.

Share this post


Link to post
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
Sign in to follow this  
Followers 0