Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Getting rid of first space?

by jimmybeaches (Initiate)
on Mar 16, 2003 at 03:29 UTC ( #243410=perlquestion: print w/replies, xml ) Need Help??

jimmybeaches has asked for the wisdom of the Perl Monks concerning the following question:

Hola!!! Just back from Mexico and I like saying Hola instead of hello... it's quicker :)

So, I am new to Perl... bought a book and I think I am off the right start with chomp or chop, except I am trying to remove a blank space from the front of a line.

I am simply trying to open the contents of a text file into an array and print them, except every line AFTER the first line prints with a blank space in front of it?! Is there some way of stripping the blank space away from every line in the array?

Contents of the text file:

one. two. three. four.

the code in my script:

my $messagebody = "message.txt"; open(messagebody, $messagebody) || Error ('open', 'file'); flock (messagebody, 2) || Error ('lock', 'file'); my @messagebody = <messagebody>; print @messagebody; sub Error { print "The server can't $_[0] the $_[1]: $! \n"; exit; }

The file prints like this:

one. two. three. four.

Is there a way to reverse what chop? (if that makes any sense...)

Replies are listed 'Best First'.
Re: Getting rid of first space?
by tall_man (Parson) on Mar 16, 2003 at 04:13 UTC
    Looks to me like the print @messagebody is the culprit. It's supplying an extra space between each element of your list. You don't need to chop, just print them differently. For example:
    foreach (@messagebody) { print; }
    Update: It's somewhat odd for print to be doing that unless your $, variable is set to a space. (See perlvar for details).
Re: Getting rid of first space?
by pfaut (Priest) on Mar 16, 2003 at 04:15 UTC

    Is that your exact program? The only way I can see you getting those results would be if you interpolated the array into a string as in print "@messagebody".

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Getting rid of first space?
by thpfft (Chaplain) on Mar 16, 2003 at 04:21 UTC

    It sounds like something in your script is setting $, to a space, for some reason. Perhaps you're trying to print something out nicely somewhere else? Well, don't :)

    Anyway, it's the print statement that's inserting the spaces, i think. The reason they show up the way they do is because each item in your array still has a newline on the end, and the space is added after that.

    you have two immediate options: you can take more control over the formatting of the list with something like this:

    print join('', @messagebody);

    or you could save yourself lots of trouble, sort out the thing with $, and get rid of the newline character at the first opportunity:

    chomp( my @messagebody = <DATA> ); print join("\n", @messagebody);

    The explanation of why chomping that whole statement happens to chomp the items in the array is worth coming back to later. Meanwhile, to answer the question you actually asked, there are (at least) dozens of ways of removing the first character of each item in a list. This is the first that comes to mind:

    s/^\s+// for @messagebody;

    which will remove all leading spaces and tabs, and might do unkind things to, for example, messages containing python code...

      This line took the empty spaces out:

      print MAIL join('', @messagebody);

      but if the text file looks like this:

      one. two. three, four.
      It comes out looking like this:
      one. two. three, four.
Re: Getting rid of first space?
by jasonk (Parson) on Mar 16, 2003 at 04:15 UTC

    Are you sure those spaces aren't being added somewhere else? The code you've pasted here doesn't add them (on linux anyway).

    We're not surrounded, we're in a target-rich environment!
Re: Getting rid of first space?
by Anonymous Monk on Mar 16, 2003 at 04:18 UTC
    Actually, I forgot to mention that it is being printed in the body of an email:

    print MAIL @messagebody;

      That doesn't change the fact that it works as you have it here. If your real program has quotes around @messagebody, remove them and it will work as you expect.

      We're not surrounded, we're in a target-rich environment!
        You were right! I had print MAIL "@messagebody\n";

        print MAIL @messagebody; worked great!!!!

Re: Getting rid of first space?
by Anonymous Monk on Mar 16, 2003 at 04:45 UTC
    I had no idea that having quotes around @messagebody in the print MAIL "@messagebody\n"; line would put those spaces in... thanks to EVERYONE, especially jasonk!!!
      The reason for that is another perlvar, $". Here's a skipppet of documentation about it:
      $LIST_SEPARATOR $" This is like ``$,'' except that it applies to array values interpolated into a double-quoted string (or similar interpreted string). Default is a space. (Mnemonic: obvious, I think.)
      Which means that another way to do it is to set $" to "".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://243410]
Approved by Paladin
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2021-05-17 04:33 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (154 votes). Check out past polls.