Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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:

Hi!,
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}'

Comment on Pushing a delimiter into an array?
Select or Download Code
Re: Pushing a delimiter into an array?
by iguanodon (Curate) 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 (Abbot) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2014-07-24 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (163 votes), past polls