converting the contents of a scalar

C

cartercc

Thanks in advance for this stumper (at least it is for me).

I have a file name in a scaler, like this $text_file = <STDIN> where
the user would input 'name.txt'

I need to convert $text_file to $xml_file which would contain the value
'name.xml' Essentially, I want to take the .txt portion of the scalar
and convert it to .xml. Unfortunately, I never see the contents of
$text_file, and I don't know how to access the contents of this
variable.

TIA, CC
 
P

Paul Lalli

Thanks in advance for this stumper (at least it is for me).

I have a file name in a scaler, like this $text_file = <STDIN> where
the user would input 'name.txt'

First of all, you do know that unless you do
chomp($text_file);
then $text_file will contain "name.txt\n", rather than 'name.txt',
right?
I need to convert $text_file to $xml_file which would contain the value
'name.xml' Essentially, I want to take the .txt portion of the scalar
and convert it to .xml. Unfortunately, I never see the contents of
$text_file, and I don't know how to access the contents of this
variable.

This is about as far from a "stumper" as you can get. This is the
absolute most *basic* of using regular expressions. If you have not
done so yet, please go read:

perldoc perlretut
perldoc perlre
perldoc perlreref

Now, because I'm feeling generous:

my $xml_file = $text_file;
$xml_file =~ s/\.txt$/.xml/;

Or, in one line:

(my $xml_file = $text_file) =~ s/\.txt$/.xml/;

DO NOT SIMPLY copy and paste the above and consider your problem
solved. The fact that you had to ask this question shows a complete
lack of knowledge about one of the most fundamental concepts of Perl.
Go read those documents I referred you to. You will have a miserable
time as a Perl programmer if you do not.

Paul Lalli
 
C

cartercc

Paul said:
First of all, you do know that unless you do
chomp($text_file);
then $text_file will contain "name.txt\n", rather than 'name.txt',
right?

Yeah, I did chomp.
This is about as far from a "stumper" as you can get. This is the
absolute most *basic* of using regular expressions. If you have not
done so yet, please go read:

Different strokes for different folks. I've been using Perl for
database CGI scripts from 2000, and in the past couple of months, I've
written about three dozen heave regex scripts to handle a large number
of CSV files.

I guess I had a brain fart, because I'm feeling a lot red faced about
now. I've never had to think about this, and I guess I just didn't
think.
Now, because I'm feeling generous:

my $xml_file = $text_file;
$xml_file =~ s/\.txt$/.xml/;

Okay, I didn'l know that the Perl regex engine would interpolate scalar
variables. Now, I know.
The fact that you had to ask this question shows a complete
lack of knowledge about one of the most fundamental concepts of Perl.
Go read those documents I referred you to. You will have a miserable
time as a Perl programmer if you do not.

I will admint to ignorance. You learn something every day.

Thanks, Paul
 
A

Anno Siegel

Paul Lalli wrote:
[...]
Now, because I'm feeling generous:

my $xml_file = $text_file;
$xml_file =~ s/\.txt$/.xml/;

Okay, I didn'l know that the Perl regex engine would interpolate scalar
variables. Now, I know.

Perl does, not the regex engine, but not in the bit of code above. Where
do you think interpolation happens?

Anno
 
U

Uri Guttman

c> Okay, I didn'l know that the Perl regex engine would interpolate scalar
c> variables. Now, I know.

where is 'regex engine would interpolate scalar variables' happening
there? in fact where is any interpolation happening there? if you knew
about regexes but didn't know how to apply them, how have you been using
them? did you really think that they could only be applied to $_ (i am
guessing this from the disconnect). if so then how could you have been
coding perl and not ever seen =~ used to bind an expression (commonly a
scalar var) to the regex?

inquiring minds want to know so we can fix the docs and close this trap
so it won't confuse future perl newbies.

:-/

uri
 
B

Brian McCauley

Jim said:
($xml_file = $text_file) =~ s/[^.]*$/xml/;

If you are not sure the user will include a suffix with the file name,
I would do something like this:

$xml_file = $text_file;
if( $xml_file =~ m{ \A (.*) \. [^.]* \z }x ) {
$xml_file = $1;
}
$xml_file .= '.xml';

Wouldn't it be simpler just to do...

($xml_file = $text_file) =~ s/(\.[^.]*)?$/.xml/;
 
C

cartercc

Uri said:
where is 'regex engine would interpolate scalar variables' happening
there? in fact where is any interpolation happening there? if you knew
about regexes but didn't know how to apply them, how have you been using
them? did you really think that they could only be applied to $_ (i am
guessing this from the disconnect). if so then how could you have been
coding perl and not ever seen =~ used to bind an expression (commonly a
scalar var) to the regex?

inquiring minds want to know so we can fix the docs and close this trap
so it won't confuse future perl newbies.

Okay, confession time.

I went back and looked at a series of scripts I wrote during the course
of this week. Essentially, what is happening is that we are getting
large pipe delimited files that we have to edit and upload. We do this
through a series of steps (and unimaginitively I named the scripts
step1.plx, step2.plx, and so on). Step four consists of printing out a
document to physically check the values, which I do by writing an
OUTFILE using an appropriate format. The input file is a .txt file, and
I wanted to use the same name but with a .rtf or .doc extensions. The
user can them open them natively in Word and print them on her Windows
workstation.

My other scripts did exactly what I wanted, something like $line =~
s/","/"|"/g. I guess I had a mental lapse when thinking outside the
loop (and I mean this literally). I was so focused on the trees that I
forgot that they were a part of the forest.

LESSON: When faced with a question, take a deep breath, check the
documentation, and look at the problem from another angle. I should
have done this before posting to c.l.p.misc -- I didn't, which was my
bad. Maybe a contributing factor is that I use vi as my working editor,
and regexes in vi differ from regexes in perl, and I had on my vi hat
rather than my perl hat.

You might consider revising the documentation to emphasize that the
binding operator matches the CONTENT of the scalar in the lhs, as well
as a literal. The re tutor states as follows: "If you've been around
Perl a while, all this talk of escape sequences may seem familiar.
Similar escape sequences are used in double-quoted strings and in fact
the regexps in Perl are mostly treated as double-quoted strings. This
means that variables can be used in regexps as well. Just like
double-quoted strings, the values of the variables in the regexp will
be substituted in before the regexp is evaluated for matching
purposes." This is buried pretty deep, and I didn't find it until I
went looking for it. I don't know if it would have helped me to have
emphasized it, but I will admit that it was one of those points that
escaped me until it bit me.

Thanks much for your help. Perl has turned into a real plus for my
organization, and I have found c.l.p.misc a really good resource, and I
appreciate the participation of those willing to extend a helping hand
to those of us who happen to be on the lower end of the learning curve.

CC
 

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,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top