Re: [NEWBIE] newline question

From: Gunnar Hjalmarsson (noreply_at_gunnar.cc)
Date: 03/31/04


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


Relevant Pages

  • Validating a String in Java
    ... If a user enters characters other than the mentioned above, ... declare variables of integer types ... declar variables of string types ... Calulate the length of the string variable ls_tmp_string and store ...
    (comp.lang.java.programmer)
  • Re: JavaScript to validate User input
    ... I need to write a Java Script for a string payment_code which comes ... If a user enters characters other than the mentioned above, ... declare variables of integer types ... Calulate the length of the string variable ls_tmp_string and store ...
    (comp.lang.javascript)
  • Re: Validating a String in Java
    ... I need to write a small peice of code a string "payment_code" ... declare variables of integer types ... var li_len, li_idx, li_value ...
    (comp.lang.java.programmer)
  • JavaScript to validate User input
    ... I need to write a Java Script for a string payment_code which comes ... If a user enters characters other than the mentioned above, ... declare variables of integer types ... Calulate the length of the string variable ls_tmp_string and store ...
    (comp.lang.javascript)