Skip to main content

Questions tagged [array]

A array is the simplest data-structure for storing items in continuously memory

Filter by
Sorted by
Tagged with
0 votes
0 answers
7 views

define an array in c using a macro as length in C [migrated]

I want to defined an array, the length from a macro as below, #define A (2.5) #define B (2.0) #define LEN (A*B) // I know LEN is an integer, not decimal numbers static int array[LEN] // define ...
Keely Darien's user avatar
0 votes
3 answers
132 views

How to add a prefix to every element of an array in shell

I am reading inputs from user in my script with spaces, and those multiple inputs I want to use in a command further down by adding -l in front of each one of those inputs. Is there a way to do this ...
Heyya's user avatar
  • 19
3 votes
1 answer
453 views

jq skip iteration over null

I am using jq 1.7 Data I am operating on info.json downloaded by yt-dlp yt-dlp --write-info-json --skip-download https://www.youtube.com/watch?v=vlIO-7Rpi7c JQ - Task Now I want to remove all array ...
Porcupine's user avatar
  • 2,056
0 votes
2 answers
35 views

Iterating arrays in TCL where array names have a numeric suffix

In TCL, I have a few arrays whose names have a numeric suffix (i.e., whose names end with a number), like below: array set ps0 [ list 0 15.885 1 55.43 1 0.254 2 0.227 3 0.177 ] array set ps1 [ list 0 ...
poppycock's user avatar
4 votes
1 answer
532 views

Why does filtering a bash array using negative pattern matching and parameter expansion have unexpected results?

I'm doing a small-ish pure bash script to roll dice; as such I'm having to manipulate arrays. I want to do something akin to a filter operation in other languages: extract some of the things in the ...
CyberFoxar's user avatar
4 votes
4 answers
369 views

How to sort the list of positional parameters in POSIX sh

Is there a way to sort the list of positional parameters in POSIX sh? Each positional parameter could contain arbitrary characters (e.g. spaces, newlines, tabs, etc.). The sort algorithm should be ...
Flux's user avatar
  • 2,978
0 votes
2 answers
1k views

Bash manage space separated list

I have a rather complex shell script which processes several list of values which are read from a file as space separated values e.g. SET1="value1 value2 value3" for i in ${SET1}; do ... ...
symcbean's user avatar
  • 5,839
1 vote
2 answers
166 views

Getting an array of files built from include array and exclude array containing globs

I am wanting to do the following: Define an array of globs that specify a base collection of files to include in a process. Define an array of globs that specify files to exclude from that process. ...
bmitc's user avatar
  • 107
1 vote
1 answer
337 views

How to save the output of a sliced array as separate items rather than a string, when set as a var?

When slicing an array the output is captured in a variable as a string. When the slice is used directly it will be separate items. How can I save a sliced array in a variable without it being turned ...
Nickotine's user avatar
  • 489
2 votes
1 answer
538 views

What is difference between these two declarations of associative arrays in Bash?

I am playing a bit with associative arrays in Bash and I found the following difference when declaring the exact same associative array with and without declare. The code is as follows: #!/usr/bin/env ...
chemacabeza's user avatar
0 votes
2 answers
58 views

Read new lines from file and compare with two arrays if one or more word exists the result should not be written

Big fan of stackoverflow. Am a beginner myself and have found a lot of help on this site but have run into problems now. Today I have a function like the following. I read a text file (data.txt) for ...
SweAndreas's user avatar
-1 votes
1 answer
259 views

Zsh string variable containing multi words must be assigned to an array

How to have Zsh string variable containing multi words separated by space be assigned to an array so that each word is the array element s='run help behind me' a=($s) m=${a[0]} n=${a[1]} print "m=...
user avatar
2 votes
2 answers
476 views

For loop through a variable vector

I have a for loop and case statements. The for loop has quite a bit element list and the case statement will assign an 1D array or a vector. These values will be used in for loop after. I have the ...
BircanA's user avatar
  • 21
0 votes
3 answers
244 views

In a bash script, loop through a bash array for input to a binary

I'm running bash. Suppose I have a binary mybinary that takes interactive user input. Suppose that mybinary takes attendee names as input until the user types q and presses Enter (q for "quit&...
Andrew's user avatar
  • 17.1k
9 votes
5 answers
12k views

How can I create a multidimensional array, or something similar, with bash?

I need to create a multidimensional array with bash and I read that there is no such thing as multidimensional arrays in bash . Here are my possible data, what they look like and what I need. This is ...
Santiago's user avatar
  • 225
0 votes
1 answer
61 views

Bash create parameter named array within function

I'm attempting to write a function that writes arrays with a name that's passed in. Given the following bash function: function writeToArray { local name="$1" echo "$name" ...
Lee's user avatar
  • 519
0 votes
2 answers
46 views

Special array from list of items in current dir

I want to extract a specially formatted array from the list of current dir. $ ls Output: fileABC.ext1 filePQR.ext2 dirMNO dirFGH I want only the dir and files of specific extension I want an array ...
decipher's user avatar
  • 312
0 votes
1 answer
768 views

In bash shell, how can I filter an array of paths based on whether an element exists?

I'm using bash shell. I have this array representing file paths MY_FILES=(/path/to/file1 /tmp/abc.txt /tmp/test.sh /path/to/file2 /path/to/file3 /tmp/abc.txt) How can I form a new array of elements ...
Dave's user avatar
  • 2,608
0 votes
2 answers
340 views

Sort file array list in bash by date and bypass argument limit

So, I have a file array list in a bash shell, and I want to sort all the files in the array by the date modified, starting with the oldest being the first one in the array. However, I don't want to ...
Eduardo Perez's user avatar
1 vote
1 answer
219 views

Completed stumped by trying to use a decimal value in an array for Bash

Here is some simple test code. #!bin/bash cpm=(0 1 0.094) lv=1 attack=5 defense=9 stamina=16 echo $((cpm[lv])) mycpm=$((cpm[lv])) #mycpm=`echo "0.094" | bc -l` cq=`echo "$attack*$...
Tim50001's user avatar
0 votes
2 answers
717 views

Curl exit immediately if no internet

I want to fetch some apis from github. so this is the code array=(link1 link2 link3 link4) readarray -t item < <(for i in ${array[@]} do curl -s "$i" | jq '.tag_name' done) ...
decipher's user avatar
  • 312
0 votes
1 answer
181 views

bash script - printing a value of an array based on the value of another array

I have two arrays and want to print a value from ARRAY2 depending on the applicable value in ARRAY1. #!/usr/bin/env bash ARRAY1=(bb.service.sql bw.service.sql) ARRAY2=(bb bw) case $ARRAY1[@] in ${...
vrms's user avatar
  • 161
1 vote
1 answer
6k views

jq: error (at input.json:45): Cannot index array with string "value"

I try to convert my .json files to .csv with jq. I'm getting cannot index array with string value on all my values. .json { "organic_data": [ { "description": "...
pwrsheller's user avatar
4 votes
2 answers
302 views

Is it possible to apply substring expansion to all elements of a zsh array?

Let's say I'd like to remove the first two and last three characters from all elements of an array e.g. results=( QK9H9UtADCgnG AlaLkCADjQ krsxseW8H1VrU 6nBG94ZbCWQ ) I'd like to end up with results=(...
don_crissti's user avatar
  • 83.5k
0 votes
1 answer
585 views

How to replace matching pattern in an array

I have array with name 'arr' which has the following value - post-ro post-ro-1 post-ro-2 I have this variable: str="su scp [email protected]:/etc/bp/pg/pri/user.json /etc/bp/pg/pri" I want to ...
Madhavi Deshpande's user avatar
0 votes
2 answers
110 views

Loop over space delimited PIDs, get number of open file handles

How may I loop over a set of PIDs delimited by spaces? I think I remember the first step is like making each element its own row? For my Firefox sessions they are quite a few PIDs: $ pidof firefox ...
von spotz's user avatar
  • 435
0 votes
1 answer
55 views

linux bash shell script - pass through function array variable

So in order to be versatile, I make a lot of functions and i want to define my own variable name when i call on that function. For basic text, numbers it works and this is how I do it function ...
WingZero's user avatar
2 votes
1 answer
489 views

Capture output of a command that get stuck

I am trying to use regex for capturing some text pattern in an ouput of a command and create an array list_mqs. The problem is that this previous command get stuck and never terminates. So, when I use ...
dcubaz's user avatar
  • 23
0 votes
1 answer
106 views

Can't remove element from the array

Does anyone can explain me why the pid is not deleted from my pids list ? The for loop still occure the same number of time each time I go in this The var i is equal of the number of pids. So the loop ...
Kaleikini O'CONNOR's user avatar
4 votes
1 answer
5k views

Convert comma separated string into json array

I have the following string in bash "3.8,3.9,3.10" Is there a way using shell to convert it into a json array, i.e. ["3.8", "3.9", "3.10"]
pkaramol's user avatar
  • 2,879
0 votes
2 answers
507 views

copy multiple directories into one folder from a variable or loop array in bash?

I am trying to target 100+ directories to perform a copy. The whole script needs to be contained within one file; therefore, I can't have the directories saved in a separate file. I want to be able to ...
sflk1f0's user avatar
3 votes
1 answer
815 views

Is it possible to pass a bash array as a parameter to a function? [duplicate]

I'd like to pass an array to a bash function but I get a bad substitution error Example mapfile -t ray < <(parallel -j 0 echo ::: {1..10}) declare -p ray declare -a ray=([0]="2" [1]=...
Nickotine's user avatar
  • 489
1 vote
1 answer
3k views

Ansible: set_fact after successful variable match in array

I have following playbook ~ # cat demo.yml: - name: demo hosts: localhost gather_facts: no vars: set: task: type: var1 task: - type: var1 - type: var2 - type: ...
Rafal Niznik's user avatar
0 votes
1 answer
393 views

How to declare array from STDOUT in Buildspec, Codebuild, AWS

I'm having a problem with declaring array from STDOUT in Buildspec, which is part of Codebuild in AWS. Buildspec using Pure Bourne Shell, so I can't use this line in code declare -a available_envs=($(...
paras_san's user avatar
0 votes
1 answer
96 views

how to combine output values using echo & fsck

I am trying to make a functional script just using Partition Label, get that label, from the Label get the Device, and pass fsck -fy twice on each partition. #!/usr/bin/env bash # Filter partition by ...
Inukaze's user avatar
  • 47
-1 votes
2 answers
416 views

How do you count the number of tokens from two columns of words from a csv file inside a loop?

I have a comma-separated csv file with 50 lines. One column is for state names and the other column is for capitals (of the states). How do you make a loop where it counts the number of tokens (2, 3, ...
usuallystuck's user avatar
0 votes
1 answer
207 views

How to make Zsh interpret \n in parameter expansion

I’m using Zsh’s parameter expansion flags to split a string into an array. This works: string="one two" array=("${(f)string}") echo "${array[1]}" # Returns: # one This ...
user137369's user avatar
1 vote
1 answer
895 views

Create an array from the output of whiptail command

So, I am trying to use whiptail command to give users option to choose different things they want to install in their system. I use the whiptail --checklist command as below: name =$(whiptail --title &...
BishwashK's user avatar
1 vote
1 answer
534 views

Can I import a single row (indexed) from a csv file into a zsh array?

I want to parse a csv file in zsh row by row and store it in an array (without the commas). Is it possible to import a single row into an array in zsh, then afterwards grab the next row? The issue is ...
Pogchamp_prob's user avatar
5 votes
3 answers
1k views

Case on multiple variables at once bash

I want to know if there is any way to case on multiple variables at once, like this: #/bin/bash arr1=(1 2) arr2=(3 4) foo=1 bar=2 case $foo && $bar in ${arr1[@]}) echo "...
GamerAwesome949's user avatar
1 vote
2 answers
152 views

What's the upper-bound for an array index/subscript in GNU bc?

I've found that there is an upper-bound on your subscripts/indices in an array in GNU bc. Running interactively and asking to let arr[100000000]=42 returns an error: Runtime error (func=(main), adr=...
Mike Pierce's user avatar
1 vote
1 answer
3k views

Call a command from a shell script, passing most arguments, allowing arguments with blanks

I am have a wrapper run_sas.sh around a command sas that runs SAS code in batch. A typical call looks like this ./run_sas.sh -sysin /my_code/my_program.sas -log /my_log_folder/my_program.log run_sas....
Dirk Horsten's user avatar
1 vote
1 answer
182 views

Assigning an array value in zsh for gsettings keybindings automation

I think this script should work to set keybinds, but it's currently quite borked--UPDATE: it has been unborked, and now works. It is ugly though, and I'm soliciting ways to make it more elegant, as I ...
cryptograthor's user avatar
0 votes
2 answers
69 views

get value of variable which is prefixed by another variable in bash

I'm running offlineimap on a bunch of accounts and want to check the exit code of each run and perform some actions accordingly. I have 6 separate email accounts that I run against with a lot of code ...
Tony Barganski's user avatar
1 vote
1 answer
1k views

How to append to an array with a dynamic variable name?

section_example=(one two three) name=example; section_$name+=(four) bash: syntax error near unexpected token `four' Section names are unknown beforehand. eval and declare -a output the same error. ...
user avatar
8 votes
2 answers
3k views

In bash, how do I get the index of the last element of an array without a loop

In bash, is it possible to get the index of the last element of an array (that might be sparse) without looping through the entire array like so: a=( e0 e1 ... ) i=0 while [ "$i" -lt $(( ${#...
fuumind's user avatar
  • 449
3 votes
1 answer
796 views

Cut or Awk to save lines on array in bash

I have a scenario that I have to put lines of a file .txt inside an array, example of file content; type of host | | first host | | | | second host | | | | | | same ip ...
Carlos Gomes's user avatar
1 vote
1 answer
4k views

Ansible convert dictionary to mix of dictionary/array

I use following files: variable file: db.yml x86_64: alpine: version: 3.15.0 debian: version: 11.3.0 aarch64: alpine: version: 3.15.0 debian: version: 11.3.0 playbook: ...
Rafal Niznik's user avatar
0 votes
1 answer
832 views

convert a uint8 array ( javascript new Uint8Array() ) to binary format (or binary mode ?) in shell / bash?

The question is about convert from new Uint8Array() generated from javascript into a normal file (file that can be used with other programs like ffmpeg) ** I don't know the right term to use if it's ...
John R's user avatar
  • 115
0 votes
1 answer
579 views

Counting file extensions in a string array

I have a $FILES array which consists of strings of 4 filenames: FILES=$(ls /my/directory | shuf -n 4) The contents of the array look something like: file1.jpg file2.jpg file3.gif file4.mp4 I want to ...
Nemdr's user avatar
  • 11

1
2 3 4 5
13