Find the answer to your Linux question:
Results 1 to 5 of 5
I'm having trouble with a variable in a perl script. This is my first perl script (probably obvious). Code: #!/usr/bin/perl $TODAY=`date "+%A"`; $NAME=mike; $xml_file="/opt/$NAME.$TODAY.xml"; print $TODAY; print $xml_file; output Code: ...
  1. #1
    Linux Newbie
    Join Date
    May 2007
    Posts
    106

    [SOLVED] set varialbe in perl

    I'm having trouble with a variable in a perl script. This is my first perl script (probably obvious).

    Code:
    #!/usr/bin/perl
    
    $TODAY=`date "+%A"`;
    $NAME=mike;
    $xml_file="/opt/$NAME.$TODAY.xml";
    
    print $TODAY;
    print $xml_file;
    output

    Code:
    Friday
    /opt/mike.Friday
    I expected the second line of output to be:
    /opt/mike.Friday.xml

    what gives?

  2. #2
    Linux Newbie
    Join Date
    May 2007
    Posts
    106
    to summarize: any time the variable $TODAY is called the rest of the filename is dropped.

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    You're having trouble with newlines -- too many in some places, not enough in others.

    If you look carefully at your output, you'll almost certainly see that ".xmp" was printed, but on a separate line. That's because when you got the string from date it included a newline. You can use the chomp function to remove it.

    When you print, include "\n" to add a newline, typically at the end of whatever it is you wish to print.

    See perldoc -f chomp, etc., for details.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Linux Newbie
    Join Date
    May 2007
    Posts
    106
    thanks for the suggestion, I've been slammed this week so I haven't had a chance to test, but I will asap

    thanks again, I'll let you know what I find

  5. #5
    Linux Newbie
    Join Date
    May 2007
    Posts
    106
    Quote Originally Posted by drl View Post
    Hi.

    You're having trouble with newlines -- too many in some places, not enough in others.

    If you look carefully at your output, you'll almost certainly see that ".xmp" was printed, but on a separate line. That's because when you got the string from date it included a newline.
    You're right!! The .xml is printed on the next line. I didn't notice it at first because it is printed next to the command prompt. Now I can set about fixing it.

    Thanks so much!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...