Как сделать Natural Sort в классическом ASP? [Дубликат]

Это хорошо документированное поведение PHP. См. предупреждение на странице foreach php.net

Warning

Ссылка на $ value , а последний элемент массива остается даже после цикла foreach . Рекомендуется уничтожить его unset ().

$a = array('a', 'b', 'c', 'd');

foreach ($a as &$v) { }
unset($v);
foreach ($a as $v) { }

print_r($a);

EDIT

Попытайтесь пошаговое руководство к тому, что на самом деле происходит здесь

$a = array('a', 'b', 'c', 'd');
foreach ($a as &$v) { }   // 1st iteration $v is a reference to $a[0] ('a')
foreach ($a as &$v) { }   // 2nd iteration $v is a reference to $a[1] ('b')
foreach ($a as &$v) { }   // 3rd iteration $v is a reference to $a[2] ('c')
foreach ($a as &$v) { }   // 4th iteration $v is a reference to $a[3] ('d')

                          // At the end of the foreach loop,
                          //    $v is still a reference to $a[3] ('d')

foreach ($a as $v) { }    // 1st iteration $v (still a reference to $a[3]) 
                          //    is set to a value of $a[0] ('a').
                          //    Because it is a reference to $a[3], 
                          //    it sets $a[3] to 'a'.
foreach ($a as $v) { }    // 2nd iteration $v (still a reference to $a[3]) 
                          //    is set to a value of $a[1] ('b').
                          //    Because it is a reference to $a[3], 
                          //    it sets $a[3] to 'b'.
foreach ($a as $v) { }    // 3rd iteration $v (still a reference to $a[3]) 
                          //    is set to a value of $a[2] ('c').
                          //    Because it is a reference to $a[3], 
                          //    it sets $a[3] to 'c'.
foreach ($a as $v) { }    // 4th iteration $v (still a reference to $a[3]) 
                          //    is set to a value of $a[3] ('c' since 
                          //       the last iteration).
                          //    Because it is a reference to $a[3], 
                          //    it sets $a[3] to 'c'.

1
задан J_Coder 21 June 2016 в 01:44
поделиться

1 ответ

Для теории см. здесь (следуйте ссылкам!). Практическая демонстрация

recordset:

Option Explicit

Const adInteger          =          3 ' 00000003
Const adVarChar          =        200 ' 000000C8

Dim sInp : sInp = "Z1,Z3,Z2,Z20,Z10"
Dim aInp : aInp = Split(sInp, ",")
WScript.Echo "A:", Join(aInp)

Dim oRS : Set oRS = CreateObject("ADODB.Recordset")
oRS.Fields.Append "Word", adVarChar, 50
oRS.Fields.Append "Length", adInteger
oRS.Open
Dim sWord
For Each sWord In aInp
    oRS.AddNew
    oRS.Fields("Word").value = Left(sWord, 1)
    oRS.Fields("Length").value = CInt(Mid(sWord, 2))
    oRS.UpDate
Next
oRS.Sort = "Word, Length"

Dim aTable : aTable = oRS.GetRows()
ReDim aOut(UBound(aTable, 2))
Dim i
For i = 0 To UBound(aOut)
    aOut(i) = aTable(0, i) & aTable(1, i)
Next
WScript.Echo "B:", Join(aOut)

ArrayList

Option Explicit

Dim sInp : sInp = "Z1,Z3,Z2,Z20,Z10,E1,D3,C2,B20,A10"
Dim aInp : aInp = Split(sInp, ",")
WScript.Echo "A:", Join(aInp)

Dim oNAL : Set oNAL = CreateObject( "System.Collections.ArrayList" )
Dim oSB  : Set oSB  = CreateObject( "System.Text.StringBuilder" )
Dim sWord
For Each sWord In aInp
    oSB.AppendFormat_2 "{0}{1,4}", Left(sWord, 1), CInt(Mid(sWord, 2))
    sWord = oSB.ToString()
    oSB.Length = 0
    oNAL.Add sWord
Next
oNAL.Sort

ReDim aOut(oNAL.Count - 1)
Dim i
For i = 0 To UBound(aOut)
    aOut(i) = Left(oNAL(i), 1) & CInt(Mid(oNAL(i), 2))
Next
WScript.Echo "B:", Join(aOut)
4
ответ дан Community 16 August 2018 в 14:35
поделиться
Другие вопросы по тегам:

Похожие вопросы: