Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl script behavior makes no sense!!!

Status
Not open for further replies.

eslie

Programmer
Mar 11, 2010
1
0
0
US
Problem: MAIN array (my @Sort_Order) gets corrupted by subroutine that does not touch the array???

Process:
- open(MAIN_IFH,$IFH); # in main
- split 1st column and store values in my @Sort_Order.


Here’s where it gets crazy:
- loop thru @Sort_Order using $line-count
- @Sort_Order gets corrupted where I exit the loop. ie ( line-count == 6 ) -> (Value 6 gets corrupt) by &Why_Does_This_Happen();

&Why_Does_This_Happen():
- The open(WHY_IFH,$IFH);
- while( <WHY_IFH> ) { last; } currputs @Sort_Order

- If open(WHY_IFH,$empty); is an empty >> value wiull be null -> 6) ::
- If open(WHY_IFH,$IFH); value 6) has the unsplit data record in IFH;


======================== Printed from &Why_Does_This_Happen(); ===================================
Before while( <WHY_IFH> ): @Sort_Order = ARRAY(0x65cfb0)
1) :111:
2) :222:
3) :333:
4) :444:
5) :555:
6) :666:
7) :777:
8) :888:
9) :999:
After while( <WHY_IFH> ): @Sort_Order = ARRAY(0x65cfb0)
1) :111:
2) :222:
3) :333:
4) :444:
5) :555:
6) :111 , 03-01-23 => 211 32
:
7) :777:
8) :888:
9) :999:

========================== Perl Script =========================================
use strict;
use v5.28.1;

our $Ref_Sort_Order;
our $IFH = 'INPUT_FILE.TXT';
open(MAIN_IFH,$IFH);

my (@Sort_Order,$line_number);
while ( <MAIN_IFH> ) {
my ($phoneNum) = split;
push(@Sort_Order,$phoneNum);
}
close IFH;

$Ref_Sort_Order = \@Sort_Order; # Ref to use for print only
my $lineCount;

foreach( @Sort_Order ) {
if( ++$lineCount == 6 ) { &Why_Does_This_Happen(); exit; }
}

#-----------------------------------------------------------------------------------------------
sub Why_Does_This_Happen {
open(WHY_IFH,$IFH) or die $!;
Print_Array("Before while( <WHY_IFH> ): \@Sort_Order = $Ref_Sort_Order\n");

while( <WHY_IFH> ) { last; }
close WHY_IFH;

Print_Array("After while( <WHY_IFH> ): \@Sort_Order = $Ref_Sort_Order\n");
return;
}
#-----------------------------------------------------------------------------------------------
sub Print_Array {
my $ofh = 'test.txt';
open(OFH,">$ofh");

my $msg = shift;
say OFH $msg;

my $line;
foreach( @$Ref_Sort_Order ) {
$line = ' ' x (3 - length(++$line)) . $line;
say OFH "$line) :$_:";
}

close OFH;
system($ofh);
return;
}

========================== INPUT_FILE.TXT =====================================
111 , 03-01-23 => 211 32
222 , 03-02-23 => 222 40
333 , 03-03-23 => 233 101
444 , 03-04-23 => 244 113
555 , 03-35-23 => 255 104
666 , 03-06-23 => 266 34
777 , 03-07-23 => 277 119
888 , 03-08-23 => 288 116
999 , 03-09-23 => 299 28

========================== Printed from MAIN: if( lineCount < 6 ) { ==========================

1) :111 , 03-01-23 => 211 32
:
2) :111 , 03-01-23 => 211 32
:
3) :111 , 03-01-23 => 211 32
:
4) :111 , 03-01-23 => 211 32
:
5) :111 , 03-01-23 => 211 32
:
6) :666:
7) :777:
8) :888:
9) :999:

 
 https://files.engineering.com/getfile.aspx?folder=36171ebd-0842-4381-8741-9dbb96040f51&file=INPUT_FILE.TXT
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top