perl regex question

LimeGreen

Banned
Oct 23, 2001
145
0
0
I want to get a directory listing, and right now it's on this computer (windows), and I need to either remove all the <DIR> or just escape them in an html sense (not sure how to do this, <!-- --> ??). I'm not sure if I need to escape the actual < and > within the regex as well. Well here is what I have and of course it doesn't work, if anyone could clean it up for me:

# these are only getting things in directory tournaments which begin with "t"

@output = system("dir tournaments\\t*");
# @output = system("ls tournaments/t*");

foreach (@output) {

# instead of completely removing this i'd like to just "escape" it html-wise.

$_ =~ s/<DIR>//g;

# sorry the space is only there because
was making a new line (in this post)

print "$_< BR>\n"; }
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
i'll have to space these out also (due to forums being html :p)

you want to change the <'s to:
& lt;

and the >'s to:
& gt;
 

LimeGreen

Banned
Oct 23, 2001
145
0
0
nevermind, i just realized that @output isn't receiving any data. does anyone know how i can get system() to pipe all output into @output ?
 

LimeGreen

Banned
Oct 23, 2001
145
0
0
well i didn't know them, but now i do, and i'll use them for when this goes onto the unix computer where it belongs.

but for now i'm just creating it on this PC, which is windows.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
opendir() and readdir() should work just as well on win32, that's why I mentioned it.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
#!/usr/bin/perl -w

# Perl knows how to distinguish / on unix and windows, and it will automatically
# convert / to \\ on windows
# So, instead of (what my teacher says ugly characters) \\, use / as the directory separator
$dirname = "C:/tournaments/";

opendir (DIR, "$dirname") || die "Error! Cannot open directory for listing\n\n";

foreach (readdir DIR) # Sorting optional, but recommended
{
...# next if the things does not start with a t (hint:: use regexp / pattern matching)
...# Do your cleanup
...# Substitute & and < and >
...# Store the good "stuffs" in your list (array)
}

closedir DIR;

## End program

and yeah, like what Nothinman said, opendir(), readdir(), etc, works both on windows and unix... and good perl programmer should always try to avoid system() calls and using backticks `` as much as possible (esp. if the modules are readily available in Perl)

i'd give you the full code, but then again i don't know if this is homework or hobby programming, so ......
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
foreach (readdir DIR) # Sorting optional, but recommended
{

If you want, you can just do:

@directory_list = readdir(DIR);

And the whole directory listing will be put in that array.

You still probably want to foreach through it eventually, but you can use a regexp to weed out what you want directly on the array beforehand.