How to read a multi-dimensional array from a text file?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

HGAutomator
Posts: 202
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to read a multi-dimensional array from a text file?

Post by HGAutomator »

Thanks Claudio,

But I'm not having a problem saving the array to a file. I'm having a problem converting the array from a array-shaped string in a file, to an array in the program.

In this example, you already have the array declared and populated, and you're sending it to a text file.

I'm trying to do the reverse.
Claudio Ricardo wrote: Mon Feb 07, 2022 12:26 am Hi... Two options to convert your array (as is) to .txt file
Comment and discomment lines... (need add more for/next bucles)
Compiled with HMG 3.4.4 32 bits.
HGAutomator
Posts: 202
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to read a multi-dimensional array from a text file?

Post by HGAutomator »

Got it, I'll post the snippet this week, but here's the temporary snippet for the purposes of debugging. At the end, Text_x contains the full array of arrays, including the code blocks.

Some of it is probably redundant, like maybe removing the carriage returns. Will confirm during the week.

Thanks everyone, for your help and suggestions.

Code: Select all

Text_c := memoread("TempArry.txt")
Text_c := AllTrim( Text_c )
Text_c := StrTran( Text_c, ";", ""  )
Text_c := StrTran( Text_c, Chr(13), ""  )
Text_c := StrTran( Text_c, Chr(10), ""  )
Char_c := Right( Text_c, 1 )
Char_c := Left( Text_c, 1 )

If Right( Text_c, 1 ) = "}"
	Text_c := Left( Text_c, Len( Text_c )-1 )
Endif
// Trim it, and compare
Text_c := AllTrim( Text_c )

If Left( Text_c, 1 ) = "{"
	Text_c := " " + SubStr( Text_c, 2 )
Endif
// Trim it, and compare.
Text_c := AllTrim( Text_c )

// Check one more time.
Char_c := Right( Text_c, 1 )
Char_c := Left( Text_c, 1 )

Text_x := { &Text_c }
This is the full array. It's completely imported from the text file into a run-time array, at the finale of the debugging session above.

{ ;
{"First","First Item", ;
{{"First SubItem", "First SubItem from First", {||qout("First SubItem from First")}},;
{"Second SubItem", "Second SubItem from First", {||qout("Second SubItem from First")}},;
{"Third SubItem", "Third SubItem from First", {||qout("Third SubItem from First")}} } ;
}, ;
{"Second","Second Item", ;
{{"First SubItem", "First SubItem from Second", {||qout("First SubItem from Second")}},;
{"Second SubItem", "Second SubItem from Second", {||qout("Second SubItem from Second")}},;
{"Third SubItem", "Third SubItem from Second", {||qout("Third SubItem from Second")}} } ;
}, ;
{"Third","Third SubItem",0}, ;
{"Fourth","Fourth SubItem",{||qout("Fourth SubItem")} } ;
}
HGAutomator
Posts: 202
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to read a multi-dimensional array from a text file?

Post by HGAutomator »

Here it is, working fine:

Code: Select all

Local ArrayImport_a := {}
Local ArrayImport_s := ""
Local Text_c := ""

If MenuConfigFile_s == NIL
	MenuConfigFile_s := "MainMenu.ini"
EndIf

ArrayImport_s := memoread(MenuConfigFile_s)
ArrayImport_s := StrTran( ArrayImport_s, " ", "" )
ArrayImport_s := StrTran( ArrayImport_s, ";", "" )
ArrayImport_s := StrTran( ArrayImport_s, Chr(13), "" )
ArrayImport_s := StrTran( ArrayImport_s, Chr(10), "" )
ArrayImport_s := AllTrim( ArrayImport_s )
If Right( ArrayImport_s, 1 ) = "}"
	ArrayImport_s := Left( ArrayImport_s, Len( ArrayImport_s )-1 )
Endif
If Left( ArrayImport_s, 1 ) = "{"
	ArrayImport_s := " " + SubStr( ArrayImport_s, 2 )
Endif
// For debugging purposes
Text_c := Right( ArrayImport_s, 1 )
Text_c := Left( ArrayImport_s, 1 )

ArrayImport_a := { &ArrayImport_s }
If .NOT. IsArray( ArrayImport_a )
	? "ArrayImport_a is not an array.  It's a " + ValType( ArrayImport_a ) + ". " + MenuConfigFile_s + " has to be written in the form of a Harbour array."
Endif
Post Reply