Page 1 of 1 1
Topic Options
#31181 - 2002-10-22 11:58 AM Array behaviour
Breaker Offline
Hey THIS is FUN
*****

Registered: 2001-06-15
Posts: 268
Loc: Yorkshire, England
I've never done a great deal of work with arrays before, and certainly not multi-dimensional ones, but I seem to have hit a slight limitation.

Basically, what I want to do is this:

code:
$x = 1,2,3
Dim $y
for $count = 0 to ubound($x)
$y[$count] = $x[$count]
next

As I understand it, this should be the same as

code:
$x = 1,2,3
Dim $y
$y = $x

But for some reason, only the second example works. Even avoiding the For...Next construct and just trying...

code:
$x = 1,2,3
Dim $y
$y[0] = $x[0]

...fails. Is this because I'm not declaring the size of the $y array when I Dim it? Should I try something like...

code:
$x = 1,2,3
Dim $y[ubound($x)]

...to get around this? Any advice would be appreciated. Later on in my process, I will want to assign the resultant $y array to an element of the $z array, which will become a series of $y's. Is this going to fail horribly? Can anyone suggest another way of doing this? Or will I need to do something entirely different to start working with multi-dimensional arrays?

Thanks in advance,

Breaker
_________________________
================================================
Breaker


Top
#31182 - 2002-10-23 12:08 AM Re: Array behaviour
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
eh...

the basic question is that you are pointing to:
$y[0]

when you have dimmed it as undeclared.
so it is null and it's not array.

when you directly use it:
$y

you declare it when you put something inside it.

so, if you want to pass some array in there:
$y=$myarray

or on multidim arrays:
$y=$myarray[5]

there is nothing more to it, but to keep keep up in your mind what kix sees.

when you don't say array has 5 values, how you can point to first ($y[0]) element when you haven't said that it even has any elements...

I hope this makes it simpler, as my mumble is starting to look really bad on the screen...
_________________________
!

download KiXnet

Top
#31183 - 2002-10-23 12:28 AM Re: Array behaviour
Breaker Offline
Hey THIS is FUN
*****

Registered: 2001-06-15
Posts: 268
Loc: Yorkshire, England
Lonkero... thanks. I understand that part (I think) but then looking at my actual code, I'm not sure why its failing. I'd better post the actual code I've got. Basically it's using srvcheck.exe to generate a temp file listing shares and permissions on those shares for a server, and then parsing that file to give me a listing of those share details.

The file is generated in the format

quote:
\\server\share1
[tab][tab]BUILTIN\Administrators [tab] Full Control
[tab][tab]DOMAIN\Usergroup [tab] Change

\\server\share2
[tab][tab]BUILTIN\Administrators [tab] Full Control
[tab][tab]DOMAIN\Usergroup1 [tab] Change
[tab][tab]DOMAIN\Usergroup2 [tab] Read

\\server\share3 etc....

So my code, reading line by line, first checks whether I am reading the details for a new share, a new ACE, or the blank line in between shares.

Because I don't know how many shares, or permissions entries for each share I will have, I'm trying to build the results into an array of strings like this:

code:
$Share[0] = "\\server\share1"
$Share[1] = "BUILTIN\Administrators"
$Share[2] = "Full Control"
$Share[3] = "DOMAIN\Usergroup"
$Share[4] = "Change"

And then build these arrays, which will all be of different sizes, into a larger array called $Shares, where each element will represent a share on the server.

The code I'm currently working with is...

code:
$shellstr = "cmd.exe /c srvcheck.exe \\" + $computer + " > c:\shares.txt"
? $shellstr
Shell $shellstr
$x = Open(1,"c:\shares.txt")
$x = Readline(1)
$ShareCount = 0
While @ERROR = 0
$PermsCount = 0
If Instr($x,"\\")
$ShareCount = $ShareCount + 1
$Share[$PermsCount] = $x
$PermsCount = $PermsCount + 1
$x = Readline(1)
Endif
While Instr($x," ")
$x = Right($x,(Len($x)-2))
$y = Split($x," ")
$Share[$PermsCount] = $y[0]
$PermsCount = $PermsCount + 1
$Share[PermsCount] = $y[1]
$PermsCount = $PermsCount + 1
$x = Readline(1)
Loop
If $x = ""
$x = Readline(1)
Endif
If $ShareCount
$Shares[$ShareCount - 1] = $Share
Endif
Loop
$x = Close(1)

It falls over at the line $Share[$PermsCount] = $y[0]. I suppose this is because when I first use $y, $PermsCount is 0. What should I do to get around this - I don't see how I can declare the size of $Share in advance?

Thanks for the quick response,

Breaker

[ 22. October 2002, 12:28: Message edited by: Breaker ]
_________________________
================================================
Breaker


Top
#31184 - 2002-10-23 12:46 AM Re: Array behaviour
Breaker Offline
Hey THIS is FUN
*****

Registered: 2001-06-15
Posts: 268
Loc: Yorkshire, England
Hmm... just discovered Redim Preserve in the manual - terrible habit of mine, leaving the manual as a last resort - and I haven't looked at it for a while now...

So I redimension the $share array before I add another element to it and the routine seems to work OK. Now I want to turn this routine into a function, something like SharePerms($computer) which would return this array of arrays.

Can I then iterate through this larger array like this?

code:
For Each $Share In $SharePerms
For Each $element In $Share
? $element
Next
?
Next

_________________________
================================================
Breaker


Top
#31185 - 2002-10-22 01:44 PM Re: Array behaviour
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
well, yes.

I do it differently (in checker), but only diff is that I know the array size beforehand so can point to parts directly...

it should work like a charm.
_________________________
!

download KiXnet

Top
#31186 - 2002-10-22 03:20 PM Re: Array behaviour
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You might also want to take a look at SPLIT to split the TABbed lines into an array automatically and maybe even create a two-dimensional array like DIM $x[5,10]
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 363 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.054 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org