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

Push and list context

by uksza (Abbot)
on Mar 14, 2005 at 23:19 UTC ( #439495=perlquestion: print w/replies, xml ) Need Help??
uksza has asked for the wisdom of the Perl Monks concerning the following question:

Hello wise monks,

is any better (more elegant) way to do it:
my @array = qw/one two three/; my @out; push @out,"@array\n"; #push @out, reverse @array; <-- "threetwoone" push @out, "@{[reverse @array]}"; print @out;

thanks for yours wisdom,
Lukasz

Replies are listed 'Best First'.
Re: Push and list context
by Errto (Vicar) on Mar 14, 2005 at 23:34 UTC
    I'm not sure whether your main issue is print isn't adding spaces when you print a list, or that what you really want to do is push onto an array a string consisting of another list joined with spaces. If the latter, Mugatu is right. If the former, then you can do either
    push @out, reverse @array; print join " ", @out;
    or
    push @out, reverse @array; { local $, = " "; print @out; }
    Update: forgot the reverse
Re: Push and list context
by Mugatu (Monk) on Mar 14, 2005 at 23:28 UTC

    If I understand you correctly, this would be the preferred way:

    push @out, join " ", reverse @array;

    Or, to mimic the example more exactly:

    push @out, join $", reverse @array;
Re: Push and list context
by tlm (Prior) on Mar 15, 2005 at 02:37 UTC

    It's hard to say what better/more elegant would be in the absence of some context. That said, if all I wanted to do was to print an array forwards and backwards, with spaces between the elements, I'd just do something like

    my @array = qw(one two three); print "@{[@array, reverse @array]}\n";
    If you need to keep the reverse array, then
    my @yarra = reverse my @array = qw(one two three); print "@array @yarra\n";

    the lowliest monk

Re: Push and list context
by sh1tn (Priest) on Mar 14, 2005 at 23:49 UTC
    my @array = qw/one two three/; my @out = reverse @array; # or @out = (@out, reverse @array);


Re: Push and list context
by tphyahoo (Vicar) on Mar 15, 2005 at 11:56 UTC
    Putting arrays in quotes seems to be the same as joining them. That is, it adds spaces betweeen the elements. This works, and is real easy to read.
    use warnings; use strict; my @array = qw/one two three/; my @reversed = reverse @array; print "@array @reversed", "\n";
      Putting arrays in quotes seems to be the same as joining them. That is, it adds spaces betweeen the elements.

      Array interpolation uses $" as the separator, which defaults to a space, but can be changed to anything. Thus, "@array" is equivalent to join($", @array) (which I had in my earlier reply, but didn't really explain all the way).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://439495]
Approved by Errto
help
Chatterbox?
[erix]: and now you are agnostic?
[james28909]: arunbear, your picking bits of history to fit your own narrative. right now, humans are smarter than in any of those times.
[james28909]: in order for there to be a mutation, there has to be a universe with laws setup that even allow a mutation.
[james28909]: everything past the first two things you piked from my posts are irrelevant to me and my beliefs. they are side effects.
[james28909]: two or three. if it has to do with how humans are handling said situations, then that is after they were able to evolve into those problems. agnostic means only believe with evidence. i took that from a tyson video
[james28909]: and it seems that racism suggest that there is indeed a better race, but that is for evolution to decide
[1nickt]: "right now, humans are smarter than in any of those times": define "smart"
[erix]: ah, now you're appraoch downvote-territory ;)

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2017-12-15 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (433 votes). Check out past polls.

    Notices?