1

So I'm trying to access a hash table in Racket, but I can not figure out why it's not working.

When I call hash->list on this hash:

{"26220765": [
{
  "queue": "RANKED_SOLO_5x5",
  "name": "Viktor's Marauders",
  "participantId": "26220765",
  "entries": [
     {
        "leaguePoints": 0,
        "isFreshBlood": false,
        "isHotStreak": false,
        "division": "IV",
        "isInactive": false,
        "isVeteran": true,
        "losses": 168,
        "playerOrTeamName": "iGT500",
        "playerOrTeamId": "32156611",
        "wins": 173
     },
     {
        "leaguePoints": 0,
        "isFreshBlood": true,
        "isHotStreak": false,
        "division": "V",
        "isInactive": false,
        "isVeteran": false,
        "losses": 255,
        "playerOrTeamName": "dragdan",
        "playerOrTeamId": "20430418",
        "wins": 265
     },

It returns

((|26220765| #hasheq((name . "Viktor's Marauders") (queue . "RANKED_SOLO_5x5") (tier . "PLATINUM") (entries . (#hasheq((playerOrTeamId . "32156611") (division . "IV") (playerOrTeamName . "iGT500") (leaguePoints . 0) (wins . 173) (losses . 168) (isHotStreak . #f) (isVeteran . #t) (isFreshBlood . #f) (isInactive . #f)) #hasheq((playerOrTeamId . "20430418") (division . "V") (playerOrTeamName . "dragdan") (leaguePoints . 0) (wins . 265) (losses . 255) (isHotStreak . #f) (isVeteran . #f)

I can not access the hash by using the key 26220765. I've tried defining it as summoner-id and passing it, i've tried using '26220765, it doesn't work. I don't understand why it's being displayed with vertical bars, and if I try '|26220765| or |26220765| it doesn't work either.

I had another hash table where I needed to access the hash "champions", and using 'champions worked, so why isn't this working?

6
  • Using '|26220765| should work. It's being displayed with vertical bars because it's a symbol, but it's only composed of numeric characters, so it needs to be escaped. Apr 14, 2015 at 2:45
  • @AlexisKing '|26220765 works, I don't know what I was doing before. But what if I have the number 26220765 defined as summoner-id? basically I need '|summoner-id|, but I think I need to escape summoner-id so what's between the '| | is a number, but that number changes depending on who I look up with the API. Any ideas?
    – suhmedoh
    Apr 20, 2015 at 17:01
  • Is summoner-id a string or a number? Apr 20, 2015 at 18:59
  • @AlexisKing It's a number; I ended up doing this: (define summ-key (if (not (hash? league-hash-json)) (printf "No ranked data to judge.\n") (hash-keys league-hash-json))) because at this particular point, it's always returning a hash with the id as the key, and another hash as teh value, and i found that hash-keys lists all the keys in a hash, but only the outtermost layer, if you get what I mean? so this will always be a list with the correct id, so I just take the car of the list and I know it will always match(if they exist). thanks for your help though!
    – suhmedoh
    Apr 20, 2015 at 19:31
  • You probably want to use (string->symbol (number->string summoner-id)). Apr 20, 2015 at 19:46

2 Answers 2

1

This works:

#lang racket/base
(require json)
(define js (string->jsexpr "{\"123\": \"val\"}"))
js ;=> '#hasheq((|123| . "val"))
(hash-ref js '|123|) ;=> "val"

Do you have a similarly short example of what doesn't work for you?

p.s. You may find people more motivated to answer, if you take a moment to accept and/or upvote answers. Like Racket string to literal? and Getting a specific hash-table from a list in racket?.

1
  • It says I need to have 15 reputation to update something, which is why I haven't been doing that, and maybe a it's because I'm on mobile but I do not see a way to pick answer and Mark my question as solved
    – suhmedoh
    Apr 15, 2015 at 19:12
0

In Racket there are different constructors for hash tables. The difference between them is that the hash table uses different comparators (eq? , equal? , eqv?, ...).

That's probably the reason why it doesn't work always like you would want it to. The different hash tables in racket

In fact when making a hash table it has to know which comparator to use to check for equality. You could for example imagine a generic implementation where you have to pass your own lambda to be used as comparator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.