in reply to Reading a array

No; $#arr1 is returning the correct number of elements in the array; it's just not returning the number of elements you expect to be in the array. Where do you populate @arr1?


Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Replies are listed 'Best First'.
Re^2: Reading a array
by jethro (Monsignor) on Aug 08, 2008 at 14:10 UTC
    Don't think so. My interpretation of his code and his question is that he wants to use symbolic references: See "I have several arrays in my script" and "with $ARGV[1] as the array *name*". So in $arr1 is the name of the real array and he wants to access that.

      You're quite likely correct; it's just that from the code fragment given by the OP, I couldn't find where his array was being populated. I also tend to think that an event like $#arr1 giving an incorrect value is unlikely; I would expect that $#arr1 is among the Perl features least likely to have any bugginess. My thinking (I'm easily confused by symbolic references, so I'm likely wrong) is that $#arr1 isn't quite the same as scalar(@$arr1)-1.


      Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

        Exactly, if there is a @arr1 and you access it without any symbolic reference, then $#arr1 is scalar(@arr1)-1. Only @arr1 is accessed here, the value of $arr1 is irrelevant and isn't used at all

        If we use $arr1 (totally different variable from @arr1) as a symbolic reference, then

        @bingo=(1,2,3); $arr1='bingo'; print $#{$arr1}, scalar(@$arr1)-1, scalar(@bingo)-1;

        would print 222, as all three print expressions would be equivalent. In this case @arr1 is irrelevant and not used at all, only $arr1 as the symbolic reference and @bingo as the array