Re: file handling

2010-02-11 Thread John W. Krahn

elavazhagan perl wrote:

Hi,


Hello,


 I have a program which reads the input file,updates once and outputs in a
new file.
Ex.The first line L1 will be updated to L2.
I need to put this into a loop so that the scripts will be invoked 1000
times and the output contains the appended data(L1 TO L1000).
Please can you suggest few methods.


It is not clear from your description or from your code exactly what you 
want to do.  Do you want 1,000 separate files or one file with the data 
replicated 1,000 times or something else?


Anyway, let's have a look at the code:

[ Note that you should indent your code consistently to aide in 
readability. ]



#! c:/perl/bin/perl

use strict;

my $file = shift;
my $old = $file;
my $new = "$file.tmp.$$";
my $bak = "$file.bak";

#File Handling
open(OLD, "< $old") or die "can't open $old: $!";
open(NEW, "> $new") or die "can't open $new: $!";

# Correct typos, preserving case
while () {
#Condition to match the Field ID and incremanet by 20.
#
if($_ =~ /\/)


Why are you capturing to $1 when you are not using it?


{
my $val= $2;
my $newval=$val+20;
$_ =~ s/$val/$newval/;


You shouldn't do that.  See below for explanation.

print "OLD:$val \t NEW:$newval\n" ; 
}


#Condition to match the line and increament by 1.
# 
if ($_ =~ /(L\d{2,3})\_/g)


Why are you using the /g global option if the pattern only occurs once 
in the string?  The data you supplied does not match the pattern 
/(L\d{2,3})_/ so is the data wrong or the pattern wrong or something else?



  {
 my $line = $1;
if($line =~ /(\d+)/g)


Why are you matching \d again when you already know that \d exists in 
the string?  Why are you using the /g global option if the pattern only 
occurs once in the string?



{
my $n_line = $1;
my $newline = $n_line+1;
$_=~ s/$line/L$newline/;


You *really* shouldn't do that.


print "OLD:$line \t NEW:L$newline\n" ;
}

 }
 #Condition to increment the final segment in the above string
  if($_=~ /\

Why are you capturing to $1, $2 and $3 when you are not using them?


 {
 my $value=$4;
 my $newvalue=$value+20;
 $_=~ s/$value/$newvalue/;


You are running a pattern match using a string (in this case $value) 
that may occur anywhere in the string, not just the place where you 
originally found it, and may contain regular expression meta-characters 
that could cause the pattern to fail.


 print "OLD:$value \t NEW:$newvalue\n" ; 
 }
 
(print NEW $_)  or die "can't write to $new: $!";

}


close(OLD)  or die "can't close $old: $!";
close(NEW)  or die "can't close $new: $!";
rename($old, $bak)  or die "can't rename $old to $bak: $!";
rename($new, $old)  or die "can't rename $new to $old: $!";



Your while loop would probably be better written as:

# Correct typos, preserving case
while (  ) {
#Condition to match the Field ID and incremanet by 20.
#
if ( s/(/ $1 . ( $2 + 20 ) . '">' /e )
{
print "OLD:$2 \t NEW:", $2 + 20, "\n";
}

#Condition to match the line and increament by 1.
# 
if ( s/_L(\d{2,3})_/ '_L' . ( $1 + 1 ) . '_' /e )
{
print "OLD:L$1 \t NEW:L", $1 + 1, "\n";
}

#Condition to increment the final segment in the above string
if ( s/(http://learn.perl.org/




Re: file handling

2010-02-11 Thread John W. Krahn

elavazhagan perl wrote:

On 2/11/10, John W. Krahn  wrote:


elavazhagan perl wrote:


I have a program which reads the input file,updates once and outputs in
a new file.
Ex.The first line L1 will be updated to L2.
I need to put this into a loop so that the scripts will be invoked 1000
times and the output contains the appended data(L1 TO L1000).
Please can you suggest few methods.


It is not clear from your description or from your code exactly what you
want to do.  Do you want 1,000 separate files or one file with the data
replicated 1,000 times or something else?


I will try my best to make you clear.
Once we execute the script,it should generate the data for the 1000
lines.Now the script just converts the data for a single line


The data you provided has 156 "lines" in it.  Do you consider all that 
data to be just one "line"?



i.e Line1 will
be converted to line 2 and some increaments.
In order to get my desired data,I need to execute the script 1000 times,and
each output will be appended in a new file which will containthe whole
data.
I am trying to update the script so that for a single execution,it will
produce the whole data.
Hope I make it clear.Please let me know if any ambiguityon this.


What you want sounds easy enough to do.  If you could just clear up what 
you mean by "line"?




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.   -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: file handling

2010-02-12 Thread elavazhagan perl
Hope,this time I will make you clear.

Our script converts the original content to  current output ,what we need is
the desired output.

*ORIGINAL CONTENT:*

   








   
 

*CURRENT OUTPUT:*


   








   
 

*DESIRED OUTPUT:*

 
   








   
 


Thanks.


On 2/12/10, John W. Krahn  wrote:
>
> elavazhagan perl wrote:
>
>> On 2/11/10, John W. Krahn  wrote:
>>
>>>
>>> elavazhagan perl wrote:
>>>

 I have a program which reads the input file,updates once and outputs in
 a new file.
 Ex.The first line L1 will be updated to L2.
 I need to put this into a loop so that the scripts will be invoked 1000
 times and the output contains the appended data(L1 TO L1000).
 Please can you suggest few methods.

 It is not clear from your description or from your code exactly what you
>>> want to do.  Do you want 1,000 separate files or one file with the data
>>> replicated 1,000 times or something else?
>>>
>>
>> I will try my best to make you clear.
>> Once we execute the script,it should generate the data for the 1000
>> lines.Now the script just converts the data for a single line
>>
>
> The data you provided has 156 "lines" in it.  Do you consider all that data
> to be just one "line"?
>
> i.e Line1 will
>> be converted to line 2 and some increaments.
>> In order to get my desired data,I need to execute the script 1000
>> times,and
>> each output will be appended in a new file which will containthe whole
>> data.
>> I am trying to update the script so that for a single execution,it will
>> produce the whole data.
>> Hope I make it clear.Please let me know if any ambiguityon this.
>>
>
> What you want sounds easy enough to do.  If you could just clear up what
> you mean by "line"?
>
>
>
>
> John
> --
> The programmer is fighting against the two most
> destructive forces in the universe: entropy and
> human stupidity.   -- Damian Conway
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: file handling

2010-02-12 Thread John W. Krahn

elavazhagan perl wrote:

Hope,this time I will make you clear.

Our script converts the original content to  current output ,what we need is
the desired output.


So I take it by "line" you actually mean "Field record"?



*ORIGINAL CONTENT:*

   








   
 

*CURRENT OUTPUT:*


   








   
 

*DESIRED OUTPUT:*

 
   








   
 


The original 'Field ID' starts out at 2 and ends up at 20980.  If 
you increment that by 20 each time then that is 49 changes to 'Field 
ID'.  The 'string ID' value starts out at 1 and ends up at 1000 which is 
999 changes.  If both values are changed at the same time then they 
should both have the same number of changes.  So how do you determine 
when to change one value and not the other?




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.   -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: file handling

2010-02-12 Thread elavazhagan perl
Yes, you are absolutely right..
We need to increment the line by one  and field ID by 20.
Whatever my be the fied id..
Thanks,

On 2/12/10, John W. Krahn  wrote:
>
> elavazhagan perl wrote:
>
>> Hope,this time I will make you clear.
>>
>> Our script converts the original content to  current output ,what we need
>> is
>> the desired output.
>>
>
> So I take it by "line" you actually mean "Field record"?
>
>
> *ORIGINAL CONTENT:*
>>
>> 
>>   
>>
>>
>>
>>
>>
>>
>>
>>
>>   
>> 
>>
>> *CURRENT OUTPUT:*
>>
>> 
>>   
>>
>>
>>
>>
>>
>>
>>
>>
>>   
>> 
>>
>> *DESIRED OUTPUT:*
>>
>>  
>>   
>>
>>> />
>>
>>
>>
>>
>>
>>
>>   
>> 
>>
>
> The original 'Field ID' starts out at 2 and ends up at 20980.  If you
> increment that by 20 each time then that is 49 changes to 'Field ID'.  The
> 'string ID' value starts out at 1 and ends up at 1000 which is 999 changes.
>  If both values are changed at the same time then they should both have the
> same number of changes.  So how do you determine when to change one value
> and not the other?
>
>
>
>
> John
> --
> The programmer is fighting against the two most
> destructive forces in the universe: entropy and
> human stupidity.   -- Damian Conway
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: file handling

2010-02-12 Thread John W. Krahn

elavazhagan perl wrote:

Yes, you are absolutely right..
We need to increment the line by one  and field ID by 20.
Whatever my be the fied id..
Thanks,


OK, I give up.  Does anyone else want to try eliciting a cogent and 
informative response?



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.   -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: file handling

2010-02-12 Thread Robert Wohlfarth
On Fri, Feb 12, 2010 at 3:32 AM, elavazhagan perl <
elavazhagan.p...@gmail.com> wrote:

> *ORIGINAL CONTENT:*
> 
>   
>
>
>
>
>
>
>
>
>   
> 
>
> *DESIRED OUTPUT:*
>
>  
>   
>
>
>
>
>
>
>
>
>   
> 
>

In summary, you want to...

   1. Read an entire file into one string.
   2. Using a regular expression, get the current value of "Field ID".
   3. Loop from 2 to 1000...
  1. Increment "Field ID" by 20.
  2. Substitute the new "Field ID" for the old one, using regular
  expressions.
  3. Substitute the counter (L) in the  field.
  4. Write the entire block out to a new file.

Is that accurate?

-- 
Robert Wohlfarth


Re: file handling

2010-02-19 Thread elavazhagan perl
Guys,

Here I come with the code for the dynamic file update.
Please let me know your comments and concerns with this code.
Thanks for your support and comments.

#

#! c:/perl/bin/perl
use File::Copy;

my $file = shift;
my $old = $file;
my $new = "$file.tmp.$$";
my $bak = "$file.bak";

copy($old, $new) or die "can't copy the file:$!";
open(OLD, "< $old") or die "can't open $old: $!";
open(NEW, ">> $new") or die "can't open $new: $!";
print "new file ",$new,"\n\n";

$count=1000;

# Correct typos, preserving case
for($i=1;$i<=$count;$i++) {

 seek(OLD,0,0);
 while (  ) {

  #Condition to match the Field ID and incremanet by 20.
  #
  if ( s/(/ $1 . ( $2 + ($i*20) ) . '">' /e )
  {
   print "OLD:$2 \t NEW:", $2 + ($i*20), "\n";
  }

  #Condition to match the line and increament by 1.
  # 
  if ( s/_L(\d{1,3})_/ '_L' . ( $1 + ($i*1) ) . '_' /e )
  {
   print "OLD:L$1 \t NEW:L", $1 + ($i*1), "\n";
  }

  #Condition to increment the final segment in the above string
  if ( s/( wrote:
>
> On Fri, Feb 12, 2010 at 3:32 AM, elavazhagan perl <
> elavazhagan.p...@gmail.com> wrote:
>
>> *ORIGINAL CONTENT:*
>> 
>>   
>>
>>
>>
>>
>>
>>
>>
>>
>>   
>> 
>>
>> *DESIRED OUTPUT:*
>>
>>  
>>   
>>
>>> />
>>
>>
>>
>>
>>
>>
>>   
>> 
>>
>
> In summary, you want to...
>
>1. Read an entire file into one string.
>2. Using a regular expression, get the current value of "Field ID".
>3. Loop from 2 to 1000...
>   1. Increment "Field ID" by 20.
>   2. Substitute the new "Field ID" for the old one, using regular
>   expressions.
>   3. Substitute the counter (L) in the  field.
>   4. Write the entire block out to a new file.
>
> Is that accurate?
>
> --
> Robert Wohlfarth
>
>


RE: File Handling

2003-01-14 Thread Dan Muey
Try using double quotes around the $filename part when you open it to write.

Dan

-Original Message-
From: Colin Johnstone [mailto:[EMAIL PROTECTED]] 
Sent: Tuesday, January 14, 2003 8:32 AM
To: [EMAIL PROTECTED]
Subject: File Handling


Gidday all,

Im having trouble reading and writing to this file, It's probably just something 
silly. Heres my code. I have set the permissions on the file to 775.


my ($filename);

$filename = "/home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt";

if( -e $filename ){
  print "Yeah - File exists";
}

my @participants;

open IN, "<$filename" || die( "Cannot Open: $!" );
while( my $line =  ){
  chomp $line;
  push( @participants, $line );
}
close IN;

print "Num elements array participants = ".scalar( @participants )."";

my $outString;
$outString  = "";
$outString .= $fields{'SelectCity'} . ",";
$outString .= $fields{'Workshop1'} . ",";
$outString .= $fields{'Workshop2'} . ",";
$outString .= $fields{'Salutation'} . ",";
$outString .= $fields{'FirstName'} . ",";
$outString .= $fields{'LastName'} . ",";
$outString .= $fields{'Title'} . ",";
$outString .= $fields{'CompanyName'} . ",";
$outString .= $fields{'CompanyAddress'} . ",";
$outString .= $fields{'Suburb'} . ",";
$outString .= $fields{'State'} . ",";
$outString .= $fields{'PostCode'} . ",";
$outString .= $fields{'PhoneNumber'} . ",";
$outString .= $fields{'Mobile'} . ",";
$outString .= $fields{'EmailAddress'};

print "Out string =$outString";

push( @participants, $outString );

print "Num elements array participants = ".scalar( @participants )."";

print "@participants\n\n";

open( OUTFILE, '>$filename') or die( "Cannot open file: $!"); while( @participants ){
  my $val = shift( @participants );
  print( OUTFILE "$val\n" );
}
close( OUTFILE ) or die( "Cannot close file: $!");


Here is a sample of the output I get on the screen


Yeah - File exists
Num elements array participants = 2
Out string =Melbourne,Next Generation Alpha Servers - Simplifying Cluster Management 
with Single System Image,HP Single Server Strategy - Itanium is 
real,vcxzvcxzv,vcxzvcxz,vxzcvcxz,zxcvcxzvxc,vzcxvzcx,xczvcxzv,vzxcvzcxv,vcxv
zcxv,vzxcvzcxv,vzcxvxzcv,vzxcvzxcvz,[EMAIL PROTECTED]
Num elements array participants = 3
Melbourne,Next Generation Alpha Servers - Simplifying Cluster Management with Single 
System Image,HP Single Server Strategy - Itanium is 
real,vcxzvcxzv,vcxzvcxz,vxzcvcxz,zxcvcxzvxc,vzcxvzcx,xczvcxzv,vzxcvzcxv,vcxv
zcxv,vzxcvzcxv,vzcxvxzcv,vzxcvzxcvz,[EMAIL PROTECTED]


Im pulling my hair out, maybe I've been at it too long today, Im going to bed, 
hopefully the morning will bring a solution.

Any help appreciated.

Colin



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling

2003-01-14 Thread Rob Dixon
Hi Colin

It looks like your file contains two blank lines to start with. The data
from your hash should be appended to it after the run. Try dumping your
array with:

print "<<$_>>\n" foreach @participants;

then you can see where each record starts and ends.

HTH,

Rob


"Colin Johnstone" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> Gidday all,
>
> Im having trouble reading and writing to this file, It's probably just
> something silly.
> Heres my code. I have set the permissions on the file to 775.
>
> 
> my ($filename);
>
> $filename =
> "/home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt";
>
> if( -e $filename ){
>   print "Yeah - File exists";
> }
>
> my @participants;
>
> open IN, "<$filename" || die( "Cannot Open: $!" );
> while( my $line =  ){
>   chomp $line;
>   push( @participants, $line );
> }
> close IN;
>
> print "Num elements array participants = ".scalar( @participants )."";
>
> my $outString;
> $outString  = "";
> $outString .= $fields{'SelectCity'} . ",";
> $outString .= $fields{'Workshop1'} . ",";
> $outString .= $fields{'Workshop2'} . ",";
> $outString .= $fields{'Salutation'} . ",";
> $outString .= $fields{'FirstName'} . ",";
> $outString .= $fields{'LastName'} . ",";
> $outString .= $fields{'Title'} . ",";
> $outString .= $fields{'CompanyName'} . ",";
> $outString .= $fields{'CompanyAddress'} . ",";
> $outString .= $fields{'Suburb'} . ",";
> $outString .= $fields{'State'} . ",";
> $outString .= $fields{'PostCode'} . ",";
> $outString .= $fields{'PhoneNumber'} . ",";
> $outString .= $fields{'Mobile'} . ",";
> $outString .= $fields{'EmailAddress'};
>
> print "Out string =$outString";
>
> push( @participants, $outString );
>
> print "Num elements array participants = ".scalar( @participants )."";
>
> print "@participants\n\n";
>
> open( OUTFILE, '>$filename') or die( "Cannot open file: $!");
> while( @participants ){
>   my $val = shift( @participants );
>   print( OUTFILE "$val\n" );
> }
> close( OUTFILE ) or die( "Cannot close file: $!");
> 
>
> Here is a sample of the output I get on the screen
>
> 
> Yeah - File exists
> Num elements array participants = 2
> Out string =Melbourne,Next Generation Alpha Servers - Simplifying Cluster
> Management with Single System Image,HP Single Server Strategy - Itanium is
>
real,vcxzvcxzv,vcxzvcxz,vxzcvcxz,zxcvcxzvxc,vzcxvzcx,xczvcxzv,vzxcvzcxv,vcxv
> zcxv,vzxcvzcxv,vzcxvxzcv,vzxcvzxcvz,[EMAIL PROTECTED]
> Num elements array participants = 3
> Melbourne,Next Generation Alpha Servers - Simplifying Cluster Management
> with Single System Image,HP Single Server Strategy - Itanium is
>
real,vcxzvcxzv,vcxzvcxz,vxzcvcxz,zxcvcxzvxc,vzcxvzcx,xczvcxzv,vzxcvzcxv,vcxv
> zcxv,vzxcvzcxv,vzcxvxzcv,vzxcvzxcvz,[EMAIL PROTECTED]
> 




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling

2003-01-15 Thread John W. Krahn
Colin Johnstone wrote:
> 
> Gidday all,

Hello,

> Im having trouble reading and writing to this file, It's probably just
> something silly.
> Heres my code. I have set the permissions on the file to 775.
> 
> 
> my ($filename);
> 
> $filename =
> "/home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt";

You can declare and assign in one statement:

my $filename =
'/home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt';


> if( -e $filename ){
>   print "Yeah - File exists";
> }
> 
> my @participants;
> 
> open IN, "<$filename" || die( "Cannot Open: $!" );

The high precedence of || means that that in interpreted as:

open IN, ( "<$filename" || die( "Cannot Open: $!" ) );

Which is not what you want.  You should use the low precedence or
instead:

open IN, "<$filename" or die "Cannot Open: $!";

Or use parenthesis for the open function:

open( IN, "<$filename" ) || die "Cannot Open: $!";


> while( my $line =  ){
>   chomp $line;
>   push( @participants, $line );
> }

You can declare and assign to the array in one statement:

chomp( my @participants =  );


> close IN;
> 
> print "Num elements array participants = ".scalar( @participants )."";
> 
> my $outString;
> $outString  = "";
> $outString .= $fields{'SelectCity'} . ",";
> $outString .= $fields{'Workshop1'} . ",";
> $outString .= $fields{'Workshop2'} . ",";
> $outString .= $fields{'Salutation'} . ",";
> $outString .= $fields{'FirstName'} . ",";
> $outString .= $fields{'LastName'} . ",";
> $outString .= $fields{'Title'} . ",";
> $outString .= $fields{'CompanyName'} . ",";
> $outString .= $fields{'CompanyAddress'} . ",";
> $outString .= $fields{'Suburb'} . ",";
> $outString .= $fields{'State'} . ",";
> $outString .= $fields{'PostCode'} . ",";
> $outString .= $fields{'PhoneNumber'} . ",";
> $outString .= $fields{'Mobile'} . ",";
> $outString .= $fields{'EmailAddress'};

It looks like you need a join here:

my $outString = join ',',
@fields{ qw( SelectCity Workshop1 Workshop2 Salutation FirstName
 LastName Title CompanyName CompanyAddress Suburb State
 PostCode PhoneNumber Mobile EmailAddress ) };


> print "Out string =$outString";
> 
> push( @participants, $outString );
> 
> print "Num elements array participants = ".scalar( @participants )."";
> 
> print "@participants\n\n";
> 
> open( OUTFILE, '>$filename') or die( "Cannot open file: $!");
> while( @participants ){
>   my $val = shift( @participants );
>   print( OUTFILE "$val\n" );
> }

A foreach loop is more efficient then shifting each element from an
array:

foreach( @participants ) {
  print OUTFILE "$_\n" ;
}


> close( OUTFILE ) or die( "Cannot close file: $!");
> 



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File Handling

2003-01-15 Thread Colin Johnstone
John,

Thank you for your help.

Colin

-Original Message-
From: John W. Krahn [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, 15 January 2003 11:00 PM
To: [EMAIL PROTECTED]
Subject: Re: File Handling


Colin Johnstone wrote:
>
> Gidday all,

Hello,

> Im having trouble reading and writing to this file, It's probably just
> something silly.
> Heres my code. I have set the permissions on the file to 775.
>
> 
> my ($filename);
>
> $filename =
> "/home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt";

You can declare and assign in one statement:

my $filename =
'/home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt';


> if( -e $filename ){
>   print "Yeah - File exists";
> }
>
> my @participants;
>
> open IN, "<$filename" || die( "Cannot Open: $!" );

The high precedence of || means that that in interpreted as:

open IN, ( "<$filename" || die( "Cannot Open: $!" ) );

Which is not what you want.  You should use the low precedence or
instead:

open IN, "<$filename" or die "Cannot Open: $!";

Or use parenthesis for the open function:

open( IN, "<$filename" ) || die "Cannot Open: $!";


> while( my $line =  ){
>   chomp $line;
>   push( @participants, $line );
> }

You can declare and assign to the array in one statement:

chomp( my @participants =  );


> close IN;
>
> print "Num elements array participants = ".scalar( @participants )."";
>
> my $outString;
> $outString  = "";
> $outString .= $fields{'SelectCity'} . ",";
> $outString .= $fields{'Workshop1'} . ",";
> $outString .= $fields{'Workshop2'} . ",";
> $outString .= $fields{'Salutation'} . ",";
> $outString .= $fields{'FirstName'} . ",";
> $outString .= $fields{'LastName'} . ",";
> $outString .= $fields{'Title'} . ",";
> $outString .= $fields{'CompanyName'} . ",";
> $outString .= $fields{'CompanyAddress'} . ",";
> $outString .= $fields{'Suburb'} . ",";
> $outString .= $fields{'State'} . ",";
> $outString .= $fields{'PostCode'} . ",";
> $outString .= $fields{'PhoneNumber'} . ",";
> $outString .= $fields{'Mobile'} . ",";
> $outString .= $fields{'EmailAddress'};

It looks like you need a join here:

my $outString = join ',',
@fields{ qw( SelectCity Workshop1 Workshop2 Salutation FirstName
 LastName Title CompanyName CompanyAddress Suburb State
 PostCode PhoneNumber Mobile EmailAddress ) };


> print "Out string =$outString";
>
> push( @participants, $outString );
>
> print "Num elements array participants = ".scalar( @participants )."";
>
> print "@participants\n\n";
>
> open( OUTFILE, '>$filename') or die( "Cannot open file: $!");
> while( @participants ){
>   my $val = shift( @participants );
>   print( OUTFILE "$val\n" );
> }

A foreach loop is more efficient then shifting each element from an
array:

foreach( @participants ) {
  print OUTFILE "$_\n" ;
}


> close( OUTFILE ) or die( "Cannot close file: $!");
> 



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File handling

2003-02-13 Thread Bob Showalter
Pankaj Kapare wrote:
> Hi
> I want to to file handling in perl.In that I  have one file
> which contains some lines.Now I want to modify some of lines like..
> My file conatins line one
> line two
> line three
> password="password";
> Login Id ="xyz"
> line 6
> and so on
> 
> if i want to write new passord and login id then how can i do
> that? Can  anybody help me with sample code
> Thanks in advance!
> Pankaj

Start with the FAQ article:

   perldoc -q 'change one line'

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File handling

2003-02-13 Thread Paul Kraus
Not tested. I am sure there is a more elegant way to do this but this
should do the trick. It will create a new file with the changes. If you
want to edit it inline you will have to do some research.


#Open files for input and output
open (IN,"outfile") or die "Can not open file for writing\n";
my ($password,$login)=('password','login');
while(){
chomp;
#Assuming that password is at start of line and only
thing on line.
if (/^password=/){
print OUT "password=\"$password\";";
next;
}
if (/^login=/){
print OUT "Login Id = \"$login\";";
next;
}
print OUT "$_\n";
}


-Original Message-
From: Bob Showalter [mailto:[EMAIL PROTECTED]] 
Sent: Thursday, February 13, 2003 10:56 AM
To: 'Pankaj Kapare'; [EMAIL PROTECTED]
Subject: RE: File handling


Pankaj Kapare wrote:
> Hi
> I want to to file handling in perl.In that I  have one file which 
> contains some lines.Now I want to modify some of lines like.. My file 
> conatins line one line two
> line three
> password="password";
> Login Id ="xyz"
> line 6
> and so on
> 
> if i want to write new passord and login id then how can i do that? 
> Can  anybody help me with sample code Thanks in advance!
> Pankaj

Start with the FAQ article:

   perldoc -q 'change one line'

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File handling

2003-02-13 Thread Paul Kraus
This doesn't work with activestate on windows.

 perldoc -q 'change one line'

Output
No documentation for perl FAQ keyword `'change' found.

-Original Message-
From: Bob Showalter [mailto:[EMAIL PROTECTED]] 
Sent: Thursday, February 13, 2003 10:56 AM
To: 'Pankaj Kapare'; [EMAIL PROTECTED]
Subject: RE: File handling


Pankaj Kapare wrote:
> Hi
> I want to to file handling in perl.In that I  have one file which 
> contains some lines.Now I want to modify some of lines like.. My file 
> conatins line one line two
> line three
> password="password";
> Login Id ="xyz"
> line 6
> and so on
> 
> if i want to write new passord and login id then how can i do that? 
> Can  anybody help me with sample code Thanks in advance!
> Pankaj

Start with the FAQ article:

   perldoc -q 'change one line'

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File handling

2003-02-13 Thread Bob Showalter
Paul Kraus wrote:
> This doesn't work with activestate on windows.
> 
>  perldoc -q 'change one line'
> 
> Output
> No documentation for perl FAQ keyword `'change' found.

Use double quotes for brain-dead windoze:

   perldoc -q "change one line"

(I just noticed this FAQ answer has changed significantly from 5.6.1 to 5.8)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File handling

2003-02-13 Thread Ramón Chávez
Try Here:

http://www.perldoc.com/perl5.6/pod/perlfaq5.html

-rm-
- Original Message -
From: Paul Kraus <[EMAIL PROTECTED]>
To: 'Bob Showalter' <[EMAIL PROTECTED]>; 'Pankaj Kapare'
<[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Thursday, February 13, 2003 10:55 AM
Subject: RE: File handling


> This doesn't work with activestate on windows.
>
>  perldoc -q 'change one line'
>
> Output
> No documentation for perl FAQ keyword `'change' found.
>
> -Original Message-
> From: Bob Showalter [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, February 13, 2003 10:56 AM
> To: 'Pankaj Kapare'; [EMAIL PROTECTED]
> Subject: RE: File handling
>
>
> Pankaj Kapare wrote:
> > Hi
> > I want to to file handling in perl.In that I  have one file which
> > contains some lines.Now I want to modify some of lines like.. My file
> > conatins line one line two
> > line three
> > password="password";
> > Login Id ="xyz"
> > line 6
> > and so on
> >
> > if i want to write new passord and login id then how can i do that?
> > Can  anybody help me with sample code Thanks in advance!
> > Pankaj
>
> Start with the FAQ article:
>
>perldoc -q 'change one line'
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File Handling

2004-04-05 Thread Traeder, Philipp
Hi Prabu,

> I have wrote a script to search for a pattern and replace 
> it in all files of a directory,that i specified at 
> commandline.I want another one thing is to be done in the 
> script.That's,it should search only for the type of files I 
> specified at commandline.That is,it should get the extension 
> of file name and search only those files in the directory.

>I have made a attempt in this script,but it goes on asking 
> extension for all the files in directory.
>Plz help me in getting the things right in the following script.
>Tell the changes to be made in this.
>  
> #! /usr/bin/perl 
> 
> print "Enter a path name: ";
> my $path=;
> chomp($path);
> opendir THISDIR, "$path" or die "serious dainbramage: $!";
> my @allfiles = readdir THISDIR;
>  
> # get all files
> foreach $file (@allfiles){
> $filetoopen = $path ."/" .$file;
>  

The following lines are the problem:

> # filter to check the type of file
> print "Enter the type of extension:";
> my $ext=;
> chomp($ext);

You're asking for the extension inside the foreach-loop, therefore your
application
asks you for the extension everytime it processes a file. Simply move those
lines up
(in front of the "get all files" comment), and everything should work. :-)

> ($str1,$str2) = split(/./, $filetoopen);
> if($str2 eq $ext)
> {
> print $str2;
> $filetoopen1 = join(".",$str1,$str2);
> print $filetoopen1;
> open(IN, "<$filetoopen1") || die "cannot open file\n";
> open(OUT, ">$test") || die "cannot open file\n";
> while (){
> if (/$com/){
> s/$com/td>\n\\script>/g;
> }
> if (/$img/){
> s/$img/\n

Re: File Handling problem.

2008-03-27 Thread yitzle
Text files don't /have/ "pages". The number of lines per page depends
on the printer driver -> the font size, margin size, etc.
If you know the number of lines the print driver does per page, you
can fill to that point with newlines based on the number of lines
already outputted.
Or you might be interested in a PostScript/PDF module from CPAN...

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: File Handling problem.

2008-03-28 Thread Ben Bullock
On Mar 28, 1:35 am, [EMAIL PROTECTED] (Ay) wrote:
> Hi,
>
> Current task requires me to combine a few files into a single file
> ( abc. txt ) where in each file has to be in a single page. I was able
> to create a combined file, but not able to ensure that each file
> resides in a page. Attempted a few things like 'format_lines_left'
> i.e $-.  in vain... One of the possibilities left with me is to give
> sufficient "\n" between each file that way ensuring that each file
> read in resides in a page (which is something that I would like to do
> at the last). Is there any other mechanism that can be done to ensure
> that each file resides in a page.. say page break character or
> something...

The traditional page break character for text files is the form-feed
character, which is
ascii 12, 0x0C, \f in Perl and C, or ^L if you use Emacs.

There's more information here:

http://en.wikipedia.org/wiki/Form_feed

and in Google.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: file handling syntax

2002-02-09 Thread John W. Krahn

Steve Doerr wrote:
> 
> Greetings!  Could someone here offer a tip about declaring a file
> variable that is itself part variable?
> 
> I can't seem to find an example of this online and I hope someone here
> could help.
> 
> I'd like to put a path variable in the declaration, but can't seem to
> get it to work.
> 
> What would the proper syntax for this be?
> 
> $file="$form->{path}tbdump.csv";

my $file = $form->{path} . 'tbdump.csv';

> open(tbdump, ">$file");

open TBDUMP, ">$file" or die "Cannot open $file: $!";

> print tbdump "Check that it writes the file.\n";

print TBDUMP "Check that it writes the file.\n";

> close(tbdump);

close TBDUMP;



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File handling using CGI

2006-07-26 Thread Prabu

I BioKid wrote:

One simple question -
I need to accept a file from the user and to store it as temp1.
then I need to give this file as an input of another program :
I wrote a script like this, but it is not working : Help ?

#!/usr/bin/perl -w
use CGI;
my $q = new CGI;
my $file = $q->param('file');
print $q->header();

# Reading file from user and writing to another file temp1
open (FOO, ">temp1");
while (<$file>)
   {
   print FOO $_;
   }
close FOO;

# internaly executing xxx program; taking temp1 as input
`/usr/bin/xxx temp1`;

#temp.xxx is output of usr/bin/xxx
@psa = `cat temp1.xxx`;

foreach $i(@psa)
{
   print "$i";
   print "";
}


Can u explain whats your requirement little more clearly.

You mean reading the file name from the user and copying the file to 
temp1 and then using this temp1.


then you can use the

`cp file1 temp1`

and then to use it in another file.

If not please explain it little more :(

--
Prabu.M.A
When I was born I was so surprised
I didnt talk for a period and half
 -Gracie Allen


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: File handling using CGI

2006-07-26 Thread I BioKid

Dear Prabu and all,

My purpose is simple,
I have a web-form, that will accept a file,
I need to use this file as an input of a program in my server,
then I need to print the out put on the web page.

I am able to do the second part, but am not able to get the users file to a
variable or array ?
I just want to know how to pass the users file to a file called temp in my
server.

I tried this many time :( - pls help
--
thanks in advance !!!


RE: File handling using CGI

2006-07-27 Thread Charles K. Clarkson
I BioKid wrote:

: I am able to do the second part, but am not able to get
: the users file to a variable or array ?

Read the "Files and I/O" section of the "perlintro"
file in the perl documentation.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
Free Market Advocate
Web Programmer

254 968-8328


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: File handling using CGI

2006-07-27 Thread Ken Foskey
On Wed, 2006-07-26 at 23:57 +0530, I BioKid wrote:
> One simple question -
> I need to accept a file from the user and to store it as temp1.
> then I need to give this file as an input of another program :
> I wrote a script like this, but it is not working : Help ?
> 
> #!/usr/bin/perl -w
> use CGI;
> my $q = new CGI;
> my $file = $q->param('file');
> print $q->header();
> 
> # Reading file from user and writing to another file temp1
> open (FOO, ">temp1");
> while (<$file>)
> {
> print FOO $_;
> }
> close FOO;
> 
> # internaly executing xxx program; taking temp1 as input
> `/usr/bin/xxx temp1`;
> 
> #temp.xxx is output of usr/bin/xxx
> @psa = `cat temp1.xxx`;
> 
> foreach $i(@psa)
> {
> print "$i";
> print "";
> }


I think that you may want to look up pipes.  You can pass in data to a
program, have it process it and then get the output out from that
program without using an intermediate file.

I am curious why you wrote this:

>> @psa = `cat temp1.xxx`;
>>
>>foreach $i(@psa)

When you had this a little earlier in your code.

>>open (FOO, ">temp1");
>>while (<$file>)



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: File Handling question - easy

2001-08-02 Thread Michael Fowler

On Thu, Aug 02, 2001 at 11:03:23AM -0700, Jon wrote:
> My problem is, I want to do this for a bunch of different patterns (ex. 
> ".txt", ".this", ".that").

You could say <*.txt *.this *.that>, or you can use opendir, readdir, and a
regex (perhaps grep).


> How can I make what is inside the angle brackets a variable?

You don't, it can confuse Perl and users having to look at your code.  Use
glob() instead of angle brackets, in this case.

$pats = "*.txt *.this *.that";
print join("\n", glob($pats);


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File Handling question - easy

2001-08-02 Thread Bob Showalter

> -Original Message-
> From: Jon [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, August 02, 2001 2:03 PM
> To: [EMAIL PROTECTED]
> Subject: File Handling question - easy
> 
> 
> I don't know if I sent my first email correctly.  Sorry for 
> the repeat if I 
> did, I'm new :)
> 
> Hello,
> The code below takes all files in my current directory that 
> have filenames 
> ending with ".txt". Using this stuff I can loop through those 
> files and do 
> what I need to do.
> 
> My problem is, I want to do this for a bunch of different 
> patterns (ex. 
> ".txt", ".this", ".that"). How can I make what is inside the 
> angle brackets 
> a variable? I've tried to use eval to build my while 
> statement and I can't 
> make it work. Help Please?
> 
> while(<*.txt>)
> {
> ### do stuff, given a filename
> }

Use glob():

   my $patterns = '*.txt *.this *.that';
   for (glob($patterns)) { ... }

P.S. I'm surprised this works with while(). I didn't realize fileglobs
were magical inside while(), but it appears they are... Is this documented?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling question - easy

2001-08-02 Thread Jon

Bob-Mike-Jerry,
Thanks guys

Jon

At 01:36 PM 8/2/01 -0500, Jerry Preston wrote:
>Jon,
>
>Read all the files in the directory then go through them based on the
>file ext
>and do what you want.
>
>   opendir( PROGRAM, "./" );
>   foreach $program ( readdir( PROGRAM )) {
> if( $program =~ ".txt" ) {
>### do stuff, given a filename
> }elsif( $program =~ ".this" ) {
>### do stuff, given a filename
> }
>   }
>
>Jerry
>
>
>Jon wrote:
> >
> > I don't know if I sent my first email correctly.  Sorry for the repeat if I
> > did, I'm new :)
> >
> > Hello,
> > The code below takes all files in my current directory that have filenames
> > ending with ".txt". Using this stuff I can loop through those files and do
> > what I need to do.
> >
> > My problem is, I want to do this for a bunch of different patterns (ex.
> > ".txt", ".this", ".that"). How can I make what is inside the angle brackets
> > a variable? I've tried to use eval to build my while statement and I can't
> > make it work. Help Please?
> >
> > while(<*.txt>)
> > {
> > ### do stuff, given a filename
> > }
> >
> > Jon
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
>
>--
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling question - easy

2001-08-02 Thread Jerry Preston

Jon,

Read all the files in the directory then go through them based on the
file ext 
and do what you want.

  opendir( PROGRAM, "./" );
  foreach $program ( readdir( PROGRAM )) {
if( $program =~ ".txt" ) {
### do stuff, given a filename
}elsif( $program =~ ".this" ) {
### do stuff, given a filename
}
  }

Jerry


Jon wrote:
> 
> I don't know if I sent my first email correctly.  Sorry for the repeat if I
> did, I'm new :)
> 
> Hello,
> The code below takes all files in my current directory that have filenames
> ending with ".txt". Using this stuff I can loop through those files and do
> what I need to do.
> 
> My problem is, I want to do this for a bunch of different patterns (ex.
> ".txt", ".this", ".that"). How can I make what is inside the angle brackets
> a variable? I've tried to use eval to build my while statement and I can't
> make it work. Help Please?
> 
> while(<*.txt>)
> {
> ### do stuff, given a filename
> }
> 
> Jon
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling question - easy

2001-08-02 Thread Michael Fowler

On Thu, Aug 02, 2001 at 02:31:28PM -0400, Bob Showalter wrote:
> P.S. I'm surprised this works with while(). I didn't realize fileglobs
> were magical inside while(), but it appears they are... Is this documented?

Yes, perldoc perlop, in the I/O Operators section (5.6.1 version):

  A (file)glob evaluates its (embedded) argument only when it is
  starting a new list.  All values must be read before it will start
  over.  In list context, this isn't important because you automatically
  get them all anyway.  However, in scalar context the operator returns
  the next value each time it's called, or "undef" when the list has run
  out.  As with filehandle reads, an automatic "defined" is generated
  when the glob occurs in the test part of a "while", because legal glob
  returns (e.g. a file called 0) would otherwise terminate the loop.
  Again, "undef" is returned only once.


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File Handling question - easy

2001-08-02 Thread Jon

Bob,
I got it from "Perl Black Book" by S. Holzner, section on globs.  Very very 
good book to learn Perl from, extremely easy to read, excellent for 
beginners and I'm not going to know this for a long while but I think it is 
an excellent book for intermediate Perl dudes too.  End of section reads 
like this:

Let me give you a useful hint: Using the glob function internally, Perl 
allows you to write expressions like this, which will print the names of 
the files with the extension .txt:

while (<*.txt>) {
 print;
}




> > while(<*.txt>)
> > {
> > ### do stuff, given a filename
> > }
>
>Use glob():
>
>my $patterns = '*.txt *.this *.that';
>for (glob($patterns)) { ... }
>
>P.S. I'm surprised this works with while(). I didn't realize fileglobs
>were magical inside while(), but it appears they are... Is this documented?
>
>--
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File Handling question - easy

2001-08-03 Thread Bob Showalter

> -Original Message-
> From: Michael Fowler [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, August 02, 2001 2:48 PM
> To: Bob Showalter
> Cc: [EMAIL PROTECTED]
> Subject: Re: File Handling question - easy
> 
> 
> On Thu, Aug 02, 2001 at 02:31:28PM -0400, Bob Showalter wrote:
> > P.S. I'm surprised this works with while(). I didn't 
> realize fileglobs
> > were magical inside while(), but it appears they are... Is 
> this documented?
> 
> Yes, perldoc perlop, in the I/O Operators section (5.6.1 version):
> 
>   A (file)glob evaluates its (embedded) argument only when it is
>   starting a new list.  All values must be read before it 
> will start
>   over.  In list context, this isn't important because 
> you automatically
>   get them all anyway.  However, in scalar context the 
> operator returns
>   the next value each time it's called, or "undef" when 
> the list has run
>   out.  As with filehandle reads, an automatic "defined" 
> is generated
>   when the glob occurs in the test part of a "while", 
> because legal glob
>   returns (e.g. a file called 0) would otherwise 
> terminate the loop.
>   Again, "undef" is returned only once.

Thanks for the cite.

I played around with this a bit and found that:

   perl -e 'print scalar(glob("*")) for (1..2)'

prints two different files, while

   perl -e 'print scalar(glob("*")), scalar(glob("*"))'

prints the same file twice. I tried this on 5.005_03 and 5.6.1.

Wonder why? Seems like its related to the loop context and not merely
to scalar context.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling question - easy

2001-08-03 Thread Michael Fowler

On Thu, Aug 02, 2001 at 03:00:24PM -0400, Bob Showalter wrote:
> I played around with this a bit and found that:
> 
>perl -e 'print scalar(glob("*")) for (1..2)'
> 
> prints two different files, while
> 
>perl -e 'print scalar(glob("*")), scalar(glob("*"))'
> 
> prints the same file twice. I tried this on 5.005_03 and 5.6.1.
> 
> Wonder why? Seems like its related to the loop context and not merely
> to scalar context.

It has to do with it being the same operator; you're effectively starting a
new glob with the second scalar(glob '*').  I assume there is some context
assigned to the op code itself.  Consider a similar example to your for
loop:

{
my $file = glob('*');
last unless defined($file);

print $file;

redo;
}


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling. Reading and Writting.

2007-05-14 Thread Tom Phoenix

On 5/14/07, Bruno Schroeder <[EMAIL PROTECTED]> wrote:


I am trying to read and write in a file, I am using something like:

open (FILE, "+

A little better is to include $! in the message, maybe like this:

 open FILE, "+
for my $line () {
 print $line;
 if($line eq "X\n")
 {
print FILE "b\n"


Depending upon your I/O system, you may need to use seek() whenever
you switch from reading to writing, or from writing to reading.

It looks as if you're trying to edit a text file "in place". Although
that's possible for some simple cases, it's generally easier to use
Perl's $^I functionality.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: File Handling. Reading and Writting.

2007-05-15 Thread Ken Foskey

> It looks as if you're trying to edit a text file "in place". Although
> that's possible for some simple cases, it's generally easier to use
> Perl's $^I functionality.
> 

What is $^I?


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: File Handling. Reading and Writting.

2007-05-15 Thread Chas Owens

On 5/15/07, Ken Foskey <[EMAIL PROTECTED]> wrote:


> It looks as if you're trying to edit a text file "in place". Although
> that's possible for some simple cases, it's generally easier to use
> Perl's $^I functionality.
>

What is $^I?


It is a special scalar variable that turns on/off in-place editing.

from perldoc perlvar
  $^I The current value of the inplace-edit extension.  Use "undef"
  to disable inplace editing.  (Mnemonic: value of -i switch.)

from perldoc perlrun
  -i[extension]
   specifies that files processed by the "<>" construct are to be
   edited in-place.  It does this by renaming the input file, opening
   the output file by the original name, and selecting that output
   file as the default for print() statements.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: File Handling. Reading and Writting.

2007-05-15 Thread Bruno Schroeder
Hello Tom!
Hello All!

You helped a lot Tom. Still... I have some trouble.

For sure $^I is easier to use. Although in my case i would need to rewrite a 
lot of code. I will do it, but it will take a while to test it. I found good 
information on $^I at 
http://perldoc.perl.org/perlfaq5.html#How-can-I-use-Perl%27s-%27-i%27-option-from-within-a-program%3F

I found some information for seek() at 
http://perldoc.perl.org/functions/seek.html

I tryed to use seek but it did not work. Can you help me on that, please? I 
am using Windows XP. The following example writes at the end of the file.

use strict;
my $file = "teste_rw.txt";
open (FILE, "+<", $file) or die "Can not open $file: $!.";
for my $line () {
print $line;
seek(FILE, 0, 1);
print FILE "b\n";
seek(FILE, 0, 1);
}
my $a_while = 2;
sleep($a_while);
seek(FILE, tell(FILE), 0);
close FILE;

Thank you.
Bruno


""Tom Phoenix"" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> On 5/14/07, Bruno Schroeder <[EMAIL PROTECTED]> wrote:
>
>> I am trying to read and write in a file, I am using something like:
>>
>> open (FILE, "+
> A little better is to include $! in the message, maybe like this:
>
>  open FILE, "+or die "Can't open r/w 'teste_rw.txt': $!";
>
>> for my $line () {
>>  print $line;
>>  if($line eq "X\n")
>>  {
>> print FILE "b\n"
>
> Depending upon your I/O system, you may need to use seek() whenever
> you switch from reading to writing, or from writing to reading.
>
> It looks as if you're trying to edit a text file "in place". Although
> that's possible for some simple cases, it's generally easier to use
> Perl's $^I functionality.
>
> Hope this helps!
>
> --Tom Phoenix
> Stonehenge Perl Training 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: File Handling. Reading and Writting.

2007-05-15 Thread Tom Phoenix

On 5/15/07, Bruno Schroeder <[EMAIL PROTECTED]> wrote:


I tryed to use seek but it did not work. Can you help me on that, please? I
am using Windows XP. The following example writes at the end of the file.

use strict;
my $file = "teste_rw.txt";
open (FILE, "+<", $file) or die "Can not open $file: $!.";
for my $line () {
print $line;
seek(FILE, 0, 1);
print FILE "b\n";
seek(FILE, 0, 1);
}
my $a_while = 2;
sleep($a_while);
seek(FILE, tell(FILE), 0);
close FILE;


Here's some untested code that may do something resembling what you want:

 my $file = "teste_rw.txt";
 open (FILE, "+<", $file) or die "Can not open '$file' r/w: $!";

 # $next_loc is the location of the next line to process
 my $next_loc = tell FILE;  # probably 0

 while (1) {

   my $current_loc = $next_loc;
   # seek before each read or write
   seek(FILE, $current_loc, 0) or die;
   my $line = ;
   last if not defined $line;  # undef at eof
   $next_loc = tell FILE;

   print $line;

   # Get the replacement string (somehow).
   my $repl = &replacement_for($line);

   die "Can't replace '$line' with '$repl'"
 unless length($line) == length($repl);

   # seek before each read or write
   seek(FILE, $current_loc, 0) or die;
   print FILE $repl;
 }

 close FILE;

I'm not sure why your code used sleep, so I omitted it. Cheers!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: File Handling. Reading and Writting.

2007-05-15 Thread Bruno Schroeder
Thank you Tom!
We realy have lots of ways to do everything. In this case, is easier to use 
$^I functionality, and I did. Although, it is very nice to see this code 
bellow.

Cheers!

""Tom Phoenix"" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> On 5/15/07, Bruno Schroeder <[EMAIL PROTECTED]> wrote:
>
>> I tryed to use seek but it did not work. Can you help me on that, please? 
>> I
>> am using Windows XP. The following example writes at the end of the file.
>>
>> use strict;
>> my $file = "teste_rw.txt";
>> open (FILE, "+<", $file) or die "Can not open $file: $!.";
>> for my $line () {
>> print $line;
>> seek(FILE, 0, 1);
>> print FILE "b\n";
>> seek(FILE, 0, 1);
>> }
>> my $a_while = 2;
>> sleep($a_while);
>> seek(FILE, tell(FILE), 0);
>> close FILE;
>
> Here's some untested code that may do something resembling what you want:
>
>  my $file = "teste_rw.txt";
>  open (FILE, "+<", $file) or die "Can not open '$file' r/w: $!";
>
>  # $next_loc is the location of the next line to process
>  my $next_loc = tell FILE;  # probably 0
>
>  while (1) {
>
>my $current_loc = $next_loc;
># seek before each read or write
>seek(FILE, $current_loc, 0) or die;
>my $line = ;
>last if not defined $line;  # undef at eof
>$next_loc = tell FILE;
>
>print $line;
>
># Get the replacement string (somehow).
>my $repl = &replacement_for($line);
>
>die "Can't replace '$line' with '$repl'"
>  unless length($line) == length($repl);
>
># seek before each read or write
>seek(FILE, $current_loc, 0) or die;
>print FILE $repl;
>  }
>
>  close FILE;
>
> I'm not sure why your code used sleep, so I omitted it. Cheers!
>
> --Tom Phoenix
> Stonehenge Perl Training 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/