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

using perl 6 book trying first example

by reedx032 (Acolyte)
on Sep 08, 2010 at 05:11 UTC ( #859263=perlquestion: print w/ replies, xml ) Need Help??
reedx032 has asked for the wisdom of the Perl Monks concerning the following question:

I downloaded the latest rakudo star and built it on Mac OS X 10.5 with gcc 4.01 supplied with xcode 3.1.4. The tests appear to pass. I then downloaded the using perl 6 book and tried the first example
use v6; my $file = open 'scores'; my @names = $file.get.split(' '); my %matches; my %sets; for $file.lines -> $line { my ($pairing, $result) = $line.split(' | '); my ($p1, $p2) = $pairing.split(' vs '); my ($r1, $r2) = $result.split(':'); %sets{$p1} += $r1; %sets{$p2} += $r2; if $r1 > $r2 { %matches{$p1}++; } else { %matches{$p2}++; } } my @sorted = @names.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse +; for @sorted -> $n { say "$n has won %matches{$n} matches and %sets{$n} sets"; }
on the input
Beth Ana Charlie Dave Ana vs Dave | 3:0 Charlie vs Beth | 3:1 Ana vs Beth | 2:3 Dave vs Charlie | 3:0 Ana vs Charlie | 3:1 Beth vs Dave | 0:3
The following error is generated upon running the program:
Method 'split' not found for invocant of class '' in <anon> at line 12:type.p6 in main program body at line 1
I've tried both retyping the code and directly copy-pasting from the book and the same results are observed. Has anyone successfully run the code in the book. I'm not sure if this is an issue specific to my build, or if there is a mistake in the code in the book. Thanks, Dana

Comment on using perl 6 book trying first example
Select or Download Code
Re: using perl 6 book trying first example
by snoopy (Deacon) on Sep 08, 2010 at 05:58 UTC
    Hi Dana,

    I've managed to run your example script on both the latest rakudo built from git, plus rakudo star 2010.07.

    I do notice however, that as soon as I put a blank line anywhere in the input file, I get the same error message as you:

    Beth Ana Charlie Dave Ana vs Dave | 3:0 Charlie vs Beth | 3:1 Ana vs Beth | 2:3 Dave vs Charlie | 3:0 Ana vs Charlie | 3:1 Beth vs Dave | 0:3
    Produces:
    snoopy@snoopy-laptop:~/git/rakudo$ ./perl6 /tmp/score.p6 Method 'split' not found for invocant of class '' in <anon> at line 12:/tmp/score.p6 in main program body at line 1
    So I think it's got something to do with the input data or the line separators. Maybe the cut-and-paste introduced something?
      Thank you! There was a blank line at the end of my input file. I guess when the program tries to split the blank line, it leaves $result with no value. (undef?) That non-existent object has no split method I guess. Back to learning stuff...
Re: using perl 6 book trying first example
by moritz (Cardinal) on Sep 08, 2010 at 06:36 UTC
    As snoopy pointed out, additional whitespace or newlines can cause that problem. In that case $result ends up as Any, and you get that error (which includes less information than it should, due to a known bug in Rakudo).

    Two weeks ago I've added a note about the lack of robustness, and now expanded it a bit.

    We've decided not to use regexes for splitting or data extraction that early in the book, because they will be explained only two chapters later.

    Perl 6 - links to (nearly) everything that is Perl 6.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://859263]
Approved by GrandFather
Front-paged by mr_mischief
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-10-24 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls