converting the contents of a scalar

Discussion in 'Perl Misc' started by cartercc@gmail.com, May 4, 2006.

  1. Guest

    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
     
    , May 4, 2006
    #1
    1. Advertising

  2. Paul Lalli Guest

    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
     
    Paul Lalli, May 4, 2006
    #2
    1. Advertising

  3. Guest

    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
     
    , May 4, 2006
    #3
  4. Anno Siegel Guest

    <> 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.
     
    Anno Siegel, May 4, 2006
    #4
  5. Uri Guttman Guest

    >>>>> "c" == cartercc <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 4, 2006
    #5
  6. 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/;
     
    Brian McCauley, May 4, 2006
    #6
  7. Guest

    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
     
    , May 5, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don Adams
    Replies:
    1
    Views:
    604
    Martin Honnen
    Mar 5, 2004
  2. Kamarulnizam Rahim
    Replies:
    4
    Views:
    226
    Robert Klemme
    Jan 28, 2011
  3. Clint Olsen
    Replies:
    6
    Views:
    366
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  4. Chuckb
    Replies:
    6
    Views:
    126
    David H. Adler
    Feb 10, 2004
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    168
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page