HP Forums
Size of a list passed to a function? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Size of a list passed to a function? (/thread-8162.html)



Size of a list passed to a function? - toml_12953 - 04-14-2017 12:30 PM

I'm trying to get the size of a list passed to a function. I've tried this:

PHP Code:
EXPORT dimension(mylist)
BEGIN
  
PRINT(SIZE(mylist));
END

and

PHP Code:
EXPORT dimension(mylist)
BEGIN
  
PRINT(SIZE({mylist}));
END

but neither works although SIZE does work from the Home screen.

Tom L


RE: Size of a list passed to a function? - Tyann - 04-14-2017 12:43 PM

PHP Code:
EXPORT dimension(mylist)
BEGIN
  
PRINT(SIZE(mylist));
END
fonctionne pour moi sur la machine physique.

Works for me on the physical machine.


RE: Size of a list passed to a function? - toml_12953 - 04-14-2017 02:17 PM

(04-14-2017 12:43 PM)Tyann Wrote:  
PHP Code:
EXPORT dimension(mylist)
BEGIN
  
PRINT(SIZE(mylist));
END
fonctionne pour moi sur la machine physique.

Works for me on the physical machine.

I over simplified my program. I'm shooting for a program to automatically create a multi-dimensional list and am hoping to use a variable number of dimensions. Right now, I have to create a loop for each dimension. Here's a better sample.
It works for a fixed number of dimensions if I take out the PRINT.

PHP Code:
EXPORT dimension(varname,mylist)
BEGIN
    local v
,a;
    
v:=HVars(varname);
    print(
SIZE(mylist));
    for 
R:=1 to mylist(1) do
        for 
C:=1 to mylist(2) do
            for 
a:=1 to mylist(3) do    
                
v(R,C,a):=0;
            
end;
        
end;
    
end;
    return 
v;
END



RE: Size of a list passed to a function? - cyrille de brébisson - 04-14-2017 02:29 PM

Hello,

What are you passing as a parameter to the dimention function?

Cyrille


RE: Size of a list passed to a function? - toml_12953 - 04-14-2017 02:42 PM

(04-14-2017 02:29 PM)cyrille de brĂ©bisson Wrote:  Hello,

What are you passing as a parameter to the dimention function?

Cyrille

I'm calling it like this: As I said, it works fine if I take out the PRINT.

dimension(L1,{4,3,5})

Tom L


RE: Size of a list passed to a function? - Didier Lachieze - 04-14-2017 02:58 PM

(04-14-2017 02:17 PM)toml_12953 Wrote:  I'm shooting for a program to automatically create a multi-dimensional list and am hoping to use a variable number of dimensions.

This will create recursively a multi-dimensional list whose dimensions are passed as a list and initialized with 0:
Code:
EXPORT dimension(mylist)
BEGIN
  LOCAL l:=tail(mylist),v:={};
  IFTE(SIZE(mylist)==1,MAKELIST(0,I,1,mylist(1)),MAKELIST(dimension(l),I,1,my​list(1)));
END;

dimension({3,2,2}) returns {{{0,0},{0,0}},{{0,0},{0,0}},{{0,0},{0,0}}}


RE: Size of a list passed to a function? - toml_12953 - 04-14-2017 03:20 PM

(04-14-2017 02:58 PM)Didier Lachieze Wrote:  
(04-14-2017 02:17 PM)toml_12953 Wrote:  I'm shooting for a program to automatically create a multi-dimensional list and am hoping to use a variable number of dimensions.

This will create recursively a multi-dimensional list whose dimensions are passed as a list and initialized with 0:
Code:
EXPORT dimension(mylist)
BEGIN
  LOCAL l:=tail(mylist),v:={};
  IFTE(SIZE(mylist)==1,MAKELIST(0,I,1,mylist(1)),MAKELIST(dimension(l),I,1,my​list(1)));
END;

dimension({3,2,2}) returns {{{0,0},{0,0}},{{0,0},{0,0}},{{0,0},{0,0}}}

Very elegant! As usual, I try to do things the hard way. Thank you for saving me a lot of work!

Tom L