Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Trying to read print in a loop inside a subroutine

by kaushik9918 (Sexton)
on May 20, 2020 at 07:09 UTC ( #11116963=perlquestion: print w/replies, xml ) Need Help??

kaushik9918 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to open a file inside a subroutine to basically substitute some lines in the file. But since, it was not working, I tried a simpler way of printing a line instead of substitute, for debug purposes. Following is the subroutine code.

sub replace { while(<INPUT_FILE>){ my $cell=$_[0]; our $rpl; if ($_=~ /^TASK\|VALUE = (.*)/ ){ my $task=$1; chomp $task; $rpl= $cell.'_'.$task.'_bunch_rpl' +; print "000: $rpl\n"; } elsif ($_=~ /^(.*)\|VALUE = (.*)/ ){ 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_F +ILE>){ if ($rpl_s +ub =~ /^$line/) { print +"\n 111: $ip_val"; } + } close REPLAY_F +ILE; } elsif ($_=~ /^\s*$/){ print "\n"; return ; } } }

The code prints the following as of now.

000: lfr_task62_bunch_rpl 111: 2.0.9.0 111: INLINE 111: POWER 000: aaa_task14_bunch_rpl

Expected output is:

000: lfr_task62_bunch_rpl 111: 2.0.9.0 111: INLINE 111: POWER 000: aaa_task14_bunch_rpl 111: 0.45 111: NO

The input sample is :

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

Can someone tell me the mistake I am doing here?

Replies are listed 'Best First'.
Re: Trying to read print in a loop inside a subroutine
by hippo (Chancellor) on May 20, 2020 at 11:06 UTC
    Can someone tell me the mistake I am doing here?

    Unlikely, because it isn't self-contained. You are reading 1 set of input from the INPUT_FILE handle (which I'm guessing corresponds to the sample input you have provided) and 2 other sets of input from the REPLAY_FILE handle which have not been provided to us. Without an SSCCE, the my best guess is that you don't really want a regex match at if ($rpl_sub =~ /^$line/) but rather a substring match.

    See the Basic Debugging Checklist for how you can help yourself. Good luck.

      you are right, I am looking for

      if ($rpl_sub =~ /^$ip_var\|VALUE/)
      instead of   if ($rpl_sub =~ /^$line/)
Re: Trying to read print in a loop inside a subroutine
by haukex (Chancellor) on May 20, 2020 at 10:31 UTC
      Ok
Re: Trying to read print in a loop inside a subroutine
by clueless newbie (Deacon) on May 21, 2020 at 00:34 UTC

    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

      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.

Re: Trying to read print in a loop inside a subroutine
by perlfan (Priest) on May 22, 2020 at 03:54 UTC
    open(REPLAY_FILE, "+<$rpl")
    Not the solution, but open files this way:
    open my $REPLAY_FILE, '+<', $rpl or die $!; while (my $line = <$REPLAY_FILE>) { #... do stuff with $line } close $REPLAY_FILE;
    See open for more information.
      Sure -thanks for the tip.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11116963]
Approved by haukex
Front-paged by haukex
Considered: [haukex]: Edit: remove blank lines from code (OP msg'd)
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2020-06-05 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (37 votes). Check out past polls.

    Notices?