Jump to content
Sign in to follow this  
CyberSlug

UDF to create an array using list notation!

Recommended Posts

There's nothing especially insightful about the function, but it might be useful to you. Watch out for word wrap. Array starts at index zero.

Question: Is there a limit on the number of paramters a function can take?

;;; Example usage;;;

Dim $primes = list(2, 3, 5, 7, 11)
For $i = 0 to Ubound($primes)-1
    MsgBox(4096, "info", $primes[$i])
Next


;;; The User-defined function;;;

Func list($x0, $x1=0, $x2=0, $x3=0, $x4=0, $x5=0, $x6=0, $x7=0, $x8=0, $x9=0, $x10=0, $x11=0, $x12=0, $x13=0, $x14=0, $x15=0, $x16=0, $x17=0, $x18=0, $x19=0, $x20=0, $x21=0, $x22=0, $x23=0, $x24=0, $x25=0, $x26=0, $x27=0, $x28=0, $x29=0, $x30=0, $x31=0, $x32=0, $x33=0, $x34=0, $x35=0, $x36=0, $x37=0, $x38=0, $x39=0, $x40=0, $x41=0, $x42=0, $x43=0, $x44=0, $x45=0, $x46=0, $x47=0, $x48=0, $x49=0, $x50=0, $x51=0, $x52=0, $x53=0, $x54=0, $x55=0, $x56=0, $x57=0, $x58=0, $x59=0, $x60=0, $x61=0, $x62=0, $x63=0, $x64=0, $x65=0, $x66=0, $x67=0, $x68=0, $x69=0, $x70=0, $x71=0, $x72=0, $x73=0, $x74=0, $x75=0, $x76=0, $x77=0, $x78=0, $x79=0, $x80=0, $x81=0, $x82=0, $x83=0, $x84=0, $x85=0, $x86=0, $x87=0, $x88=0, $x89=0, $x90=0, $x91=0, $x92=0, $x93=0, $x94=0, $x95=0, $x96=0, $x97=0, $x98=0, $x99=0, $x100=0, $x101=0, $x102=0, $x103=0, $x104=0, $x105=0, $x106=0, $x107=0, $x108=0, $x109=0, $x110=0, $x111=0, $x112=0, $x113=0, $x114=0, $x115=0, $x116=0, $x117=0, $x118=0, $x119=0, $x120=0, $x121=0, $x122=0, $x123=0, $x124=0, $x125=0, $x126=0, $x127=0, $x128=0, $x129=0, $x130=0, $x131=0, $x132=0, $x133=0, $x134=0, $x135=0, $x136=0, $x137=0, $x138=0, $x139=0, $x140=0, $x141=0, $x142=0, $x143=0, $x144=0, $x145=0, $x146=0, $x147=0, $x148=0, $x149=0, $x150=0, $x151=0, $x152=0, $x153=0, $x154=0, $x155=0, $x156=0, $x157=0, $x158=0, $x159=0, $x160=0, $x161=0, $x162=0, $x163=0, $x164=0, $x165=0, $x166=0, $x167=0, $x168=0, $x169=0, $x170=0, $x171=0, $x172=0, $x173=0, $x174=0, $x175=0, $x176=0, $x177=0, $x178=0, $x179=0, $x180=0, $x181=0, $x182=0, $x183=0, $x184=0, $x185=0, $x186=0, $x187=0, $x188=0, $x189=0, $x190=0, $x191=0, $x192=0, $x193=0, $x194=0, $x195=0, $x196=0, $x197=0, $x198=0, $x199=0, $x200=0, $x201=0, $x202=0, $x203=0, $x204=0, $x205=0, $x206=0, $x207=0, $x208=0, $x209=0, $x210=0, $x211=0, $x212=0, $x213=0, $x214=0, $x215=0, $x216=0, $x217=0, $x218=0, $x219=0, $x220=0, $x221=0, $x222=0, $x223=0, $x224=0, $x225=0, $x226=0, $x227=0, $x228=0, $x229=0, $x230=0, $x231=0, $x232=0, $x233=0, $x234=0, $x235=0, $x236=0, $x237=0, $x238=0, $x239=0, $x240=0, $x241=0, $x242=0, $x243=0, $x244=0, $x245=0, $x246=0, $x247=0, $x248=0, $x249=0, $x250=0, $x251=0, $x252=0, $x253=0, $x254=0, $x255=0)
    Local $array[@numParams]
    Local $i
    For $i = 0 to @NumParams -1
        $array[$i] = Eval("x" & $i)
    Next
    Return $array
EndFunc

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

lol all the parameters are longer than the actual function


[font="Times"] If anyone remembers me, I am back. Maybe to stay, maybe not.----------------------------------------------------------------------------------------------------------[/font][font="Times"]Things I am proud of: Pong! in AutoIt | SearchbarMy website: F.R.I.E.S.A little website that is trying to get started: http://thepiratelounge.net/ (not mine)[/font][font="Times"] ----------------------------------------------------------------------------------------------------------[/font][font="Arial"]The newbies need to stop stealing avatars!!! It is confusing!![/font]

Share this post


Link to post
Share on other sites

See, this is where it would be handy for functions just to accept an unlimited amount of parameters, and then have the function create an array that you can access the params with.

ie:

Func Stuff()
    If @NumParams > 0 Then
        For $i = 0 to @NumParams
            MsgBox(0, 'Param ' & $i, $FuncParams[$i])
        Next
    EndIf
EndFunc

Stuff('Hello', 'World')

Or even if you put the number of parameters passed into the $FuncParams variable.

It could work just like the $CommandLine variable.

Edited by Saunders

Share this post


Link to post
Share on other sites

There's nothing especially insightful about the function, but it might be useful to you.  Watch out for word wrap.  Array starts at index zero.

<{POST_SNAPBACK}>

:lmao: this looks alot like _ArrayCreate() included in the installer...

only it currently has a limit of 21 params.... o:)


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

Share this post


Link to post
Share on other sites

See, this is where it would be handy for functions just to accept an unlimited amount of parameters, and then have the function create an array that you can access the params with.

ie:

Func Stuff()
    If @NumParams > 0 Then
        For $i = 0 to @NumParams
            MsgBox(0, 'Param ' & $i, $FuncParams[$i])
        Next
    EndIf
EndFunc

Stuff('Hello', 'World')

Or even if you put the number of parameters passed into the $FuncParams variable.

It could work just like the $CommandLine variable.

<{POST_SNAPBACK}>

This one of the myriad things about C that I do not like and much prefer the C++ method. In this case, having the C notation for passing an unknown number of parameters would be poweful and useful, however, in virtually every other situation it adds an unnecessary level of confusion. The actual number of parameters should be known at design/compile time. There are rare times where the other notation would be useful, yes, but I don't think its worth the added complexity for a small usage case.

I also think people would mis-use it. I see people using Call("Function") instead of invoking Function() directly so I can't imagine what kind of stupid code we'd get to see if the new notation were added.

And for an example... find your nearest C\C++ programmer(s). Ask them how many times they've had an app crash because they forgot to pass a parameter to a printf() style function. Then ask them how many times they've had an app crash because they forgot to pass something to a function taking optional parameters (The C++ way). Anybody who says 0 to the former is lying, has a bad memory, or doesn't use C. The C++ programmer may not have gotten the results they expected when they forgot the parameter, but I bet the app didn't crash, either.

Share this post


Link to post
Share on other sites

@Valik

Yes, I must admit that aside from a function like the one being discussed here, I can't think of a practical use for implementing something like this in the code. But perhaps there could be a built-in function that does what CyberSlug's does? It would be a little cleaner than having to include an outside UDF if nothing else.

Of course, then we get into the whole problem with nested arrays again...

Share this post


Link to post
Share on other sites

@Valik

Yes, I must admit that aside from a function like the one being discussed here, I can't think of a practical use for implementing something like this in the code. But perhaps there could be a built-in function that does what CyberSlug's does? It would be a little cleaner than having to include an outside UDF if nothing else.

Of course, then we get into the whole problem with nested arrays again...

<{POST_SNAPBACK}>

I'd rather see a "proper" method for assigning to arrays directly, myself. Preferably the C\C++ way as thats what I'm used to.

Share this post


Link to post
Share on other sites
Guest Guidosoft

@Cyberslug........

You crazy. :lmao: lol jk.

Did you write a AutoIt script to write all those params?

Oh, and I know a func just like that built right into AutoIt. Observe:

$MyArray = StringSplit("Item 1, Item 2, Item 3, Roach, Pe pe, Yummy, mmmm", ",")

I think that jon should do like Saunders suggested and make it so it can handle unlimited params.

Edited by Guidosoft

Share this post


Link to post
Share on other sites

I'd rather see a "proper" method for assigning to arrays directly, myself.  Preferably the C\C++ way as thats what I'm used to.

<{POST_SNAPBACK}>

Just out of curiousity, could you give me an example of the method you're used to? I assume that it's not like the method that AutoIt is currently using. And does C/C++ support nested arrays? Or just bi-dimensional ones like AutoIt?

Share this post


Link to post
Share on other sites

Did you write a AutoIt script to write all those params?

$t = ""
For $i = 1 to 255
    $t = $t & ", $x" & $i & "=0"
Next
ClipPut($t)

Oh, and I know a func just like that built right into AutoIt. Observe:

$MyArray = StringSplit("Item 1, Item 2, Item 3, Roach, Pe pe, Yummy, mmmm", ",")

Yep, though the first array element is at one instead of zero. I wrote the StringSplit example :lmao:

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

Just out of curiousity, could you give me an example of the method you're used to? I assume that it's not like the method that AutoIt is currently using. And does C/C++ support nested arrays? Or just bi-dimensional ones like AutoIt?

<{POST_SNAPBACK}>

int array1[5] = { 1, 2, 3, 4, 5 };
int array2[5][2] = { {0,1}, {2,3}, {4,5}, {6,7}, {8,9} };

And if you're really adventurous...

int array3[5][2][3] = 
    {
        { {0,1,2}, {3,4,5} },
        { {6,7,8}, {9,10,11} },
        { {12,13,14}, {15,16,17} },
        { {18,19,20}, {21,22,23} },
        { {24,25,26}, {27,28,20} }
    };

Share this post


Link to post
Share on other sites

Hmm.. yeah I could go for syntax like that in AutoIt. You can still define items individually too though right?

Dim $Array[5] = { 'Apples', 'Oranges', 'Oops!', 'Pears', 'Avocados' }

$Array[2] = 'Grapes'

Something like that? What happens if you define array to be 5 items, and then only fill in 4? Just leave the last one empty? Could you declare a completely empty array, just so it could be filled afterwards by another function?

Also, I guess nested is out of the question... Unless you did something like:

Dim $MyArray[3] = { 'One', 'Two', 'Three' }
Dim $HerArray[3] = { 'Something', 'Else', $MyArray }

But then you're just getting confusing.

Share this post


Link to post
Share on other sites

yes, you can have empty arrays in c++.

i wouldn't really like c++ syntax in autoit.

edit: your thing about nested arrays bugs me

Edited by Xenogis

[font="Times"] If anyone remembers me, I am back. Maybe to stay, maybe not.----------------------------------------------------------------------------------------------------------[/font][font="Times"]Things I am proud of: Pong! in AutoIt | SearchbarMy website: F.R.I.E.S.A little website that is trying to get started: http://thepiratelounge.net/ (not mine)[/font][font="Times"] ----------------------------------------------------------------------------------------------------------[/font][font="Arial"]The newbies need to stop stealing avatars!!! It is confusing!![/font]

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...