PHP-like associative arrays

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Makoto WATANABE
Posts: 196
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: PHP-like associative arrays

Postby Makoto WATANABE » Jun 17, 2020 10:58

Dear srvaldez;

Thanks for your support.
The example program compiled successfully and the results were displayed.

By the way, I think that it is practical to use a variable instead of a literal in the "****" part of "array.Item(****) = 123".
I would appreciate it if anyone could extend it.
jdebord
Posts: 529
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Re: PHP-like associative arrays

Postby jdebord » Jun 18, 2020 7:19

This seems to work:

Code: Select all

for i as integer = 0 to 10
  array.Item(i) = i
  print array.Item(i)
next i
Makoto WATANABE
Posts: 196
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: PHP-like associative arrays

Postby Makoto WATANABE » Jun 18, 2020 7:52

Dear jdebord

Thanks for your advice.
I'm sorry my expression is so poorly worded that it doesn't convey my intentions.
I would like to give the following (****) as a variable.

Code: Select all

array.Item("bla") =
array.Item(123) =
s.item("color") =
s.item("fruit") =
s.item(1) =

Thank you very much.
MrSwiss
Posts: 3631
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: PHP-like associative arrays

Postby MrSwiss » Jun 18, 2020 13:36

Makoto WATANABE wrote:I would like to give the following (****) as a variable.

Just use a String for something like that:

Code: Select all

Dim As String   stars = String(4, "*")

Print stars : Sleep
srvaldez
Posts: 2527
Joined: Sep 25, 2005 21:54

Re: PHP-like associative arrays

Postby srvaldez » Jun 18, 2020 14:21

something like this?

Code: Select all

#include "assocarray.bi"

DefineAssocArrayType( Integer )
DefineAssocArrayType( String )

Dim As IntegerArray array
Dim As StringArray s
Dim as string stars="***"

array.Item("bla") = 123
array.Item(123) = 124
array.Item(123) = 124324

Print array.Item("bla"), array.Item(123)

s.item("color") = "red"
s.item("fruit") = "oranges"
s.item(1) = "one"
s.item(stars) = "*******"

print "s.item(""color"") = ";s.item("color")
print "s.item(""fruit"") = ";s.item("fruit")
print "s.item(1) = ";s.item(1)
print "s.item(stars) = ";s.item(stars)
print "s.item(""***"") = ";s.item("***")

Sleep
srvaldez
Posts: 2527
Joined: Sep 25, 2005 21:54

Re: PHP-like associative arrays

Postby srvaldez » Jun 18, 2020 14:41

@Makoto WATANABE
I suggest that you also have a look at the mixed type by the same author, the corrected Mixed.bi can be found here viewtopic.php?p=273280#p273280
Makoto WATANABE
Posts: 196
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: PHP-like associative arrays

Postby Makoto WATANABE » Jun 18, 2020 15:27

Dear All!

Thank you all for your advice.

I'm sorry to confuse you.
I'm sorry my expression is so poorly worded that it doesn't convey my intentions.
What I expect is the following image.

Code: Select all

#include "assocarray.bi"
DefineAssocArrayType( Integer )

Dim As IntegerArray array
Dim CharacterString As String '** variable **

CharacterString = "bla"
array.Item(CharacterString) = 123

CharacterString = "123"
array.Item(CharacterString) = 124324

Print array.Item("bla"), array.Item("123")

Sleep
srvaldez
Posts: 2527
Joined: Sep 25, 2005 21:54

Re: PHP-like associative arrays

Postby srvaldez » Jun 18, 2020 16:25

I don't understand your problem, it works as the associative array should
when you do: CharacterString = "bla" and then array.Item(CharacterString) = 123 it's equivalent to array.Item("bla") = 123
srvaldez
Posts: 2527
Joined: Sep 25, 2005 21:54

Re: PHP-like associative arrays

Postby srvaldez » Jun 18, 2020 17:51

@Makoto WATANABE
it occurred to me that perhaps you are using Japanese fonts in the CharacterString, not sure it will work, but if that's the problem then others more knowledgeable than me will help you.
Makoto WATANABE
Posts: 196
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: PHP-like associative arrays

Postby Makoto WATANABE » Jun 19, 2020 1:10

Dear srvaldez;

Thanks for your patience with my childish misconceptions.

>do: CharacterString = "bla" and then array.Item(CharacterString) = 123 it's equivalent to array.Item("bla") = 123

I tried it before and had a feeling it wouldn't work, but now it works correctly.
I'm sorry to confuse you.
Thank you very much.
Makoto WATANABE
Posts: 196
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: PHP-like associative arrays

Postby Makoto WATANABE » Jun 19, 2020 8:19

Hi !

I have run into another trouble.
Please give me your wisdom of God(FreeBASIC) for my foolishness.

I'm trying to set unique numbers to 676 strings from AAA to AZZ (ItemMasterA.txt).
However, there is interference between the 139th AFI and the AIT which should be the 228th.

ItemMasterA.txt

Code: Select all

AAA
AAB
AAC
AAD
AAE
AAF
AAG
AAH
AAI
AAJ
AAK
AAL
AAM
AAN
AAO
AAP
AAQ
AAR
AAS
AAT
AAU
AAV
AAW
AAX
AAY
AAZ
ABA
ABB
ABC
ABD
ABE
ABF
ABG
ABH
ABI
ABJ
ABK
ABL
ABM
ABN
ABO
ABP
ABQ
ABR
ABS
ABT
ABU
ABV
ABW
ABX
ABY
ABZ
ACA
ACB
ACC
ACD
ACE
ACF
ACG
ACH
ACI
ACJ
ACK
ACL
ACM
ACN
ACO
ACP
ACQ
ACR
ACS
ACT
ACU
ACV
ACW
ACX
ACY
ACZ
ADA
ADB
ADC
ADD
ADE
ADF
ADG
ADH
ADI
ADJ
ADK
ADL
ADM
ADN
ADO
ADP
ADQ
ADR
ADS
ADT
ADU
ADV
ADW
ADX
ADY
ADZ
AEA
AEB
AEC
AED
AEE
AEF
AEG
AEH
AEI
AEJ
AEK
AEL
AEM
AEN
AEO
AEP
AEQ
AER
AES
AET
AEU
AEV
AEW
AEX
AEY
AEZ
AFA
AFB
AFC
AFD
AFE
AFF
AFG
AFH
AFI
AFJ
AFK
AFL
AFM
AFN
AFO
AFP
AFQ
AFR
AFS
AFT
AFU
AFV
AFW
AFX
AFY
AFZ
AGA
AGB
AGC
AGD
AGE
AGF
AGG
AGH
AGI
AGJ
AGK
AGL
AGM
AGN
AGO
AGP
AGQ
AGR
AGS
AGT
AGU
AGV
AGW
AGX
AGY
AGZ
AHA
AHB
AHC
AHD
AHE
AHF
AHG
AHH
AHI
AHJ
AHK
AHL
AHM
AHN
AHO
AHP
AHQ
AHR
AHS
AHT
AHU
AHV
AHW
AHX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
ARV
ARW
ARX
ARY
ARZ
ASA
ASB
ASC
ASD
ASE
ASF
ASG
ASH
ASI
ASJ
ASK
ASL
ASM
ASN
ASO
ASP
ASQ
ASR
ASS
AST
ASU
ASV
ASW
ASX
ASY
ASZ
ATA
ATB
ATC
ATD
ATE
ATF
ATG
ATH
ATI
ATJ
ATK
ATL
ATM
ATN
ATO
ATP
ATQ
ATR
ATS
ATT
ATU
ATV
ATW
ATX
ATY
ATZ
AUA
AUB
AUC
AUD
AUE
AUF
AUG
AUH
AUI
AUJ
AUK
AUL
AUM
AUN
AUO
AUP
AUQ
AUR
AUS
AUT
AUU
AUV
AUW
AUX
AUY
AUZ
AVA
AVB
AVC
AVD
AVE
AVF
AVG
AVH
AVI
AVJ
AVK
AVL
AVM
AVN
AVO
AVP
AVQ
AVR
AVS
AVT
AVU
AVV
AVW
AVX
AVY
AVZ
AWA
AWB
AWC
AWD
AWE
AWF
AWG
AWH
AWI
AWJ
AWK
AWL
AWM
AWN
AWO
AWP
AWQ
AWR
AWS
AWT
AWU
AWV
AWW
AWX
AWY
AWZ
AXA
AXB
AXC
AXD
AXE
AXF
AXG
AXH
AXI
AXJ
AXK
AXL
AXM
AXN
AXO
AXP
AXQ
AXR
AXS
AXT
AXU
AXV
AXW
AXX
AXY
AXZ
AYA
AYB
AYC
AYD
AYE
AYF
AYG
AYH
AYI
AYJ
AYK
AYL
AYM
AYN
AYO
AYP
AYQ
AYR
AYS
AYT
AYU
AYV
AYW
AYX
AYY
AYZ
AZA
AZB
AZC
AZD
AZE
AZF
AZG
AZH
AZI
AZJ
AZK
AZL
AZM
AZN
AZO
AZP
AZQ
AZR
AZS
AZT
AZU
AZV
AZW
AZX
AZY
AZZ

Code: Select all

#Include "assocarray.bi"

DefineAssocArrayType( Integer )

Dim Shared As IntegerArray ArrayItemID
Dim Shared ItemID As String     
Dim Shared Counter As Integer
'**************************************
   Dim file_name As String
   Dim file_num As Integer
   Dim FirstDigit As Integer
   Dim CharacterString As String
   
   file_name = "ItemMasterA.txt"
   file_num = FreeFile( )                 '' 有効なファイル番号を検索します

   If( Open( file_name For Input As #file_num ) ) Then
      Print "ERROR: 開こうとしたファイル名 " ; file_name
      Sleep
      End -1
   End If
   
   Counter = 0
   Do Until Eof( file_num )               '' ファイルの端に達するまで、繰り返します。
      ItemID =""

      Line Input #file_num, CharacterString           '' テキストの行を読みます。
      ItemID = Left(CharacterString,3)
     
      If ArrayItemID.Item(ItemID) = 0 Then
         Counter = Counter + 1
         ArrayItemID.Item(ItemID) = Counter
         If Counter = 139 Then
            Print ItemID
            Print Counter
            Print
         EndIf
      Else
         Print ArrayItemID.Item(ItemID)
         Print ItemID
         Print Counter
         Print
      End If
     
   Loop
   
   Print ItemID
   Print Counter

   Close #file_num                        '' ファイル番号を通したファイルを閉じます。
   
   Sleep

Please let me know where and how to fix it.
srvaldez
Posts: 2527
Joined: Sep 25, 2005 21:54

Re: PHP-like associative arrays

Postby srvaldez » Jun 19, 2020 9:57

Hi Makoto WATANABE

there's certainly a problem, try this variation of your code

Code: Select all

#Include "assocarray.bi"

DefineAssocArrayType( Integer )

Dim Shared As IntegerArray ArrayItemID
Dim Shared ItemID As String     
Dim Shared As Integer Counter, i, j

Counter = 0
For i = Asc("A") To Asc("Z")
   For j = Asc("A") To Asc("Z")
      ItemID = "A" + Chr(i) + Chr(j)
      Counter += 1
      ArrayItemID.Item(ItemID) = Counter
   Next
Next

Counter = 0
For i = Asc("A") To Asc("Z")
   For j = Asc("A") To Asc("Z")
      ItemID = "A" + Chr(i) + Chr(j)
      Counter += 1
      If (ArrayItemID.Item(ItemID) - Counter) <> 0 Then
         Print "ItemID = ";ItemID, "Counter = ";Counter, "ArrayItemID.Item(";ItemID;") = ";ArrayItemID.Item(ItemID)
      End If
   Next
Next
Print "----------------------------------------------------------------------"
Print "ArrayItemID.Item(ALD) = ";ArrayItemID.Item("ALD"), "ArrayItemID.Item(ARJ) = ";ArrayItemID.Item("ARJ")
Print "ArrayItemID.Item(AMM) = ";ArrayItemID.Item("AMM"), "ArrayItemID.Item(AVP) = ";ArrayItemID.Item("AVP")

the output I get is

Code: Select all

collision: (string)ARJ with  (string) 10944240
collision: (string)AVP with  (string) 10944432
ItemID = ALD  Counter =  290              ArrayItemID.Item(ALD) =  452
ItemID = AMM  Counter =  325              ArrayItemID.Item(AMM) =  562
----------------------------------------------------------------------
ArrayItemID.Item(ALD) =  452              ArrayItemID.Item(ARJ) =  452
ArrayItemID.Item(AMM) =  562              ArrayItemID.Item(AVP) =  562

I don't know how to fix this
Last edited by srvaldez on Jun 19, 2020 10:25, edited 1 time in total.
dodicat
Posts: 6719
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: PHP-like associative arrays

Postby dodicat » Jun 19, 2020 10:15

My results

Code: Select all

 
_________________
-gen gas  32 bit
AFI
 139

 290
ARJ
 451

 325
AVP
 560

AZZ
 674
 
___________________
 -gen gcc 32 bit
 
 AFI
 139

 139
AIT
 227

AZZ
 675

 
___________________
 -gen gcc 64 bit
 
 
 AFI
 139

 290
ARJ
 451

 325
AVP
 560

AZZ
 674

___________________
 -gen gas64
 
 AFI
 139

 290
ARJ
 451

 325
AVP
 560

AZZ
 674
 
 
srvaldez
Posts: 2527
Joined: Sep 25, 2005 21:54

Re: PHP-like associative arrays

Postby srvaldez » Jun 19, 2020 10:27

this implementation of associative array does not handle collisions, it reports them but does not resolve them, see my updated previous post
srvaldez
Posts: 2527
Joined: Sep 25, 2005 21:54

Re: PHP-like associative arrays

Postby srvaldez » Jun 19, 2020 12:04

there's another hasmap implementation by Zamaster viewtopic.php?p=216439 but it works in 32-bit only, he uses inline-asm and the 64-bit parts are wrong
however, I run the previous test using his hashmap and there were no problems

Code: Select all

#include "hashmap.bi"

dsm_HashMap_define(zstring, integer)

using dsm

dim as HashMap(zstring, integer) ArrayItemID
Dim Shared ItemID As String     
Dim Shared As Integer Counter, i, j

Counter = 0
For i = Asc("A") To Asc("Z")
   For j = Asc("A") To Asc("Z")
      ItemID = "A" + Chr(i) + Chr(j)
      Counter += 1
      ArrayItemID.insert( ItemID, Counter )
   Next
Next

Counter = 0
For i = Asc("A") To Asc("Z")
   For j = Asc("A") To Asc("Z")
      ItemID = "A" + Chr(i) + Chr(j)
      Counter += 1
      If (ArrayItemID.retrieve(ItemID) - Counter) <> 0 Then
         Print "ItemID = ";ItemID, "Counter = ";Counter, "ArrayItemID.retrieve(";ItemID;") = ";ArrayItemID.retrieve(ItemID)
      End If
   Next
Next

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 3 guests