in reply to Re: Returning a list from a sub
in thread Returning a list from a sub

Thanks for the great advice.
I'm still stuck, though, on how to loop through this list from the calling sub.
sub TSM_Enquire { my $Slot, $Type, $Label; open(IN,"/bbs/rtscripts/adsm_cmd q libv|"); while (<IN>) { next unless (/3584/); s/,//; @Fields = split ' '; $Label = $Fields[2]; $Slot = $Fields[$#Fields-1]; $Type = $Fields[3]; push @List, "$Slot $Type $Label"; } close(IN); return @List; } sub Process_Columns { my $Count=0; my $Simple; my @Column_Info = ( [ 1024, 1067 ], # Column 1 start stop [ 1068, 1093 ], # Column 2 start stop [ 1094, 1137 ], # Column 3 start stop [ 1138, 1163 ], # Column 4 start stop [ 1164, 1207 ], # Column 5 start stop [ 1208, 1233 ], # Column 6 start stop [ 1234, 1277 ], # Column 7 start stop [ 1278, 1321 ] ); # Column 8 start stop if ($LibraryType =~ "tsm") { @List = TSM_Enquire; } else { @List = Tapeutil_Enquire; } for ($Count=1;$Count<=8;$Count++) { # Traverse columns my $StartPos = $Column_Info[$Count][1]; my $Pos = $StartPos; my $Column_Count = 0; my $EndPos = $Column_Info[$Count][2]; my $NewPos=0; my $NewLabel=""; my $NewRec=""; while ($NewRec = @List) { chomp $NewRec; $NewRec =~ s/^ //; ($Pos, $Type, $Label) = split(' ', $NewRec, 9999); for ($I=1;$I<=($EndPos-$StartPos)+1;$I++) { <snip>
I that the above is wrong (because it doesn't work, read as: doesn't do what I want).
Any ideas, comments, etc?
TIA
coec

Replies are listed 'Best First'.
Re: Re: Re: Returning a list from a sub
by seattlejohn (Deacon) on Jul 24, 2002 at 05:28 UTC
    It looks suspiciously like you have a scoping issue with @List. What is probably happening is that you have (either explicitly or implicitly) created a *global* variable called @List, rather than a local variable within each subroutine. I would suggest adding my @List; to the beginning of your TSM_Enquire and Process_Columns subs, which will give them each a private copy of the list that is empty each time the sub is first executed. For more details, you can check out the sections on scoping in perldoc perlsub.

    Also, you might also want to use strict; at the beginning of your program to help catch errors like this in the future.

    If that doesn't solve the problem, it might be worth trying some simple debugging tricks, like adding warn statements on subroutine entry and exit and when you are manipulating @List or other variables that are central to what your code is trying to accomplish.