For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2006 > regular expression in a variable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author regular expression in a variable
Curt Shaffer

2006-02-28, 6:57 pm

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 the life of me figure out the syntax I need.



Any help appreciated!



Curt








Paul Lalli

2006-02-28, 6:57 pm

Curt Shaffer wrote:
> I need to set a variable to a filename where only 1 section of the file is
> static.


I have no idea what this means.

(Yes, I know what each of the words set, variable, filename, section,
and static all mean. I just don't understand how they relate to one
another in the above sentence.)

> For example:
>
> $filename =~ /test/;
>
> Where the following:
>
> Print "$filename\n";
>
> Would produce:
>
> 123test456.txt


And I have no idea what this is trying to convey.

A pattern match in void context does nothing. Printing the variable
$filename simply shows us that $filename was at some point set equal to
the string '123test456.txt'.

I have no idea what it is you're trying to accomplish.

> The only way I see this being possible is with regular expressions but I
> can't for the life of me figure out the syntax I need.


Nor can I, because you haven't come close to defining your problem
statement.

At the absolute *least*, show us some sample input and desired output.
Show the setup for your problem - in actual runnable code - and show us
what you want to happen once the part that you are having problems with
has been completed.

Paul Lalli

Timothy Johnson

2006-02-28, 6:57 pm


You're not being very clear what it is you're trying to do. I can see
two ways of interpreting this.

Regular expressions are mostly for checking the format of text to see if
certain conditions "match". You might be asking how to do this:

########################

use strict;
use warnings;
opendir(DIR,".") or die("Couldn't open the current directory!\n");
my @files =3D readdir(DIR);
foreach my $file(sort @files){
if($file =3D~ /(.*test.*)/i){
print "MATCH: $file\n";
}
}

#########################

You can also use the $1 variable to capture the last text string that
matched the part of the regular expression between the parentheses.


If, on the other hand, you're trying to generate file names, then
regular expressions aren't what you're looking for.

my $prefix =3D "123";
my $postfix =3D "456";
my $filename =3D $prefix."test".$postfix;
print $filename."\n";




-----Original Message-----
From: Curt Shaffer [mailto:cshaffer@gmail.com]=20
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.=20

For example:

$filename =3D~ /test/;

Where the following:

Print "$filename\n";

Would produce:

123test456.txt


DJ Stunks

2006-02-28, 6:57 pm


Paul Lalli wrote:
> Curt Shaffer wrote:
>
> I have no idea what this means.
>
> (Yes, I know what each of the words set, variable, filename, section,
> and static all mean. I just don't understand how they relate to one
> another in the above sentence.)


_Bart vs. Australia_

[slide shows theater marquee reading "Yahoo Serious Festival"]
Lisa: "I know those words, but that sign makes no sense."

:))

-jp

Timothy Johnson

2006-02-28, 6:57 pm


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 looking in the current directory with the
opendir line, but if you do, don't forget that you can't move the files
by filename alone, you must add the path as a prefix)

-----Original Message-----
From: Curt Shaffer [mailto:cshaffer@gmail.com]=20
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 things up. The second
solution
will not work for me because the other parts of the filename will be
unknown. The only part I know will always be in the filename is "test"
(only
an example).=20

So the full story is this:

I need to look in a directory to see if a file with "test" in the name
exists. If it does I need to move that file to a staging directory to be
sftp'd out to a vendor. The manner in which I am doing that is to move
the
file named $filename (whose value is the result of the regex match) to
the
staging directory. Then sftp the $filename to the appropriate place.

Does that help a bit? If not I apologize.

Curt

-----Original Message-----
From: Timothy Johnson [mailto:tjohnson@zonelabs.com]=20
Sent: Tuesday, February 28, 2006 3:30 PM
To: Curt Shaffer; beginners@perl.org
Subject: RE: regular expression in a variable


You're not being very clear what it is you're trying to do. I can see
two ways of interpreting this.

Regular expressions are mostly for checking the format of text to see if
certain conditions "match". You might be asking how to do this:

########################

use strict;
use warnings;
opendir(DIR,".") or die("Couldn't open the current directory!\n");
my @files =3D readdir(DIR);
foreach my $file(sort @files){
if($file =3D~ /(.*test.*)/i){
print "MATCH: $file\n";
}
}

#########################

You can also use the $1 variable to capture the last text string that
matched the part of the regular expression between the parentheses.


If, on the other hand, you're trying to generate file names, then
regular expressions aren't what you're looking for.

my $prefix =3D "123";
my $postfix =3D "456";
my $filename =3D $prefix."test".$postfix;
print $filename."\n";




-----Original Message-----
From: Curt Shaffer [mailto:cshaffer@gmail.com]=20
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.=20

For example:

$filename =3D~ /test/;

Where the following:

Print "$filename\n";

Would produce:

123test456.txt



Curt Shaffer

2006-02-28, 6:57 pm

Thanks for the response. Let me try to clear things up. The second solution
will not work for me because the other parts of the filename will be
unknown. The only part I know will always be in the filename is "test" (only
an example).

So the full story is this:

I need to look in a directory to see if a file with "test" in the name
exists. If it does I need to move that file to a staging directory to be
sftp'd out to a vendor. The manner in which I am doing that is to move the
file named $filename (whose value is the result of the regex match) to the
staging directory. Then sftp the $filename to the appropriate place.

Does that help a bit? If not I apologize.

Curt

-----Original Message-----
From: Timothy Johnson [mailto:tjohnson@zonelabs.com]
Sent: Tuesday, February 28, 2006 3:30 PM
To: Curt Shaffer; beginners@perl.org
Subject: RE: regular expression in a variable


You're not being very clear what it is you're trying to do. I can see
two ways of interpreting this.

Regular expressions are mostly for checking the format of text to see if
certain conditions "match". You might be asking how to do this:

########################

use strict;
use warnings;
opendir(DIR,".") or die("Couldn't open the current directory!\n");
my @files = readdir(DIR);
foreach my $file(sort @files){
if($file =~ /(.*test.*)/i){
print "MATCH: $file\n";
}
}

#########################

You can also use the $1 variable to capture the last text string that
matched the part of the regular expression between the parentheses.


If, on the other hand, you're trying to generate file names, then
regular expressions aren't what you're looking for.

my $prefix = "123";
my $postfix = "456";
my $filename = $prefix."test".$postfix;
print $filename."\n";




-----Original Message-----
From: Curt Shaffer [mailto:cshaffer@gmail.com]
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 "$filename\n";

Would produce:

123test456.txt


Curt Shaffer

2006-02-28, 6:57 pm

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 the rest of the script. When I then try to use $file outside
of that routine, it is no longer the same value.

I really appreciate your help!



-----Original Message-----
From: Timothy Johnson [mailto:tjohnson@zonelabs.com]
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 the $filename you want. All you have to do after that is move
it.

(you can change it so it isn't looking in the current directory with the
opendir line, but if you do, don't forget that you can't move the files
by filename alone, you must add the path as a prefix)

-----Original Message-----
From: Curt Shaffer [mailto:cshaffer@gmail.com]
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 things up. The second
solution
will not work for me because the other parts of the filename will be
unknown. The only part I know will always be in the filename is "test"
(only
an example).

So the full story is this:

I need to look in a directory to see if a file with "test" in the name
exists. If it does I need to move that file to a staging directory to be
sftp'd out to a vendor. The manner in which I am doing that is to move
the
file named $filename (whose value is the result of the regex match) to
the
staging directory. Then sftp the $filename to the appropriate place.

Does that help a bit? If not I apologize.

Curt

-----Original Message-----
From: Timothy Johnson [mailto:tjohnson@zonelabs.com]
Sent: Tuesday, February 28, 2006 3:30 PM
To: Curt Shaffer; beginners@perl.org
Subject: RE: regular expression in a variable


You're not being very clear what it is you're trying to do. I can see
two ways of interpreting this.

Regular expressions are mostly for checking the format of text to see if
certain conditions "match". You might be asking how to do this:

########################

use strict;
use warnings;
opendir(DIR,".") or die("Couldn't open the current directory!\n");
my @files = readdir(DIR);
foreach my $file(sort @files){
if($file =~ /(.*test.*)/i){
print "MATCH: $file\n";
}
}

#########################

You can also use the $1 variable to capture the last text string that
matched the part of the regular expression between the parentheses.


If, on the other hand, you're trying to generate file names, then
regular expressions aren't what you're looking for.

my $prefix = "123";
my $postfix = "456";
my $filename = $prefix."test".$postfix;
print $filename."\n";




-----Original Message-----
From: Curt Shaffer [mailto:cshaffer@gmail.com]
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 "$filename\n";

Would produce:

123test456.txt



Wagner, David --- Senior Programmer Analyst --- WG

2006-02-28, 6:57 pm

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 the rest of the script. When I then
> try to use $file outside of that routine, it is no longer the same
> value.=20
>=20
> I really appreciate your help!
>=20
>=20

So are you trying to collect all the files up front and then go through th=
e processing? You already have the test in the loop and could just write a =
subroutine and pass it $file and do all your work there.

If you want to hold on to those fiels which match your criteria, then use =
another array to capture as in
@matchedfiles =3D grep( /*.test.*/, @files );

Either way, you have the data in place or the other.

Wags ;)=20
>=20
> -----Original Message-----
> From: Timothy Johnson [mailto:tjohnson@zonelabs.com]
> Sent: Tuesday, February 28, 2006 3:47 PM
> To: Curt Shaffer; beginners@perl.org
> Subject: RE: regular expression in a variable
>=20
>=20
> 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=20
> it.
>=20
> (you can change it so it isn't looking in the current directory with
> the opendir line, but if you do, don't forget that you can't move the
> files=20
> by filename alone, you must add the path as a prefix)
>=20
> -----Original Message-----
> From: Curt Shaffer [mailto:cshaffer@gmail.com]
> Sent: Tuesday, February 28, 2006 12:42 PM
> To: Timothy Johnson; beginners@perl.org
> Subject: RE: regular expression in a variable
>=20
> Thanks for the response. Let me try to clear things up. The second
> solution
> will not work for me because the other parts of the filename will be
> unknown. The only part I know will always be in the filename is "test"
> (only
> an example).
>=20
> So the full story is this:
>=20
> I need to look in a directory to see if a file with "test" in the name
> exists. If it does I need to move that file to a staging directory to
> be sftp'd out to a vendor. The manner in which I am doing that is to
> move=20
> the
> file named $filename (whose value is the result of the regex match) to
> the
> staging directory. Then sftp the $filename to the appropriate place.
>=20
> Does that help a bit? If not I apologize.
>=20
> Curt
>=20
> -----Original Message-----
> From: Timothy Johnson [mailto:tjohnson@zonelabs.com]
> Sent: Tuesday, February 28, 2006 3:30 PM
> To: Curt Shaffer; beginners@perl.org
> Subject: RE: regular expression in a variable
>=20
>=20
> You're not being very clear what it is you're trying to do. I can see
> two ways of interpreting this.
>=20
> Regular expressions are mostly for checking the format of text to see
> if certain conditions "match". You might be asking how to do this:
>=20
> ########################
>=20
> use strict;
> use warnings;
> opendir(DIR,".") or die("Couldn't open the current directory!\n");
> my @files =3D readdir(DIR);
> foreach my $file(sort @files){
> if($file =3D~ /(.*test.*)/i){
> print "MATCH: $file\n";
> }
> }
>=20
> #########################
>=20
> You can also use the $1 variable to capture the last text string that
> matched the part of the regular expression between the parentheses.
>=20
>=20
> If, on the other hand, you're trying to generate file names, then
> regular expressions aren't what you're looking for.
>=20
> my $prefix =3D "123";
> my $postfix =3D "456";
> my $filename =3D $prefix."test".$postfix;
> print $filename."\n";
>=20
>=20
>=20
>=20
> -----Original Message-----
> From: Curt Shaffer [mailto:cshaffer@gmail.com]
> Sent: Tuesday, February 28, 2006 12:20 PM
> To: beginners@perl.org
> Subject: regular expression in a variable
>=20
> I need to set a variable to a filename where only 1 section of the
> file=20
> is
> static.
>=20
> For example:
>=20
> $filename =3D~ /test/;
>=20
> Where the following:
>=20
> Print "$filename\n";
>=20
> Would produce:
>=20
> 123test456.txt




****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************

Curt Shaffer

2006-02-28, 6:57 pm

Yes I am trying to collect the file (the result will only produce 1 match)
And I need that into a variable that can be used for the processing.



-----Original Message-----
From: Wagner, David --- Senior Programmer Analyst --- WGO
[mailto:David.Wagner@freight.fedex.com]
Sent: Tuesday, February 28, 2006 4:19 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 the rest of the script. When I then
> try to use $file outside of that routine, it is no longer the same
> value.
>
> I really appreciate your help!
>
>

So are you trying to collect all the files up front and then go
through the processing? You already have the test in the loop and could just
write a subroutine and pass it $file and do all your work there.

If you want to hold on to those fiels which match your criteria,
then use another array to capture as in
@matchedfiles = grep( /*.test.*/, @files );

Either way, you have the data in place or the other.

Wags ;)
>
> -----Original Message-----
> From: Timothy Johnson [mailto:tjohnson@zonelabs.com]
> 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 the $filename you want. All you have to do after
> that is move
> it.
>
> (you can change it so it isn't looking in the current directory with
> the opendir line, but if you do, don't forget that you can't move the
> files
> by filename alone, you must add the path as a prefix)
>
> -----Original Message-----
> From: Curt Shaffer [mailto:cshaffer@gmail.com]
> 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 things up. The second
> solution
> will not work for me because the other parts of the filename will be
> unknown. The only part I know will always be in the filename is "test"
> (only
> an example).
>
> So the full story is this:
>
> I need to look in a directory to see if a file with "test" in the name
> exists. If it does I need to move that file to a staging directory to
> be sftp'd out to a vendor. The manner in which I am doing that is to
> move
> the
> file named $filename (whose value is the result of the regex match) to
> the
> staging directory. Then sftp the $filename to the appropriate place.
>
> Does that help a bit? If not I apologize.
>
> Curt
>
> -----Original Message-----
> From: Timothy Johnson [mailto:tjohnson@zonelabs.com]
> Sent: Tuesday, February 28, 2006 3:30 PM
> To: Curt Shaffer; beginners@perl.org
> Subject: RE: regular expression in a variable
>
>
> You're not being very clear what it is you're trying to do. I can see
> two ways of interpreting this.
>
> Regular expressions are mostly for checking the format of text to see
> if certain conditions "match". You might be asking how to do this:
>
> ########################
>
> use strict;
> use warnings;
> opendir(DIR,".") or die("Couldn't open the current directory!\n");
> my @files = readdir(DIR);
> foreach my $file(sort @files){
> if($file =~ /(.*test.*)/i){
> print "MATCH: $file\n";
> }
> }
>
> #########################
>
> You can also use the $1 variable to capture the last text string that
> matched the part of the regular expression between the parentheses.
>
>
> If, on the other hand, you're trying to generate file names, then
> regular expressions aren't what you're looking for.
>
> my $prefix = "123";
> my $postfix = "456";
> my $filename = $prefix."test".$postfix;
> print $filename."\n";
>
>
>
>
> -----Original Message-----
> From: Curt Shaffer [mailto:cshaffer@gmail.com]
> 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 "$filename\n";
>
> Would produce:
>
> 123test456.txt




****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************

Timothy Johnson

2006-02-28, 6:57 pm


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 to do is declare a variable outside of the brackets.
You could even use a subroutine, like the one below. When it matches,
I'm returning the name of the file. If I get through the whole loop
without matching, I'm returning 0 to indicate that it failed.

#########################

use strict;
use warnings;

my $file;

if($file =3D GetTestFileName()){
print "I got it! $file\n";
}else{
print "Failed to locate test file!\n";
}


sub GetTestFileName{
opendir(DIR,".") or die("Couldn't open the current directory!\n");
my @files =3D readdir(DIR);
foreach my $testfile(sort @files){
if($testfile =3D~ /(.*test.*)/i){
return $testfile;
}
}
return 0;
}

##########################




-----Original Message-----
From: Curt Shaffer [mailto:cshaffer@gmail.com]=20
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 be $file in your example) and put it into a variable
that I
can use through the rest of the script. When I then try to use $file
outside
of that routine, it is no longer the same value.

I really appreciate your help!



<snip>


Tommy Grav

2006-02-28, 6:57 pm

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 this line
if($file =~ /(.*test.*)/i){
print "MATCH: $file\n";
}
}


Cheers
Tommy


tgrav@mac.com
http://homepage.mac.com/tgrav/

"Any intelligent fool can make things bigger,
more complex, and more violent. It takes a
touch of genius -- and a lot of courage --
to move in the opposite direction"
-- Albert Einstein



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com