I'm trying to work through and insert some data into a table that contains an array
INSERT INTO SUBURB VALUES (
'Perth',
'5684',
SDO_GEOMETRY(
2003,
NULL,
NULL,
SDO_ELEM_INFO_ARRAY(1,1003,1)
SDO_ORDINATE_ARRAY(391209,6464720 390919,6463010 392833,6463280 392902,6464290 391209,6464720)
)
);
I'm getting syntax error on the SDO_ORDINATE_ARRAY line and can't work it out.
The following syntax is wrong
SDO_ORDINATE_ARRAY(391209,6464720 390919,6463010 392833,6463280 392902,6464290 391209,6464720)
The general format of an array in postgressql is
'{ val1 delim val2 delim ... }'
if this is a one dimension array replace the whitespace with a comma.
If you want to store a multi dimension array use curly brackets
e.g.
'{{1,2,3},{4,5,6},{7,8,9}}'
Reference https://www.postgresql.org/docs/9.1/arrays.html
To write an array value as a literal constant, enclose the element
values within curly braces and separate them by commas. (If you know
C, this is not unlike the C syntax for initializing structures.) You
can put double quotes around any element value, and must do so if it
contains commas or curly braces. (More details appear below.) Thus,
the general format of an array constant is the following:
Related
I'm having a hard time figuring out what this code does, because googling square brackets doesn't yield appropriate results for the way the search engine works.
id2 is a 1x265 array (so basically a 1d vector with 265 values)
m is a 1x245 array (so basically a 1d vector with 245 values)
id2 = id2([m m(end)+1]);
For what I've seen so far, there always is a comma between the first and second value in the square brackets.
If it was
id2 = id2[m, m(end)+1]
In my little Matlab experience I would have known its meaning but this is not the case, never seen this one before.
The square brackets are also enclosed in brackets ( ) after id2 so this makes me think that
id2 = id2([m m(end)+1]) and id2 = id2[m, m(end)+1] are two completely different things.
Can you explain me what that code does please?
[1, 2] and [1 2] are equivalent. Either a comma or a space can denote element separation when building arrays using square brackets.
Indexing, using parentheses (), has to be done using commas: A(3,1), not A(3 1). The same holds for argument lists in functions: mean(A,[],1) needs commas to separate the various parameters.
id2 = id2([m m(end)+1]); should then be clear: you build an array [m m(end)+1], i.e. you take m and add one extra element, m(end)+1, to its end. These should be integers, presumably, since the look like they are indexing into id2. Given the above, id2 = id2([m, m(end)+1]); is exactly equivalent.
I can recommend reading this post on the various ways of indexing in MATLAB.
PowerShell drops the trailing zero of an array element's value when that value contains a dot. Unless I wrap the value in quotes. Unfortunately I need to retain the trailing zero and the script users fail to use quotes reliably.
How can I coerce PowerShell to preserve the trailing zero?
The array element format is Number.Number
The array element may have 1 to n trailing zeros
Single element arrays retain trailing zeros
Multi element arrays drop trailing zeros
Strongly typing the parameter as [string[]] does not resolve the issue.
Example:
function Get-Customer {
Param
(
[string[]]$CustomerId
)
$CustomerId
}
> Get-Customer -CustomerId 654.1,654.10,654.1721,654.1720
654.1 #CORRECT
654.1 #INVALID
654.1721 #CORRECT
654.172 #INVALID
You cannot preserve that 0 if the caller does not put it in quotes. The fact is that those values will be interpreted as numbers because they fit the pattern of number literals.
So if you can't change your caller's behavior then they will be numbers before they ever enter your function. Your [string[]] cast will convert the number to a string, but it's already a number at that point and will follow number -> string rules.
PowerShell is very lenient when it comes to type conversion, or rather, it tries hard to successfully convert types when there is a mismatch, so it will be difficult to throw an error in this case too (you have no access to the original values to know anything is wrong as this happens during parameter binding).
You could do this:
function Get-Customer {
Param
(
[ValidateScript({
$_ -is [string]
})]
[object[]]$CustomerId
)
$CustomerId
}
It would force the values passed in to already be a [string], which kind of sucks for all the other cases where string conversion would have been useful though.
I'm with briantist, to reduce quoting, you could split one string:
function Get-Customer {
Param
(
[ValidateScript({
$_ -is [string]
})]
[object[]]$CustomerId
)
$CustomerId -split ','
}
> Get-Customer -CustomerId '654.1,654.10,654.1721,654.1720',"1.000,2.00"
654.1
654.10
654.1721
654.1720
1.000
2.00
How about this trick. No commas, no quotes, still getting an array and maintaining all your items as is, like this...
function Get-CustomerId
{ $args }
New-Alias -Name cid -Value Get-CustomerId
cid 654.1 654.10 654.1721 654.1720
# Results
654.1
654.10
654.1721
654.1720
I am trying to split the string by ':' and store it in an array, so something that looks like a:b:c:d:x:y:z will be stored in an array which holds, a, b, c, d, x, y, z as elements.
What I have written is
IFS = ':' read - r -a ARR <<< "$INFO"
where INFO is a string which is being read in from a file containing multiple strings in the aforementioned format.
I get an error saying "IFS: command not found".
I am reading them in this way:
while read INFO
Lastly when I try to assign the first element in the array to a variable, I am getting an error:
export NAME = $INFO[0]
the two errors I get here are export: '=' not a valid identifier and export: '[0]: not a valid identifier
I am relatively a newcomer to bash.
The basic problem here is that your code contains spaces in places where they aren't allowed. For instance, the following is perfectly fine syntax (though it fails to comply with POSIX conventions on variable naming, which advises lowercase characters be used for application-defined names):
info_string='a:b:c:d:x:y:z'
IFS=: read -r -a info_array <<< "$info_string"
Similarly, on a dereference, you need curly braces, and (again) can't put spaces around the =:
name=${info_array[0]}
This works:
s=a:b:c:d #sample string
IFS=:
a=( $s ) #array
printf "'%s' " "${a[#]}" #prints 'a' 'b' 'c' 'd'
The syntax to get the n-th item in an array is
${array_name[$index]}
(The curlies are required), so you need export NAME="${INFO[0]}" (assignments normally don't need to be quoted, however with export, declare, local, and similar, it's better to quote).
https://www.lukeshu.com/blog/bash-arrays.html is a good tutorial on how bash arrays work.
Say you have a zsh array like:
a=("x y" "v w")
I want to take the first word of every element, say:
b=()
for e in $a; {
b=($b $e[(w)0])
}
So now I have what I need in b:
$ print ${(qq)b}
'x' 'v'
Is there a way to do this in a single expansion expression? (i.e. not needing a for loop for processing each array element and accumulating the result in a new array).
It could be possible to take the word by removing from the first occurrence of a white space to the end in each element of the array like this:
$ print ${(qq)a%% *}
'x' 'v'
It coulde be noted that the%% expression (and some others) could be used for array elements:
In the following expressions, when name is an array and the substitution is not quoted, or if the ‘(#)’ flag or the name[#] syntax is used, matching and replacement is performed on each array element separately.
...
${name%pattern}
${name%%pattern}
If the pattern matches the end of the value of name, then substitute the value of name with the matched portion deleted; otherwise, just substitute the value of name. In the first form, the smallest matching pattern is preferred; in the second form, the largest matching pattern is preferred.
-- zshexpn(1): Expansion, Parameter Expansion
Concatenating the nested arrays {{1,2}} and {{3,4}} is no problem at all:
SELECT array_cat(
ARRAY[ARRAY[1,2]]
, ARRAY[ARRAY[3,4]]
)
array_cat
---------------
{{1,2},{3,4}}
But how to concatenate {{1,2}} and {{3}} in order to get {{1,2},{3}}?
SELECT array_cat(
ARRAY[ARRAY[1,2]]
, ARRAY[ARRAY[3]]
)
psql: …: ERROR: cannot concatenate incompatible arrays
DETAIL: Arrays with differing element dimensions are not compatible
for concatenation.
This is impossible in PostgreSQL. Multi-dimensional arrays must have the same number of element dimensions, just as the error message informs. Per documentation:
Multidimensional arrays must have matching extents for each dimension. A mismatch causes an error.
You might want to pad with NULL or some other dummy value ...