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

Array whitespace

by jgiller (Novice)
on May 20, 2004 at 21:23 UTC ( [id://355086]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
open PASSWD, "ypcat passwd|"; my @nis_passwd=<PASSWD>; close PASSWD; print "@nis_passwd";
gets...
entry1:jfduwrn##:etc etc
problem is each subsequent line has one whitespace character. entry2:sdvuur294:etc etc
and so on. HATE THE WHITESPACE!
I am doing something wrong? Can I remove this whitespace cleanly? I need to write the data back to the passwd file so I need a clean file. Thanks! Joe

Replies are listed 'Best First'.
Re: Array whitespace
by hardburn (Abbot) on May 20, 2004 at 21:28 UTC

    When you put a list into a string, it's concatnated with a space inbetween each list entry. Remove the quotes around the array, and then set $, (as documented in [perldoc://perlvar]).

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re: Array whitespace
by Belgarion (Chaplain) on May 20, 2004 at 21:29 UTC

    Are you simply trying to print the output of the ypcat command? If so, try the following code:

    open PASSWD, "ypcat passwd|"; while (<PASSWD>) { print; } close PASSWD;

    Your current code is stringifying a list which does something like:

    join(' ', @list);

    One final answer, if you change your last line to:

    print @nis_passwd;

    (notice without the double quotes), the extra space will be removed.

Re: Array whitespace
by davido (Cardinal) on May 21, 2004 at 04:06 UTC
    The special variable $" determines what character(s) will be used to join array elements when they're interpolated into a double-quoted string.

    Consider the following example:

    my @array = qw/1 2 3 4 5/; print "@array\n"; { local $" = "\n"; print "@array\n"; } { local $" = " see perlvar "; print "@array\n"; } { local $" = ""; print "@array\n"; } __OUTPUT__ 1 2 3 4 5 1 2 3 4 5 1 see perlvar 2 see perlvar 3 see perlvar 4 see perlvar 5 12345

    Hope this helps!


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://355086]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2025-06-17 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.