Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > converting the contents of a scalar

Reply
Thread Tools

converting the contents of a scalar

 
 
cartercc@gmail.com
Guest
Posts: n/a
 
      05-04-2006
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
 
 
 
cartercc@gmail.com
Guest
Posts: n/a
 
      05-04-2006
Paul Lalli wrote:
> 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

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-04-2006
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-04-2006
>>>>> "c" == cartercc <(E-Mail Removed)> writes:

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


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

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      05-04-2006

Jim Gibson wrote:
>
> ($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/;

 
Reply With Quote
 
cartercc@gmail.com
Guest
Posts: n/a
 
      05-05-2006
Uri Guttman wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding contents on yaml file without overwriting actual contents Kamarulnizam Rahim Ruby 4 01-28-2011 09:10 AM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
Can I restrict both attribute contents and element contents in schema Don Adams XML 1 03-05-2004 12:48 PM
converting scalar to an array of elements Chuckb Perl Misc 6 02-10-2004 03:14 AM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments