Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Yep! There are several possible explainations I guess.

  • I've been reading the wrong "worked examples".
  • I (still) haven't done enough Haskell to have extablished a good approach to programming with it.

    Perhaps you could show me the steps you would take to tackling the sort of problem I have been trying to get to grips with?

    For the purpose of the exercise, I decided to index the words in a bunch of files. To get a start on the exercise, in Perl, I decided to just count the words, here simply defined as whitespace delimited groups of characters, and write the results to a file. This allows me to get something going and I can then work on refining the definition of a "word" and and decide how I am going to store the index.

    My first attempt at writing this program (from scratch, just now) was:

    #! perl -slw use strict; use G; ## Expands wildcards arguments as would be done by most *nix sh +ells. our $INDEX ||= 'words.index'; my %index; if( -e $INDEX ) { open INDEX, '<', $INDEX or die "$INDEX: $!"; m[^([^:]+):(\d+)$] and $index{ $1 } = $2 while <INDEX>; close INDEX; } while( my $file = @ARGV ) { open FILE, '<', $file or warn "$file:$!" and next; while( <FILE> ) { $index{ $_ }++ for split ' '; } close $file; } my( $key, $val ); open INDEX, '>', $INDEX or die "$INDEX : $!"; print INDEX "$key:$val" while ( $key, $val ) = each %index; close INDEX;

    I then ran a quick sanity check:

    P:\test>perl -c windex-1.pl windex-1.pl syntax OK

    So far, so good. Now try it out:

    P:\test>windex-1.pl *.pl 710:No such file or directory at P:\test\windex-1.pl line 15. 710:No such file or directory at P:\test\windex-1.pl line 15. 710:No such file or directory at P:\test\windex-1.pl line 15. 710:No such file or directory at P:\test\windex-1.pl line 15. ...

    Whoops! A quick look and I see I missed out the keyword pop in line 14:

    while( my $file = pop @ARGV ) {

    Try again:

    P:\test>windex-1.pl *.pl 306836-trietest.pl:Permission denied at P:\test\windex-1.pl line 15, < +FILE> line 83342.

    Okay, I've got 1 file with bad permissions in the directory. And what ended up in words.index?

    P:\test>type words.index expletive:2 $midVal:30 9f:8 proven:2 AT:10 happpen:2 inconsequential:2 perverted:2 $x;:24 TTGGGTCAGCGAATGTCACATTTGGAATGGGAATCGGATGATGGGGGCGA:2 ++$x):2 greif:2 yvvvvvvvvvvhvevwvvy:2 @exclude_keys:2 $hh2,:2 scrapped:2 classe:2 sod:2 $array[2]->set($_);:2 Cleaner,:2 $expand_ratio,:8 CACGTCTCCACCCGAAGACTGTGGAATGCTACTATTAAAATACTATTTTT:2 cdpr:2 BLANK_NODE:4 paws:6 "\n$_:2 ...

    Okay. The word definition is ecclectic, but is seems to be doing what I set out to do, now I can start to refine it. Not a bad start for 10 minutes coding.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

    In reply to Re: FP languages have a bottom-up bias? by BrowserUk
    in thread TMTOWTDI... and most of them are wrong by tlm

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post; it's "PerlMonks-approved HTML":



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others exploiting the Monastery: (4)
    As of 2024-04-25 15:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found