basicdez has asked for the wisdom of the Perl Monks concerning the following question:

I am just wondering is someone could kindly tell me what I am missing with this array referencing pass I am attempting to complete. It keeps printing out the array value as -1 which translates to a null array. HELP PLEASE Thanks dez
#!/usr/local/bin/perl -w @BankIN = (1,2,3); @BankOUT = (4,4,4); use strict 'subs'; arrayX(\@BankIN, \@BankOUT); sub arrayX{ my($a1, $a2)=@_; my $BankCount = 0; my $arg = shift(@ARGV); print "$#a1\n"; while ($BankCount <= $#a1 ) { if ($a1[$BankCount] eq $arg) { $arg = $a2[$BankCount]; $BankCount = $#a1; } $BankCount++; } print "$arg\n"; }

Replies are listed 'Best First'.
Re: Why is this not finding my array value.
by chipmunk (Parson) on Jan 19, 2001 at 01:00 UTC
    $#a1 is the index of the last element in the array @a1. To access the array referred to by $a1, you use additional syntax with that variable. For example:
    $idx = $#{$a1}; # index of last element $idx = $#$a1; # same $count = @{$a1}; # number of elements $count = @$a1; # same $item = $a1->[1]; # element 1 $item = ${$a1}[1]; # same @slice = @{$a1}[0, 1]; # slice of elements
    For more on using references, check out perlreftut (reference tutorial - offsite link), perlref (reference reference), perllol (lists of lists), and perldsc (data structures cookbook).
      Thanks I got it working now. peace dez
Re: Why is this not finding my array value.
by cat2014 (Monk) on Jan 19, 2001 at 00:53 UTC

    my first suggestion: use strict;.

    my second suggestion: use Data::Dumper;

    learn them, love them, debug problems like this much easier.

    if you used strict, you would see this error:

    Global symbol "a1" requires explicit package name at practice/temp lin +e 20. Global symbol "a2" requires explicit package name at practice/temp lin +e 28.

    from there, you'll see that you need to dereference.
    print "$#a1\n"; becomes print "$#$a1\n"; and if ($a1[$BankCount] eq $arg) becomes  if ($a1->[$BankCount] eq $arg)

    that said, i don't really understand what you're doing with   my $arg       = shift(@ARGV); --when i run your program, i get use of unitialized value where you try to compare against $arg. are you passing your $arg in off the command line?

    here's what i get running your script with use strict;, Data::Dumper, and the array reference fixes:

    $VAR1 = [ 1, 2, 3 ]; $VAR1 = [ 4, 4, 4 ]; 2 Use of uninitialized value at practice/temp line 25. Use of uninitialized value at practice/temp line 25. Use of uninitialized value at practice/temp line 25. Use of uninitialized value at practice/temp line 34.

    here's the code:

    use strict; use Data::Dumper; my @BankIN = (1,2,3); my @BankOUT = (4,4,4); use strict 'subs'; arrayX(\@BankIN, \@BankOUT); sub arrayX{ my($a1, $a2)=@_; print Dumper($a1); print Dumper($a2); my $BankCount = 0; my $arg = shift(@ARGV); print "$#$a1\n"; while ($BankCount <= $#$a1 ) { if ($a1->[$BankCount] eq $arg) { $arg = $a2->[$BankCount]; $BankCount = $#$a1; } $BankCount++; } print "$arg\n"; }
      The only reason I was using my $arg = shift(@ARGV) was because this was a quick little script to get the passing of a variable to work. I am going to now implement it into a program that uses XML::Simple and parses an XML tree and evaluates a main Bank number coming in to manipulate between one test environment and the other. this saves a ton of time for my configuration team and others. I will not be using the passing a variable in production. Your other help worked perfectly and I am now in line towards a better understanding of all of this array referencing stuff. Thanks so much for you help. Greatly appreciated. As Thorin would say "I am forever in service to you!" :)
Re: Why is this not finding my array value.
by lemming (Priest) on Jan 19, 2001 at 00:49 UTC
    For deferencing Line #15:
    make $#a1 into $#{$a1}

    I'll update on other bits, once I figure out what you're trying to do.

    Update: At this point you might as well scroll through the other answers, follow the links from chipmunk's post to learn what you are doing wrong with refs.

    You may want to check you Data input. You only use one argument, but you don't check. I'm a bit leary of getting commandline arguments from inside a subroutine that isn't labeled as using that behaviour, but that's a style issue.

      $#$a1 will also work, and save you 2 key strokes ; )
      You will also want to change $arg = $a2[$BankCount]; to $arg = $$a2[$BankCount];
      Got it working now. Thanks for the help.