Sort a list of object identifiers: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: used OUT template, simplified program, elided the use of finding the maximum width number in the OID.)
Line 442: Line 442:
This REXX version supports the new and old format   (supports a leading decimal point).
This REXX version supports the new and old format   (supports a leading decimal point).
<lang rexx>/*REXX program performs a sort of OID (Object IDentifiers ◄── used in Network data).*/
<lang rexx>/*REXX program performs a sort of OID (Object IDentifiers ◄── used in Network data).*/
$= 1.3.6.1.4.1.11.2.17.19.3.4.0.10 ,
$= 1.3.6.1.4.1.11.2.17.19.3.4.0.10 , /* ◄──┐ */
1.3.6.1.4.1.11.2.17.5.2.0.79 ,
1.3.6.1.4.1.11.2.17.5.2.0.79 , /* ◄──┤ */
1.3.6.1.4.1.11.2.17.19.3.4.0.4 ,
1.3.6.1.4.1.11.2.17.19.3.4.0.4 , /* ◄──┼─◄─ six OID numbers (as a list).*/
1.3.6.1.4.1.11150.3.4.0.1 ,
1.3.6.1.4.1.11150.3.4.0.1 , /* ◄──┤ */
1.3.6.1.4.1.11.2.17.19.3.4.0.1 ,
1.3.6.1.4.1.11.2.17.19.3.4.0.1 , /* ◄──┤ */
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0 /* ◄──┘ */
#=words($) /*#: the number of OIDs in the $ list.*/
#=words($) /*#: the number of OIDs in the $ list.*/
do i=1 for #; @.i=word($, i) /*build the @ array from each word in $*/
do i=1 for #; @.i=word($,i) /*build the @ array from each word in $*/
end /*i*/
end /*i*/
L=length(#) /*length of the number of words in $. */
L=length(#) /*length of the number of words in $.*/
a=translate($, , .); q=words(a) /*remove all the decimal points in $. */
w=0 /*W: the widest decimal number in A. */
do k=1 for q; w=max(w, length(word(a,k))) /*find width of widest number in A, */
end /*k*/ /* (after decimal points have */
/* been replaced with blanks). */
call show 'before sort ───► ' /*display the @ array before sorting.*/
call show 'before sort ───► ' /*display the @ array before sorting.*/
say copies('░', 79) /*display fence, separate before &after*/
say copies('░', 79) /*display fence, separate before &after*/
call norma 1 /*expand all the internal OID numbers. */
call norma 1; call bSort #; call norma 0 /*expand/sort/shrink the internal OID's*/
call bSort # /*invoke bubble sort to sort numbers. */
call norma 0 /*shrink all the internal OID numbers. */
call show ' after sort ───► ' /*display the @ array after sorting. */
call show ' after sort ───► ' /*display the @ array after sorting. */
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
Line 469: Line 462:
do j=1 for m; _=j+1; if @.j>@._ then parse value @.j @._ 0 with @._ @.j ok
do j=1 for m; _=j+1; if @.j>@._ then parse value @.j @._ 0 with @._ @.j ok
end /*j*/ /* [↑] swap two out─of─order elements.*/
end /*j*/ /* [↑] swap two out─of─order elements.*/
end /*m*/; return
end /*m*/; return /* [↑] use a simple bubble sort. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
norma: arg LZ; do j=1 for #; x=@.j; y=left(.,left(x,1)==.) /*construct Y version.*/
norma: arg LZ; do j=1 for #; x=translate(@.j, , .); y= /*construct X version. */
if y==. then x=substr(x, 2) /*X have leading dot?*/
do k=1 for words(x); _=word(x, k) /*get a number in X. */
do while x\==''; parse var x z "." x /*extract the number. */
if LZ then y=y right(_,30,0); else y=y _+0 /*add/elide leading 0's*/
if LZ then y=y || right(z, w, 0). /*added leading 0's ?*/
end /*k*/ /*adjust number, append*/
else y=y || z+0 || . /*elide " " */
@.j = translate( space(y), ., ' ') /*reconstitute number. */
end /*while*/ /* [↑] Y is now adj.*/
end /*j*/ /*LZ: Leading Zero(es).*/
@.j=strip(y, 'T', .) /*use new Y version.*/
return /*── ─ ─ */
end /*j*/ /*LZ: Leading Zero(es)*/
return /*── ─ ─ */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do s=1 for #; say right("OID number",20) right(s,L) arg(1) @.s; end; return</lang>
show: do a=1 for #; say right("OID number",20) right(a,L) arg(1) @.a; end; return</lang>
'''output''' &nbsp; when using the (internal) default numbers:
{{out|output|text=&nbsp; when using the (internal) default input:}}
<pre>
<pre>
OID number 1 before sort ───► 1.3.6.1.4.1.11.2.17.19.3.4.0.10
OID number 1 before sort ───► 1.3.6.1.4.1.11.2.17.19.3.4.0.10