How to Generate An Expression to A LOL?

How to Generate An Expression to A LOL?

am 24.04.2011 14:08:18 von z sway

--20cf3071cc3681eccc04a1a8f27e
Content-Type: text/plain; charset=ISO-8859-1

the expression only contains number and"+*()"
the form of list is like[op,node1,node2...],op is "+"or"*",node can be a
number or a child list.
e.g :1*2+3*4*(5+6)
[ '+', ['*', '1', '2'], ['*', '3', '4', ['+','5', '6'] ] ]
Thanks!

--20cf3071cc3681eccc04a1a8f27e--

Re: How to Generate An Expression to A LOL?

am 24.04.2011 14:51:57 von Shlomi Fish

Hi "Z",

On Sunday 24 Apr 2011 15:08:18 z sway wrote:
> the expression only contains number and"+*()"
> the form of list is like[op,node1,node2...],op is "+"or"*",node can be a
> number or a child list.
> e.g :1*2+3*4*(5+6)
> [ '+', ['*', '1', '2'], ['*', '3', '4', ['+','5', '6'] ] ]
> Thanks!

Well, I think I understand your question. If you:

1. Want to parse the statements look at the various parser generators on CPAN:

http://perl-begin.org/uses/text-parsing/#parser-generators

This seems what you want. Using them requires some knowledge in the underlying
theory of the so-called "formal languages" and "context-free grammars" (whose
study has a lot of discrete mathematics formalism, but is conceptually very
simple), and parsers generators tend to be very "subtle and quick to become
angry" beasts - i.e: they are hard to get right and often expose
implementation details.

2. If you want to generate a serial expression from a tree, then this can be
easily done sub-optimally using recursion, but will require more logic to do
properly (i,e: while omitting unnecessary parentheses).. See:

http://en.wikipedia.org/wiki/Computer_algebra_system

Implementing a full user-friendly system like that is probably out-of-scope
for what you want, and serious, proprietary, mathematical software (e.g:
Mathematica, Matlab, Maple, etc.) can cost you a lot of money (unless you are
a student).

--------------

Hope that helps, because your message was a bit vague. My mind reading device
is on repair, not to mention that it is limited to a 10 kilometer radius
beyond which I am unable to read people's mind clearly enough, due to natural
technological limitations. ;-) (Old joke - ignore.).

Regards,

Shlomi Fish

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/

I hope that you agree with me that 99.9218485921% of the users wouldn't bother
themselves with recompilation (or any other manual step for that matter) to
make their games run 1.27127529900685765% faster ;-) -- Nadav Har'El

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to Generate An Expression to A LOL?

am 25.04.2011 12:36:07 von Shlomi Fish

Hi "Z",

you replied to me in private (unintentionally, I presume), so I'm CCing the
list on my reply (I hope it's OK).

On Monday 25 Apr 2011 12:07:54 z sway wrote:
> Hi,sorry I hadn't said it clearly,what I want to do is to generate a LOL
> from an expression and try* *not to use any extra modules.

OK, one thing you should realise is that, to quote my late grandfather, Perl
without CPAN is like a cat without whiskers[whiskers]. While it sometimes
makes sense to reduce dependencies[Deps], often many modules on CPAN are self-
contained, or have very few dependencies and you can stick them in one place
and forget about them or pack them in PAR. See:

http://www.shadowcat.co.uk/blog/matt-s-trout/but-i-cant-use- cpan/

(Matt S. Trout has an offensive and insulting style, so you have been warned,
but he still says wise things of substance.).

Anyway, you can put one of the parser generators on your system, and I think
byacc-perl generates self-contained Perl code.

> So far i've only got an idea that spilt the expression into a list_A,in
> which each element is a number or"+*()",use list_B as a stack,pop from
> list_A,push to list_B under some rules,finally got the tree in list_B.
>
> I think my method is a bit complicated,there should be a easier one even if
> can't detect some mistakes like (5+)2)*5 or 6++7*8
>

Implementing your own Parse-Recdescent (or worse LALR/GLR/etc.) parser is
prone to errors and inadvisable, especially if you don't have a lot of
theoretical background. Unless this is homework in compilation theory or a
similar course (which most universities don't really do in Perl, for better or
for worse), then please use a parser generator, in case you're interested in
writing your code quickly and correctly, rather than re-inventing your own
square wheel (and trust me - it will be square).

If you *are* interested in learning more about compiler theory and parsing,
then I liked the Dragon book (read the green one I think, but there's already
a newer Purple dragon one which should be better), and while it has some
issues, is pretty good:

http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniqu es,_and_Tools

There are many other online and offline books, tutorials and resources (on
http://www.wikibooks.org/ / etc.), and you should be good to go. But doing it
without learning it first is not a good idea. To quote
http://en.wikiquote.org/wiki/Euclid , "There is no royal road to geometry",
because sometimes various issues in computer science require learning some
theory to do properly. Note though that parsing theory is not *that* hard.

Sorry for the long post.

Regards,

Shlomi Fish

[whiskers] - I have only understood this sentence later on when I realised
that cats with damaged whiskers lose their balance, and are very non-
functional:

http://en.wikipedia.org/wiki/Whiskers

It didn't help that my grandfather said it in Hebrew, using the same word for
"whiskers" as the Hebrew word for "moustache", which is completely inessential
for humans to survive. There is an English idiom called "the Cat's whiskers",
but I think it means more like "the top of the top" or "la creme de la creme".

[Deps] - It is discussed here:

http://www.onlamp.com/pub/a/onlamp/2004/04/08/disaster_recov ery.html

It kinda reminds me of "Can you build a package in one command?" from the
"McConnell test"/"Joel test":

http://www.joelonsoftware.com/articles/fog0000000043.html


--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Understand what Open Source is - http://shlom.in/oss-fs

There is no IGLU Cabal! None of them could pass the Turing test. But strangely
enough a computer program they coded, could.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to Generate An Expression to A LOL?

am 25.04.2011 18:28:47 von z sway

--e0cb4e43b285bb892704a1c0b3e5
Content-Type: text/plain; charset=ISO-8859-1

Thank you for being so patient to a newbie.
It's just homework and I think it's not that deep because the course is only
about Perl.
I'm interested in parsing, but I haven't learnt much about it yet.

2011/4/25 Shlomi Fish

> Hi "Z",
>
> you replied to me in private (unintentionally, I presume), so I'm CCing the
> list on my reply (I hope it's OK).
>
> On Monday 25 Apr 2011 12:07:54 z sway wrote:
> > Hi,sorry I hadn't said it clearly,what I want to do is to generate a LOL
> > from an expression and try* *not to use any extra modules.
>
> OK, one thing you should realise is that, to quote my late grandfather,
> Perl
> without CPAN is like a cat without whiskers[whiskers]. While it sometimes
> makes sense to reduce dependencies[Deps], often many modules on CPAN are
> self-
> contained, or have very few dependencies and you can stick them in one
> place
> and forget about them or pack them in PAR. See:
>
> http://www.shadowcat.co.uk/blog/matt-s-trout/but-i-cant-use- cpan/
>
> (Matt S. Trout has an offensive and insulting style, so you have been
> warned,
> but he still says wise things of substance.).
>
> Anyway, you can put one of the parser generators on your system, and I
> think
> byacc-perl generates self-contained Perl code.
>
> > So far i've only got an idea that spilt the expression into a list_A,in
> > which each element is a number or"+*()",use list_B as a stack,pop from
> > list_A,push to list_B under some rules,finally got the tree in list_B.
> >
> > I think my method is a bit complicated,there should be a easier one even
> if
> > can't detect some mistakes like (5+)2)*5 or 6++7*8
> >
>
> Implementing your own Parse-Recdescent (or worse LALR/GLR/etc.) parser is
> prone to errors and inadvisable, especially if you don't have a lot of
> theoretical background. Unless this is homework in compilation theory or a
> similar course (which most universities don't really do in Perl, for better
> or
> for worse), then please use a parser generator, in case you're interested
> in
> writing your code quickly and correctly, rather than re-inventing your own
> square wheel (and trust me - it will be square).
>
> If you *are* interested in learning more about compiler theory and parsing,
> then I liked the Dragon book (read the green one I think, but there's
> already
> a newer Purple dragon one which should be better), and while it has some
> issues, is pretty good:
>
> http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniqu es,_and_Tools
>
> There are many other online and offline books, tutorials and resources (on
> http://www.wikibooks.org/ / etc.), and you should be good to go. But doing
> it
> without learning it first is not a good idea. To quote
> http://en.wikiquote.org/wiki/Euclid , "There is no royal road to
> geometry",
> because sometimes various issues in computer science require learning some
> theory to do properly. Note though that parsing theory is not *that* hard.
>
> Sorry for the long post.
>
> Regards,
>
> Shlomi Fish
>
> [whiskers] - I have only understood this sentence later on when I realised
> that cats with damaged whiskers lose their balance, and are very non-
> functional:
>
> http://en.wikipedia.org/wiki/Whiskers
>
> It didn't help that my grandfather said it in Hebrew, using the same word
> for
> "whiskers" as the Hebrew word for "moustache", which is completely
> inessential
> for humans to survive. There is an English idiom called "the Cat's
> whiskers",
> but I think it means more like "the top of the top" or "la creme de la
> creme".
>
> [Deps] - It is discussed here:
>
> http://www.onlamp.com/pub/a/onlamp/2004/04/08/disaster_recov ery.html
>
> It kinda reminds me of "Can you build a package in one command?" from the
> "McConnell test"/"Joel test":
>
> http://www.joelonsoftware.com/articles/fog0000000043.html
>
>
> --
> ------------------------------------------------------------ -----
> Shlomi Fish http://www.shlomifish.org/
> Understand what Open Source is - http://shlom.in/oss-fs
>
> There is no IGLU Cabal! None of them could pass the Turing test. But
> strangely
> enough a computer program they coded, could.
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
>

--e0cb4e43b285bb892704a1c0b3e5--

Re: How to Generate An Expression to A LOL?

am 26.04.2011 06:12:25 von Shlomi Fish

Hi z sway,

On Monday 25 Apr 2011 19:28:47 z sway wrote:
> Thank you for being so patient to a newbie.
> It's just homework and I think it's not that deep because the course is
> only about Perl.
> I'm interested in parsing, but I haven't learnt much about it yet.
>

OK, thanks for keeping me (and the rest of the list) updated. I think my
advice is still solid, and now you need to see what you need to know by
yourself. Good luck on your homework, and feel free to let us know if you run
into a more concrete problem with your Perl coursework.

For more ways to get help with Perl you should go over most of the stuff on
http://perl-begin.org/ (caveat: while perl-begin is fully free-content/open-
content and I encourage people to contribute to it, it was my idea and is
almost entirely my own solo project. I still think it's very good and many
people I referred to agreed with me, but you may prefer http://learn.perl.org/
, so your kilometrage may vary.).

Regards,

Shlomi Fish

> 2011/4/25 Shlomi Fish
>
> > Hi "Z",
> >
> > you replied to me in private (unintentionally, I presume), so I'm CCing
> > the list on my reply (I hope it's OK).
> >
> > On Monday 25 Apr 2011 12:07:54 z sway wrote:
> > > Hi,sorry I hadn't said it clearly,what I want to do is to generate a
> > > LOL from an expression and try* *not to use any extra modules.
> >
> > OK, one thing you should realise is that, to quote my late grandfather,
> > Perl
> > without CPAN is like a cat without whiskers[whiskers]. While it sometimes
> > makes sense to reduce dependencies[Deps], often many modules on CPAN are
> > self-
> > contained, or have very few dependencies and you can stick them in one
> > place
> > and forget about them or pack them in PAR. See:
> >
> > http://www.shadowcat.co.uk/blog/matt-s-trout/but-i-cant-use- cpan/
> >
> > (Matt S. Trout has an offensive and insulting style, so you have been
> > warned,
> > but he still says wise things of substance.).
> >
> > Anyway, you can put one of the parser generators on your system, and I
> > think
> > byacc-perl generates self-contained Perl code.
> >
> > > So far i've only got an idea that spilt the expression into a list_A,in
> > > which each element is a number or"+*()",use list_B as a stack,pop from
> > > list_A,push to list_B under some rules,finally got the tree in list_B.
> > >
> > > I think my method is a bit complicated,there should be a easier one
> > > even
> >
> > if
> >
> > > can't detect some mistakes like (5+)2)*5 or 6++7*8
> >
> > Implementing your own Parse-Recdescent (or worse LALR/GLR/etc.) parser is
> > prone to errors and inadvisable, especially if you don't have a lot of
> > theoretical background. Unless this is homework in compilation theory or
> > a similar course (which most universities don't really do in Perl, for
> > better or
> > for worse), then please use a parser generator, in case you're interested
> > in
> > writing your code quickly and correctly, rather than re-inventing your
> > own square wheel (and trust me - it will be square).
> >
> > If you *are* interested in learning more about compiler theory and
> > parsing, then I liked the Dragon book (read the green one I think, but
> > there's already
> > a newer Purple dragon one which should be better), and while it has some
> > issues, is pretty good:
> >
> > http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniqu es,_and_Tools
> >
> > There are many other online and offline books, tutorials and resources
> > (on http://www.wikibooks.org/ / etc.), and you should be good to go. But
> > doing it
> > without learning it first is not a good idea. To quote
> > http://en.wikiquote.org/wiki/Euclid , "There is no royal road to
> > geometry",
> > because sometimes various issues in computer science require learning
> > some theory to do properly. Note though that parsing theory is not
> > *that* hard.
> >
> > Sorry for the long post.
> >
> > Regards,
> >
> > Shlomi Fish
> >
> > [whiskers] - I have only understood this sentence later on when I
> > realised that cats with damaged whiskers lose their balance, and are
> > very non- functional:
> >
> > http://en.wikipedia.org/wiki/Whiskers
> >
> > It didn't help that my grandfather said it in Hebrew, using the same word
> > for
> > "whiskers" as the Hebrew word for "moustache", which is completely
> > inessential
> > for humans to survive. There is an English idiom called "the Cat's
> > whiskers",
> > but I think it means more like "the top of the top" or "la creme de la
> > creme".
> >
> > [Deps] - It is discussed here:
> >
> > http://www.onlamp.com/pub/a/onlamp/2004/04/08/disaster_recov ery.html
> >
> > It kinda reminds me of "Can you build a package in one command?" from the
> > "McConnell test"/"Joel test":
> >
> > http://www.joelonsoftware.com/articles/fog0000000043.html
> >
> >
> > --
> > ------------------------------------------------------------ -----
> > Shlomi Fish http://www.shlomifish.org/
> > Understand what Open Source is - http://shlom.in/oss-fs
> >
> > There is no IGLU Cabal! None of them could pass the Turing test. But
> > strangely
> > enough a computer program they coded, could.
> >
> > Please reply to list if it's a mailing list post - http://shlom.in/reply
> > .

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Interview with Ben Collins-Sussman - http://shlom.in/sussman

XSLT is what Chuck Norris has nightmares of.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to Generate An Expression to A LOL?

am 28.04.2011 19:06:15 von z sway

--20cf3071cc3633452904a1fd93c8
Content-Type: multipart/alternative; boundary=20cf3071cc3633452204a1fd93c6

--20cf3071cc3633452204a1fd93c6
Content-Type: text/plain; charset=GB2312
Content-Transfer-Encoding: quoted-printable

Hi, I've finished it using recursion.


#!/usr/bin/perl -w
use strict;
use Data::Dumper;

#²»¼ì²éÊäÈëËãʽÊÇ·ñ=D5=F DÈ·
print "ÊäÈë×ÔÈ»ÊýËãʽ£¨Ö»=D4=C AÐí=
¼Ó·¨¡¢³Ë·¨¡¢À¨ºÅ£©\n";
print "ÊäÈëÍê³É°´»Ø³µÈ·ÈÏ=A3=A CÍË=
³öÇëÊäÈë0\n\n";
while (1) {
print "Enter the expression : ";
chomp($_ =3D );
last if not $_;
my @tree =3D grow($_);
print Dumper(\@tree);
my $result =3D calculate(\@tree);
print "\nresult\=3D$result\n\n";
my $answer =3D eval "$_";
print "Still have bug,the correct answer is $answer\n" if ($answer !=3D
$result);

sub grow {
my $expr =3D $_[0];
my ($part, @part, $node);
if ($expr =3D~ /\(/){
@part =3D ($expr =3D~ / \( (?:(?>[^()]+) | (?R))* \) /xg); #ÌáÈ¡=C0=
¨ºÅÖÐÄÚÈ=DD
foreach $part (@part) {
$part =3D~ s/^\(|\)$//g;
my @temp =3D grow($part); #µÝ¹éµ÷ÓÃ
$part =3D \@temp;
}
$expr =3D~ s/\( ((?>[^()]+) | (?R))* \)/tag/xg; #ԭʽÖÐÀ¨=
ºÅÄÚÈÝÔÝʱÓÃ×Ö·û¡°tag=A1 ±Ìæ´=FA
}
my @node=3D ($expr =3D~ /\d+|tag/g); #´ÓËãʽÖÐÌáÈ¡=
Êý×Ö
my $i =3D 0;
foreach $node (@node) {
if ($node eq "tag"){ #tagÄÚÈÝ»¹Ô­³ÉÉú³ÉµÄ=
Ê÷
$node =3D $part[$i];
$i++;
}
}
my @op=3D($expr =3D~ /[+*()]/g); #´ÓËãʽÖÐÌáÈ¡=CB=
ã·=FB
return basic (\@node, \@op);
}

sub basic { #½«²»º¬À¨ºÅµÄËãʽת=B3=C 9=
Ê÷
my($a, $b, $i);
($a, $b) =3D @_;
my @node =3D @$a;
my @op =3D @$b;
for($i =3D 0; $i < @op; $i++){ #ת»»³Ë·¨
if ($op[$i] eq "*"){
my $end =3D $i+1;
while (defined($op[$end])&&($op[$end] eq "*")){
$end++ if $end < @op;
}
my $t =3D $end - $i;
my @new =3D @node[$i..$end];
unshift (@new, "*");
splice(@node, $i, $t+1, \@new);
splice(@op, $i, $t,);
}
}
if (@op){
unshift (@node, "+");
}else{
@node=3D@{$node[0]};
}
return @node;
}

sub calculate{
my $a =3D $_[0];
return $a if ref($a) ne "ARRAY";
my @tree =3D @$a;
my $tree;
my $result =3D 0;
if ($tree[0] eq "+"){
shift @tree;
foreach $tree(@tree){
$result =3D $result + calculate($tree);
}
} elsif ($tree[0] eq "*"){
$result =3D 1;
shift @tree;
foreach $tree(@tree){
$result =3D $result * calculate($tree);
}
}
return $result;
}
}
1;

--20cf3071cc3633452204a1fd93c6
Content-Type: text/html; charset=GB2312
Content-Transfer-Encoding: quoted-printable

Hi, I've finished it using recursion.

>
#!/usr/bin/perl -w
use strict;
use Data::Dump=
er;

#²»¼ì²éÊäÈëËãʽ=CA=
Ç·ñÕýÈ=B7
print "ÊäÈë×ÔÈ»Êý=
Ëãʽ£¨Ö»ÔÊÐí¼Ó·¨¡¢³Ë ·¨¡¢=C0=
¨ºÅ£=A9\n";

print "ÊäÈëÍê³É°´»Ø³µÈ·ÈÏ=A3=
¬Í˳öÇëÊäÈ=EB0\n\n";
while (1) {
iv style=3D"margin-left: 40px">  print "Enter the expression : =
";
chomp($_ =3D <STDIN>=
;);

last if not $_;
gin-left: 40px"> my @tree =3D grow($_);
x"> print Dumper(\@tree);
my $result=
=3D calculate(\@tree);

print "\nresult\=3D$result\n\n"=
;
my $answer =3D eval "$_"=
;
print "Still have bug,the cor=
rect answer is $answer\n" if ($answer !=3D $result);


>sub grow {
my $expr =3D $_[0]; >
my ($part, @part, $node);
e=3D"margin-left: 80px">
if ($expr =3D~ /\(/){
@part =3D (=
$expr =3D~ / \( (?:(?>[^()]+) | (?R))* \) /xg); #ÌáÈ¡À¨=BA=
ÅÖÐÄÚÈ=DD
foreach $par=
t (@part) {

$part =3D~ s/^\(|\)$//g;
my @t=
emp =3D grow($part); #µÝ¹éµ÷ÓÃ
eft: 160px"> $part =3D \@temp;
}=

$expr =3D~ s/\( ((?>[^()]+) | (?R))* \)/tag/xg; #ԭʽÖÐ=
À¨ºÅÄÚÈÝÔÝʱÓÃ×Ö·û&ldquo ;tag”=CC=
æ´=FA
}
n-left: 80px"> my @node=3D ($expr =3D~ /\d+|tag/g); #´ÓËãʽÖ=
ÐÌáÈ¡Êý×Ö

my $i =3D 0;
foreach $node (@node) =
{
if ($node eq "tag"){ =
#tagÄÚÈÝ»¹Ô­³ÉÉú³ÉµÄÊ÷
=3D"margin-left: 160px"> $node =3D $part[$i];

$i++;
120px"> }
}
in-left: 80px"> my @op=3D($expr =3D~ /[+*()]/g); #´ÓËãʽÖÐ=
ÌáÈ¡Ëã·û

return basic (\@node, \@op);
}
=

>sub basic { #½«²»º¬À¨ºÅµÄËãʽת=B3=
ÉÊ=F7
my($a, $b, $i);

($a, $b) =3D @_;
n-left: 80px"> my @node =3D @$a;
my =
@op =3D @$b;
for($i =3D 0; $i < @=
op; $i++){ #ת»»³Ë·¨

if ($op[$i] eq "*"){
v style=3D"margin-left: 160px"> my $end =3D $i+1;
in-left: 160px"> while (defined($op[$end])&&($op[$end] eq "*=
")){

$end++ if $end < @op;
style=3D"margin-left: 160px"> }
=
my $t =3D $end - $i;
my @new =
=3D @node[$i..$end];

unshift (@new, "*");
iv style=3D"margin-left: 160px"> splice(@node, $i, $t+1, \@new); v style=3D"margin-left: 160px"> splice(@op, $i, $t,);
margin-left: 120px">
}
}
: 80px"> if (@op){
unshift (@node,=
"+");
}else{
yle=3D"margin-left: 120px">
@node=3D@{$node[0]};
}
tyle=3D"margin-left: 80px"> return @node;
0px">}

-left: 40px">
sub calculate{
my $a =3D $_[0]=
;
return $a if ref($a) ne "ARR=
AY";
my @tree =3D @$a; >

my $tree;
my $result =3D 0;
iv style=3D"margin-left: 80px"> if ($tree[0] eq "+"){
tyle=3D"margin-left: 120px"> shift @tree;
120px">
foreach $tree(@tree){
$result =
=3D $result + calculate($tree);
}<=
/div>
} elsif ($tree[0] eq "*"=
;){

$result =3D 1;
n-left: 120px"> shift @tree;
fore=
ach $tree(@tree){
$result =3D $re=
sult * calculate($tree);

}
>  } 
return $result; iv>
}
40px">}

1;



=


--20cf3071cc3633452204a1fd93c6--
--20cf3071cc3633452904a1fd93c8
Content-Type: application/octet-stream; name="08300720014-07.pl"
Content-Disposition: attachment; filename="08300720014-07.pl"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_gn1xmgo30

IyEvdXNyL2Jpbi9wZXJsIC13DQp1c2Ugc3RyaWN0Ow0KdXNlIERhdGE6OkR1 bXBlcjsNCg0KI7K7
vOyy6crkyOvL48q9yse38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YWxj
dWxhdGV7DQoJbXkgJGEgPSAkX1swXTsNCglyZXR1cm4gJGEgIGlmIHJlZigk YSkgbmUgIkFSUkFZ
IjsgDQogICAgbXkgQHRyZWUgPSBAJGE7DQoJbXkgJHRyZWU7DQoJbXkgJHJl c3VsdCA9IDA7DQoJ
aWYgKCR0cmVlWzBdIGVxICIrIil7DQoJCXNoaWZ0IEB0cmVlOw0KCQlmb3Jl YWNoICR0cmVlKEB0
cmVlKXsNCgkJCSRyZXN1bHQgPSAkcmVzdWx0ICsgY2FsY3VsYXRlKCR0cmVl KTsNCgkJfQ0KICAg
IH0gZWxzaWYgKCR0cmVlWzBdIGVxICIqIil7DQoJCSRyZXN1bHQgPSAxOw0K CQlzaGlmdCBAdHJl
ZTsNCgkJZm9yZWFjaCAkdHJlZShAdHJlZSl7DQoJCQkkcmVzdWx0ID0gJHJl c3VsdCAqIGNhbGN1
bGF0ZSgkdHJlZSk7DQoJCX0NCiAgICB9IGVsc2Ugew0KCQkkcmVzdWx0ID0g JHRyZWVbMF07DQoJ
fQ0KCXJldHVybiAkcmVzdWx0Ow0KfQ0KfQ0KMTs=

--20cf3071cc3633452904a1fd93c8
Content-Type: text/plain; charset=us-ascii

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

--20cf3071cc3633452904a1fd93c8--

Re: How to Generate An Expression to A LOL?

am 28.04.2011 19:16:25 von Uri Guttman

>>>>> "zs" == z sway writes:

zs> Hi, I've finished it using recursion.
zs> #!/usr/bin/perl -w

use warnings is better

zs> use strict;
zs> use Data::Dumper;

zs> while (1) {
zs> print "Enter the expression : ";
zs> chomp($_ =3D );
zs> last if not $_;
zs> my @tree =3D grow($_);

it is better to use a named variable than $_. $_ can cause issues as it
is a global (recent perl's can make it lexical but still a name is better).

zs> print Dumper(\@tree);
zs> my $result =3D calculate(\@tree);
zs> print "\nresult\=3D$result\n\n";

=3D does not need escaping in strings.

zs> my $answer =3D eval "$_";

that is very dangerous, allowing user input to be evaled. do you need to
do that?


zs> sub grow {
zs> my $expr =3D $_[0];

this is better:
my ( $expr ) =3D @_ ;

zs> my ($part, @part, $node);

don't declare vars until they are first used.

zs> if ($expr =3D~ /\(/){

you need to indent your code. it is very hard to read without it.=20

zs> @part =3D ($expr =3D~ / \( (?:(?>[^()]+) | (?R))* \) /xg); #æ=90=
取括号中

my @part =3D ...


zs> foreach $part (@part) {
zs> $part =3D~ s/^\(|\)$//g;
zs> my @temp =3D grow($part); #递归调用
zs> $part =3D \@temp;

no need for the @temp array. in fact if you name something temp, you
likely don't need it at all.

$part =3D [ grow($part) ] ;

zs> my $a =3D $_[0];

don't use $a (or $b) for your own variables. they are reserved for use
by the sort function. also they are bad names in general as they don't
tell the reader what they are for.

uri

--=20
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to Generate An Expression to A LOL?

am 29.04.2011 12:13:22 von z sway

--bcaec51ba14d7698e504a20becf1
Content-Type: text/plain; charset=GB2312
Content-Transfer-Encoding: quoted-printable

Hi, grow($part) returns an array, I don't know how to create a ref to arra=
y
in one step.
@$part =3D grow($part) doesn't work here.To my knowledge, it is used for
changing the array $part refers to, but $part is not a ref , it¡®s a st=
ring,
it can't refer anything until it's changed to a ref.
$part =3D \(grow($part)) doesn't work either, it creates refs to elements =
in
the array.

2011/4/29 Uri Guttman

> >>>>> "zs" == z sway writes:
>
> zs> foreach $part (@part) {
> zs> $part =3D~ s/^\(|\)$//g;
> zs> my @temp =3D grow($part); #µÝ¹éµ÷ÓÃ
> zs> $part =3D \@temp;
>
> no need for the @temp array. in fact if you name something temp, you
> likely don't need it at all.
>
> $part =3D [ grow($part) ] ;
>
> zs> my $a =3D $_[0];
>
> don't use $a (or $b) for your own variables. they are reserved for use
> by the sort function. also they are bad names in general as they don't
> tell the reader what they are for.
>
> uri
>
> --
> Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.co=
m--
> ----- Perl Code Review , Architecture, Development, Training, Support
> ------
> --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com------=
---
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

--bcaec51ba14d7698e504a20becf1--

Re: How to Generate An Expression to A LOL?

am 29.04.2011 13:57:24 von Rob Dixon

On 29/04/2011 11:13, z sway wrote:
>
> Hi, grow($part) returns an array, I don't know how to create a ref to =
array
> in one step.
>
> @$part =3D grow($part) doesn't work here.To my knowledge, it is used fo=
r
> changing the array $part refers to, but $part is not a ref , it¡®s =
a string,
> it can't refer anything until it's changed to a ref.
>
> $part =3D \(grow($part)) doesn't work either, it creates refs to elemen=
ts in
> the array.

Assigning to @$part would work fine if $part didn't already hold a
value. As it is, you must either use a separate variable:

my $part2;
@$part2 =3D grow($part);

or copy the return from the subroutine into an anonynous array:

$part =3D [ grow($part) ];

But I think it would be best if the subroutine just returned a
reference to an array in the first place.

Rob

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to Generate An Expression to A LOL?

am 29.04.2011 18:23:52 von Mike McClain

On Thu, Apr 28, 2011 at 01:16:25PM -0400, Uri Guttman wrote:
> >>>>> "zs" == z sway writes:

>
> zs> sub grow {
> zs> my $expr = $_[0];
>
> this is better:
> my ( $expr ) = @_ ;

Better how?

Thanks,
Mike
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to Generate An Expression to A LOL?

am 29.04.2011 23:55:18 von Uri Guttman

>>>>> "MM" == Mike McClain writes:

MM> On Thu, Apr 28, 2011 at 01:16:25PM -0400, Uri Guttman wrote:
>> >>>>> "zs" == z sway writes:
MM>
>>
zs> sub grow {
zs> my $expr = $_[0];
>>
>> this is better:
>> my ( $expr ) = @_ ;

MM> Better how?

* it doesn't require indexing so it is faster.
* it is the most standard way to get sub args.
* it easily allows for adding args by just putting another var
in the ().
* you can assign the rest of the args to an array or hash (after the
scalar args).

***** oh, it is also 1 char (not counting white space) shorter!

at least 4+ good reasons there! and not one downside reason i can think
of.

and those are also good reasons not to use shift on @_ except when it
really is needed.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to Generate An Expression to A LOL?

am 01.05.2011 18:32:14 von Mike McClain

On Fri, Apr 29, 2011 at 05:55:18PM -0400, Uri Guttman wrote:
> >>>>> "MM" == Mike McClain writes:

> zs> my $expr = $_[0];
> >>
> >> this is better:
> >> my ( $expr ) = @_ ;
>
> MM> Better how?
>
> * it doesn't require indexing so it is faster.
> * it is the most standard way to get sub args.
> * it easily allows for adding args by just putting another var
> in the ().
> * you can assign the rest of the args to an array or hash (after the
> scalar args).
>
> ***** oh, it is also 1 char (not counting white space) shorter!
>
> at least 4+ good reasons there! and not one downside reason i can think
> of.
>
> and those are also good reasons not to use shift on @_ except when it
> really is needed.
> uri

Those look like good reasons to me.
Thanks,
Mike
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/