Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Pushing a delimiter into an array?

by rnaeye (Pilgrim)
on Mar 31, 2014 at 01:11 UTC ( #1080343=perlquestion: print w/replies, xml ) Need Help??
rnaeye has asked for the wisdom of the Perl Monks concerning the following question:

I want to push scalar values into an array and put a 'tab' as delimiter between them. Line is long and I have to put many 'tab'. Can I also push "\t" between values without typing between each value? Is there another way that Monks would suggest? Thank you.

 push (@array, $value1, "\t", $value2, "\t", $value3,...)

or something similar to the following awk OFS command

 awk '{OFS="\t"; print $1,$2,$3,$5,$4}'

Replies are listed 'Best First'.
Re: Pushing a delimiter into an array?
by iguanodon (Priest) on Mar 31, 2014 at 01:56 UTC
    Do you really need the tabs in the array? If you just want to print the values separated by tabs do
    print join("\t", @array);
      Great advice thanks. I just needed to print them!
Re: Pushing a delimiter into an array?
by NetWallah (Canon) on Mar 31, 2014 at 02:56 UTC
    You could also use perl's $LIST_SEPARATOR $" (see perldoc perlvar).
    perl -E 'my @x=0..5; local $"=qq|\t|; say "@x"' 0 1 2 3 4 5
    Slightly more dramatic:
    perl -E 'my @x=0..5; local $"=qq|= \t =|; say "=@x="' =0= =1= =2= =3= =4= =5=

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Re: Pushing a delimiter into an array?
by 2teez (Priest) on Mar 31, 2014 at 06:11 UTC

    ..something similar to the following awk OFS command..

    That would be perl's $OUTPUT_FIELD_SEPERATOR i.e $,. It works similar to $", except that you don't have to interpolated your array values in a double-quoted string.

    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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1080343]
Approved by graff
and a log crumbles through the grate...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2018-01-23 16:18 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (248 votes). Check out past polls.