Re: [NEWBIE] newline question
From: Gunnar Hjalmarsson (noreply_at_gunnar.cc)
Date: 03/31/04
- Next message: Helmut Blass: "Problems with installing MIME"
- Previous message: pkent: "Re: Spawn child processes - VMS perl 5.6.1"
- In reply to: Jan Biel: "[NEWBIE] newline question"
- Next in thread: Brian McCauley: "Re: [NEWBIE] newline question"
- Reply: Brian McCauley: "Re: [NEWBIE] newline question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 31 Mar 2004 20:03:09 +0200
Jan Biel wrote:
> The original perl script looks like this:
use strict; # Make Perl help you detect errors
use warnings; # "-
> $filein = 'a.txt';
> $fileout = 'b.txt';
my $filein = 'a.txt';
my $fileout = 'b.txt';
----^^
Declare variables with my()
> open(INFO, $filein);
> open(INFO2, ">$fileout");
open INFO, $filein or die $!;
open INFO2, "> $fileout" or die $!;
----------------------------^^^^^^^^^^
Check if file was successfully opened
> @lines = <INFO>;
my @lines = <INFO>;
> grep(s/\n//g,@lines);
That works, but it's clearer written as:
@lines = map { tr/\n//d; $_ } @lines;
Now it's time for reflection. :)
@lines is an array, and at this point, it contains three elements. You
seem to want to concatenate the elements to a string. That can be
done like this:
my $string = join '', @lines;
> grep(s/ab/found/g,@lines);
That takes one element at a time, and replaces occurrences of the
sting 'ab'. None of the elements contains that string, so nothing happens.
You can apply the s/// operator to $string instead:
$string =~ s/ab/found/g;
> print INFO2 @lines;
print INFO2 "$string\n";
> close(INFO);
> close(INFO2);
> --------------------------------
HTH
-- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
- Next message: Helmut Blass: "Problems with installing MIME"
- Previous message: pkent: "Re: Spawn child processes - VMS perl 5.6.1"
- In reply to: Jan Biel: "[NEWBIE] newline question"
- Next in thread: Brian McCauley: "Re: [NEWBIE] newline question"
- Reply: Brian McCauley: "Re: [NEWBIE] newline question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|