RE: search and replace confusion



Jason Balicki <mailto:kodak@xxxxxxxxxxxxxxxxxxxxxxxx> wrote:

: in the file, I want to convert it to:
:
: DTSTART:20050616T070000Z
:
: But with my code below this line prints as:
:
: T070000Z20050616
:
: overwriting instead of appending the time value.

Not when I tried it. It was appended.

I did need to correct a typo, but this line
doesn't do what you want.

: my $ownerline = join "", $_, ",*\n"/;

my $ownerline = join "", $_, ",*\n";


: foreach my $argnum (0 .. $#ARGV){
: my $inputfile = $ARGV[$argnum];
: open FILE, '<', $inputfile or die "Cannot open $inputfile:
: $!";


Why not just loop through file names and avoid the extra
variable?

foreach my $file ( @ARGV ) {
open FILE, '<', $file or die "Cannot open $file: $!";

:
: while ( <FILE> ) {
: chomp $_;

Why chomp it here? You are most likely not going to chomp the
line.



: if (/DTSTART:/) {
: my $startline= $_ . "T070000Z\n";
: print $startline;

You're jumping through too many hoops. Print $_ each time.
If you find a line which needs appending, chomp it and append
it first.

while ( <FILE> ) {

if ( /DTSTART:/ or /DTEND:/ ) {
# Same as "chomp $_;".
chomp;

# The ".=" operator appends new text to a string.
$_ .= "T070000Z\n";

} elsif ( /X-SQ-EVTOWNERS:/ ) {
chomp;
$_ .= ",*\n";

}

# same as "print $_;".
print;
}


.



Relevant Pages

  • RE: question about appending spaces to each line of a file
    ... To: Nupur Pande ... Subject: Re: question about appending spaces to each line of a file ... > I want to read each line from a file, chomp off the newline character, ... newline should disappear. ...
    (perl.beginners)
  • Re: luactivate and the -s switch
    ... Synchronization of selected files is in progress... ... Appending selected files... ... Except the "Overwriting selected files" never takes place. ...
    (comp.sys.sun.admin)
  • RE: search and replace confusion
    ... some iCal files so that a specific application will read ... overwriting instead of appending the time value. ... X-SQ-EVTREADABLEUSERS: ...
    (perl.beginners)
  • search and replace confusion
    ... some iCal files so that a specific application will read ... overwriting instead of appending the time value. ... X-SQ-EVTREADABLEUSERS: ...
    (perl.beginners)
  • Re: gui problem
    ... guidata(hObject, timeplot5); ... The second line is overwriting (not appending to) the first ... and hence the handles structure is being lost. ...
    (comp.soft-sys.matlab)