perl code file reading need explanation

Need help to understand perl code

Page 1 of 1

6 Replies - 2044 Views - Last Post: 13 May 2009 - 09:07 PM

#1 Sun751   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-December 08

perl code file reading need explanation

Post icon  Posted 12 May 2009 - 04:42 PM

undef $/;	  # read in whole file, not just one line or paragraph
while ( <OLD> ) {

					  while (/(\s*update\s+statistics.*?;\s*\n)/sgm)
						{
								print NEW "$1\n";
						}
						while (/(\s*alter\stable.*?;\s*\n)/sgm)
						{
								print NEW "$1\n";
						}
}


the above code works correctly but I am having problem to understand it,

1st while = while(<>) its going to read all the file into $_,
but what about 2nd while? is it going to re-read the $_ matching the condition and send the out-put to filehandle NEW????

Need Help to make my self clear!!!!

This post has been edited by Sun751: 12 May 2009 - 04:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: perl code file reading need explanation

#2 Ed_Bighead   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Re: perl code file reading need explanation

Posted 12 May 2009 - 09:44 PM

Why don't you post whatever file is going to be read and the rest of that program, and I'll see what I can do to help. It'll help me help you faster if I can actually run the program.
Was This Post Helpful? 0
  • +
  • -

#3 Sun751   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-December 08

Re: perl code file reading need explanation

Posted 13 May 2009 - 12:39 AM

View PostSun751, on 12 May, 2009 - 03:42 PM, said:

undef $/;	  # read in whole file, not just one line or paragraph
while ( <OLD> ) {

					  while (/(\s*update\s+statistics.*?;\s*\n)/sgm)
						{
								print NEW "$1\n";
						}
						while (/(\s*alter\stable.*?;\s*\n)/sgm)
						{
								print NEW "$1\n";
						}
}


the above code works correctly but I am having problem to understand it,

1st while = while(<>) its going to read all the file into $_,
but what about 2nd while? is it going to re-read the $_ matching the condition and send the out-put to filehandle NEW????

Need Help to make my self clear!!!!



This is my code:-

#!/usr/bin/perl 

my $a = 'C:\perlJunk\tmp.txt';
my $b = 'C:\perlJunk\tmp1.txt';

open ($fh, "<$a") || die "Cant open"; 
open ($fhh,">$b") || die "Cant open"; 
undef $/;
while (<$fh>)
{
	while (/(\s*update\s+statistics.*?;\s*\n)/sgm)
	{
	
		print $fhh "$1\n";
	}
	while (/(\s*alter\stable.*?;\s*\n)/sgm)
	{
		print $fhh "$1\n";
	}
}
close $fhh; 
close $fh; 



And this is my source file:-
#######################################
update statistics medium for table ar (
des_acc, trn_num)
resolution 2.50000 0.95000 ;
update statistics high for table aualalis (
acc_ref, mdu_acc, mdu_ref)
resolution 0.50000 ;
update statistics medium for table aualalis (
cre_dte_tme, mod_dte_tme, mod_opr)
resolution 2.50000 0.95000 ;
alter table "auth".auareffw add constraint primary key (trn_num)
constraint "auth".auareffw_pk ;
create unique index "auth".auasdepr_i1 on "auth".auasdepr (ast_num)
using btree ;
alter table "auth".auasdepr add constraint primary key (ast_num)
constraint "auth".auasdepr_pk ;
###############################################

I do understand up to reading file and getting the source file into $_ through "while (<$fh>)"
but not getting how another two while loop is working, to let you know its giving correct output BUT HOW????
Was This Post Helpful? 0
  • +
  • -

#4 dsherohman   User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: perl code file reading need explanation

Posted 13 May 2009 - 03:53 AM

View PostSun751, on 12 May, 2009 - 11:42 PM, said:

1st while = while(<>) its going to read all the file into $_,
but what about 2nd while? is it going to re-read the $_ matching the condition and send the out-put to filehandle NEW????

It works because "while" does not affect $_ under any circumstances.

In the outer while loop, $_ is being set by the <> operator, which reads a line from the filehandle and puts it into $_ (1). The "while" is just testing that the value which has been assigned(2) is true and continues to execute the loop until it returns a false value.

(1) ...unless another destination is specified, as in "$line = <OLD>".

(2) Assignments return the assigned value. e.g., "$x = 3" returns the value "3".
Was This Post Helpful? 0
  • +
  • -

#5 chorny_cpan   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 13-May 09

Re: perl code file reading need explanation

Posted 13 May 2009 - 12:58 PM

Rewriting with modern style:

#!/usr/bin/perl

my $infile = 'C:\perlJunk\tmp.txt';
my $outfile = 'C:\perlJunk\tmp1.txt';

open (my $fh, '<',$infile) || die "Cant open";
open (my $fhh, '>', $outfile) || die "Cant open";
undef $/;
my $content=<$fh>;
	while ($content=~/(\s*update\s+statistics.*?;\s*\n)/sgm) #repeat while it is found
	{
   
		print $fhh "$1\n";
	}
	while ($content=~/(\s*alter\stable.*?;\s*\n)/sgm)
	{
		print $fhh "$1\n";
	}
close $fhh;
close $fh;



Consider reading "Beginning Perl".
Was This Post Helpful? 0
  • +
  • -

#6 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: perl code file reading need explanation

Posted 13 May 2009 - 03:48 PM

the last two "while" loops work because you have included the "g" modifier on the end of the regexp. It sort of means:

while the pattern is found we continue to assign it to $1 and keep track of where the last match was and move on to the next match until the end of the string/line.

If you had not included the "g" modifier the "while" loops would never terminate and your perl program would appear to just be sitting there doing nothing.
Was This Post Helpful? 0
  • +
  • -

#7 mcwolf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 26-April 09

Re: perl code file reading need explanation

Posted 13 May 2009 - 09:07 PM

View PostKevinADC, on 13 May, 2009 - 02:48 PM, said:

the last two "while" loops work because you have included the "g" modifier on the end of the regexp. It sort of means:

while the pattern is found we continue to assign it to $1 and keep track of where the last match was and move on to the next match until the end of the string/line.

If you had not included the "g" modifier the "while" loops would never terminate and your perl program would appear to just be sitting there doing nothing.

:^: Interesting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1