Page 1 of 1 1
Topic Options
#126768 - 2004-09-16 01:33 PM Sort inifile
Hande Offline
Fresh Scripter

Registered: 2002-01-11
Posts: 17
Loc: Finland
Hi,

I have created inifile which I can see servers uptime.
Is it possible to create new inifile which is sorted that biggest uptime value is first second biggest second so on??

Part of my inifile
[Server1]
IP=193.198.193.1
Uptime=4
Desc=Server1
[Server2]
IP=193.198.193.2
Uptime=9
Desc=Server2
[Server3]
IP=193.198.193.3
Uptime=23
Desc=Server3
[Server4]
IP=193.198.193.4
Uptime=17
Desc=Server4
[Server5]
IP=193.198.193.5
Uptime=12
Desc=Server5
[Server6]
IP=193.198.193.6
Uptime=27
Desc=Server6

And new inifile should look like this:
[Server6]
IP=193.198.193.6
Uptime=27
Desc=Server6
[Server3]
IP=193.198.193.3
Uptime=23
Desc=Server3
[Server4]
IP=193.198.193.4
Uptime=17
Desc=Server4
[Server5]
IP=193.198.193.5
Uptime=12
Desc=Server5
[Server2]
IP=193.198.193.2
Uptime=9
Desc=Server2
[Server1]
IP=193.198.193.1
Uptime=4
Desc=Server1

Hande

Top
#126769 - 2004-09-16 01:42 PM Re: Sort inifile
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Yeah, you could; Read the ini file into an array, sort the array with one of the excellent udfs we got here for that task, and write the sorted array to a new ini-file ...

But, why would you want to do this ? You want to log to an ini file ??
Please explain
_________________________



Top
#126770 - 2004-09-16 01:54 PM Re: Sort inifile
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I can't see any reason for this.
ini-files are not ment to be wise.
and their sections don't have any real meaning. they just are.

if you really wanted to log the stuff by uptime, you should have the section name being the uptime and value and it's data be the server stuff.
so, instead of:
Quote:

[Server1]
IP=193.198.193.1
Uptime=4
Desc=Server1




go with something like:
[4]
Server1=193.198.193.1
_________________________
!

download KiXnet

Top
#126771 - 2004-09-16 02:14 PM Re: Sort inifile
Hande Offline
Fresh Scripter

Registered: 2002-01-11
Posts: 17
Loc: Finland
Quote:

Yeah, you could; Read the ini file into an array, sort the array with one of the excellent udfs we got here for that task, and write the sorted array to a new ini-file ...

But, why would you want to do this ? You want to log to an ini file ??
Please explain




Just For fun
I have 41 different servers which uptime I want to see.
I've modified this script
http://helpdesk.kixtart.org/Scripts/Uptime.asp
(There you can see that redline and days which are readed in inifile)
And just for fun I want to sort these lines.

-Hande

Top
#126772 - 2004-09-16 02:15 PM Re: Sort inifile
Hande Offline
Fresh Scripter

Registered: 2002-01-11
Posts: 17
Loc: Finland
Quote:

I can't see any reason for this.
ini-files are not ment to be wise.
and their sections don't have any real meaning. they just are.

if you really wanted to log the stuff by uptime, you should have the section name being the uptime and value and it's data be the server stuff.
so, instead of:
Quote:

[Server1]
IP=193.198.193.1
Uptime=4
Desc=Server1




go with something like:
[4]
Server1=193.198.193.1




I can't do that because different servers may have same uptime value.
So if the uptime values are same they are overwrited.

-Hande

Top
#126773 - 2004-09-16 02:28 PM Re: Sort inifile
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
no they aren't.
if you use proper ini-functions like writeprofilestring(), the data is appended, not overwritten.
_________________________
!

download KiXnet

Top
#126774 - 2004-09-17 09:59 AM Re: Sort inifile
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

Yeah, you could; Read the ini file into an array, sort the array with one of the excellent udfs we got here for that task, and write the sorted array to a new ini-file ...




Zero fill the "uptime" field to a suitable length, append the other data and add to an array. Sort the array then split the data out and use directly - no need to change the order in the ini file.

Top
#126775 - 2004-09-17 11:35 AM Re: Sort inifile
Hande Offline
Fresh Scripter

Registered: 2002-01-11
Posts: 17
Loc: Finland
Quote:

Zero fill the "uptime" field to a suitable length, append the other data and add to an array. Sort the array then split the data out and use directly - no need to change the order in the ini file.




Could you give me some example?
I've got no idea where to start...

Top
#126776 - 2004-09-17 12:06 PM Re: Sort inifile
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Ok, here is an example.

This script takes your INI file and displays the servers in descending uptime order.

The output using your example file looks like this:
Server Description  IP Address        Up Time
------------------ ---------- -------
Server6 193.198.193.6 27
Server3 193.198.193.3 23
Server4 193.198.193.4 17
Server5 193.198.193.5 12
Server2 193.198.193.2 9
Server1 193.198.193.1 4


Code:
Break ON

$=SetOption("Explicit","ON")

Dim $sIni,$asData
Dim $sSection,$sKey,$sDelimiter
Dim $iIndex,$sSpaces30

$sSpaces30=" "
$sIni=".\uptime.ini"
$sDelimiter="|@@=@@|"
$iIndex=0

For Each $sSection in Split(ReadProfileString($sIni,"",""),Chr(10))
If $sSection
Redim Preserve $asData[$iIndex]
$asData[$iIndex]=
Right($sSpaces30+ReadProfileString($sIni,$sSection,"Uptime"),10)
+$sDelimiter
+ReadProfileString($sIni,$sSection,"Desc")
+$sDelimiter
+ReadProfileString($sIni,$sSection,"IP")
$iIndex=$iIndex+1
EndIf
Next
; Array loaded, now sort it.
$asData=QS($asData)
Left("Server Description"+$sSpaces30,20)+Left("IP Address"+$sSpaces30,15)+Right($sSpaces30+"Up Time",10)+@CRLF
Left("------------------"+$sSpaces30,20)+Left("----------"+$sSpaces30,15)+Right($sSpaces30+"-------",10)+@CRLF
; Now display in sorted order.
While $iIndex
$iIndex=$iIndex-1
$sSection=Split($asData[$iIndex],$sDelimiter)
Left($sSection[1]+$sSpaces30,20)
+Left($sSection[2]+$sSpaces30,15)
+Right($sSpaces30+$sSection[0],10)+@CRLF
Loop

Exit 0
; BrianTX's Quicksort
function qs($a)
DIM $b[32],$c[32],$d,$e,$f,$g,$h,$i,$j,$k,$l
$b[0]=0
$c[0]=UBOUND($a)
$d=0
While $d >=0
$e=$b[$d]
$f=$c[$d]
While $e < $f
$h=$e+($f-$e)/2
$k=$a[$e]
$A[$e]=$A[$h]
$A[$h]=$k
$i=$e+1
$j=$f
$l=0
Do
While ($i<$j) AND $A[$e] > $A[$i]
$i=$i+1
Loop
While ($j>=$i) AND $A[$j] > $A[$e]
$j=$j-1
Loop
IF $i>=$j
$l=1
ELSE
$k=$A[$i]
$A[$i]=$A[$j]
$A[$j]=$k
$j=$j-1
$i=$i+1
ENDIF
Until $l=1
$k=$a[$e]
$a[$e]=$a[$j]
$a[$j]=$k
$g=$j
If $g-$e <= $f - $g
If $g+1 < $f
$b[$d]=$g+1
$c[$d]=$f
$d=$d+1
Endif
$f=$g-1
Else
If $g-1 > $e
$b[$d]=$e
$c[$d]=$g-1
$d=$d+1
Endif
$e=$g+1
Endif
Loop
$d=$d-1
Loop
$qs=$a
Endfunction


Top
#126777 - 2004-09-17 01:40 PM Re: Sort inifile
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
still way too complex.
is your ini-file used for something else than this?
_________________________
!

download KiXnet

Top
#126778 - 2004-09-17 03:47 PM Re: Sort inifile
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

still way too complex.




Not really. The only complicated bit is the QS() quick sort routine, which is a black box UDF.

Top
#126779 - 2004-09-17 03:54 PM Re: Sort inifile
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
well, if he would put his ini with the uptime-sections, he would not need to sort it at all.
_________________________
!

download KiXnet

Top
#126780 - 2004-09-17 04:00 PM Re: Sort inifile
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
He would still need to sort it.

If you check the link he posted earlier you will see what he wants.

The INI file is used to generate a simple HTML page with a bar graph of uptime.

He wants the output sorted so that the machines are reported with the highest up time at the top, and the shortest at the bottom of the display.

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
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.072 seconds in which 0.034 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