Using Substr and Regular expressions.

K

kent.westmoreland

Hello,

I am attempting to take a string similar to the following as input:

response_93_johndoe_1171994965031.xml

and break it up into the following output:

93johndoe.txt

The format of the input will always be of the type
"response_<somenumber>_<somename>_<longstringofnumbers>.xml"

I would like this to be dynamic, such that I can run a perl script
from the command line, and it will take the above file name as input,
and give me the second name as output.

Any Ideas?

My Ultimate goal is to be able to create a log file based on the name
of the input file and store it as a variable to be used later.

Thanks,
Kent
 
M

Manish

Hello,

I am attempting to take a string similar to the following as input:

response_93_johndoe_1171994965031.xml

and break it up into the following output:

93johndoe.txt

The format of the input will always be of the type
"response_<somenumber>_<somename>_<longstringofnumbers>.xml"

I would like this to be dynamic, such that I can run a perl script
from the command line, and it will take the above file name as input,
and give me the second name as output.

Any Ideas?

My Ultimate goal is to be able to create a log file based on the name
of the input file and store it as a variable to be used later.

Thanks,
Kent

#!/usr/local/bin/perl -w

use strict;
my $pat = qr/response_(\d+)_(\w+)_.*.xml/;
my $d = "response_93_johndoe_1171994965031.xml";
my ($num, $name) = ($d =~ m"$pat");
print "$num$name.txt\n";

Should do the trick.
 
D

Dan Mercer

: Hello,
:
: I am attempting to take a string similar to the following as input:
:
: response_93_johndoe_1171994965031.xml
:
: and break it up into the following output:
:
: 93johndoe.txt
:
: The format of the input will always be of the type
: "response_<somenumber>_<somename>_<longstringofnumbers>.xml"
:
: I would like this to be dynamic, such that I can run a perl script
: from the command line, and it will take the above file name as input,
: and give me the second name as output.
:
: Any Ideas?
:
: My Ultimate goal is to be able to create a log file based on the name
: of the input file and store it as a variable to be used later.

My guess is that if you have to ask this question, you're not ready
to program in perl yet. A number of solutions are available:

use strict;
use warnings;
my $num;
my $name;
($num,$name) = (split /_/,$ARGV[0])[1,2];

is just one of many possibilities.

Dan Mercer

:
: Thanks,
: Kent
:
 
X

Xicheng Jia

Hello,

I am attempting to take a string similar to the following as input:

response_93_johndoe_1171994965031.xml

and break it up into the following output:

93johndoe.txt

The format of the input will always be of the type
"response_<somenumber>_<somename>_<longstringofnumbers>.xml"

I would like this to be dynamic, such that I can run a perl script
from the command line, and it will take the above file name as input,
and give me the second name as output.

Any Ideas?

My Ultimate goal is to be able to create a log file based on the name
of the input file and store it as a variable to be used later.

Thanks,
Kent

For your input, you can do it on the command line.

echo response_93_johndoe_1171994965031.xml |
perl -F_ -alne 'print "$F[1]$F[2].txt"'

Regards,
Xicheng
 
D

Dr.Ruud

(e-mail address removed) schreef:
I am attempting to take a string similar to the following as input:
response_93_johndoe_1171994965031.xml
and break it up into the following output:
93johndoe.txt

perl -wle'
my $input = q{response_93_johndoe_1171994965031.xml};
my $output = join(q{}, (split '_', $input)[1..2]) . q{.txt};
print $output;
'

perl -wle'
my $input = q{response_93_johndoe_1171994965031.xml};
print qq{$1$2.txt} if $input =~ /_([0-9]+)_([a-z0-9]+)/;
'

(know that [a-z] can match more than 26 different characters)
 
B

Brian McCauley

#!/usr/local/bin/perl -w

use strict;

Unless compatability with old Perl is an issue "use warnings" is
preferable to the -w switch
my $pat = qr/response_(\d+)_(\w+)_.*.xml/;

I think it would be better anchored.

my $pat = qr/^response_(\d+)_(\w+)_.*\.xml$/;
my ($num, $name) = ($d =~ m"$pat");

Although the m operator _allows_ you to use non-standard regex
delimiters, IMHO, it aids clairy not to do so for no reason:

my ($num, $name) = $d =~ /$pat/;

Actually you can say...

my ($num, $name) = $d =~ $pat;

....but IMHO that's less clear.
 
M

Manish

Unless compatability with old Perl is an issue "use warnings" is
preferable to the -w switch

sure


I think it would be better anchored.

my $pat = qr/^response_(\d+)_(\w+)_.*\.xml$/;

sure


Although the m operator _allows_ you to use non-standard regex
delimiters, IMHO, it aids clairy not to do so for no reason:

sorry, couldn't follow *use non-standard regex ...*, can you explain a
little more?
 
A

A. Sinan Unur

....


sorry, couldn't follow *use non-standard regex ...*, can you explain a
little more?

Using the " characters as a delimeter in the match operator above is
non-standard. While there is nothing preventing you from using that
character, it is better not get too carried away and stick with the most
common case unless there is a very good reason to do so. Hence, things
are clearer if you use /$pat/ rather than m"$pat" which looks to me like
"spat" and makes it harder to read your code.

If what is between the delimeters contains a bunch of / characters, it
is common to use braces of exclamation marks as delimiters.

If you do not want variable interpolation, you should use single quotes
as delimiters.

Sinan
 
K

kent.westmoreland

My guess is that if you have to ask this question, you're not ready
to program in perl yet. A number of solutions are available:

use strict;
use warnings;
my $num;
my $name;
($num,$name) = (split /_/,$ARGV[0])[1,2];

is just one of many possibilities.

Dan Mercer

Dan,

Your blunt response "My guess is that if you have to ask this
question, you're not ready to program in perl yet." is not far from
the truth.

I only recently started to "learn" perl ( 2 days ago). I am modifying
a script that was given to me to preform a specific task. I wanted to
make the script easier to use but I was not having much luck with the
tutorials I found on the web. I figured posting here was a great
place to find information that I required.

I have tried both Manish and your suggestions ( both of which work
fine) with a few modifications.

Some of my main holdbacks on perl where syntactical ( I actually had
something similar to your version using the split function but only
one variable).

Anyway, thanks to everyone for all of your examples and help! Now I
have a lot of things to research.
Kent
 
D

Dan Mercer

:
: > My guess is that if you have to ask this question, you're not ready
: > to program in perl yet. A number of solutions are available:
: >
: > use strict;
: > use warnings;
: > my $num;
: > my $name;
: > ($num,$name) = (split /_/,$ARGV[0])[1,2];
: >
: > is just one of many possibilities.
: >
: > Dan Mercer
: >
:
: Dan,
:
: Your blunt response "My guess is that if you have to ask this
: question, you're not ready to program in perl yet." is not far from
: the truth.
:
: I only recently started to "learn" perl ( 2 days ago). I am modifying
: a script that was given to me to preform a specific task. I wanted to
: make the script easier to use but I was not having much luck with the
: tutorials I found on the web. I figured posting here was a great
: place to find information that I required.
:
: I have tried both Manish and your suggestions ( both of which work
: fine) with a few modifications.
:
: Some of my main holdbacks on perl where syntactical ( I actually had
: something similar to your version using the split function but only
: one variable).
:
: Anyway, thanks to everyone for all of your examples and help! Now I
: have a lot of things to research.
: Kent

Try SAMS "Teach Yourself Perl in 24 hours" (Clinton Pierce). Worked
for me.

Dan Mercer

:
:
:
:
 
K

kent.westmoreland

:
: > My guess is that if you have to ask this question, you're not ready
: > to program in perl yet. A number of solutions are available:
: >
: > use strict;
: > use warnings;
: > my $num;
: > my $name;
: > ($num,$name) = (split /_/,$ARGV[0])[1,2];
: >
: > is just one of many possibilities.
: >
: > Dan Mercer
: >
:
: Dan,
:
: Your blunt response "My guess is that if you have to ask this
: question, you're not ready to program in perl yet." is not far from
: the truth.
:
: I only recently started to "learn" perl ( 2 days ago). I am modifying
: a script that was given to me to preform a specific task. I wanted to
: make the script easier to use but I was not having much luck with the
: tutorials I found on the web. I figured posting here was a great
: place to find information that I required.
:
: I have tried both Manish and your suggestions ( both of which work
: fine) with a few modifications.
:
: Some of my main holdbacks on perl where syntactical ( I actually had
: something similar to your version using the split function but only
: one variable).
:
: Anyway, thanks to everyone for all of your examples and help! Now I
: have a lot of things to research.
: Kent

Try SAMS "Teach Yourself Perl in 24 hours" (Clinton Pierce). Worked
for me.

Dan Mercer

:
:
:
:


Hi Dan,

Thanks for the recommendation! I will check that book out!

Thanks for everyones help!
Kent
 
B

Broke

For myself I would like to recommend the simply excellent
book or Randal L. Schwartz, & Tom Christiansen
Published by O'REILLY.

The book is carefully paced and you have corrected exercises
at the end of each chapter.
This was tremendously useful for me because I am the learn
by doing type of person.
In a few minutes you will be able to write useful scripts !

Many thanks to you Randal L. Swartz and the others I already
mentioned: without you I would never had learned Perl. The
book from Larry Wall was rather difficult for to digest because
I am also a beginner. This is in fact this book of Larry Wall:
programming Perl that you will need after reading the excellent
book of Mr. Swartz and the others I already mentioned.

I simply love Perl it became my obsession !
 
T

Tad McClellan

Broke said:
For myself I would like to recommend the simply excellent
book or Randal L. Schwartz, & Tom Christiansen
Published by O'REILLY.


That is the 2nd edition of the book, and kind of old.

"Learning Perl" is currently in its 4th edition.

I simply love Perl it became my obsession !


You are not alone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top