×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Perl script behavior makes no sense!!!

Perl script behavior makes no sense!!!

Perl script behavior makes no sense!!!

(OP)
[b]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:

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close