Array ( data structure ) support and being able to index elements

0 votes
asked Nov 8 in Wanted features by kasra
edited Nov 8

hi,

it would be nice if we could  have array ( data structure ) support and being able to index the elements.

On the website there is an exemple  showing how to use a while loop inot a function or procedure.

Based on that exemple here is the type of thing i would like to achieve : 

@startuml

'!$colors=["red","yellow","blue","green","white"];

'!$shapes=["circle","rectangle","square","triangle"];

title Array support exemple

!procedure $foo($arg)

  :procedure start;

  !while $arg!=0

    !$i=2

    #palegreen:arg=$arg;

    !while $i!=0

    'it would be nice here to be able able

    'indexing the arrays like so  $colors[$arg], $shapes[$i]

      :arg=$arg and i=$i;      

      !$i = $i - 1

    !endwhile

    !$arg = $arg - 1

  !endwhile

  :procedure end;

!endprocedure

start

$foo(2)

end

@enduml 

If there is already a feature that helps us to accomplish this task, could you please give me a link in the documentation ?

Thanks. 

1 Answer

0 votes
answered Nov 9 by The-Lu (10,120 points)
 
Best answer

Hello K.,

FYI, PlantUML can manage JSON array, and you can use:

!$data={
  "colors": ["red", "yellow", "blue", "green", "white"],
  "shapes": ["circle", "rectangle", "square", "triangle"]
}

And access to data:

'with JSON (zero-based array), you can have: $data.colors[$arg] and $data.shapes[$i]...

Then, here is the full example:

@startuml
!$data={
  "colors": ["red", "yellow", "blue", "green", "white"],
  "shapes": ["circle", "rectangle", "square", "triangle"]
}
title Array support example
!procedure $foo($arg)
  :procedure start;
  !while $arg!=0
    !$i=2
    #palegreen:arg=$arg;
    !while $i!=0
    'it would be nice here to be able able
    'indexing the arrays like so  $colors[$arg], $shapes[$i]
    'with json (zero-based array), you can have: $data.colors[$arg] and $data.shapes[$i]...
      #$data.colors[$arg]:arg=$arg and i=$i / $data.colors[$arg] - $data.shapes[$i];
      !$i = $i - 1
    !endwhile
    !$arg = $arg - 1
  !endwhile
  :procedure end;
!endprocedure
start
$foo(2)
end
@enduml

See also:

If that can help,
Enjoy,
Regards,
Th.

commented Nov 9 by kasra
that's perfect, exactly what i wanted !

thanks for the links.
...