Comparing and thenComparingInt not working within the same string - string

I'm trying to get some .txt files , reading them in order and then writing their content( with some logic) into an excel sheet.
The files are named like this:
FileB_60.txt
FileB_90.txt
FileB_120.txt
fileA_60.txt
fileA_90.txt
fileA_120.txt
In the end, I want them to be read just like that, ordered by name and number. If I don't use any sorting step, they're read randomly.
String fileExtension = ".txt";
try (Stream<Path> paths = Files.walk(Paths.get(resourcesPath))) {
Comparator<Path> byName = Comparator.comparing(p -> p.getFileName().toString());
Comparator<Path> byNameAndNumber = byName.thenComparingInt(p -> Integer.parseInt(p.getFileName().toString().split("_")[1].replace(fileExtension, "")));
for (Path eachFilePath : paths
.filter(p -> p.getFileName().toString().endsWith(fileExtension))
.sorted(byNameAndNumber)
.collect(Collectors.toList()))
...
This way, they're read like:
FileB_120.txt
FileB_60.txt
FileB_90.txt
fileA_120.txt
fileA_60.txt
fileA_90.txt
So it seems like it's ordering only by the name (capital letter first)
I was wondering if I'm doing something wrong here, since I'm sorting within the same string

One important thing about thenComparing... methods: the comparator injected is only used when the initial comparator (called byName in your example) considers the 2 elements are equal, which is never the case in your example.
The comparator byName must compare only the first part of the file name (the one before the _).
Comparator<Path> byName = Comparator.comparing(
p -> p.getFileName().toString().split("_")[0]);

Related

Remove a map item in terraform

If I use the following yamldecode, I get the following output:
output "product" {
value = distinct(yamldecode(file("resource/lab/abc.yaml"))["account_list"]["resource_tags"][*]["TAG:product"])
}
Output:
+ product = [
+ "fargate",
+ "CRM",
]
I want fargate to be removed from my output and the expected output is this:
+ product = [
+ "CRM"
]
Please let me know how I can do this.
output "product" {
value = compact([for x in distinct(yamldecode(file("resource/lab/abc.yaml"))["account_list"]["resource_tags"][*]["TAG:product"]) : x == "fargate" ? "" : x])
}
I get this output:
test = [
"enter",
]
compact function solved the problem.
The Terraform language is based on functional principles rather than imperative principles and so it does not support directly modifying an existing data structure. Instead, the best way to think about goals like this is how to define a new value which differs from your existing value in some particular way.
For collection and structural types, the most common way to derive a new value with different elements is to use a for expression, which describes a rule for creating a new collection based on the elements of an existing collection. For each element in the source collection we can decide whether to use it in the result at all, and then if we do decide to use it we can also describe how to calculate a new element value based on the input element value.
In your case you seem to want to create a new list with fewer elements than an existing list. (Your question title mentions maps, but the question text shows a tuple which is behaving as a list.)
To produce a new collection with fewer elements we use the optional if clause of a for expression. For example:
[for x in var.example : x if x != "fargate"]
In the above expression, x refers to each element of var.example in turn. Terraform first evaluates the if clause, and expects it to return true if the element should be used. If so, it will then evaluate the expression immediately after the colon, which in this case is just x and so the result element is identical to the input element.
Your example expression also includes some unrelated work to decode a YAML string and then traverse to the list inside it. That expression replaces var.example in the above expression, as follows:
output "products" {
value = toset([
for x in yamldecode(file("${path.module}/resource/lab/abc.yaml"))["account_list"]["resource_tags"][*]["TAG:product"]: x
if x != "fargate"
])
}
I made some other small changes to the above compared to your example:
I named the output value "products" instead of "product", because it's returning a collection and it's Terraform language idiom to use plural names for collection values.
I wrapped the expression in toset instead of distinct, because from your description it seems like this is an unordered collection of unique strings rather than an ordered sequence of strings.
I added path.module to the front of the file path so that this will look for the file in the current module directory. If your output value is currently in a root module then this doesn't really matter because the module directory will always be the current working directory, but it's good practice to include this so that it's clear that the file belongs to the module.
Therefore returning a set is more appropriate than returning a list because it communicates to the caller of the module that they may not rely on the order of these items, and therefore allows the order to potentially change in future without it being a breaking change to your module.

Calling a vector from a string

I am attempting to write an algorithm that selects a specific reference standard (vector) as a function of temperature. The temperature values are stored in a structure ( procspectra(i).temperature ). My reference standards are stored in another structure ( standards.interp.zeroed.ClOxxx ) where xxx are numbers such as 200, 210, 220, etc. I have built the rounding construct and paste it below.
for i = 1:length(procspectra);
if mod(-procspectra(i).temperature,10) > mod(procspectra(i).temperature,10);
%if mod(-) > mod(+) round down, else round up
tempvector(i) = procspectra(i).temperature - mod(procspectra(i).temperature,10);
else
tempvector(i) = procspectra(i).temperature + mod(-procspectra(i).temperature,10);
end
clostd = strcat('standards.interp.zeroed.ClO',num2str(tempvector(i)));
end
This construct works well. Now, I have built a string which is identical to the name of the vector I want to invoke, but I'm uncertain how to actually call the vector given that this is encoded as a string. Ideally I want to do something within the for-loop like:
parameters(i).standards.ClOstandard = clostd
where I actually am assigning that parameter structure to be the same as the vector I have saved in the standards structure I have previously generated (and not just a string)
Could anyone help out?
Don't construct clostd like that (containing the full variable name), make it contain only the last field name instead:
clostd = ['ClO' num2str(tempvector(i))];
parameters(i).standards.ClOstandard = standards.interp.zeroed.(clostd);
This is the syntax of accessing a structure's field dynamically, using a string. So the following three are equivalent:
struc.Cl0123
struc.('Cl0123')
fieldn='Cl0123'; struc.(fieldn)

how in matlab check for existence of a structure with fields in the workspace

I have a string and want to check if in the workspace exist any variable with the same name. In the workspace I have also many structures M.N.O M.N.N M.N.M etc. I can only check if there exist a variable with the name M. How to go deeper into this structure?
I tried:
exist('M.N')
YesNo = any(strcmp(who,'M.N.O'))
evalin('base','exist(''M.N.O'',''var'')')
all give me the same problem so I am stuck.
You can use isfield to check if a variable has a specific field. See the link for examples!
For your example, you'd need:
isfield(M,'N')
and if true, you can go deeper:
isfield(M.N,'O')
Notice that
isfield(M,'N.O')
won't work ;)
One option: write a recursive function to expand structures down to their leaf fields, appending the fields to a list.
(untested, conceptual code - probably won't work quite right as is)
function varlist = getStructFields(var,varlist)
if isstruct(var)
fn = fieldnames(var);
varlist = vertcat(varlist,fn); %# append fields to the list
for field = fn' %# ' create row vector; iterate through fields
varlist = getStructFields(var.(char(field)), varlist); %# recursion here
end
end
end
Then you can use the any(strcmp(who,'M.N.O')) check you already came up with.

constructing data-type instances from CSV

I have CSV data (inherited - no choice here) which I need to use to create data type instances in Haskell. parsing CSV is no problem - tutorials and APIs abound.
Here's what 'show' generates for my simplified trimmed-down test-case:
JField {fname = "cardNo", ftype = "str"} (string representation)
I am able to do a read to convert this string into a JField data record. My CSV data is just the values of the fields, so the CSV row corresponding to JField above is:
cardNo, str
and I am reading these in as List of string ["cardNo", "str"]
So - it's easy enough to brute-force the exact format of "string representation" (but writing Java or python-style string-formatting in Haskell isn't my goal here).
I thought of doing something like this (the first List is static, and the second list would be read file CSV) :
let stp1 = zip ["fname = ", "ftype ="] ["cardNo", "str"]
resulting in
[("fname = ","cardNo"),("ftype =","str")]
and then concatenating the tuples - either explicitly with ++ or in some more clever way yet to be determined.
This is my first simple piece of code outside of tutorials, so I'd like to know if this seems a reasonably Haskellian way of doing this, or what clearly better ways there are to build just this piece:
fname = "cardNo", ftype = "str"
Not expecting solutions (this is not homework, it's a learning exercise), but rather critique or guidelines for better ways to do this. Brute-forcing it would be easy but would defeat my objective, which is to learn
I might be way off, but wouldn't a map be better here? I guess I'm assuming that you read the file in with each row as a [String] i.e.
field11, field12
field21, field22
etc.
You could write
map (\[x,y] -> JField {fname = x, ftype = y}) data
where data is your input. I think that would do it.
If you already have the value of the fname field (say, in the variable fn) and the value of the ftype field (in ft), just do JField {fname=fn, ftype=ft}. For non-String fields, just insert a read where appropriate.

Erlang Building Dynamic File Names

I am trying to create a file with a name based on an integer value from a function, clearly below does not work but gives you the idea :
getValue() -> 1.
createFile() ->
{ok, File} = file:open( getValue(), [write]),
io:format(File,"Test~n"),
file:close(File).
This ought to be simple, even with Erlangs lack of support for strings, so I must just be missing something obvious ( as is the price of being new to something ) :
If you just want to open a file whose name is "1", then you can use integer_to_list/1 to do that (since a string is just a list of integers for the ASCII values of the characters):
getValue() -> 1.
....
{ok, File} = file:open(integer_to_list(getValue()), [write]),
If you're wanting to create a filename based on the value from getValue/0, then the same principle applies, but you just create your filename from gluing several lists together.

Resources