Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Base_Data: Switch to arrays for what should be arrays. #62

Open
Artoria2e5 opened this issue Aug 25, 2015 · 3 comments
Open

Base_Data: Switch to arrays for what should be arrays. #62

Artoria2e5 opened this issue Aug 25, 2015 · 3 comments
Assignees
Labels
Milestone

Comments

@Artoria2e5
Copy link
Contributor

In our code, multiple stuffs that should be arrays (e.g. damn flags) are stored as strings, causing undesired results sometime, especially with param expansions.

Basic compatibility for data patterns can be achived in such way:

# Ultra-ugly varnames to avoid colision
typeof(){ local ___t_{a,t,d,c}; for ___t_c; do read ___t_{a,t,d} <<< "$(declare -p "$___t_c")"; echo "$___t_t"; done; }
if [[ "$(typeof should_be_arr_but_was_not)" != *a* ]]; then
   read -ra should_be_arr_but_was_not <<< "$should_be_arr_but_was_not"
fi

However, commands cannot return arrays. All they can do are strings. Fortunately, we have \0:

foo_func(){
  for i in a b c; do
    if [ -e "$i" ]; then
      magic_"$i" &&
      printf '%s\0' "$i"
    fi || break
  done
}
# http://mywiki.wooledge.org/BashFAQ/020
unset a i
while IFS= read -rd '' file; do
  a[i++]="$file"
done < <(foo_func)

Or we will make all array-like outputting functions to put their output into a certain $_ab_tmp_arr, or use declare -n directives.

@Artoria2e5
Copy link
Contributor Author

The current typeof doesn't recurse into levels of references, so a recursive version is also needed.

@Artoria2e5
Copy link
Contributor Author

[TG @JeffBai] @MingcongBai Updated read trick. Can be slow.

@Artoria2e5 Artoria2e5 added this to the 2.0.0 milestone Mar 7, 2016
@Artoria2e5
Copy link
Contributor Author

Looks like I forgot all the opts in official_builds…

To-morrow, to-morrow, to-morrow!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant