Removing whitespace and linebreaks in CSS files

A

ahjiang

Hi all,

Rather new to perl.

Would like to know if there is any existing modules that remove
whitespace and line breaks in cascading stylesheet files.

Appreciate any inputs
 
P

Paul Lalli

Rather new to perl.

http://learn.perl.org is a good place to start
Also, run `perldoc perlintro` on your command line to get an
introduction to the language.
Would like to know if there is any existing modules that remove
whitespace and line breaks in cascading stylesheet files.

That problem is too simplistic for an entire module. It's one line of
code.

s/\s{2,}|\n/ /g for @lines;

That will go through all lines contained in @lines, and replace any
sequences of more than one whitespace, and all newlines, with a single
space. It is a trivial matter both to open the file and get its
content into @lines, and another trivial matter to print the modified
@lines to the file.

perldoc perlopentut
for both.

Paul Lalli
 
A

ahjiang

thanks for the advice..will look into it..

u really amaze me with the single line code!!!
 
R

robic0

http://learn.perl.org is a good place to start
Also, run `perldoc perlintro` on your command line to get an
introduction to the language.


That problem is too simplistic for an entire module. It's one line of
code.

s/\s{2,}|\n/ /g for @lines;

s/\s/g will remove all
\s is a whitespace character and represents [\ \t\r\n\f]
is that what you want, or do you know what you want?
 
P

Paul Lalli

robic0 said:
s/\s/g will remove all

That will not remove anything. It will not do anything but generate a
compiler error.
\s is a whitespace character and represents [\ \t\r\n\f]
is that what you want, or do you know what you want?

Why are you talking to the OP in a reply to my message?

Paul Lalli
 
A

ahjiang

sorry can u briefly explain what this line does?

s/\s{2,}|\n/ /g for @lines;

for all the lines...
s/\s{2,}|\n/ /g ??????
 
G

Guest

(e-mail address removed) wrote:
: sorry can u briefly explain what this line does?

: s/\s{2,}|\n/ /g for @lines;

^ s=substitute /value/replacement/

Replaces value found by a replacement; the final g means "global", all
occurrences in searched data.

You look for a value defined as \s{2,}|\n which reads as:
every occurrence of whitespace (= \s) which is at least
two characters long, and has no defined upper limit (={2,}),
or, alternatively, (= | ), a newline (= \n).

Your replacement is a single space.
: for all the lines...
: s/\s{2,}|\n/ /g ??????

Check the perlre manpage.

Oliver.
 
A

Anno Siegel

Paul Lalli said:
http://learn.perl.org is a good place to start
Also, run `perldoc perlintro` on your command line to get an
introduction to the language.


That problem is too simplistic for an entire module. It's one line of
code.

s/\s{2,}|\n/ /g for @lines;

That's exactly what tr///'s squeeze-option does:

tr/ \n/ /s for @lines;

Anno
 
P

Paul Lalli

Anno said:
That's exactly what tr///'s squeeze-option does:

tr/ \n/ /s for @lines;

Dang it. I *knew* there was a cleaner way to write that, but it wasn't
coming to me. I always manage to forget about tr///. Thanks, Anno.

Paul Lalli
 
J

John W. Krahn

Anno said:
That's exactly what tr///'s squeeze-option does:

tr/ \n/ /s for @lines;

Don't forget the other characters in \s:

tr/ \f\t\r\n/ /s for @lines;


:)

John
 
A

Anno Siegel

John W. Krahn said:
Don't forget the other characters in \s:

tr/ \f\t\r\n/ /s for @lines;

Ah, I misread the code somehow and thought it only did blank and line feed.

That is the one drawback of tr/// that you must expand \s yourself to be
as general as s///.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top