anonymous7
Знающий
- Сообщения
- 40
- Репутация
- 8
Как записать 2D массив в файл и желательно прочитать его из файла в массив 2D.
#include <array.au3>
;Запись в ini
Dim $items[4][2]=[[4,2],[1,"text1"],[2,"text2"],[3,"text3"]]
_ArrayDisplay($items,"$items")
$Fname3="massiv.ini"
For $i=1 to $items[0][0]-1
For $j=0 to $items[0][1]-1
IniWrite($Fname3, "", "$items" & $i & "-" & $J, $Items[$i][$j])
Next
next
IniWrite($Fname3, "", "$Items" & $i+1, "exit")
;Вывод из ini
Dim $items2[1][2]
$items2[0][0] = IniRead($Fname3, "", "$items" & "0" & "-" & "0","")
$items2[0][1] = IniRead($Fname3, "", "$items" & "0" & "-" & "1","")
redim $items2[$items2[0][0]][$items2[0][1]]
_ArrayDisplay($items2,"$items2")
For $i=1 to $items[0][0]-1
For $j=0 to $items[0][1]-1
$items2[$i][$j] = IniRead($Fname3, "", "$items" & $i & "-" & $J, "")
Next
next
_ArrayDisplay($items2,"$items2")
$var = IniReadSection("C:\Temp\myfile.ini", "section2")
If @error Then
MsgBox(4096, "", "Error occured, probably no INI file.")
Else
For $i = 1 To $var[0][0]
MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
Next
EndIf
; This is the INI file we will write to. It will be created on the Desktop.
$sIni = @DesktopDir & "\AutoIt-Test.ini"
; Demonstrate creating a new section using a string as input.
$sData = "Key1=Value1" & @LF & "Key2=Value2" & @LF & "Key3=Value3"
IniWriteSection($sIni, "Section1", $sData)
; Demonstrate creating a new section using an array as input.
$aData1 = IniReadSection($sIni, "Section1") ; Read in what we just wrote above.
For $i = 1 To UBound($aData1) - 1
$aData1[$i][1] &= "-" & $i ; Change the data some
Next
IniWriteSection($sIni, "Section2", $aData1) ; Write to a new section.
; Demonstrate creating an array manually and using it as input.
Dim $aData2[3][2] = [ [ "FirstKey", "FirstValue" ], [ "SecondKey", "SecondValue" ], [ "ThirdKey", "ThirdValue" ] ]
; Since the array we made starts at element 0, we need to tell IniWriteSection() to start writing from element 0.
IniWriteSection($sIni, "Section3", $aData2, 0)
#Include <File.au3>
#include <Array.au3>
$Path = @ScriptDir&'\file.txt'
$Razdelit=' - разделитель - '
Dim $arr1[3][2] = [[1,2],[3,4],[5,6]]
_ArrayDisplay($arr1, 'Массив arr1')
$tmp=''
For $i = 0 to UBound($arr1)-1
$tmp&=$arr1[$i][0]&$Razdelit&$arr1[$i][1]&@CRLF
Next
$file = FileOpen($Path,2)
FileWrite($file, $tmp)
FileClose($file)
MsgBox(0, 'Message', 'Записано, проверь')
;=======================================
Dim $aTmp1
If Not _FileReadToArray($Path,$aTmp1) Then
MsgBox(4096,"Ошибка", "Не могу прочитать в массив")
Exit
EndIf
Dim $arr1_New[$aTmp1[0]][2]
For $i = 1 to $aTmp1[0]
$aTmp2=StringSplit($aTmp1[$i], $Razdelit, 1)
If $aTmp2[0]>1 Then
$arr1_New[$i-1][0]=$aTmp2[1]
$arr1_New[$i-1][1]=$aTmp2[2]
EndIf
Next
_ArrayDisplay($arr1_New, 'Массив arr1_New')
$tmp&=$arr1[$i][0]&$Razdelit&$arr1[$i][1]&@CRLF
$tmp&=
$tmp=''
#include <Array.au3>
$iUb2 = 2
$sIniFile = @ScriptDir & '\test.ini'
$sSection = 'Base'
$aSection = IniReadSection($sIniFile, $sSection)
If @error Then
MsgBox(16, 'Error', 'IniReadSection')
Exit
EndIf
Dim $aResult[$aSection[0][0] + 1][$iUb2] = [['Ключ', 'Значения']]
For $i = 1 To $aSection[0][0]
$aTemp = StringSplit($aSection[$i][1], '|')
If $iUb2 < $aTemp[0] + 1 Then
$iUb2 = $aTemp[0] + 1
ReDim $aResult[$aSection[0][0] + 1][$iUb2]
EndIf
$aResult[$i][0] = $aSection[$i][0]
For $j = 1 To $aTemp[0]
$aResult[$i][$j] = $aTemp[$j]
Next
Next
_ArrayDisplay($aResult)
если бы не было вот этого столбца:
#include <Array.au3>
$iUb2 = 2
$sIniFile = @ScriptDir & '\test.ini'
$sSection = 'Base'
$aSection = IniReadSection($sIniFile, $sSection)
If @error Then
MsgBox(16, 'Error', 'IniReadSection')
Exit
EndIf
Dim $aResult[$aSection[0][0]][$iUb2]
For $i = 0 To $aSection[0][0] - 1
$aTemp = StringSplit($aSection[$i + 1][1], '|')
If $iUb2 < $aTemp[0] Then
$iUb2 = $aTemp[0]
ReDim $aResult[$aSection[0][0]][$iUb2]
EndIf
For $j = 0 To $aTemp[0] - 1
$aResult[$i][$j] = $aTemp[$j + 1]
Next
Next
_ArrayDisplay($aResult)