Perl RegExp  | | |
October 19th, 2002, 06:37 PM
|
#1 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,386
|
OK I have a VERY simple script that's not playing fair  lol Code: #! /usr/bin/perl -w
my $rootdir = "/home/vass/Stuff";
my $imgtag = "<img src=";
opendir(DNAME,$rootdir) || die "Unable to open dir";
open(HTML, ">$rootdir/pics.html") || die "damnit, can't open file $!";
print HTML "<html>";
print HTML "<head>";
while($filename = readdir(DNAME))
{
$filename =~ s/ /%20/;
print HTML "$imgtag$filename> \n";
}
closedir(DNAME);
print HTML "</head>";
print HTML "</html>"; I'm pretty sure you can figure out what it does, but I'm just learning perl 
My problem is I thought the regular expression would remove ALL instances of (in this case) the space and replace it with the %20!?!
It only seems to replace 1  |
| |
October 19th, 2002, 07:14 PM
|
#2 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Alberta, Canada
Posts: 563
|
I've only done very little with regular expressions, but do you need an asterisk '*' in there?? Quote:
You can follow any character, wildcard, or series of characters and/or wildcard with a repetiton. Here's where you start getting some power: * Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times link | so you'd end up with (something like this I think!!):
$filename =~ s/* /%20/; or is it this: $filename =~ s/ */%20/;
also, just above that quote from that page there's this: "\s Match whitespace character"
so could you do something like this(??):
$filename =~ s/*\s/%20/; or would it be this: $filename =~ s/\s*/%20/; 
just remember what I said at the start, me not so good with regular expressions , so I could be waaay off... 
g'luck!! 
hhmmm, might have to start playin with these little goodies sometime(regular expressions)... don't have perl installed, but I have php and I believe it has 'em... 
PS: woohoo!! # 501!!!!  |
| |
October 20th, 2002, 03:55 AM
|
#3 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,386
|
ok we're getting closer  lol Code: while($filename = readdir(DNAME))
{
$filename =~ tr/ /%20/;
print HTML "$imgtag$filename> \n";
} OK
tr works better than s for this one lol
HOWEVER... I can't get it replace the text with %20.. instead it replaces it with % 
I've tried it with star, I've tried with
$filename =~ tr/\s/%20/; -- doesn't like that at all
$filename =~ tr/[ ]/[%20]/; -- same results
$filename =~ tr/ */%20/; -- same results
still no luck 
__________________
Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.
|
| |
October 20th, 2002, 11:13 AM
|
#4 (permalink)
| | Member
Join Date: Nov 2001 Location: Bloomington IN
Posts: 219
|
It might be trying to interpret the %. Maybe through a backslash in front of it.
tr/ /\%20/ |
| |
October 20th, 2002, 05:36 PM
|
#5 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,386
|
I tried that... same results  |
| |
October 20th, 2002, 06:48 PM
|
#6 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Alberta, Canada
Posts: 563
|
well, I got perl installed for iis. Set up a test suite, and it worked! ONCE, and only once...
now, as soon as it hits the "opendir()" statement, it just stops processing... bah... have to get back to it later, need a shower, and some food!
ciao!  |
| |
October 20th, 2002, 07:26 PM
|
#7 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,386
|
lol
you get active perl?
hmmm lemme see if that'll work on this winbox
-- edit --
ok ya it has same prob over here 
with s/ /%20 it does replace it with %20 but it only replaces one
with tr/ /%20 it replaces all of them.. with %
I realize that tr/ / replaces on a per character basis so I think its only replacing the first char it finds in the /%20
Now how to get it to either replace all with s/ / or replace with all the characters with tr/ / LOL
ahhhh and these are the days of our lives
Last edited by vass0922 : October 20th, 2002 at 07:32 PM.
|
| |
October 20th, 2002, 07:39 PM
|
#8 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,386
|
AHH HA!! think I got it Code: #! /usr/bin/perl -w
my $rootdir = "C:\\";
my $imgtag = "<img src=";
my $replace = "%20";
opendir(DNAME,$rootdir) || die "Unable to open dir";
open(HTML, ">$rootdir/pics.html") || die "damnit, can't open file $!";
print HTML "<html>";
print HTML "<head>";
while($filename = readdir(DNAME))
{
$filename =~ s/ /$replace/g;
print HTML "$imgtag$filename> \n";
}
closedir(DNAME);
print HTML "</head>";
print HTML "</html>"; I needed the $filename =~ s/ /$replace/ g; for global replace at the end of the line 
its the little things that get ya
Thanks for the help
hmm now to replace the & ampersand .. happen to know the char for that?  lol
I'll start looking... |
| |
October 20th, 2002, 07:41 PM
|
#9 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Alberta, Canada
Posts: 563
|
well, actually, I think it did replace all spaces with the full %20, but when I deleted the contents from pics.html and tried it again it just wouldn't do it!! only the once, then it just stopped!! no error messages or anything... can't even get a simple "print" statement to work after the opendir() command...
back to have another look now tho!! hehehe |
| |
October 20th, 2002, 07:45 PM
|
#10 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,386
| |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |