23 votes

Today I learned that Bash has hashmaps

8 comments

  1. [2]
    hungariantoast
    Link
    note that unset FLY_REGIONS["a100-40gb"] is incorrectly quoted: it gets expanded as a glob (like ls files.[ch]pp). for instance it will match a file called FLY_REGIONSa if it exist, so you’ll end...
    8 votes
    1. vord
      Link Parent
      Thank you for all these bash posts by the way. I'm loving it.

      Thank you for all these bash posts by the way. I'm loving it.

      4 votes
  2. [5]
    goose
    Link
    I make great use of associative arrays (hashmaps) to reduce calls to an sqlite database I store static information in. I make an API call to return 10 values I care about for a data point. I store...

    I make great use of associative arrays (hashmaps) to reduce calls to an sqlite database I store static information in.

    I make an API call to return 10 values I care about for a data point. I store these values in the database for future use. I also store them in the associate array val_one[${dataPoint}]="${value_one}". By doing this, if I need that data point later in the script (or on a future run, in which I'm getting the data from the sqlite db instead of an additional API call), I can call function getValOne "${dataPoint}"

    function getValOne {
    if [[ -z "${1}" ]]; then
        echo "give me a data point u luddite"
        return 1
    fi
    if [[ -n "${val_one[${1}]}" ]]; then
        # Value already assigned
        return 0
    fi
    # No value assigned, get one from sqlite
    val_one["${1}"]="$(sqlite3 [...])"
    }
    

    It's sped up my scripts a decent amount that have a large number of potential sqlite lookups.

    Unfortunately, I've started having to pad my array elements with an underscore, as some of my data points are mixed strings of integers and letters. Every now and then a string beginning with an integer will be interpreted (I forget, it's either interpreted lexicographically and it shouldn't be, or it shouldn't be and it is. Either way, padding with a leading underscore fixed that.)

    2 votes
    1. [4]
      chundissimo
      Link Parent
      Ha, I both love and hate this. Redis would probably be a pretty easy drop-in solution?

      Ha, I both love and hate this. Redis would probably be a pretty easy drop-in solution?

      2 votes
      1. [3]
        Comment deleted by author
        Link Parent
        1. [2]
          chundissimo
          Link Parent
          Oh I see, so part of this is that SQLite isn’t running at all times? Then yeah if the same constraint is applied to redis I think you’re right. In terms of processes you could of course just run...

          Oh I see, so part of this is that SQLite isn’t running at all times? Then yeah if the same constraint is applied to redis I think you’re right. In terms of processes you could of course just run one redis process that is single threaded, but it sounds like that may not fit your solution

          1 vote
          1. [2]
            Comment deleted by author
            Link Parent
            1. chundissimo
              Link Parent
              Good point, I forgot that’s how SQLite works.

              Good point, I forgot that’s how SQLite works.

              1 vote
      2. goose
        Link Parent
        Anything would be a better drop-in solution. My primary job is a patient care facing role in healthcare, I'm pretty far from anything tech, IT, or development. I started tinkering around on Ubuntu...

        Anything would be a better drop-in solution. My primary job is a patient care facing role in healthcare, I'm pretty far from anything tech, IT, or development. I started tinkering around on Ubuntu as a teen back in 2007, and all of my *nix experience comes from using it for hobby/passion projects over the years. I should have learned Python, or something, years ago. But I just didn't have the time or mental fortitude when I could cop out and be like "Eh, bash script".

        1 vote
  3. saturnV
    Link
    speaking of unexpected places where hashmaps are built-in, POSIX C has them

    speaking of unexpected places where hashmaps are built-in, POSIX C has them

    2 votes