I need to set a variable to a filename where only 1 section of the file is
static.
For example:
$filename =~ /test/;
Where the following:
Print $filename\n;
Would produce:
123test456.txt
The only way I see this being possible is with regular expressions but I
can't for
: Curt Shaffer [mailto:[EMAIL PROTECTED]
Sent: Tuesday, February 28, 2006 12:20 PM
To: beginners@perl.org
Subject: regular expression in a variable
I need to set a variable to a filename where only 1 section of the file
is
static.
For example:
$filename =~ /test/;
Where the following:
Print
by filename alone, you must add the path as a prefix)
-Original Message-
From: Curt Shaffer [mailto:[EMAIL PROTECTED]
Sent: Tuesday, February 28, 2006 12:42 PM
To: Timothy Johnson; beginners@perl.org
Subject: RE: regular expression in a variable
Thanks for the response. Let me try to clear
your help!
-Original Message-
From: Timothy Johnson [mailto:[EMAIL PROTECTED]
Sent: Tuesday, February 28, 2006 3:47 PM
To: Curt Shaffer; beginners@perl.org
Subject: RE: regular expression in a variable
So what part are you stuck on then? It looks like the first suggestion
gets you
28, 2006 3:47 PM
To: Curt Shaffer; beginners@perl.org
Subject: RE: regular expression in a variable
So what part are you stuck on then? It looks like the first
suggestion gets you the $filename you want. All you have to do after
that is move
it.
(you can change it so it isn't
PM
To: Curt Shaffer; Timothy Johnson; beginners@perl.org
Subject: RE: regular expression in a variable
Curt Shaffer wrote:
That appears to work! The part I am stuck on is how to I take that
value (which would now be $file in your example) and put it into a
variable that I can use through
;
}
##
-Original Message-
From: Curt Shaffer [mailto:[EMAIL PROTECTED]
Sent: Tuesday, February 28, 2006 1:11 PM
To: Timothy Johnson; beginners@perl.org
Subject: RE: regular expression in a variable
That appears to work! The part I am stuck on is how to I take that value
(which would now
The $file is only valid inside the foreach scope. To get a global value
use:
use strict;
use warnings;
my $file # the variable is now global
opendir(DIR,.) or die(Couldn't open the current directory!\n);
my @files = readdir(DIR);
foreach $file(sort @files){ # The my has been removed in
, February 28, 2006 5:50 PM
To: Curt Shaffer; beginners@perl.org
Subject: RE: regular expression in a variable
If you declare a variable with my(), it only exists within the current
scope (NOTE: always add 'use strict' and 'use warnings' whenever you
can at the top of your scripts).
What you'll have
Thank you for pointing me in the right direction. That did work.
Ahmer
-Original Message-
From: BOLCATO CHRIS (esm1cmb) [mailto:[EMAIL PROTECTED]
Sent: Monday, June 21, 2004 10:43 AM
To: Khan, Ahmer H; [EMAIL PROTECTED]
Subject: RE: Using regular expression as a variable.
Hello
Hi,
I'm having a problem where somehow a regexp does not work and the
program results in an error when the regexp is called using a variable.
Here is my code.
1 $filename = 'This is a directory\file\sub.txt';
2 $regex = '\\(\w+\.\w+)$';
3 if ($filename =~ /$regex/)
4 {print $1;} else
5 {print
Hi.
See inline comments..
[snip]
Here is my code.
1 $filename = 'This is a directory\file\sub.txt';
2 $regex = '\\(\w+\.\w+)$';
$regex = '\\(\w+\.\w+\)$';
Missed to escape the last ) ...
[snip]
However if I copy/paste the actual regexp from line 2 into line 3 and
not use the variable, the program
-Original Message-
From: Anders Holm [mailto:[EMAIL PROTECTED]
Sent: Monday, June 21, 2004 8:22 AM
To: Khan, Ahmer H
Cc: [EMAIL PROTECTED]
Subject: Re: Using regular expression as a variable.
Hi.
See inline comments..
[snip]
Here is my code.
1 $filename = 'This is a directory\file\sub.txt';
2
Hello,
Hi,
I'm having a problem where somehow a regexp does not work and the program
results in an error when the regexp is called using a variable.
Here is my code.
1 $filename = 'This is a directory\file\sub.txt';
2 $regex = '\\(\w+\.\w+)$';
3 if ($filename =~ /$regex/)
4 {print $1;} else
5
14 matches
Mail list logo