Previous Thread
Next Thread
Print Thread
Rate Thread
#101298 09/24/2000 6:55 PM
Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
I could this example from that tutorial of PERL BY EXAMPLE, and I got the following code:
Code
code:

The program would end up producing: 39

My question is why would it produce 39?
I know that the brackets are the highest in precendence, but what do they actually do?
I know if you took out the brackets around
Code
code:
That you would get the output as 1, and that I understand...Just the brackets I don't really get?

------------------
Quality Pages designing the world professional web pages for affordable prices.

Sponsored Links
Joined: Sep 2000
Posts: 32
Member
Member
Offline
Joined: Sep 2000
Posts: 32
Actually, the problem isn't in the brackets. Look at:
Code
code:
Your first thought is that it decrements the value of $firstVar before it adds it, but actually it does so after. If you change it to --$firstVar it will work like you would expect, and print 38.

Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
Yes but my true question would be, what exactly do the brackets here:

Code
code:
other than tell PERl to execute this part before anything else?
Thanks for the reply though SonnyBlack.
[Linked Image]

------------------
Quality Pages designing the world professional web pages for affordable prices.

Joined: Sep 2000
Posts: 32
Member
Member
Offline
Joined: Sep 2000
Posts: 32
Pyros, from what I understand, the stuff in the brackets is like an abbreviated if..then..else statement. It would read: if $secondVar, value is 1, otherwise it is 0. I'm not sure but I think it evaluates $secondVar on a true false basis, but I think you could specify otherwise.

Joined: Aug 2000
Posts: 3,590
Moderator
Moderator
Offline
Joined: Aug 2000
Posts: 3,590
Quote
quote:

Correct I believe. That is a tenary thingie [Linked Image] Mark may be able to provide a better description..


------------------
Da Wannabe Cannuck

Sponsored Links
Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
Yeah that's what it's like. It's inside a Terrnary Operator, so I think it's saying something like if $secondVar is true, then 1, else is 0..
But I'm just not too sure why the brackets are there other than for evaulity that code first...
Thanks for everything SonnyBlack!

[edit=yeah thanks atom, I just say you posted a minute before..hehe]

------------------
Quality Pages designing the world professional web pages for affordable prices.

This message has been edited by Pyros on September 25, 2000 at 05:27 PM

Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
OH yeah also, I was wondering by any chance if any of you guys had a list of ASCII tables?
Cause as far as I'm concerned 65 is equal to A right? I just wanted to the others....
Not too urgent though...
Thanks again....

------------------
Quality Pages designing the world professional web pages for affordable prices.

Joined: Sep 2000
Posts: 32
Member
Member
Offline
Joined: Sep 2000
Posts: 32
Ternary refers to the number of operands, but it is a conditional operator. (I had to look that up [Linked Image]) But as far as the brackets go, I guess they are there to keep things seperate, because you can have much more than just a simple condition there.

Pyros, I used to have an ascii table but who knows where it is now [Linked Image] Glad to help, BTW

This message has been edited by SonnyBlack on September 25, 2000 at 06:19 PM

Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
Wait a sec, take a look at the following code involving brackets again.

Code
code:

The program produces the following output:
-16
-16
16

My question is, the 1st and 3rd are almost alike except for the brackets. What exactly are the brackets doing there? Are they making it go from a negative to a postivte? ALl I know is that it's telling PERL to evaulatue the stuff inside the brackets first!
Thanks again Sonny...

------------------
Quality Pages designing the world professional web pages for affordable prices.

Joined: Sep 2000
Posts: 32
Member
Member
Offline
Joined: Sep 2000
Posts: 32
$firstVar = -2 ** 4;
$secondVar = -(2 ** 4);

These two are the same. I think the brackets dont matter because "**" is evaluated first.

$thirdVar = (-2) ** 4;

Here, the sign change is made before the power is applied. It does the same thing on my graphing calculator... jeez I made a lot of errors that way [Linked Image]

Sponsored Links
Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
Quote
quote:
FIrst off, yeah I do think the brackets matter, because if you make a list on the Order of Precedence, the brackets would definitley be at tge top, because they have a precedence of 22 (if I'm not mistaken).

The brackets make a sign change, can you expalin that in a bit more detail.
Sonny thank you for putting up with me, I know it's hard, but I really appreciate the help you're giving me....

Pyros


------------------
Quality Pages designing the world professional web pages for affordable prices.

Joined: Sep 2000
Posts: 14
Junior Member
Junior Member
Offline
Joined: Sep 2000
Posts: 14
I use EditPlus for all my text/html/perl/java scripting needs. It is basically notepad with lots of features. I would never use anything else except notepad until I found EditPlus.
It is customizeable down to the last drop. I wouldn't recommend anything except this.
btw it has an ascii chart.
http://www.editplus.com/
Enjoy =)

------------------
-Twenty7
http://www.englishhouse.com/twenty7/

That game called Infantry
http://infantry.nmebase.com/

This message has been edited by Twenty7 on September 26, 2000 at 12:23 AM

Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
Ok thanks Twenty, I'll go check that site out now...
[Linked Image]

------------------
Quality Pages designing the world professional web pages for affordable prices.

Joined: Sep 2000
Posts: 32
Member
Member
Offline
Joined: Sep 2000
Posts: 32
I guess I could have worded that better [Linked Image] Let me try again.

$firstVar = -2 ** 4;
$secondVar = -(2 ** 4);

In both cases, 2 is taken to the 4th (not -2), and then the number is made negative. In these cases, the brackets dont matter only because the result is the same. Edit: remember "**" has a higher precedence than "-"

$thirdVar = (-2) ** 4;

In this case, it does what you would expect, returning a positive result. That is because the minus sign is already in the brackets, and it has to change it to a negative value.

Quote
quote:
Hey, no problem, Pyros. I never really put that much thought into it before, so this helps me out too [Linked Image]

This message has been edited by SonnyBlack on September 25, 2000 at 07:02 PM

Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
Sonny, thank you so much. I fully understand what you have just said to me.
Thank you!

Pyros

------------------
Quality Pages designing the world professional web pages for affordable prices.

Joined: Sep 2000
Posts: 32
Member
Member
Offline
Joined: Sep 2000
Posts: 32
Quote
quote:
No problem! Glad I finally made some sense [Linked Image]

------------------
goodfellasweb.com

Joined: Aug 2000
Posts: 3,590
Moderator
Moderator
Offline
Joined: Aug 2000
Posts: 3,590
Code
quote:</font><HR>Originally posted by Pyros:
Wait a sec, take a look at the following code involving brackets again.

<BLOCKQUOTE><font size="1" face="Verdana, Arial">code:

The program produces the following output:
-16
-16
16

My question is, the 1st and 3rd are almost alike except for the brackets. What exactly are the brackets doing there? Are they making it go from a negative to a postivte?


Let's take this back to math class >[Linked Image]) Remember the order of operations(Please Excuse my dear aunt Sally [Linked Image]). So for the first one the power is first: The computer things 2 ** 4 before the - is evaluated. For the second, it is doing the exact same. No need for the paren's, since the exponent is already evaluated first. For the last one, what is in the paren's is always evaluated first. That means the number being taken to the power is -2. When taking a negative number to a power, it will always be positive.

Speaking of math, does anybody know of a CGI script that would handle imaginary numbers(what is returned when taking the square root of a negative number) I would like to see the way the computer has to think to do that [Linked Image]



------------------
Da Wannabe Cannuck

Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
OK thanks atom, see you're saying if I have a single negative number inside the parens, they will always be changed into postives?

------------------
Quality Pages designing the world professional web pages for affordable prices.

Joined: Aug 2000
Posts: 3,590
Moderator
Moderator
Offline
Joined: Aug 2000
Posts: 3,590
Yes, whenever you take square, or take a number to any power for that matter) it will always be positive.

------------------
Da Wannabe Cannuck

Joined: Sep 2000
Posts: 32
Member
Member
Offline
Joined: Sep 2000
Posts: 32
Actually, atom, that only works for even exponents. If you take a negative number to an odd power, it will still be negative. Ex: (-2) ** 3 = -8 ... (-2)(-2)(-2) = -8

Oh yes, and you should only use integers as exponents. Otherwise, if you take (-2) ** 3.5 you will get a non-real answer. It will do that for all decimals that are odd (after the decimal place, that is). If you dont know why it does that, is because 3.5 = 7/2. That means you are taking (-2 ** 7) which results in a negative number, and then taking the square root of it. Jeez, enough math lesson for now [Linked Image] Hope that explains a bit.

------------------
goodfellasweb.com

Joined: Aug 2000
Posts: 3,590
Moderator
Moderator
Offline
Joined: Aug 2000
Posts: 3,590
Quote
quote:
My bad. I knew that >:0)

Quote
quote:
And we all love non-real answers >:0) Imanginary numbers -- The devil's gift to math, hehe. Though they aren't complicated, they just don't make sense [Linked Image]



------------------
Da Wannabe Cannuck

Joined: Sep 2000
Posts: 32
Member
Member
Offline
Joined: Sep 2000
Posts: 32
Quote
quote:
I figured you did [Linked Image]

Quote
quote:
You're telling me! But, it only gets worse from there [Linked Image]



------------------
goodfellasweb.com

Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
Ok, thanks you Sonny and Atom for everything. You guys really did explain this well. Thank you very much.

Pyros

------------------
Quality Pages designing the world professional web pages for affordable prices.


Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
Bill B
Bill B
Issaquah, WA
Posts: 87
Joined: December 2001
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20221218)