Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

for vs. reverse

by Corion (Patriarch)
on Mar 26, 2001 at 13:58 UTC ( [id://67150]=perlquestion: print w/replies, xml ) Need Help??

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

I was playing around with some sillyness ensuing the usual Emacs vs. vi arguments, and while I've not seen any other use than this silly problem, I'm stumped how inelegant my solution to the problem is.

The idea is to print the sentences

Why do we have to hide from the police, daddy ? Because they use emacs and we use vi, son. Why do we have to hide from the police, daddy ? Because they use vi and we use emacs, son.

The obvious "optimization" I'm aiming for is to somehow replace the double instances of Emacs and vi with something pulled from an array. The first idea I came up with, was the following :

print "Why do we have to hide from the police, daddy ?\n Because they + use $_->[0] and we use $_->[1], son.\n" for ($_,reverse @$_) for (["emacs","vi"])

This idea was a nice idea, but it dosen't work, as Perl does not want double for statements within one statement. It would also raise the question which of the postfix for statements would be evaluated first.

My second solution then was

do { foreach ($_, [reverse @$_]){print "Why do we have to hide from the p +olice, daddy ?\n Because they use $_->[0] and we use $_->[1], son.\n +"} } for (["emacs","vi"])'

which is inelegant, but at least it works now. But the do strikes me as superfluous.

Any help or suggestions for my aestethic problem welcomed ;-)

Replies are listed 'Best First'.
Re: for vs. reverse
by danger (Priest) on Mar 26, 2001 at 14:24 UTC

    Well, here's yet another version (but without 'for', 'reverse', or 'map'):

    printf <<HERE x 2, qw(emacs vi)[0,1,1,0]; Why do we have to hide from the police, daddy? Because they use %s and we use %s, son. HERE

    Update: Hmm, I only tested the above with 5.6.0 -- you can't directly subscript the qw() operator in 5.00503 it seems. Easily fixed by either wrapping the qw() inside another level of parens, or using the more literal:  ('emacs', 'vi')[0,1,1,0].

Re: for vs. reverse
by jbert (Priest) on Mar 26, 2001 at 19:41 UTC
    OK, its a little sick and twisted, but it *is* one statement and it doesn't have a for or while loop:
    @_ = sort { print "Why do we have to hide from the police, daddy ? Because they use $a and we use $b, son.\n" if $a ne $b; } ( 'emacs', 'vi', 'emacs' );
      Hah! That's g-r-r-r-r-r-reat!

      A few touch-ups:

      () = sort { print "Why do we have to hide from the police, daddy ?\nBecause they + use $a and we use $b, son.\n" } qw(emacs vi emacs);
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
Re: for vs. reverse
by jlp (Friar) on Mar 26, 2001 at 14:17 UTC
    my spin on it:
    print "Why do we have to hide from the police, daddy? Because they use $_->[0] and we use $_->[1]\n" for ($a = [qw(emacs vi)],[ reverse @$a ]); I love abusing $a and $b.
Re: for vs. reverse
by Tyke (Pilgrim) on Mar 26, 2001 at 14:06 UTC
    well you _could_ try
    print qq(Why do we have to hide from the police, daddy ?\n Because the +y use $_->[0] and we use $_->[1], son.\n) for map {$_, [reverse @$_]}[qw(emacs vi)]
    but it's still pretty ugly, and not terribly extensible
Re: for vs. reverse
by jeroenes (Priest) on Mar 26, 2001 at 14:10 UTC
    map { print "Why do we have to hide from the police, daddy ?\n Because th +ey use $_->[0] and we use $_->[1], son.\n" for ($_, [reverse @$_]); }(["emacs","vi"]);
    replacing for with map worked, but I had to pull in an extra array-ref. Dunno why it worked without that one.

    Cheers,

    Jeroen
    "We are not alone"(FZ)

Re (tilly) 1: for vs. reverse
by tilly (Archbishop) on Mar 27, 2001 at 03:37 UTC
    I am surprised that nobody used the fact that negative subscripts work:
    @a = qw(emacs vi); print "Why do we have to hide from the police, daddy ? Because they use $a[$_] and we use $a[$_-1], son. " for 0..1;
(jcwren) Re: for vs. reverse
by jcwren (Prior) on Mar 27, 2001 at 05:21 UTC

    Ovid will prolly make mincemeat of me for my regexp atrocity, but what the heck...

    print $_="Why do we have to hide from the police, daddy? Because they use vi and we use emacs, son.\n", join('',(/(.*)(vi)(.*)(emacs)(.*)/s)[0,3,2,1,4]);
    --Chris

    e-mail jcwren
      Hmmm. That inspires me. Without arrays, loops, REs...
      print (("Why do we have to hide from the police, daddy ? Because they use ", "emacs", " and we use ", "vi", ", son. ")[0..4,0,3,2,1,4]);
Re: for vs. reverse
by DeaconBlues (Monk) on Mar 26, 2001 at 19:09 UTC
    This works, maybe this is not as good as the others.
    @_ = (@{$_ = [qw{emacs vi}]}, reverse @$_); print "Why do we have to hide from the police, daddy ? Because they us +e ", shift @_, " and we use ", shift @_, ", son.\n" while(@_);
    but I can't figure out why this doesn't work. Doesn't shift work on @_ by default?
    @_ = (@{$_ = [qw{emacs vi}]}, reverse @$_); print "Why do we have to hide from the police, daddy ? Because they us +e ", shift, " and we use ", shift, ", son.\n" while(@_);
      but I can't figure out why this doesn't work. Doesn't shift work on @_ by default?

      Yes and no: see shift. This is an example of Do What I Mean (DWIM). Inside a subroutine or format, shift works on @_ by default. Outside a subroutine, however, shift works on @ARGV by default.

      #!perl -l @ARGV = '@ARGV'; @_ = '@_'; print '$ARGV[0] = ', shift; mysub(@_); sub mysub { print '$_[0] = ', shift; }
Re: for vs. reverse
by Fingo (Monk) on Mar 27, 2001 at 02:54 UTC
    Here's my pitiful attempt. (Look mom! No arrays!)
    sub print_stuff { print "\nWhy do we have to hide from the police, daddy?\nBecause they +use $_[0] and we use $_[1], son.\n" } print_stuff("emacs","vi"); print_stuff("vi","emacs");

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-03-29 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found