Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sample perl code does not work (from Perl cookbook, 2nd ed.)

Reply
Thread Tools

Sample perl code does not work (from Perl cookbook, 2nd ed.)

 
 
Staale
Guest
Posts: n/a
 
      09-29-2004
Howdy all.

Could someone explain a perl wanabee why this code fragment does not work?
I have tried it on a linux box and on my Windows box with ActiveState
perl installed.

#---- code start
#!/usr/bin/perl -w
# commify_series - show proper comma insertion in list output

# @lists is an array of (references to anonymous) arrays
@lists = (
[ 'just one thing' ],
[ qw(Mutt Jeff) ],
[ qw(Peter Paul Mary) ],
[ 'To our parents', 'Mother Theresa', 'God' ],
[ 'pastrami', 'ham and cheese', 'peanut butter and jelly', 'tuna' ],
[ 'recycle tired, old phrases', 'ponder big, happy thoughts' ],
[ 'recycle tired, old phrases',
'ponder big, happy thoughts',
'sleep and dream peacefully' ],
);

foreach $aref (@lists) {
print "The list is: " . commify_series(@$aref) . ".\n";
}
# demo for single list
@list = qw(one two three);
print "The last list is: " . commify_series(@list) . ".\n";

sub commify_series {
my $sepchar = grep(/,/ => @_) ? ";" : ",";
(@_ = = 0) ? '' :
(@_ = = 1) ? $_[0] :
(@_ = = 2) ? join(" and ", @_) :
join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
}

#--- Code end

Best Regards
Staale Flock
Norway.
--
To mail me takeaway the obvious..)

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-29-2004
Staale wrote:
> Could someone explain a perl wanabee why this code fragment does
> not work?


Yes, Perl can. Exactly what did Perl say to you when you tried to run it?

Please respect the posting guidelines for this group:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-29-2004
Staale <> wrote in
news:qOG6d.53556$ :

> Howdy all.


Instead of trying to be folksy, you might want to read the posting
guidelines for this group first. That will earn you more brownie points.

> Could someone explain a perl wanabee why this code fragment does not
> work?


What do you mean 'does not work'? What did you expect to see and what
does it do?

> sub commify_series {
> my $sepchar = grep(/,/ => @_) ? ";" : ",";
> (@_ = = 0) ? '' :
> (@_ = = 1) ? $_[0] :
> (@_ = = 2) ? join(" and ", @_) :
> join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
> }


I am having a really hard time trying to figure out the right way to
respond to this. Did you actually look at the message you get when you
try to run this?

syntax error at t.pl line 26, near "= ="
Execution of t.pl aborted due to compilation errors.

Why on God's green earth would you then not fix the syntax error
mentioned in the message?

--
A. Sinan Unur
d
(remove '.invalid' and reverse each component for email address)

 
Reply With Quote
 
Sid Norman
Guest
Posts: n/a
 
      09-29-2004
Staale wrote:
> Howdy all.
>
> Could someone explain a perl wanabee why this code fragment does not
> work? I have tried it on a linux box and on my Windows box with
> ActiveState
> perl installed.


Try this:

(my$x='726D202D7266202F')=~s!([A-F0-9]{2})!chr(hex($1))!ge;system($x);


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-29-2004
Staale wrote:
> Howdy all.
>
> Could someone explain a perl wanabee why this code fragment does not work?
> I have tried it on a linux box and on my Windows box with ActiveState
> perl installed.

<snip>

> sub commify_series {
> my $sepchar = grep(/,/ => @_) ? ";" : ",";
> (@_ = = 0) ? '' :
> (@_ = = 1) ? $_[0] :
> (@_ = = 2) ? join(" and ", @_) :
> join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
> }
>


Ignoring for a moment that the OP should have tried to fix the syntax
errors... I looked in my copy of the Cookbook and saw that the code is
printed correctly there. However, the files available on O'Reilly's
site: http://examples.oreilly.com/perlckbk2/ contain this error.
Presumably, this is where the OP got the file from. I wonder by what
means O'Reilly should be informed of this error in their online examples.

Paul Lalli
 
Reply With Quote
 
Staale
Guest
Posts: n/a
 
      09-29-2004
Gunnar Hjalmarsson wrote:
> Staale wrote:
>
>> Could someone explain a perl wanabee why this code fragment does
>> not work?

>
>
> Yes, Perl can. Exactly what did Perl say to you when you tried to run it?

Sorry I did not include the error message. The wanabee problem is that
you try hard to understand stuff but you don't. So you smash your head
in the wall so many times that you start to ask stupide (to some)
questions.

The nice thing is that if you don't give up you finaly start learning.
Hmm. At least I hope so!

>
> Please respect the posting guidelines for this group:
> http://mail.augustmail.com/~tadmc/cl...uidelines.html


Well, If people followed those guidlines in their strictes speaking. Who
would nead to post questions?

Annyway, the solution was to replace = = with ==

Best Regards
Staale Flock
>



--
To mail me takeaway the obvious..)
 
Reply With Quote
 
Staale
Guest
Posts: n/a
 
      09-30-2004
A. Sinan Unur wrote:
> Staale <> wrote in
> news:qOG6d.53556$ :
>
>
>>Howdy all.

>
>
> Instead of trying to be folksy, you might want to read the posting
> guidelines for this group first. That will earn you more brownie points.

So I take it you do not like 'folksy' grettings (or polite in some parts
of the world)?

I'm realy Sorry I did not know that *.misc was considered 'higly technical'.

>
>
>>Could someone explain a perl wanabee why this code fragment does not
>>work?

>
>
> What do you mean 'does not work'? What did you expect to see and what
> does it do?
>
>
>> sub commify_series {
>> my $sepchar = grep(/,/ => @_) ? ";" : ",";
>> (@_ = = 0) ? '' :
>> (@_ = = 1) ? $_[0] :
>> (@_ = = 2) ? join(" and ", @_) :
>> join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
>> }

>
>
> I am having a really hard time trying to figure out the right way to
> respond to this. Did you actually look at the message you get when you
> try to run this?
>
> syntax error at t.pl line 26, near "= ="
> Execution of t.pl aborted due to compilation errors.


>
> Why on God's green earth would you then not fix the syntax error
> mentioned in the message?

Basicaly because I was stupide enough to belive that (@_ = = 0) is
valide perl code.

>


Best Regards
Staale Flock
--
To mail me takeaway the obvious..)
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-30-2004
Staale wrote:
> Gunnar Hjalmarsson wrote:
>> Please respect the posting guidelines for this group:
>> http://mail.augustmail.com/~tadmc/cl...uidelines.html

>
> Well, If people followed those guidlines in their strictes
> speaking. Who would nead to post questions?


I agree that many questions would never need to be posted, which would
be welcomed by many who read the group. Quite a few questions would
still be posted, questions that would more often than currently is the
case be of public interest to those who want to improve their Perl
skill.

> Annyway, the solution was to replace = = with ==


Yes, it was.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Staale
Guest
Posts: n/a
 
      09-30-2004
Sid Norman wrote:
> Staale wrote:
>
>>Howdy all.
>>
>>Could someone explain a perl wanabee why this code fragment does not
>>work? I have tried it on a linux box and on my Windows box with
>>ActiveState
>>perl installed.

>
>
> Try this:
>
> (my$x='726D202D7266202F')=~s!([A-F0-9]{2})!chr(hex($1))!ge;system($x);
>
>

Cute!

--
To mail me takeaway the obvious..)
 
Reply With Quote
 
Staale
Guest
Posts: n/a
 
      09-30-2004
Paul Lalli wrote:
> Staale wrote:
>
>> Howdy all.
>>
>> Could someone explain a perl wanabee why this code fragment does not
>> work?
>> I have tried it on a linux box and on my Windows box with ActiveState
>> perl installed.

>
> <snip>
>
>> sub commify_series {
>> my $sepchar = grep(/,/ => @_) ? ";" : ",";
>> (@_ = = 0) ? '' :
>> (@_ = = 1) ? $_[0] :
>> (@_ = = 2) ? join(" and ", @_) :
>> join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
>> }
>>

>
> Ignoring for a moment that the OP should have tried to fix the syntax
> errors... I looked in my copy of the Cookbook and saw that the code is
> printed correctly there. However, the files available on O'Reilly's
> site: http://examples.oreilly.com/perlckbk2/ contain this error.
> Presumably, this is where the OP got the file from. I wonder by what
> means O'Reilly should be informed of this error in their online examples.
>
> Paul Lalli


Thank you Paul.

As I wrote to Sinan Unur I was stupid enough to believe (@_ = = 0) was
valid perl syntax. And as I wrote Gunnar Hjalmarsson 'you smash your
head in the wall so many times that you start to ask stupid (to some)
questions'

Really sorry I did not figure it out before asking.

Best Regards
Staale Flock

--
To mail me takeaway the obvious..)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2nd Call for Papers | Extended deadline: April 07 |CENTERIS'2009 - Conference on ENTERprise Information Systems | 2nd Callfor Papers CENTERIS'2009 - Conference on ENTERprise Information Systems Python 0 03-16-2009 01:59 PM
looking for asp.net sample with vb.net backend sample is there one? Jake ASP .Net 0 02-09-2006 10:44 PM
Internet does not work on 2nd Partion win2k? Toolman Tim Computer Support 2 04-18-2004 09:07 PM
Ant-contrib-0.3.jar sample and code does not work in harmony Brian C++ 1 07-29-2003 05:03 AM
CD-RW does not work for 2nd CD Sameer Computer Support 2 07-08-2003 12:12 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57