Professional Web Applications Themes

Perl variable "leading white spaces" Please help - PERL Miscellaneous

Hello everyone. I have been following misc posts, as well as reading several FAQ's on this issue, unfortunatley I cannot locate a solution. I am hoping that someone will be able to provide me with the simple answer. My problem has to do with the leading white spaces after the first line when calling data using the variable. Here is my code: open (PREVIEW, "<preview.txt") or &error("Unable to open the data file for reading"); flock PREVIEW, 2; previewcontent=<PREVIEW>; close(PREVIEW); Later down the script there is this code: print "Content-type: text/html \n\n"; print <<PRINTHTML; <html><head><title>My Script></title></head> <body> <textarea rows="5" name="headerdata" cols="50">previewcontent</textarea> ...

  1. #1

    Default Perl variable "leading white spaces" Please help

    Hello everyone. I have been following misc posts, as well as reading several
    FAQ's on this issue, unfortunatley I cannot locate a solution. I am hoping
    that someone will be able to provide me with the simple answer. My problem
    has to do with the leading white spaces after the first line when calling
    data using the variable. Here is my code:

    open (PREVIEW, "<preview.txt") or &error("Unable to open the data file for
    reading");
    flock PREVIEW, 2;
    previewcontent=<PREVIEW>;
    close(PREVIEW);

    Later down the script there is this code:

    print "Content-type: text/html \n\n";
    print <<PRINTHTML;
    <html><head><title>My Script></title></head>
    <body>
    <textarea rows="5" name="headerdata" cols="50">previewcontent</textarea>
    </body>
    </html>
    PRINTHTML
    exit;

    On most FAQ's and other support pages, this issue is usually caused by
    adding "" 's to the variable when assigning it's value ... in my case, i'm
    just opening a text file, and assigning all data to the previewcontent
    variable ... no "" 's present. When I run the script, the open file command
    works correctly and shows the data in the textarea box, but on every line
    except the first, all lines lead off with a white space like below:

    This is the first line data
    This is the second line data
    This is the third line data
    This is the forth line data ... and so on.

    This is a big problem because below the textarea box there is a button to
    save the data back to the text file ... and it saves these whitespaces with
    it. Everytime the script loads, an additional white space is added without
    end. Please help me! I wouldnt have posted this topic unless I has searched
    everywhere I could first. Thank you to all who read and/or respond with a
    solution! Hoping to deliver a solution to my boss soon.

    TIA Randy


    \Dandy\ Randy Guest

  2. #2

    Default Re: Perl variable "leading white spaces" Please help

    [posted & mailed]

    On Sun, 20 Jul 2003, "Dandy" Randy wrote:
    >Hello everyone. I have been following misc posts, as well as reading several
    >FAQ's on this issue, unfortunatley I cannot locate a solution. I am hoping
    >that someone will be able to provide me with the simple answer. My problem
    >has to do with the leading white spaces after the first line when calling
    >data using the variable. Here is my code:
    I suggest you look in the FAQ:

    perldoc -q spaces

    yields the following:

    ================================================== =====================
    =head1 Found in /usr/lib/perl5/5.00503/pod/perlfaq5.pod

    =head2 Why do I get weird spaces when I print an array of lines?

    Saying

    print "lines\n";

    joins together the elements of C<lines> with a space between them.
    If C<lines> were C<("little", "fluffy", "clouds")> then the above
    statement would print:

    little fluffy clouds

    but if each element of C<lines> was a line of text, ending a newline
    character C<("little\n", "fluffy\n", "clouds\n")> then it would print:

    little
    fluffy
    clouds

    If your array contains lines, just print them:

    print lines;
    ================================================== =====================

    There are a couple ways you can fix this problem:

    1. join the elements of the array together into a scalar beforehand, and
    print the scalar

    $text = join "", array;

    2. set the $" variable to "" (locally); this means that when you print an
    array in double quotes, it will insert "" (nothing) in between the
    elements, instead of the default which is " " (a space)

    {
    local $" = "";
    print "... lines ...";
    }

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

    Jeff 'japhy' Pinyan Guest

  3. #3

    Default Re: Perl variable "leading white spaces" Please help

    Jeff 'japhy' Pinyan <pinyajrpi.edu> wrote in message news:<Pine.SGI.3.96.1030720131001.5139A-100000vcmr-64.server.rpi.edu>...
    > [posted & mailed]
    >
    > On Sun, 20 Jul 2003, "Dandy" Randy wrote:
    >
    > >Hello everyone. I have been following misc posts, as well as reading several
    > >FAQ's on this issue, unfortunatley I cannot locate a solution. I am hoping
    > >that someone will be able to provide me with the simple answer. My problem
    > >has to do with the leading white spaces after the first line when calling
    > >data using the variable. Here is my code:
    >
    > I suggest you look in the FAQ:
    >
    > perldoc -q spaces
    >
    > yields the following:
    >
    > ================================================== =====================
    > =head1 Found in /usr/lib/perl5/5.00503/pod/perlfaq5.pod
    >
    > =head2 Why do I get weird spaces when I print an array of lines?
    >
    > Saying
    >
    > print "lines\n";
    >
    > joins together the elements of C<lines> with a space between them.
    > If C<lines> were C<("little", "fluffy", "clouds")> then the above
    > statement would print:
    >
    > little fluffy clouds
    >
    > but if each element of C<lines> was a line of text, ending a newline
    > character C<("little\n", "fluffy\n", "clouds\n")> then it would print:
    >
    > little
    > fluffy
    > clouds
    >
    > If your array contains lines, just print them:
    >
    > print lines;
    > ================================================== =====================
    >
    > There are a couple ways you can fix this problem:
    >
    > 1. join the elements of the array together into a scalar beforehand, and
    > print the scalar
    >
    > $text = join "", array;
    >
    > 2. set the $" variable to "" (locally); this means that when you print an
    > array in double quotes, it will insert "" (nothing) in between the
    > elements, instead of the default which is " " (a space)
    >
    > {
    > local $" = "";
    > print "... lines ...";
    > }
    If the idea is to put the content of the file in the textarea, an
    alternative to fixing the interpolation of the array might be to slurp
    the file contents into a scalar and interpolate that, maybe localizing
    the file handle as well:

    my $text = '';
    {
    local $/;
    local *PREVIEW;
    open (PREVIEW, "<preview.txt") or die "Unable to open file: $!";
    flock PREVIEW, 2;
    $text = <PREVIEW>;
    }

    Not an expert. So, FWIW.
    Greg Guest

Similar Threads

  1. Replies: 2
    Last Post: December 8th, 02:15 AM
  2. #25366 [NEW]: form buttons of type "image" dont send "submit" $_POST variable in IE
    By jordanolsommer at imap dot cc in forum PHP Development
    Replies: 0
    Last Post: September 2nd, 07:54 PM
  3. How do you simulate "." or "source" in a perl script ?
    By John Strauss in forum Perl / CGI
    Replies: 4
    Last Post: August 11th, 10:46 AM
  4. Perl variable data "replacement"
    By Tina Mueller in forum PERL Miscellaneous
    Replies: 1
    Last Post: July 29th, 11:51 AM
  5. Perl FAQ error in "variable as a variable name"
    By Eric Pement in forum PERL Miscellaneous
    Replies: 4
    Last Post: July 11th, 02:32 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139