perl while loop for each


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl while loop for each
# 1  
Old 09-21-2012
perl while loop for each

I have the below scenario in perl

Code:
 
cd $FIDE_RECEIVE ;
# see the files that start with feedmgr.usfed.tips
$CycleDate      = &fi_get_curr_date('US','NIGHTLY_CYCLE','PROCESS');
 
head -1 GNM_GEO.DAT.EMBS* |grep -v GNM_GEO.DAT.EMBS |   awk  '{$4 " " $5}'

output for above command :
tradingdate arrival_date
--------------------------
20120910 20120911

20120910 20120911

20120910 20120911

20120910 20120911

20120910 20120911

20120910 20120911

20120910 20120911

20120910 20120911
I want to pass this out put to a while loop in perl get the second field from the each line (arrival date) ,do a comparison to the $CycleDate in the same YYYYMMDD format and want to execute a procedure by passing the in parameter (tradingdate) as first field if they match.

i am pasting entire perl here for reference .
please advise.




Code:
 ###########################################################################
# Description:  This  perl does the following:
#               1.Checks if the target load has completed successfully.If so,
#                 truncates the stage table.
#               2.Mails the no. of records loaded in case the target load has happened.
##############################################################################l
#
# Modification History:
#
#
###############################################################################
$Program = $0 ; $Program =~ s/.*\/// ;
$MyDir   = $0 ; $MyDir   =~ s/[^\/]*$// ;
push ( @INC, "$ENV{Samp_COMMON_SCR}" ) ;
push (@INC, "$ENV{Samp_SCR}") ;
$SIG{'INT'}  = "sub_handle" ;
$SIG{'TERM'} = "sub_handle" ;
require "getopts.pl" ;
require "Samp_misc.pl" ;
require "Samp_exit.pl" ;
require "Samp_db.pl";
require "Samp_job.pl";
require "Samp_log_error.pl";
require "www_gi_date_ext.pl";
###############################################################################
# Get user input.
###############################################################################
&Getopts ( "j:h" ) ;
if ( defined ( $opt_h ) )
{
    &print_help_msg ;
    &sub_exit ( 0 ) ;
}
if ( !defined ( $opt_j ) )
{
        &Samp_msg("Option j - Autosys Job name required",ERROR);
        &sub_exit(9) ;
}
else
{
        $job_name = $opt_j;
        &Samp_msg("Reading autosys job name - $job_name","",INFO,$job_name);
        $jobname = $job_name;
}
###############################################################################
# Creates connection to Database.
###############################################################################
if ( ! ( $dbh = &Samp_user_ora_connect ) )
{
  &Samp_msg ( "*** Failed to open connection to $ENV{'Samp_ORA_SID'}***","",ERROR,$job_name ) ;
  &sub_exit ( 9 ) ;
}
#=============================================================================#
# Pre-load. Makes an entry in job_run table and job_step_run table
#=============================================================================#
#$res=&Samp_ins_job_run('I',$job_name);
#if ($res) { &Samp_db_close(\$dbh); &sub_exit ( 9 ) ; }
#$res=&Samp_insupd_job_step_run('I',$job_name,'fiusicanlt_qt_ramp_chk','I');
#if ($res) { &Samp_db_close(\$dbh); &sub_exit ( 9 ) ; }
#$res=&Samp_insupd_job_step_run('I',$job_name,'wwww_LDING_CNT_VALIDN','I');
#if ($res) { &Samp_db_close(\$dbh); &sub_exit ( 9 ) ; }
# declaring the count to 5
##################Get records loaded from file generated by Informatica #############
#change the directory to $Samp_RECEIVE
cd $Samp_RECEIVE ;
# see the files that start with feedmgr.usfed.tips
$CycleDate      = &fi_get_curr_date('US','batch','PROCESS');
 @filelist = `head -1 GNM_GEO.DAT.EMBS* |grep -v GNM_GEO.DAT.EMBS |   awk  '{$4 $5}'`;
foreach $item (@filelist)
{
&Samp_msg (" item is equal to  ","",INFO,$item ) ; 
$filedate = `$item | awk '{ $2}'`;
if ($filedate =  $CycleDate)
{
###############################################################################
# Call procedure wwww_LDING_CNT_VALIDN
###############################################################################
&Samp_msg (" Job Name: $job_name ","",INFO,$job_name ) ;
$sql_header= "BEGIN Metric.IDX_wwww_LOADING_PKG.wwww_LDING_CNT_VALIDN(
                                                        :pv_out_stg_rowcount,
                                                        :pv_out_tgt_rowcount,
                                                        :pv_out_cnt_matched,
                                                        :pv_in_tgt_rowcount,
                                                        :pv_in_jobname
                                                        );
:st :=0;
                         EXCEPTION
                WHEN OTHERS THEN
                        :st:=1;
                        :st_err_no:=-1 *sqlcode;
                        :st_err:=sqlerrm;
                 END;";
my $sth = $dbh->prepare($sql_header);
$sth->bind_param_inout(":pv_out_stg_rowcount",\$stg_count,15);
$sth->bind_param_inout(":pv_out_tgt_rowcount",\$tgt_count,15);
$sth->bind_param_inout(":pv_out_cnt_matched",\$matched,15);
$sth->bind_param(":pv_in_jobname",$job_name);
$sth->bind_param(":pv_in_tgt_rowcount",$rec_cnt);
$sth->bind_param_inout(":st_err_no",\$errno,10);
$sth->bind_param_inout(":st",\$status,5);
$sth->bind_param_inout(":st_err",\$err_msg,1000);
$sth->bind_param_inout(":st",\$status,5);
$sth->execute();
&Samp_msg(" Stage Records processed $stg_count","",INFO,$job_name);
&Samp_msg("Target record count $tgt_count","",INFO,$job_name);
&Samp_msg("The matched flag:$matched","",INFO,$job_name);
&Samp_msg("Error message:$err_msg","",INFO,$job_name);
&Samp_msg(" Mailing flag $mail_ind","",INFO,$job_name);
&Samp_msg(" Status:$status","",INFO,$job_name);
  } 
 
 
else 
{
&Samp_msg ( "Received SAT Ctrl-C from the user.  Aborting.","",ERROR ) ;
&sub_exit (9) ;
}
}
 ###############################################################################
# The End.
###############################################################################
&sub_exit ( 0 ) ;
  
###############################################################################
sub sig_handle
###############################################################################
{
    my ( $signame ) = @_ ;
    if ( $signame eq "INT" )
    { &Samp_msg ( "Received SAT Ctrl-C from the user.  Aborting.","",ERROR ) ; }
    if ( $signame eq "TERM" )
    { &Samp_msg ( "Received termination signal (15).  Aborting.","",ERROR ) ; }
    $res=&Samp_upd_job_run('U',$jobname,'F');
    &Samp_db_close(\$dbh);
    &sub_exit ( 9 ) ;
}
############################################################################### 
sub print_help_msg
#
# Description: Prints the usage information for the script.
# Input:       None.
# Output:      The usage information is written to STDERR.
###############################################################################
{
    print STDERR "
Usage:
  \$Samp_PERL \$Samp_SCR/$Program -j<Job name> [-h help]
  where,
    -h   Print this message and exit.
    -j   Jobname corresponding to the perl script
" ;
}


Last edited by Don Cragun; 11-05-2015 at 05:20 AM.. Reason: Sanitize code as requested.
# 2  
Old 09-21-2012
Shelling out of perl is not perl, it is shell. In comparison, bash "while read f1 f2 ; do ... done" is so simple.

This bit in the loop seems illogical; is something missing?
Code:
if ($filedate =  $CycleDate)
{
###############################################################################
# Call procedure RAMPS_LDING_CNT_VALIDN
###############################################################################
 .
 .
 . 
 } 
else 
{
&fide_msg ( "Received SAT Ctrl-C from the user.  Aborting.","",ERROR ) ;
&sub_exit (9) ;
}

# 3  
Old 09-21-2012
iterate through each line

Quote:
Originally Posted by DGPickett
Shelling out of perl is not perl, it is shell. In comparison, bash "while read f1 f2 ; do ... done" is so simple.

This bit in the loop seems illogical; is something missing?
Code:
if ($filedate =  $CycleDate)
{
###############################################################################
# Call procedure RAMPS_LDING_CNT_VALIDN
###############################################################################
 .
 .
 . 
 } 
else 
{
&fide_msg ( "Received SAT Ctrl-C from the user.  Aborting.","",ERROR ) ;
&sub_exit (9) ;
}

HI DG,

I am trying to execute a DB procedure when the filedate is equal to cycledate.

could u advise what is not making sense here.

we are just wanted to call the procedure with the field1 as in parameter to the procedure.


also i dont bother to use while .My ultimate goal is to iterate through each line of the output.
please advise.
# 4  
Old 09-24-2012
iterate is loop. You can while, for, foreach, until or goto; which does not much matter. Often I write with no loop first, then add an unconditional loop when I am about to repeat, then add a break where I can best discover I want to leave the loop.

Why not use while like while (not input eof){ read; process;}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl loop until

I have a script that needs to wait on another script to finish. I created a sub routine to check the file for the number 0 but my until statement keeps on going. I tried eq, == and =~ but same thing. my $CHECKING_FILE = 1; do { sleep(5); $CHECKING_FILE = check_file(); ... (2 Replies)
Discussion started by: numele
2 Replies

2. Shell Programming and Scripting

While Loop in Perl

Hi All I am reading the file using while loop in Perl someting like while (my $s=<F>){ chomp($s); .. .. .. } What i want to do is after the chomp statement i used some condition, if the condition is met then it should move forward otherwise it should read the new line. How Can it be... (4 Replies)
Discussion started by: parthmittal2007
4 Replies

3. Programming

while loop perl

I am trying to create a success and fail as below in a perl script : while echo$? is 2 it should append as below to .fail file ===================== if ( open(IN, "$outputfile")) { while( $my_line = <IN> ) { #print "$my_line \n" ; return 0; ... (3 Replies)
Discussion started by: sriram003
3 Replies

4. Shell Programming and Scripting

Help with PERL loop

I wrote a script to list all lines in a file with Perl. I am having trouble with the looping part of it. My script is supposed to look at the file and as long as the file is larger than the current line it prints a new line. I am getting an error that won't stop on the while line of my code I... (4 Replies)
Discussion started by: zero3ree
4 Replies

5. Shell Programming and Scripting

until loop Perl

I am trying to print out a section of a file begining at the start and printng until a character is found. My code and input file are below. This code is printing out every line except for the line with the character which is not what I want the out put should be a file with numbers 1-4. ... (3 Replies)
Discussion started by: cold_Que
3 Replies

6. Infrastructure Monitoring

Perl Loop Problem

Another newbie question... I can not figure out how to get this running using a loop. Here is what I have now. #!/usr/bin/perl use SNMP::Info; $list="list.list"; open(DAT, $list) || die("Can't Open List"); @raw_data=<DAT>; close(DAT); foreach $dest (@raw_data) {... (2 Replies)
Discussion started by: mrlayance
2 Replies

7. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

8. Shell Programming and Scripting

perl loop keeps getting stuck

I am using a Perl script to open a series of files in a loop, separate the paragraph into lines, and output the lines into a new file. The code works perfectly fine, except when the source file is over a certain size the loop gets stuck and won’t move on to the next file. It still does what it is... (0 Replies)
Discussion started by: renthead720
0 Replies

9. Shell Programming and Scripting

for loop in perl

my $i; my $j; for($i=1;$i<=5;$i++) { for($j=$i;$j<5;$j++) { print " "; } print "$i\n"; } But the output i need is 1 12 123 1234 12345 Help me please (5 Replies)
Discussion started by: priyas
5 Replies

10. Shell Programming and Scripting

help with perl while loop

Can anyone tell me why this program won't kick out when the time gets beyond time in the loop? sub showtime { local($format,$military)=@_; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); if ((! $military) &amp;&amp; ($hour &gt; 12)) {$hour-=12;} ... (2 Replies)
Discussion started by: methos
2 Replies
Login or Register to Ask a Question