|
Joined: Mar 2000
Posts: 445
Member
|
Member
Joined: Mar 2000
Posts: 445 |
I could this example from that tutorial of PERL BY EXAMPLE, and I got the following code: The program would end up producing: 39My 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 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.
|
|
|
|
Joined: Sep 2000
Posts: 32
Member
|
Member
Joined: Sep 2000
Posts: 32 |
Actually, the problem isn't in the brackets. Look at: 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
Joined: Mar 2000
Posts: 445 |
Yes but my true question would be, what exactly do the brackets here: other than tell PERl to execute this part before anything else? Thanks for the reply though SonnyBlack. ![[Linked Image]](https://ubbdev.com/ubb/smilies/smile.gif) ------------------ Quality Pages designing the world professional web pages for affordable prices.
|
|
|
|
Joined: Sep 2000
Posts: 32
Member
|
Member
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
Joined: Aug 2000
Posts: 3,590 |
Correct I believe. That is a tenary thingie ![[Linked Image]](https://ubbdev.com/ubb/smilies/biggrin.gif) Mark may be able to provide a better description.. ------------------ Da Wannabe Cannuck
|
|
|
|
Joined: Mar 2000
Posts: 445
Member
|
Member
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
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
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]](https://ubbdev.com/ubb/smilies/biggrin.gif) ) 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]](https://ubbdev.com/ubb/smilies/smile.gif) 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
Joined: Mar 2000
Posts: 445 |
Wait a sec, take a look at the following code involving brackets again. 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
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]](https://ubbdev.com/ubb/smilies/smile.gif)
|
|
|
|
Joined: Mar 2000
Posts: 445
Member
|
Member
Joined: Mar 2000
Posts: 445 |
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
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
Joined: Mar 2000
Posts: 445 |
Ok thanks Twenty, I'll go check that site out now... ![[Linked Image]](https://ubbdev.com/ubb/smilies/smile.gif) ------------------ Quality Pages designing the world professional web pages for affordable prices.
|
|
|
|
Joined: Sep 2000
Posts: 32
Member
|
Member
Joined: Sep 2000
Posts: 32 |
I guess I could have worded that better ![[Linked Image]](https://ubbdev.com/ubb/smilies/smile.gif) 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. Hey, no problem, Pyros. I never really put that much thought into it before, so this helps me out too This message has been edited by SonnyBlack on September 25, 2000 at 07:02 PM
|
|
|
|
Joined: Mar 2000
Posts: 445
Member
|
Member
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
Joined: Sep 2000
Posts: 32 |
No problem! Glad I finally made some sense ![[Linked Image]](https://ubbdev.com/ubb/smilies/biggrin.gif) ------------------ goodfellasweb.com
|
|
|
|
Joined: Aug 2000
Posts: 3,590
Moderator
|
Moderator
Joined: Aug 2000
Posts: 3,590 |
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]](https://ubbdev.com/ubb/smilies/smile.gif) ) Remember the order of operations( Please Excuse my dear aunt Sally ![[Linked Image]](https://ubbdev.com/ubb/smilies/biggrin.gif) ). 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]](https://ubbdev.com/ubb/smilies/biggrin.gif) ------------------ Da Wannabe Cannuck
|
|
|
|
Joined: Mar 2000
Posts: 445
Member
|
Member
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
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
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]](https://ubbdev.com/ubb/smilies/biggrin.gif) Hope that explains a bit. ------------------ goodfellasweb.com
|
|
|
|
Joined: Aug 2000
Posts: 3,590
Moderator
|
Moderator
Joined: Aug 2000
Posts: 3,590 |
My bad. I knew that >:0) 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]](https://ubbdev.com/ubb/smilies/biggrin.gif) ------------------ Da Wannabe Cannuck
|
|
|
|
Joined: Sep 2000
Posts: 32
Member
|
Member
Joined: Sep 2000
Posts: 32 |
I figured you did You're telling me! But, it only gets worse from there ![[Linked Image]](https://ubbdev.com/ubb/smilies/biggrin.gif) ------------------ goodfellasweb.com
|
|
|
|
Joined: Mar 2000
Posts: 445
Member
|
Member
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.
|
|
|
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.
|
|
Posts: 1,157
Joined: July 2001
|
|
Forums63
Topics37,575
Posts293,931
Members13,823
|
Most Online6,139 Sep 21st, 2024
|
|
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
|
|
|
|