CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Regular Expressions:



May 27, 2000, 5:56 PM

Post #1 of 3 (5799 views)
Spaces Can't Post

I was trying to use the definitions I learned from Cure and the use of them from Japhy (Thanks Guys Smile) to take out a space in front of every line except the first line in a file. Because of when I move the contents of a file from one file to another the first line was ok but every line after that had a space in front of it. I tried to get rid of this by using a regex on the scalar I stored the data in like so,

$file =~ s/^ {1}//;

But it didn't work.

I thought that it would check to see if the lines would have a space in front of them and if it did it would take it out.

Is the code right and the code I used around it was probably wrong?

The Code I used around it is,

open(D, "</path/file.db")
$file =~ s/^ {1}//;
print W "$file";

Does it have to be in a foreach loop?

I am currently using a different method with while and I don't have this problem but I would like to know what I goofed up on so I can use that code in the future.



Enthusiast / Moderator

May 27, 2000, 6:36 PM

Post #2 of 3 (5798 views)
Re: Spaces [In reply to] Can't Post

First, do you know WHY all the lines but the first have an extra space in the front? Because when you do

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$string = "@array";

what you're actually saying is

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$string = join $", @array;

$" is a special Perl variable that holds the list separator -- the string that Perl puts in between elements of an array when the array is inside double quotes. It defaults to a single space: " ".

Therefore, all the elements (except the first) of @array have a " " put in front of them when they are put in $string.

Now you know WHY that happens.

Now you want to know how to FIX it. Here are several ways.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

# 1. use while()
while (<FILE> ) {
print OUTPUT;

# 2. use join()
$string = join "", <FILE>;
print OUTPUT $string;

# 3. use the proper regex
@lines = <FILE>;
$string = "@lines";
$string =~ s/^ //mg;

I strongly discourage using method 3, because it requires you store the lines of the file in an array, and then store them in a string, and then uses a regex on the string -- these are unnecessary. I suggest method 1 be used whenever possible.

About the regex... you need to use

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

s/^ //gm

First, the /m modifier changes the meanings of ^ and $. Ordinarily, ^ matches at the FRONT OF A STRING, and $ matches at the END OF A STRING (or before the ending newline of a string). With /m on, ^ matches at the front of a string as well as IMMEDIATELY after any newline, and $ matches at the end of a string as well as IMMEDIATELY before any newline.

The "^ " then means "match a single space at the BEGINNING of the string, or IMMEDIATELY after a \n".

And we have the /g modifier because we want to do this globally, for the entire string.

However, this regex needs to be changed OH-SO-SLIGHTLY, to be perfect.

What if the first element of your array REALLY DOES have a space as its first character? You don't want to delete it. You only want to remove the first character of every line BUT THE FIRST. So that means remove a space after a newline.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@lines = <FILE>;
$string = "@lines";
$string =~ s/(?<=\n) //g;

This doesn't need ^ or /m. This just says "remove a space that is preceeded by a newline, and do this globally."

[This message has been edited by japhy (edited 05-27-2000).]


May 27, 2000, 9:39 PM

Post #3 of 3 (5798 views)
Re: Spaces [In reply to] Can't Post

Thanks Japhy,

That was quite an elaborate response. Thank You.

I now understand how it works. And of course the code works great.

Also, Could you have a look at this problem that I have right now.

I have a form that allows multiple lines. It's meant to act like a temp data base. You put your list of words into the forum and submit it. The script should read the form as if it were a data base and read the according data base full of results and store them into an array. Then the perl is supposed to take out all of the lines in the array that has stored the data from the according file and print them back to the according file.

Do You See What I'm trying to do.

Here is the code I'm using,
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

print "Filtering....<br><br>";
@wordsinform = $form{'words'};
foreach $line (@wholefile)
foreach $filter (@wordsinform)
@filt=split(/ /, $filter);
if ($line =~ m/$filter/i)
print OUTPUT "$line";

print "Done";

It only uses the first line in the form to remove unwanted lines. I printed the array that holds the form's values and I found that it was delimitated by a space and in a single line.

What Do you think of it?

I hope I explained it well.



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives