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

The most TIMTOWTDI thing you can do

by cLive ;-) (Prior)
on Oct 23, 2001 at 09:49 UTC ( [id://120718]=perlmeditation: print w/replies, xml ) Need Help??

Seeing s0ttle's recent post got me thinking.

And the thought crossed my mind:

What's the most TIMTOWTDI thing you can do? ie, what action can you perform in Perl in the most distinct ways?

Eg, string concatenation:

$concat = $a.$b $concat = "$a$b" $concat = join '', $a, $b; $concat = sprintf("%s%s",$a,$b); # silly $concat =~ s/.*/$a$b/s; # sillier!
That's 5 distinct (some slightly silly) ways to do it.

Get your thinking caps on... And no cheating! :-) - eg, manipulating $a and $b first so you can then feed them into another function.

Is this golf? Na, it's Perl Pooltm

cLive ;-)

Replies are listed 'Best First'.
Re: The most TIMTOWTDI thing you can do
by blakem (Monsignor) on Oct 23, 2001 at 10:04 UTC
    Just off the top of my head:
    1.) ($concat = $a) .= $b;
    perhaps more will follow. (and they did....)
    2.) $" = $b; @_ = ($a,undef); $concat = "@_"; 3.) do { $concat .= $_ for (split(//))} for ($a,$b); 4.) my %a = ($a => $b); $concat = join'',each %a; 5.) $concat =~ s//$a/; $concat =~ s/$/$b/; 6.) $concat = <<"EOF"; chomp($concat); $a$b EOF 7.) $concat = `echo -n "$a$b"`; # for safe values of $a and $b 8.) $concat = glob("{$a}{$b}"); # not Taint safe... will choke on met +achars... 9.) ($concat) = ("$a$b" =~ /(.*)/); And finally, everyones favorite Perl6 construct... 10.) $concat = $a _ $b;

    Update: Ok, now I get it... you want us to pick a simple operation and try to find $MAX+1 ways to do it, not necessarily just find other ways to do a concat. In that case, I'll set the original $MAX rather high by pointing at Adding 2 + 2, which ties perl-based-dynamic-webpage-templating-systems for the most most "ways to do it."

    Update2: Added a few more to the list...

    -Blake

      10.) $concat = $a _ $b;

      OK, I'll stick out my neck.
      Where can I RTFM on this _ operator(?)?

      f--k the world!!!!
      /dev/world has reached maximal mount count, check forced.

Re: The most TIMTOWTDI thing you can do
by one4k4 (Hermit) on Oct 23, 2001 at 23:36 UTC
    Similar to option 5 of yours, I tried:
    #!/usr/bin/perl -w use strict; my $a = "Foo"; my $bar = "bar"; $a =~ s/(.*)/$1$bar/; print $a;

    There's always:
    1) Create insert sql statements 2) Insert into a table 3) select col1+col2 from table 4) fetchrow 5) print

    :)

    _14k4 - perlmonks@poorheart.com (www.poorheart.com)
Re: The most TIMTOWTDI thing you can do
by demerphq (Chancellor) on Oct 25, 2001 at 15:32 UTC
    Ok, heres a couple of strange ones:
    #Yabadabadoo! my $cat=unpack("A*",pack("A*A*",$s_a,$s_b)); # Serious overkill use Tie::Handle::Scalar; my $cat2; tie *FH, 'Tie::Handle::Scalar', \$cat2; print FH $s_a,$s_b;
    BTW its bad practice to use the variables $a and $b. They have special meaning to sort and you can get weird errors from them.

    Yves / DeMerphq
    --
    Have you registered your Name Space?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-20 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found