Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
The stupid question is the question not asked
 
PerlMonks  

Re: Adult learning problem

by grinder (Bishop)
on Apr 05, 2002 at 07:53 UTC ( #156849=note: print w/ replies, xml ) Need Help??


in reply to Adult learning problem

He's going to fight to the last against doing something 'childish', even though I assure him that when I'm coding professionally I'm doing little test scripts more than I'm working on the 'real' program.

Right on, jepri! I just had a quick scan on 4 different accounts here at work, that reveals some 400+ little test scripts. On each machine I have a ~/perl directory where I try out snippets to check how things work. For instance, the other day I had hesitations about how splice works, because I use it so rarely. So I whipped up "muncharr" which looks like:

#! /usr/bin/perl -w use strict; my @arr = ('a'..'z'); my $nr = shift || 6; local $, = ' '; while( scalar @arr ) { my @block = splice( @arr, 0, $nr ); print "@block\n"; }

With that, I was able to see straight away that my understanding of how splice worked was in line with reality. It took me about 2 minutes for take the time out to perldoc -f splice and test this out. I was then able to fold the principle back into my main code and safe in the knowledge that at least that part of the code was likely to be bug-free :)

And I suppose it is useful to stress that even little snippets are deserving of strict and -w. Also note how I wrote the script to use default value(s) that can be overriden from the command line.


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'


Comment on Re: Adult learning problem
Download Code
Re: Re: Adult learning problem
by Juerd (Abbot) on Apr 05, 2002 at 08:06 UTC

    A good way to get to know splice better is to re-implement push, pop, unshift and shift with it.
    A good way to get to know substr better is to create push/pop/unshift/shift functions for strings.

    Often, a good way to learn is re-inventing the wheel. I'd rather re-invent a wheel than greet the world.

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      A good way to get to know splice better is to re-implement push, pop, unshift and shift with it.

      Uhm, or you could just read the docs :-)

      D:\Development>perldoc -f splice splice ARRAY,OFFSET,LENGTH,LIST ..... The following equivalences hold (assuming "$[ == 0"): push(@a,$x,$y) splice(@a,@a,0,$x,$y) pop(@a) splice(@a,-1) shift(@a) splice(@a,0,1) unshift(@a,$x,$y) splice(@a,0,0,$x,$y) $a[$x] = $y splice(@a,$x,1,$y)
      A good way to get to know substr better is to create push/pop/unshift/shift functions for strings.

      Well.... Once you know how to implement splice using susbtr you dont need to implement push/pop/unshift or shift, but its true it might be a good exercise. Personally would suggest taking that thought a step further and reimplement Tie::CharArray, by the time you are done if you dont understand Tie's, Array implementation, Splice Implementation and Substr implementation then I would say theres something very wrong... (either with the individual or the code ;-) Oh and it probably is a good idea because you can use the CPAN module for a proper code comparison and review afterward.

      Thats what I did to learn Tie the first time anyway... :-)

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

      That certainly seems to be the standard teaching method in the scheme and lisp worlds. The number of such books I've seen that have you recoding length and the like. And it seems to work.
Advantages Of The Test Script - Re: Re: Adult learning problem
by metadoktor (Hermit) on Apr 05, 2002 at 09:13 UTC
    This is truly sage advice. I often resort to the 'test' script to fully flesh out a language construct that I don't fully understand or fully test an algorithm/idea that I'm working on.

    I find these 'test' scripts very useful. I'm glad that I'm not the only one doing this.

    metadoktor

    "The doktor is in."

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-04-17 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (441 votes), past polls