Thank you Shlomi, I'll test part 2 tomorrow when I have more time.
As for part one, what would be the better way of writing the code so that I can 
read/treat one line at a time?

thanks again
Brian




________________________________
From: Shlomi Fish <shlo...@iglu.org.il>
To: beginners@perl.org
Cc: Brian <brian5432...@yahoo.co.uk>
Sent: Sun, October 31, 2010 4:23:52 PM
Subject: Re: Removing leading whitespace and removing alternate newlines

On Sunday 31 October 2010 17:01:24 Brian wrote:
> Hi guys, long time no working with PERL :-)
> 


> I have just installed 5.2.12
> 

You probably mean perl-5.12.2 .

A few more comments on your code:

> First, I am trying to remove leading tabs & spaces but the following leaves
> a few blanks at the beginning of each line, could someone be kind enough
> to point out the error of my ways please? :-)
> 
> #!/usr/bin/perl
> 

> local $/=undef;

You probably don't want to read the entire file to memory at once, as it may 
be very large. 

> open(FILE, "smith3.txt") || die ("Error\n");

Use three-args-open, lexical filehandles, and use a more descriptive error 
than "Error.".

> $string = <FILE>;
> $string =~ s/\s//; #remove leading spaces
> 

This will remove only the first whitespace character (and in your case, from 
the entire file).

> print "$string";
> 
> 
> 
> Secondly, I would like to remove newline from alternate lines, ie I would
> like to remove from lines 1,3,5,7 etc.
> What would be the simplest way of getting even line numbers to print on the
> same line as odds?

Use something like this (untested):

[code]
#!/usr/bin/perl

use strict;
use warnings;

my $filename = shift(@ARGV)
    or die "No filename specified in command-line";

open my $fh, "<", $filename
    or die "Cannot open '$filename' for reading - $!";

my $line_num = 1;
while (my $line = <$fh>)
{
    chomp ($line);

    $line =~ s{\A\s+}{};

    print $line;

    if ($line_num % 2 == 0)
    {
        print "\n";
    }
}
continue
{
    $line_num++;
}
close($fh);
[/code]

Regards,

    Shlomi Fish

> 
> Thanks muchly
> Brian

-- 


      

Reply via email to