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
Is there a consistent way to use a PDL to index an @array? #400
Comments
I'm afraid I don't understand what you mean when you say Please show some code that demonstrates the problem you are having with indexing; indexing an ndarray with either |
I wanted to use a $pdl as an index to a perl array, so Perl needs to see the evaluation as an int. For example: @a = (1,2,3);
$p = pdl [2];
print $a[$p] The print should show "3" but it shows "1" because How do you go back and forth (reliably) between a perl int and a pdl int? |
@KJ7LNW, if what you have is a single-element ndarray, you can use use feature qw(say);
use PDL;
say pdl(2)->sclr; # 2
say pdl([2])->sclr; # 2
say pdl([[2]])->sclr; # 2 Though I'm wondering why not also convert the Perl array to a PDL ndarray? I suppose if you only want a single index, it makes sense. |
@zmughal, I think sclr is the way to go! Thanks for pointing that out. In this case The PDL::Opt::Simplex::Simple module blurs the line between PDL and Perl data types in a few places to do what it does. I'm sure in some places that is just because I'm new to PDL, but I think I need ->sclr in at least in the array of data structures case. Maybe https://metacpan.org/pod/PDL::Core#unpdl should have a note to the effect of "see also $pdl->sclr" because "sclr is generally used when a Perl scalar is required instead of a one-element ndarray." @zmughal, |
Ah, yes, for indexing those data-structures, that makes sense.
What that means is that if an ndarray contains more than one element, it # with 2.060
$ use feature qw(say); use PDL; say join " | ", $PDL::VERSION, pdl([[2]])->sclr, do { eval { pdl([3, 4])->sclr } or $@ }
2.060 | 2 | 3 # with 2.079
$ use feature qw(say); use PDL; say join " | ", $PDL::VERSION, pdl([[2]])->sclr, do { eval { pdl([3, 4])->sclr } or $@ }
2.079 | 2 | multielement ndarray in 'sclr' call at .../PDL/Core.pm line 3071. For the above example ndarrays, all have a single element, but differ in the number of dimensions: use feature qw(say);
use PDL;
my @pdls = ( pdl(2), pdl([2]), pdl([[2]]) );
say join "\n====\n", map {
$_->string . "\n" . join ' | ', $_->nelem, $_->ndims
} @pdls;
# 2
# 1 | 0
# ====
# [2]
# 1 | 1
# ====
#
# [
# [2]
# ]
#
# 1 | 2 |
There are mailing lists and IRC. More info at: https://pdl.perl.org/?page=mailing-lists. But we appreciate input here on the issue tracker as well! |
Thanks @zmughal! I think that answers my questions. Much appreciated. |
I realize this isn't a bug report, so if you prefer this type of question on a mailing list or somewhere else please direct me to the right place:
I've run into what is (for me) a confusing issue using PDL's to index perl arrays. Maybe that should always be avoided?
Here is what I've found:
Then looking at the resulting structure from
unpdl
, these are the same structure:Same structure, but
pdl 2
can be used as an array index andpdl [2]
cannot becauseint(pdl[2])==0
.The text was updated successfully, but these errors were encountered: