trying to learn perl, have a question - DFWstangs Forums
 
LinkBack Thread Tools Display Modes
post #1 of 15 (permalink) Old 02-24-2004, 03:11 PM Thread Starter
 
Join Date: Nov 2002
Posts: 5,831
Question trying to learn perl, have a question

Quote:
You can also use special operators like ++, --, +=, -=, /= and *=. These manipulate a scalar's value without needing two elements in an equation. Some people like them, some don't. I like the fact that they can make code clearer.

$a = 5;
$a++; # $a is now 6; we added 1 to it.
$a += 10; # Now it's 16; we added 10.
$a /= 2; # And divided it by 2, so it's 8.
"$a++;" why does that add 1 to the variable???
trey85stang is offline  
Sponsored Links
Advertisement
 
post #2 of 15 (permalink) Old 02-24-2004, 05:35 PM
\(_o)/
 
AbecX's Avatar
 
Join Date: Nov 2001
Location: Las Colinas
Posts: 25,373
You can use a++ or variable++ in a loop such as one that pulls data from a sql table, if a = 10 then you can add an addtional field for a check box, for instance, on http://www.dfwls1.com/fastest/
AbecX is offline  
post #3 of 15 (permalink) Old 02-24-2004, 06:22 PM
No Cerveza... No Trabajo
 
01WhiteCobra's Avatar
 
Join Date: Jun 2002
Location: Where's my beer?
Posts: 21,924
Re: trying to learn perl, have a question

Quote:
Originally posted by trey85stang
"$a++;" why does that add 1 to the variable???
++ is the increment operator. It is short hand for variable = variable + 1;

$a--;

-- is the decrement operator. It is short hand for variable = variable - 1;
01WhiteCobra is offline  
 
post #4 of 15 (permalink) Old 02-24-2004, 07:21 PM Thread Starter
 
Join Date: Nov 2002
Posts: 5,831
Quote:
Originally posted by AbecX
You can use a++ or variable++ in a loop such as one that pulls data from a sql table, if a = 10 then you can add an addtional field for a check box, for instance, on http://www.dfwls1.com/fastest/
I think that went a little too far over my head... Im still on hello world stuff, just trying to get a grasp on some diffren't varibles right now

I think the way 01 described explained what i needed, but does the following work:

$a = 5

$a+++ = 7

is the above true? or would i have to make it?

$a + 2 = 7?
trey85stang is offline  
post #5 of 15 (permalink) Old 02-24-2004, 09:17 PM
Timmay
 
Tiny Tim's Avatar
 
Join Date: Aug 2001
Posts: 4,898
Quote:
Originally posted by trey85stang
$a + 2 = 7?
yes

or

$a+= 2. I am not sure if that syntax is correct in Perl but In know it works in C.

$a+=2 is the same as $a = $a + 2
Tiny Tim is offline  
post #6 of 15 (permalink) Old 02-24-2004, 09:24 PM
No Cerveza... No Trabajo
 
01WhiteCobra's Avatar
 
Join Date: Jun 2002
Location: Where's my beer?
Posts: 21,924
Quote:
Originally posted by trey85stang
I think that went a little too far over my head... Im still on hello world stuff, just trying to get a grasp on some diffren't varibles right now

I think the way 01 described explained what i needed, but does the following work:

$a = 5

$a+++ = 7

is the above true? or would i have to make it?

$a + 2 = 7?
No.

The increment operator is ++ and that it all. There isn't a +++ or a ++++.

$a = 5;
$a++;
print "$a";

Output:
6

If you want to add 2 to $a :

$a += 2;

-or, you can shorthand

$a = $a + 2;
01WhiteCobra is offline  
post #7 of 15 (permalink) Old 02-25-2004, 10:06 AM Thread Starter
 
Join Date: Nov 2002
Posts: 5,831
ok, one last question and I'll let the thread go, if this is too early to ask.. then let me know. I will keep on studying...

Why doesn't this work???

#!/usr/local/bin/perl
$a = 5
print "this sucks and why is there only $a++; beers left\n";


in the above, why doesnt $a++; return 6?

Why does $a++; have to be made a variable to work??
trey85stang is offline  
post #8 of 15 (permalink) Old 02-25-2004, 11:16 AM
\(_o)/
 
AbecX's Avatar
 
Join Date: Nov 2001
Location: Las Colinas
Posts: 25,373
You cant print functions like that, unless you want your code to not work

#!/usr/local/bin/perl
$a = 50;
for($x=0;$x<=49;$x++){
print "this sucks and why is there only $a beers left\n";
$a--;
};
print "Crap, no more beer left \\ \n";
AbecX is offline  
post #9 of 15 (permalink) Old 02-25-2004, 11:23 AM Thread Starter
 
Join Date: Nov 2002
Posts: 5,831
Quote:
Originally posted by AbecX
You cant print functions like that, unless you want your code to not work
lol.. it doesnt do much right now anyways...
trey85stang is offline  
post #10 of 15 (permalink) Old 02-25-2004, 01:15 PM
Time Served
 
Join Date: Jan 2002
Location: Bedford, TX
Posts: 761
a++ would do the addition after it has passed the function. This is called postfix.

++a would do it before, called prefix.

Not sure about how perl works but this is how it is in C++.
Sami is offline  
post #11 of 15 (permalink) Old 02-25-2004, 02:33 PM Thread Starter
 
Join Date: Nov 2002
Posts: 5,831
Thumbs up

Im going to pick up the o'reily's book this weekend., im liking this stuff.. its fun!
trey85stang is offline  
post #12 of 15 (permalink) Old 02-25-2004, 02:39 PM
Time Served
 
Join Date: Jan 2002
Location: Bedford, TX
Posts: 761
Quote:
Originally posted by trey85stang
Im going to pick up the o'reily's book this weekend., im liking this stuff.. its fun!
Does it have to be perl? I would recommend C++ as the language to learn first. It's easy to move to other languages from there. Java is ok but not as versatile you would want it to be.
Sami is offline  
post #13 of 15 (permalink) Old 02-25-2004, 03:21 PM Thread Starter
 
Join Date: Nov 2002
Posts: 5,831
Quote:
Originally posted by Sami
Does it have to be perl? I would recommend C++ as the language to learn first. It's easy to move to other languages from there. Java is ok but not as versatile you would want it to be.
Doesnt matter, can you recommend a good c++ book?? I want to be able to work from linux... so as long as the book doesnt refer to MS C++ i dont mind. what it is
trey85stang is offline  
post #14 of 15 (permalink) Old 02-25-2004, 03:25 PM
Time Served
 
Join Date: Jan 2002
Location: Bedford, TX
Posts: 761
Quote:
Originally posted by trey85stang
Doesnt matter, can you recommend a good c++ book?? I want to be able to work from linux... so as long as the book doesnt refer to MS C++ i dont mind. what it is
For Linux...

http://www.amazon.com/exec/obidos/tg...glance&s=books

I haven't read this one but the last time I read Jesse Liberty's book it was very well written and easy to follow.

http://cplus.about.com/library/weekly/aatp032802.htm

Last edited by Sami; 02-25-2004 at 03:32 PM.
Sami is offline  
post #15 of 15 (permalink) Old 02-25-2004, 04:55 PM
\(_o)/
 
AbecX's Avatar
 
Join Date: Nov 2001
Location: Las Colinas
Posts: 25,373
We host randal schwartz box
AbecX is offline  
Sponsored Links
Advertisement
 
Reply

Bookmarks

Quick Reply
Message:
Options

Register Now



In order to be able to post messages on the DFWstangs Forums forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.

User Name:
Password
Please enter a password for your user account. Note that passwords are case-sensitive.

Password:


Confirm Password:
Email Address
Please enter a valid email address for yourself.

Email Address:
OR

Log-in










Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page
Display Modes
Linear Mode Linear Mode



Posting Rules  
You may post new threads
You may 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 On
Trackbacks are On
Pingbacks are On
Refbacks are On

 
For the best viewing experience please update your browser to Google Chrome