Convert array of integers to string [duplicate] - arrays

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 ','

Related

Converting bash array into string with - in between [duplicate]

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

Working with string multidimensionnal-arrays [duplicate]

This question already has answers here:
Creating a Jagged\Multidimensional array with a single array inside of it
(1 answer)
Return Multidimensional Array From Function
(3 answers)
Append an Array to an Array of Arrays in PowerShell
(2 answers)
Closed 4 years ago.
Issue
I am trying to reconstruct an array from the parts that constitute it but I am failing to obtain the same result. I instead end up with a single-dimension array with all the values from the respective arrays.
Question
How can I make sure that $brray is populated with the individual arrays and not only the values that they contain?
MWE
$array = #(("a1","a2"),("b1","b2"))
$a = #("a1","a2")
$b = #("b1","b2")
$brray = #()
$brray += $a
$brray += $b
function test(){
Param(
[string[]]
$array
)
return $array
}
test($array)
test($brray)
Output
$array
a1 a2
b1 b2
$brray
a1
a2
b1
b2

awk - print last field $NF of array [duplicate]

This question already has answers here:
How can I get the length of an array in awk?
(9 answers)
Closed 6 years ago.
is any chance how to print - lets say example:
A/b/c/d/e/f A B C D E F G|H|I|J|
I would like to split fields to array and print:
awk -v OFS="\t" '{split($1,a,"/"); split($3,b,"|"); print a[LAST_FIELD??],$1,$2,$3,b[1],b[2]}' input
the result should be:
f A B C g H I
I am not sure how to define "LAST_FIELD" in my array. Thank you for any help.
split returns the number of elements, so you can use that:
n = split($1,a,"/")
then:
print a[n]
You can use this awk:
awk -v OFS="\t" '{lena=split($1,a,"/"); split($NF,b,"|");
print a[lena],$2,$3,$4,b[1],b[2],b[3]}' file
f A B C G H I

Perl delete similar strings in an array [duplicate]

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;

Getting Missing element from comparison of Array's using powershell [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Comparing two arrays & get the values which are not common
I wanted a logic to get uncommon items from an array, for example:
$a=#(1,2,3,4,5,6)
$b=#(1,2,3,4,5,7,9,10)
I want the output $c to be 6 which is the missing element in $b array, priority should be only given to the array contents of $a.
Can anyone please help me out with this?
Thanks!
Either empo's approach, or
$a1=#(1,2,3,4,5,8)
$b1=#(1,2,3,4,5,6)
Compare-Object $a1 $b1 |
Where-Object { $_.SideIndicator -eq '<=' } |
Foreach-Object { $_.InputObject }
returns 8
$c = $a | ? {!($b -contains $_)}
The priority will be given to the variable you "pipe".

Resources