This question already has answers here:
How do I remove duplicate items from an array in Perl?
(11 answers)
Closed 7 years ago.
I am new to Perl programming.
I need to find and delete partially matching strings in an array.
For example in my array there are strings:
#array = qw(abcd.txt abcdeff.txt abcdweff.txt abcdefrgt.txt);
I just want the first abcd.txt to be saved to the array and to delete the rest (which are similar in the first 4 characters) i.e. so that it will print only abcd.txt when #print "#array"; is called.
my %seen;
#array = grep !$seen{ substr($_,0,4) }++, #array;
Related
This question already has answers here:
How can I join elements of an array in Bash?
(34 answers)
Closed 4 months ago.
How do I convert an array into a string with a dash '-' in between in bash. For eg, I have this array
arr=(a b c d e)
I want to convert it into a string "a-b-c-d".
I figured out this "a-b-c-d-e," but there is an unwanted dash at the end. Is there an efficient way of doing this?
Thanks
This is where the "${arr[*]}" expansion form is useful (note the double quotes and the * index). This joins the array elements using the first character of the IFS variable
arr=(a b c d e)
joined=$(IFS='-'; echo "${arr[*]}")
declare -p joined
# => declare -- joined="a-b-c-d-e"
But if you want all-but-the-last elements, you'll combine this with the ${var:offset:length} expansion
joined=$(IFS='-'; echo "${arr[*]:0: ${#arr[#]} - 1}")
declare -p joined
# => declare -- joined="a-b-c-d"
This one's a little tricker. The offset and length parts of that expansion are arithmetic expressions. I'm calculating the length as "the number of elements in the array minus one".
Note how I'm defining IFS inside the command substitution parentheses: this is overriding the variable in the subshell of the command substitution, so it won't affect the IFS variable in your current shell.
Using awk if you want to remove the last entry
$ awk -vOFS=- '{NF--;$1=$1}1' <<<${arr[#]}
a-b-c-d
or the complete array
$ awk -vOFS=- '{$1=$1}1' <<<${arr[#]}
a-b-c-d-e
This question already has answers here:
bash script loop through two variables in lock step
(2 answers)
bash shell script two variables in for loop
(8 answers)
bash script loop multiple variables
(2 answers)
Closed 1 year ago.
Let's say I have three arrays that look like the following:
a=(banana apple grape)
b=(1 2 3)
c=(yellow red purple)
If I wanted to loop through such that each index of each variable is matched, how would I do this? For example, I would want something like the following:
for i in range(0,len(a))
do
echo "$a[i] $b[i] $c[i]"
done
such that the results would be:
banana 1 yellow
apple 2 red
grape 3 purple
The syntax to access array elements is ${array[index]}.
for ((i=0 ; i < ${#a[#]} ; ++i)) ; do
echo "${a[i]} ${b[i]} ${c[i]}"
done
You can use seq to simulate Python's range:
for i in $(seq 1 ${#a[#]}) ; do
echo "${a[i-1]} ${b[i-1]} ${c[i-1]}"
done
where ${#array[#]} returns the size of the array.
This question already has answers here:
How to sort an array in Bash
(20 answers)
Closed 4 years ago.
for example, if I want to write a script that get strings as arguments, and I want to insert them to array array_of_args and then I want to sort this array and to make sorting array.
How can I do it?
I thought to sort the array (and print it to stdout) in the next way:
array_of_args=("$#")
# sort_array=()
# i=0
for string in "${array_of_args[#]}"; do
echo "${string}"
done | sort
But I don't know how to insert the sorting values to array ( to sort_array)..
You can use following script to sort input argument that may contain whitespace, newlines, glob characters or any other special characters:
#!/usr/bin/env bash
local args=("$#") # create an array of arguments
local sarr=() # initialise an array for sorted arguments
# use printf '%s\0' "${args[#]}" | sort -z to print each argument delimited
# by NUL character and sort it
# iterate through sorted arguments and add it in sorted array
if (( $# )); then
while IFS= read -rd '' el; do
sarr+=("$el")
done < <(printf '%s\0' "${args[#]}" | sort -z)
fi
# examine sorted array
declare -p sarr
This question already has answers here:
How to concatenate array of integers into comma separated string
(4 answers)
Closed 5 years ago.
I found this snippet for doing a string to array:
$c = "2,3,4,5,6,7,10..12".split(',') | % {iex $_}
How would I do the reverse to convert $c back to a string like "2,3,4,5,6,7,10,11,12"? I of course don't require to abbreviate it back to "2..7,10..12".
You are looking for -join:
$c -join ','
This question already has answers here:
Merging two Bash arrays into Cartesian key:value pairs
(5 answers)
Closed 6 years ago.
I have two arrays: array1 and array2:
array1=( a b c )
array2=( 1 2 3 )
How do I make a third array, array3:
array3=( a b c 1 2 3 )
This question is different from Combine arrays in the beginning of a for-loop (Bash) because this deals exclusively with combining arrays, not whether such a statement is legal within a for loop. Thus, it is more general.
From The Advanced Bash Scripting Guide example 27-10, with a correction:
declare -a array1=( zero1 one1 two1 )
declare -a array2=( [0]=zero2 [2]=two2 [3]=three2 )
dest=( "${array1[#]}" "${array2[#]}" )
Thus, for my case, it's:
array3=( "${array1[#]}" "${array2[#]}" )