Code Monkey home page Code Monkey logo

bash-array-utils's People

Contributors

hugot avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

perfectcube

bash-array-utils's Issues

Use return instead of exit in functions

I see that you use this check in hasValue

  if ! Array::isValid "$1"; then
    echo "$(caller): $1 is not a valid array."
    exit 1
  fi

I think it would be better to return instead of exit.

My use-case is the following:

# source array functions

if Array::hasValue my_array my_value; then
  : # do something
else
  : # do something else
fi

: # do other things

...in this case the script would simply exit, not executing the else clause or the rest of the code. I think the choice of exiting should be left to the user (with set -e or other ways).

Another way would be to offer two modes: a strict mode where the array is required to be set, and a non-strict mode where is does not matter.

By the way, you use Array::isValid "$1" || return $? in all other places, so maybe it was not intended to use exit?

Function to find an element index

Hello!

Would you be interested in offering a function that can return the index of a given element?

I was thinking something like Array::indexOf, taking an array as $1 and the element as $2, returning the index between 0 and n, or -1 if not found.

About the way to return it, we could either write to stdout, but it does not feel very efficient because we would then have to use a subprocess like index=$(Array::indexOf array elem). So maybe we would instead allow for a third parameter as the name of the variable to set with the result: Array::indexOf array elem index.

I can open a PR if you're interested! ๐Ÿ™‚

Don't reserve "array"

I believe people usually go with the (not so good) name of "array" in shell scripts (maybe I'm wrong), so why not reserve the name "_array" instead? Same for "_variable" instead of "variable", though this one seems less important.

Don't try to compare numerical values in hasValue

$ [[ 09 == +([0-9]) ]] && echo true
true  # will trigger numeric comparison with -eq, but:
$ [[ 09 -eq 09 ]] && echo true
bash: 09: value too great for base (error token is "09")

There might be other cases where the string is composed only of numbers, but the user actually want to compare it as a string.

A solution to keep this feature would be to check if the array was declared with the -i option:

$ declare -ia int_array
$ declare -p int_array
declare -ai int_array
$ if declare -p int_array | grep -q '^declare -ai'; then echo true; fi
true

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.