LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-05-2015, 07:37 PM   #1
hotspur919
LQ Newbie
 
Registered: Jan 2011
Location: Glasgow, UK
Distribution: RHEL, mint
Posts: 25

Rep: Reputation: 0
Arithmetic operator input constraints


Hi,

This should be an easy one but its confusing me unfortunately.

Please can someone tell me what the following constraint is saying?:

1≤N<100

In particular, I don't understand what the 1 <= N means. Does it simply mean N is less than or equal to 1?


Thanks in advance...
 
Old 09-05-2015, 07:47 PM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
N is between 1 and 100 including 1 but not 100. If these are integers you could say that N is between 1 and 99 including both.
 
Old 09-06-2015, 11:09 AM   #3
hotspur919
LQ Newbie
 
Registered: Jan 2011
Location: Glasgow, UK
Distribution: RHEL, mint
Posts: 25

Original Poster
Rep: Reputation: 0
Thanks for the reply, Thats kind of what I was thinking, however I am still a little confused when trying to make this syntactically correct in Bash. Given the following:

1T,A,B1000

How would I express this constraint using bash (I want to start from 1)?

it seems I have it all wrong:

for $((1<=T;A;B<=1000; 1++))
do
something
done

or is it:

for ((T >= 1; T<= 1000; A >= 1; A <= 1000; B >=1; B <=1000; 1++ ))
do
something
done



Thanks
 
Old 09-06-2015, 11:46 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
So you don't know what 1T,A,B1000 is supposed to mean? Well, where did it come from?
 
Old 09-06-2015, 01:15 PM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
just one additional comment: 1++ is illegal, you need to use a variable not a number as loop index.
 
1 members found this post helpful.
Old 09-06-2015, 03:03 PM   #6
hotspur919
LQ Newbie
 
Registered: Jan 2011
Location: Glasgow, UK
Distribution: RHEL, mint
Posts: 25

Original Poster
Rep: Reputation: 0
Sorry guys, looks like the characters I copied did not make it into my post. This is what the constraint was supposed to look like:

1<=T,A,B<=1000

I now understand what this means but do not know how to express this using a bash arithmetic operation. My samples above don't seem to cut it.

So I want to loop through pairs of integers (A and B, separated by a space) T times. Each time adding the two together.

Thanks in advance and apologies for the mistakes above.
 
Old 09-06-2015, 11:15 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
forget bash, it requires some actual programming language:

Code:
for (T=1, A=1, B=1; 
     T<=100 && A<=100 && B<=100;
     B<100? ++B: (B=1, A<100? ++A: (A=1, ++T))
    ) {
...
}
It will be *very* slow though

Last edited by NevemTeve; 09-07-2015 at 07:45 AM.
 
1 members found this post helpful.
Old 09-07-2015, 08:03 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by NevemTeve View Post
forget bash, it requires some actual programming language:
Actually you could do the same in bash:

Code:
for ((T=1, A=1, B=1; 
      T<=100 && A<=100 && B<=100;
      B<100? ++B: (B=1, A<100? ++A: (A=1, ++T))))
do
    ...
done
Even slower of course.
 
1 members found this post helpful.
Old 09-07-2015, 08:06 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Two-and-a-half suggestions:

(1) Use a real programming language. In Unix/Linux you have half-a-dozen (at least) to choose from, and many more are available at no cost. Through the magic of the #!shebang mechanism, any of these can be used to write "a shell command." The Korn shell is the only one which attempted to include a full-featured programming language as its shell-scripting tool.

(2) When you go about writing the thing, don't economize on source-code characters. For example, I think that the example above is unnecessarily terse. There's no advantage whatsoever in creating source code that a human must "puzzle out." There's also no advantage in saving microseconds. Especially in a "real" language, your source code will be efficiently translated into the actual instructions that the computer will execute.

(2-1/2) Liberally include comments in your code. Comments are free. They don't slow-down execution at all, even as they vastly increase readability and understanding.

Last edited by sundialsvcs; 09-07-2015 at 08:08 AM.
 
1 members found this post helpful.
Old 09-08-2015, 04:42 PM   #10
hotspur919
LQ Newbie
 
Registered: Jan 2011
Location: Glasgow, UK
Distribution: RHEL, mint
Posts: 25

Original Poster
Rep: Reputation: 0
Thanks guys, for the great feedback. I now think I have a grasp (however faint) on setting these up. It is kind of how I thought. i.e. there is no short cut to expressing these constraints correctly.
I have spent a little time trying to learn python (which I guess is a real language), but have since put it on the back burner to try and improve my basic shell scripting..... It may take a while :-P

Cheers! :-)
 
Old 09-10-2015, 10:05 PM   #11
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by hotspur919 View Post
Sorry guys, looks like the characters I copied did not make it into my post. This is what the constraint was supposed to look like:

1<=T,A,B<=1000

I now understand what this means but do not know how to express this using a bash arithmetic operation. My samples above don't seem to cut it.

So I want to loop through pairs of integers (A and B, separated by a space) T times. Each time adding the two together.

Thanks in advance and apologies for the mistakes above.
Could you please post, step by step what you want to happen ? Give an example using small values of A, B, T. I'm sure this can easily be done in awk.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH/NAGIOS : Invalid Arithmetic operator tkinsella Programming 2 08-26-2014 03:40 PM
[SOLVED] Strange behaviour in bash comparison when variable contains arithmetic operator michael.wegemer Programming 3 10-19-2010 05:22 AM
swaret error "invalid arithmetic operator" Blackmeth Slackware 5 04-25-2007 11:14 AM
c++ overloading input operator true_atlantis Programming 4 02-25-2004 07:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration