Page 1 of 1 1
Topic Options
#74633 - 2003-04-22 10:10 AM Making an array
zdarma Offline
Getting the hang of it

Registered: 2002-10-08
Posts: 83
Hello to one and all,

I'm not the most gifted code writer in the world, some of you may have already noticed from some of the previous scripts I've posted, but I do give it the old college try.

I'm trying to make the following script work with an Array. But the only thing I can ever get it to print out is the number of elements in the array (not the elements itself, but the actual number of elements I've declared in the the Dimensioning of the Array).

I've looked at some examples to do it, but I'm not sure how to get it to work correctly and the manual's helpfile example is about as useful as a box of matches in the middle of a windstorm.

In the example below, I'd like to be able to read from an array and ping the various machines. For a variety of reasons I've had to replace IP addresses with "machine1, machine2, etc."

The "$scf" found in the script would eventually be replaced by the Array variable.

Thanks in advance for any and all help to one and all.

Z.

code:
;******************************** Déclaration des vairables  *********************************
;
GLOBAL
$ddl,$ddl1,$bdl1,$bdl2,$ping_file,$ping,$reponse,$scf,$scf2
;
$scf="machine1"
$scf="machine2"
$scf2="machine3"
$ddl="machine4"
$bdl1="machine5"
$bdl2="machine6"
$ping_file=c:\temp\ping.txt ;fichier de sauvegarde pour les pings
$ping="" ;variable de la ligne en cours de lecture
;
;*************************************** Vérification des réponses *************************
;
CLS
? "Pinging the $scf, please wait a moment"
shell '%comspec% /c "ping $scf >> $ping_file"'
IF Open (1,$ping_file)=0
$ping=ReadLine(1)
WHILE @ERROR = 0
SELECT
CASE INSTR($ping,"de la")
$reponse = "The $scf down"
CASE INSTR($ping,"TTL")
$reponse = "The $scf is Alive"
ENDSELECT
$ping=ReadLine(1)
LOOP
$nul=close(1)
del $ping_file
ENDIF
? $reponse


Top
#74634 - 2003-04-22 10:20 AM Re: Making an array
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
could you give out what you have tried.

in this script I can make zillion arrays but would like to see what you are really after.

so, please post the code that is not working.
_________________________
!

download KiXnet

Top
#74635 - 2003-04-22 10:30 AM Re: Making an array
zdarma Offline
Getting the hang of it

Registered: 2002-10-08
Posts: 83
Lorenko,

This is what I have tried in a previous example :

code:
DIM $Array(0)		
DIM $ddl,$ddl1,$bdl1,$bdl2,$scf,$scf2
$z=7
ReDIM $array($z)
CLS
? "Pinging the $Array($z), please wait a moment"
shell '%comspec% /c "ping $Array($z) >> $ping_file"'

The script basically stays the same as in the original example I gave below. The lines come just after the Remarked line of "Vérification des réponses "

Top
#74636 - 2003-04-22 10:44 AM Re: Making an array
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
first: arrays have [] not ()

second, you can't use arrays inside strings.
_________________________
!

download KiXnet

Top
#74637 - 2003-04-22 11:05 AM Re: Making an array
zdarma Offline
Getting the hang of it

Registered: 2002-10-08
Posts: 83
? "Pinging the $Array[$z], please wait a moment".

I've modified the () for [] and have removed the above mentioned line from my script, but I still can't make it do what I want it to do.

Is what I want to do, possible at all?

Thx Z.

Top
#74638 - 2003-04-22 11:16 AM Re: Making an array
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
mm...
it seems that you want to ping all the elements.
thus you need to loop trough the elements one by one.

$array=1,2,3
for each $number in $array
$number ?
next
_________________________
!

download KiXnet

Top
#74639 - 2003-04-22 11:30 AM Re: Making an array
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Z,

as a short intermission :

this

quote:
? "Pinging the $Array[$z], please wait a moment".

won't work because of this :

quote:
Note: Unlike regular variables, arrays can not be used inside strings and can also not be assigned a value on the command line.

_________________________



Top
#74640 - 2003-04-22 11:34 AM Re: Making an array
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I think the actual not work is that he has no array defined.
only the size but nothing inside it.

nor he is referencing the elements but tries to reference the whole array.
_________________________
!

download KiXnet

Top
#74641 - 2003-04-22 11:35 AM Re: Making an array
zdarma Offline
Getting the hang of it

Registered: 2002-10-08
Posts: 83
Lorenko thanks for your 4 little lines of advise. From those little lines I was able to do what I wanted my script to do.

I've gone with the following

code:
$array=$machine1,$machine2,$machine3,$machine4
FOR EACH $element in $array
? "Pinging $element, please wait a moment"
shell '%comspec% /c "ping $element >> $ping_file"'
...
...
...
NEXT

But, I'm still trying to compare your example with the one in the manual and I still don't trully "get" the one in the manual.

Thanks again,
Z.

[ 22. April 2003, 11:57: Message edited by: zdarma ]

Top
#74642 - 2003-04-23 12:19 AM Re: Making an array
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
$array='machine1','machine2','machine3','machine4'
for $x=0 to ubound($array)
? 'pinging '+$array[$x]+', please wait a moment'
shell '%comspec% /c ping '+$Array[$x]+' >> '+$ping_file
next

[ 22. April 2003, 12:19: Message edited by: Radimus ]
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#74643 - 2003-04-22 01:12 PM Re: Making an array
zdarma Offline
Getting the hang of it

Registered: 2002-10-08
Posts: 83
Radimus thanks for your example, I adapted it for my needs and it works as well.

I was just wondering if anyone can tell me if the way Lorenko or Radimus showed me how to make the array, if one was better than the other. I noticed a couple of differences in the writting, but I'm not sure if one is better than the other.

Thx,
Z.

Top
#74644 - 2003-04-22 01:55 PM Re: Making an array
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
they are basically just the same.

for each does the counting inside whereas with for to, you need to reference the element by yourself.

with simple things there is not so much difference...
whereas with more complex code you may need to use either one to make it work as you want.
_________________________
!

download KiXnet

Top
#74645 - 2003-04-22 03:48 PM Re: Making an array
zdarma Offline
Getting the hang of it

Registered: 2002-10-08
Posts: 83
Thanks for the info Lorenko.

I think I'm starting to get it; but until I'm sure, I'll keep trying.

Z.

Top
#74646 - 2003-04-22 04:13 PM Re: Making an array
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Please take a look at the UDF Forum under WSHPing() - WSH ping routine... no temp files and Ping() - checks for reply , or returns ip-address of remote host
code:
$array='machine1, machine2'
for each $comp in $array
if not ping($comp)
? 'The computer '+$comp+' is not PINGable.'
endif
next

_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


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

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.057 seconds in which 0.02 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