Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Trying to read print in a loop inside a subroutine

by clueless newbie (Curate)
on May 21, 2020 at 00:34 UTC ( [id://11117006] : note . print w/replies, xml ) Need Help??


in reply to Trying to read print in a loop inside a subroutine

Gordon,

For me (using your data minus the leading whitespace and with files from StackOverflow) the following:

use Data::Dumper; use 5.01800; use warnings; my @cell_names; open INPUT_FILE,"<",my $ip_file="11116963.txt" or die "\n!!!ERROR OPENING INPUT FILE. EXITING SCRIPT!!!\n"; while (<INPUT_FILE>) { #warn Data::Dumper->Dump([\$_],[qw(*_)]),' '; if ($_ =~ /(.*) =\n/) { print "\nFormat not correct on line $. of input file. Exiting +script\n"; exit; } elsif ($_ =~ /(.*) =\s+\n/) { print "\nFormat not correct on line $. of input file. Exiting +script\n"; exit; } elsif ($_ =~ /(.*) = \s+(.*)/) { print "\nFormat not correct on line $. of input file. Exiting +script\n"; exit; } elsif ($_ =~ /^TASK_CELL_NAME\|VALUE = (.*)/) { my $cell_name=$1; unless (grep(/^$cell_name $/,@cell_names)) { push @cell_names,"$cell_name "; #$count++; print "\nCELL NAME: $cell_name\n"; replace($cell_name); } } # elsif ($_ =~ /^TASK_CELL_NAME\|VALUE = (.*)/) } # while (<INPUT_FILE>) close INPUT_FILE; exit; sub replace { our $rpl; while (<INPUT_FILE>) { chomp; #warn Data::Dumper->Dump([\$_],[qw($_)]),' '; my $cell=$_[0]; if ($_ =~ /^TASK\|VALUE = (.*)/) { #warn Data::Dumper->Dump([\$_],[qw(*_)]),' '; my $task=$1; #chomp $task; $rpl=$cell.'_'.$task.'_bunch_rpl'; print "000: $rpl\n"; } # if ($_ =~ /^TASK\|VALUE = (.*)/) elsif ($_ =~ /^(.*)\|VALUE = (.*)/) { #warn Data::Dumper->Dump([\$_],[qw(*_)]),' '; my $line=$_; #chomp $line; my $ip_var=$1; my $ip_val=$2; #chomp $ip_var; #chomp $ip_val; my $look=$ip_var."|VALUE"; open(REPLAY_FILE,"<",$rpl) || die "\ncannot open $rpl\n"; while (my $rpl_sub=<REPLAY_FILE>) { if ($rpl_sub =~ /^$line/) { print "\n 111: $ip_val"; } } close REPLAY_FILE; } # elsif ($_ =~ /^(.*)\|VALUE = (.*)/) elsif ($_ =~ /^\s*$/) { #warn Data::Dumper->Dump([\$_],[qw(*_)]),' '; print "\n"; return; } } # while (<INPUT_FILE>) } # sub replace

yields:

CELL NAME: lfr 000: lfr_task62_bunch_rpl 111: 2.0.9.0 111: INLINE 111: POWER CELL NAME: aaa 000: aaa_task14_bunch_rpl 111: 0.45 111: NO

May I say that the approach of open/read/close for each name/value pair isn't very efficient.

Adding data files. '11116963.txt'

TASK_CELL_NAME|VALUE = lfr TASK|VALUE = task62 TASK_VERSION|VALUE = 2.0.9.0 CHIP_PKG_TYPE|VALUE = INLINE JUNK_LINE = JUNK JUNK_LINE = JUNK FULL_ESD|VALUE = POWER TASK_CELL_NAME|VALUE = aaa TASK|VALUE = task14 CUSTOM_CELL_DENSITY|VALUE = 0.45 CUSTOM_CELL_SS|VALUE = NO

'lfr_task62_bunch_rpl' and 'aaa_task14_bunch_rpl' have the same content:

# Select fund FUND|VALUE = mmi # Select bank BANK|VALUE = citi # Select cell name TASK_CELL_NAME|VALUE = lfr # Select task TASK|VALUE = task62 # Select task version TASK_VERSION|VALUE = 1.0.9.0 # Select fund type FULL_ESD|VALUE = MUTUAL # Select customer premium CUSTOM_CELL_SS|VALUE = YES # Select customer brand density CUSTOM_CELL_DENSITY|VALUE = 0.76 # Select card chip CHIP_PKG_TYPE|VALUE|VALUE = OUTLINE

Replies are listed 'Best First'.
Re^2: Trying to read print in a loop inside a subroutine
by kaushik9918 (Sexton) on May 21, 2020 at 06:16 UTC

    Mine has a different result, doesn't print the last two lines, like yours. Very strange, I will look into this. Thanks for your time.