Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Space prepended to Array Print

by JockoHelios (Scribe)
on Jun 07, 2013 at 18:48 UTC ( #1037730=perlquestion: print w/ replies, xml ) Need Help??
JockoHelios has asked for the wisdom of the Perl Monks concerning the following question:

I've run across an odd little quirk, and I'm wondering if anyone has seen this before.

I'm loading a text file into an array. The text file has data saved from a previous array, example:

20130516,530,730
20130516,731,1100
20130516,1101,1200

To confirm that the data is loaded into the array, I printed out the loaded array using this code:

print "@LoadedArray";

All the data prints out as is it is in the text file, with one exception. After the first row of the loaded array is printed, each subsequent row has one extra space in front of it. Example, using underscores to simulate the added space:

20130516,530,730
_20130516,731,1100
_20130516,1101,1200

When I print the array using the following code:

foreach $ArrayRow( @LoadedArray ) { print "$ArrayRow"; }

the added spaces don't appear - the data prints as it is in the text file.

I don't think this is really a problem, just a curiosity I found (fingers crossed).
The only "problem" is that stuff like this tends to stick in my brain until I know why it happened :)
Dyslexics Untie !!!

Comment on Space prepended to Array Print
Re: Space prepended to Array Print
by toolic (Chancellor) on Jun 07, 2013 at 18:54 UTC
    perlvar
    $LIST_SEPARATOR $" When an array or an array slice is interpolated into a double-quoted s +tring or a similar context such as /.../ , its elements are separated by this value. Default is a + space.
      Thanks. Now something productive can occupy my neural net for a while :)
      Dyslexics Untie !!!
Re: Space prepended to Array Print
by Laurent_R (Parson) on Jun 07, 2013 at 18:57 UTC

    When you print an array with this syntax:

    print "@LoadedArray";

    Perl adds a space to seperate the array elements. Obviously your elements also have a c arriage return at the end, so that you get a new line and then a space at the begin ning of the next line (the start of the next element).

    EDIT: I was too slow, Toolic's answer came before mine.

      I get beaten to the post more often than not. Mostly because I'm not anywhere near as Perl-masterful as I'd like to be. Still counts as an up, though ;)
      Dyslexics Untie !!!
Re: Space prepended to Array Print
by 2teez (Priest) on Jun 07, 2013 at 19:16 UTC

    Hi JockoHelios,
    You have been accurately answered. But if you wouldn't mind a little illustration using some code snippets:

    use warnings; use strict; my @array = map{$_}<DATA>; print @array; print $/; print join '' => @array; print $/; print "@array"; print $/; { local $"=''; print "@array"; } __DATA__ 20130516,530,730 20130516,731,1100 20130516,1101,1200
    Output:
    20130516,530,730 20130516,731,1100 20130516,1101,1200 20130516,530,730 20130516,731,1100 20130516,1101,1200 20130516,530,730 20130516,731,1100 ## note here 20130516,1101,1200 ## and here 20130516,530,730 20130516,731,1100 20130516,1101,1200
    Note:
    I don't know how the OP is getting his/her input.
    Hope this help in some way.
    ++ for toolic

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Space prepended to Array Print
by Anonymous Monk on Jun 07, 2013 at 21:45 UTC
    Usually you want to get rid of terminating newlines on your inputs and add them back in appropriately after processing. If you had "chomp"ed your input lines you'd have gotten all your data on a single line with spaces between, which is a more typical example of this feature.
Re: Space prepended to Array Print
by grondilu (Pilgrim) on Jun 08, 2013 at 07:46 UTC

    Other comments above give you the explanation on what happened.

    Me, when I want to put the lines of a text file in an array, I usually write:

    my @data = map { chomp; $_ } <>;

    And when I want to output it, I write:

    print "$_\n" for @data

    In Perl6 I'd write:

    my @data = lines; .say for @data;
      A bit faster is to use chomp for the array:
      chomp(my @data = <>);
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1037730]
Approved by toolic
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-09-22 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (182 votes), past polls