Re: Removing leading whitespace and removing alternate newlines

2010-11-02 Thread Matthew Young
On Oct 31, 10:01 am, brian5432...@yahoo.co.uk (Brian) wrote:
 Hi guys, long time no working with PERL :-)

 I have just installed 5.2.12

 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;
 open(FILE, smith3.txt) || die (Error\n);
 $string = FILE;
 $string =~ s/\s//; #remove leading spaces

 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?

 Thanks muchly
 Brian

Is $/ a special variable of some kind?


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




Re: Removing leading whitespace and removing alternate newlines

2010-11-02 Thread shawn wilson
yeah, it's one of those that you shouldn't modify or you might start seeing
unexpected results. read perlvar or google 'perl input record separator' for
more.

On Mon, Nov 1, 2010 at 10:14 AM, Matthew Young mab...@gmail.com wrote:

 On Oct 31, 10:01 am, brian5432...@yahoo.co.uk (Brian) wrote:
  Hi guys, long time no working with PERL :-)
 
  I have just installed 5.2.12
 
  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;
  open(FILE, smith3.txt) || die (Error\n);
  $string = FILE;
  $string =~ s/\s//; #remove leading spaces
 
  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?
 
  Thanks muchly
  Brian

 Is $/ a special variable of some kind?


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





Re: Removing leading whitespace and removing alternate newlines

2010-10-31 Thread Richard Hug
$string =~ s/^\s+//;

http://oreilly.com/catalog/9781565922433/ page 43

That book is my bible!!!

On Sun, Oct 31, 2010 at 11:01 AM, Brian brian5432...@yahoo.co.uk wrote:

 Hi guys, long time no working with PERL :-)

 I have just installed 5.2.12

 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;
 open(FILE, smith3.txt) || die (Error\n);
 $string = FILE;
 $string =~ s/\s//; #remove leading spaces

 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?

 Thanks muchly
 Brian







-- 
Richard Hug
813-973-3783
cell 813-361-7946


Re: Removing leading whitespace and removing alternate newlines

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

See:

http://perl.org.il/misc.html#pl_vs_pl

 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
 

Add strict and warnings. See:

http://perl-begin.org/tutorials/bad-elements/

 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

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang

rindolf She's a hot chick. But she smokes.
go|dfish She can smoke as long as she's smokin'.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




Re: Removing leading whitespace and removing alternate newlines

2010-10-31 Thread Brian
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

-- 


  

Re: Removing leading whitespace and removing alternate newlines

2010-10-31 Thread Brian


Thanks Shlomi, your untested code worked first time.
:-)




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 :-)
 
 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


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


  

Re: Removing leading whitespace and removing alternate newlines

2010-10-31 Thread John W. Krahn

Brian wrote:

Hi guys, long time no working with PERL :-)


Hello,


I have just installed 5.2.12

First, I am trying to remove leading tabs  spaces


Just tabs and spaces?  Your code says all whitespace.


but the following leaves a
few blanks at the beginning of each line,


You code only removes one whitespace character.


could someone be kind enough to point
out the error of my ways please? :-)

#!/usr/bin/perl


use warnings;
use strict;


local $/=undef;


Or just:

local $/;


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


Better as:

open FILE, '', 'smith3.txt' or die Cannot open 'smith3.txt' $!;


$string =FILE;


my $string = FILE;


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


$string =~ s/^[ \t]+//mg; #remove leading spaces and tabs from every line


print $string;


No need to stringify something that is alraedy a string:

print $string;

perldoc -q quoting



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?


my $count;
$string =~ s/(\n)/ $count++ % 2 ? $1 : '' /eg;




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

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




Re: Removing leading whitespace and removing alternate newlines

2010-10-31 Thread Brian


Thank you John



From: John W. Krahn jwkr...@shaw.ca
To: Perl Beginners beginners@perl.org
Sent: Sun, October 31, 2010 8:59:43 PM
Subject: Re: Removing leading whitespace and removing alternate newlines

Brian wrote:
 Hi guys, long time no working with PERL :-)

Hello,

 I have just installed 5.2.12

 First, I am trying to remove leading tabs  spaces

Just tabs and spaces?  Your code says all whitespace.

 but the following leaves a
 few blanks at the beginning of each line,

You code only removes one whitespace character.

 could someone be kind enough to point
 out the error of my ways please? :-)

 #!/usr/bin/perl

use warnings;
use strict;

 local $/=undef;

Or just:

local $/;

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

Better as:

open FILE, '', 'smith3.txt' or die Cannot open 'smith3.txt' $!;

 $string =FILE;

my $string = FILE;

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

$string =~ s/^[ \t]+//mg; #remove leading spaces and tabs from every line

 print $string;

No need to stringify something that is alraedy a string:

print $string;

perldoc -q quoting


 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?

my $count;
$string =~ s/(\n)/ $count++ % 2 ? $1 : '' /eg;




John
-- 
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

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