Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get index position as value while doing do list #349

Open
TobiasNx opened this issue May 2, 2024 · 5 comments
Open

Get index position as value while doing do list #349

TobiasNx opened this issue May 2, 2024 · 5 comments

Comments

@TobiasNx
Copy link
Collaborator

TobiasNx commented May 2, 2024

For a hbz project we need the option to state the position of an value within an array, e.g. while doing do list.

There are three ways I can think of how to manage this:

a) a function index_position that adds/creates an element with the position while iterating over an list:

input:

a:
	- dog
	- cat
	- bird
set_array("animals[]")
do list("a","var":"$i")
	copy_field("$i","animals[].$append.name")
	index_position("$i","animals[].$last.pos")
end
retain("animals[]")
animals:
	- name: dog
	  pos: 1
	- name: cat
	  pos: 2
	- name: bird
	  pos: 3

  1. Something with using a function that is requested in Copy path name as value? #348. To get the path as value and delete everything but the trailing digits:

input:

a:
	- dog
	- cat
	- bird
set_array("animals[]")
do list("a","var":"$i")
	copy_field("$i","animals[].$append.name")
	path_as_value("$i","animals[].$last.pos")
end

replace_all("animals[].*.pos","^.*(\\d*)$","$1")
retain("animals[]")
animals:
	- name: dog
	  pos: 1
	- name: cat
	  pos: 2
	- name: bird
	  pos: 3

  1. Providing a counter, that adds a value everytime a path is requested:

input:

a:
	- dog
	- cat
	- bird
set_array("animals[]")
do list("a","var":"$i")
	copy_field("$i","animals[].$append.name")
	counter("$i","animals[].$last.pos")
end

retain("animals[]")
animals:
	- name: dog
	  pos: 1
	- name: cat
	  pos: 2
	- name: bird
	  pos: 3

@TobiasNx
Copy link
Collaborator Author

TobiasNx commented May 2, 2024

It could also be solved in a more complex way with a math function which is requested in #350

@blackwinter
Copy link
Member

For a hbz project we need the option to state the position of an value within an array, e.g. while doing do list.

Can you clarify what the actual use case is? Why do you need the position?

@TobiasNx
Copy link
Collaborator Author

TobiasNx commented May 2, 2024

Transforming data to mets and creating a structural reference within the metadata based on the position within the transformation.
The usecase is to replace the position() function of an xslt, or number() function, as stated in this discussion:

https://stackoverflow.com/questions/4449810/using-position-function-in-xslt

@TobiasNx
Copy link
Collaborator Author

TobiasNx commented May 2, 2024

i thought I have a workaround, but this not to be working:

would be a complex solution for this:

set_array("animals[]")
set_array("animals_index")
do list(path:"a[]","var":"$i")
	copy_field("$i","animals[].$append.name")
	copy_field("$i","animals_index.$append")
	copy_field("animals_index","animals[].$last.pos")
end

count("animals[].*.pos")
retain("animals[]")

This results in:

{
  "animals" : [ {
    "name" : "dog",
    "pos" : "6"
  }, {
    "name" : "cat",
    "pos" : "6"
  }, {
    "name" : "bird",
    "pos" : "6"
  } ]
}

PS: And this variant:

set_array("animals[]")
set_array("animals_index")
do list(path:"a[]","var":"$i")
	copy_field("$i","animals[].$append.name")
	copy_field("$i","animals_index.$append")
	copy_field("animals_index","animals[].$append.pos")
end

do list(path:"animals[]","var":"$i")
    count("$i.pos")
end

retain("animals[]")

Results in

{
  "animals" : [ {
    "name" : "dog",
    "pos" : "dog"
  }, {
    "name" : "cat",
    "pos" : "2"
  }, {
    "name" : "bird",
    "pos" : "3"
  } ]
}

So almost. This seems to be connected to #239

@blackwinter
Copy link
Member

blackwinter commented May 2, 2024

Clever idea ;) Yes, you have to do it in each iteration instead of once at the end:

set_array("animals[]")
set_array("animals_index")

do list(path: "a[]", "var": "$i")
  copy_field("$i", "animals[].$append.name")

  # keep track of items (globally)
  copy_field("$i", "animals_index.$append")

  # determine current count (locally)
  set_array("animals[].$last.pos")
  copy_field("animals_index", "animals[].$last.pos.$append")
  count("animals[].$last.pos")
end

retain("animals[]")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants