in reply to What's wrong with my code? [from a beginner]

Update: wrong: Since you've created the array @entry as a 'my' variable, and not passed this variable to your get_throughput routine correctly, and not attempted to get the variable within that routine, the array entry isn't accessable from the routine get_throughput.

They are correct, the my declaration is still in scope. My bad. </endupdate>

Simple fix, remove the 'my @entry;' line. entry then becomes global and the routine should work.

More complex fix: Pass the actual entry array. To do this, it needs to look like this.

&get_throughput(\@entry); sub get_throughput { my $entryref; foreach $entryref (@_) { ... if($entryref->[1] == "r") { $sim_time = $entryref->[3]; ...
This iterating over @_ allows you to pass more than one array reference to the subroutine, it will handle as many as you throw at it.

There are other ways to tackle this as well...

-Scott

Replies are listed 'Best First'.
Re^2: What's wrong with my code? [from a beginner]
by holli (Abbot) on Jan 27, 2005 at 19:23 UTC
    That is just plain wrong. see this code:
    my @array = (1,2,3); test (); sub test { print $array[0]; } #prints 1

    holli, regexed monk
Re^2: What's wrong with my code? [from a beginner]
by Roy Johnson (Monsignor) on Jan 27, 2005 at 19:26 UTC
    The passed argument list is ignored, but the array is within scope, and so is accessible within get_throughput.

    Caution: Contents may have been coded under pressure.
      To clarify passing values to a subroutine
      The arguments for a subroutine are treated as a list assignment to the @_ list. This script demonstrates a few key concepts:

      my @entry = ('ListItem1', 'ListItem2'); $ScalarContext = &get_throughput($Var1, $Var2, @entry, $Var3, \@entry) +; @ListContext = &get_throughput($Var1, $Var2, @entry, $Var3, \@entry); sub get_throughput { my $GetsVar1 = shift @_; #@_ is a list of your arguments my $Scalar = shift; # gets $Var2, @_ is the default for s +hift my ($List1, $List2) = @_; #@_ your list was added to @_ as indi +vidual elements my (undef, undef, $Var3) = @_; #undef discards return values my $ListRef = $_[4]; #a reference to your list #see docs for more info on refs return wantarray ? @list : $Scalar; }
      Ardemus "This, right now, is our lives."