Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

printing out tabs between each element in an array

by Win (Novice)
on Feb 18, 2008 at 13:07 UTC ( #668523=perlquestion: print w/ replies, xml ) Need Help??
Win has asked for the wisdom of the Perl Monks concerning the following question:

Good Monks,

I am printing a line out as follows:

print ARRAY_CONTENTS "@cols_for_row\n";
Instead of printing just a space between each element in the array, I would like to print out a tab character between each element in the array. Is there a simple way of doing this?

Comment on printing out tabs between each element in an array
Download Code
Re: printing out tabs between each element in an array
by Skeeve (Vicar) on Feb 18, 2008 at 13:09 UTC
    You might want to look at the join command join "\t",@cols_for_row;

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: printing out tabs between each element in an array
by McDarren (Abbot) on Feb 18, 2008 at 13:11 UTC
    Indeed there is. You can use \t to represent a tab. So probably what you want is something like (untested):
    print ARRAY_CONTENTS join("\t", @cols_for_row), "\n";
    Update: added newline

    Cheers,
    Darren

Re: printing out tabs between each element in an array
by haoess (Curate) on Feb 18, 2008 at 13:29 UTC
    I would like to print out a tab character between each element in the array

    There is a builtin special variable $":

    local $" = "\t"; my @a = qw(1 2 3); print "@a"; __END__ 1 2 3

    -- Frank

Re: printing out tabs between each element in an array
by olus (Curate) on Feb 18, 2008 at 13:33 UTC

    You can set the output field separator to the tab character.

    $" = "\t"; print ARRAY_CONTENTS "@cols_for_row\n";

    Remember to restore the output field separator to its default value after you're done with the tab formating

      That is really very useful. Thanks. I'll try to remember that for next time.

        Hey, you know what else is really useful? The documentation!

        After FIVE YEARS on this site and presumably using Perl the entire time, why do you still have trouble with the simplest beginner problems?

        Are you:

        1. An extremely persistent and elaborate troll?
        2. Some type of very convincing artificial intelligence meant to provoke and measure frustration?
        3. Stuck in what must be the most obviously incorrect career you could have chosen?
          1. I'm literally dumbfounded. Please enlighten me.

      Remember to restore the output field separator to its default value after you're done with the tab formating

      One way to make it difficult to forget is to keep the localisation within the print statement by using a do block.

      print ARRAY_CONTENTS do { local $" = "\t"; "@cols_for_row\n"; };

      I hope this is of interest.

      Cheers,

      JohnGG

Re: printing out tabs between each element in an array
by marto (Chancellor) on Feb 18, 2008 at 13:33 UTC
Re: printing out tabs between each element in an array
by Anonymous Monk on Feb 18, 2008 at 15:48 UTC
    Man you're stupid
Re: printing out tabs between each element in an array
by Anonymous Monk on Feb 19, 2008 at 00:57 UTC

    Win,

    I'd just like to offer my sincere congratulations on achieving 9 xp. That's almost 0.02 xp for every one of your 489 posts. We're all crossing our fingers on you making the 10 xp milestone.

    Best of luck!

    - A gutless coward

      Wow.... I was going to create a user profile; I've found help here a few times. You've scared that right out of me..... umm.... thanks?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2014-08-30 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls