Which Programming Language is this formula - programming-languages

Can someone tell me what language(s) the following formula is formatted for?
IF(($A) > $B,$B, IF(($A) < $C,$C,($A)))
I work in PHP and I can interpret what it's intended to do
(if $A > $B then $B, if $A < $C then $C, ELSE $A)
just not the language that would except it.

This looks like something that came out of Excel

in php it should be somthink like this...
<?php
if($A > $B){$B;}
elseif($A < $C){$C;}
else{$A;}
?>

Just following up to close this off.
First, there was a slight typo with an unecessary set of parenthesis in the example so ($A) could've just been $A in both cases.
In any event, I discovered that this was used for something that was originally written in "ABAP".

Related

Bash: Change String variable to the first word that starts with

I am trying to isolate the first interface that starts with enp
Example:
foo = "bar0s0 foo0s8 enp0s0"
startword = "enp"
result:
$foo = enp0s0
WORDS="bar0s0 foo0s8 enp0s0"
START="enp"
for w in $WORDS; do
case $w in
${START}*)
echo $w
break # break out of the for loop for first instance
;;
esac
done
foo=$(echo $foo|sed -e "s/^.*\(${startword}[^ ]*\).*$/\1/")
Here's a solution using awk, modify to suit your needs:
echo "bar0s0 foo0s8 enp888 enp0s0" | awk ' { for(i = 1; i <= NF; i++) if($i ~ "enp") { print $i; break;} } '
You tagged your question as bash, so I'll provide a simple, portable solution.
Since you provide no context to your question it's hard to determine what the best approach might be but in most cases, this should do the trick.
bash>bash --version
GNU bash, version 3.2.52(2)-release (i586-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
bash>foo="bar0s0 foo0s8 enp0s0 random word123"
bash>startword="enp"
bash>index=$(expr index "$foo" $startword)
bash>echo ${foo:$(($index -1)):6}
enp0s0
A couple of caveats:
This assumes you are in fact working with bash and your tools are somewhat limited.
This also assumes that interface names are always 6 characters.
This approach uses sub-string extraction, if you want to read up on it take a look at http://www.tldp.org/LDP/abs/html/string-manipulation.html

How to look for a value in a hash?

I have a hash like
%has = ('TCA' =>'S', 'TTC'=>'N'....)
and a
$string = 'TCA'
I want to look for my $string in the %has and if it exist, print the value using perl like this:
TCA, S
How can I do that? Thank you so much!
Check if the value exists.
if (exists $has{$string}) {
printf "%s, %s\n", $string, $has{$string};
}
Keep in mind that this is case sensitive.
You should probably read up on the various Perl functions in perldoc perlfunc

CSV Bash loop Issue with Variables

I have a csv file which im trying to loop through with the purpose to find out if an User Input is found inside the csv data. I wrote the following code which sometimes works and others doesn't. It always stops working when I try to compare to a 2+ digit number. It works OK for numbers 1 through 9, but once u enter lets say 56 , or 99 or 100, it stops working.
the csv data is comma delimited, i have about 300 lines they are just like this.
1,John Doe,Calculus I,5.0
1,John Doe,Calculus II,4.3
1,John Doe,Physics II,3.5
2,Mary Poppins,Calculus I,3.7
2,Mary Poppins,Calculus II,4.7
2,Mary Poppins,Physics I,3.7
Data is just like that, all the way down until ID #100 for a total of 300 lines. Both the sh file and csv file are in the same folder, I'm using a fresh installation of Ubuntu 12.04.3, using gedit as the text editor.
I tried Echoing the variables ID and inside the IF conditionals but it doesn't behave the way it should when testing for the same value. Could someone point me out in the right direction. Thanks
Here's the code:
#s!/bin/bash
echo "enter your user ID";
read user;
INPUT_FILE=notas.csv
while IFS="," read r- ID name asignature final;
do
if [$ID = $user]; then
userType=1;
else
userType=2;
fi
done < notas.csv
Well, your code as written has a few issues.
You have r- instead of -r on the read line - I assume that's a typo not present in your actual code or you wouldn't get very far.
Similarly, you need space around the [...] brackets: [$ID is a syntax error.
You need to quote the parameter expansions in your if clause, and/or switch bracket types. You probably make it a numeric comparison as #imp25 suggested, which I would do by using ((...)).
You probably don't want to set userType to 2 in an else clause, because that will set it to 2 for everyone except whoever is listed last in the file (ID 100, presumably). You want to set it to 2 first, outside the loop. Then, inside the loop when you find a match, set it to 1 and break out of the loop:
userType=2
while IFS=, read -r ID name asignature final; do
if (( $ID == $user )); then
userType=1;
break
fi
done < notas.csv
You could also just use shell tools like awk:
userType=$(awk -F, -vtype=2 '($1=="'"$user"'") {type=1}; END {print type}' notas.csv)
or grep:
grep -q "^$user," notas.csv
userType=$(( $? + 1 ))
etc.
You should quote your variables in the if test statement. You should also perform a numeric test -eq rather than a string comparison =. So your if statement should look like:
if [[ "$ID" -eq "$user" ]]

how to reassign page number in DJVU file?

Is there a simple way to renumber pages in a DJVU file?
Example:
I've got a book, and page 1 is actually the cover, and so on, such that the actual page 1 of the book is at, say, 10 in the document; what I'd like to do is call them something like C,i,ii,..., and then 1,2,...
I know it can be done, since I've got other books in this format with this numbering, and I'd like to do it on Linux, better if via terminal.
Thanks,
N
to renumber
for (( i=11; i<=823; i++ ))
do
djvused new.djvu -e "select $i; set-page-title $((i-10)); save"
done
to rename
djvused new.djvu -e 'select 2; set-page-title ii; save'
It's slightly offtopic. Just in case someone needs to do the same thing on Windows using PowerShell:
for($i=11; $i -le 823; $i++){
$j=($i-10)
$args = "new.djvu -e ""select $i; set-page-title $j; save"""
write-host "djvused $args"
start-process djvused $args -NoNewWindow -wait
}

csh inline math

I need to do some integer math in csh (and no, other shells are not an option, nor is bc, nor is perl, nor is python, period).
In bash my task would look like
seq 1 1 10 > m.txt #supplied from elsewhere
a=2 #supplied from elsewhere
b=3 #supplied from elsewhere
head -n $[$a*$b] m.txt # the line in question
then the question is Is there an expression in csh that computes $[$a*$b] inline?
I know that I can do # c = $a * $b in csh, but that's not inline. I did a little bit of googling and searching SO, but no success so far, so any help is greatly appreciated!
Are your use of square-brackets meant to indicate an array notation or matrix math? csh has no such built-in features.
ELSE, if you mean like bash $(($a * $b)), you can use csh cmd-substitution with backquotes to give you
head -n `expr $a \* $b` m.txt
Note that if your goal was to avoid spawning extra processes, this does not meet your goal, but it is "in-line"
Edit I see I mistyped as $( $a * $b ), see inline correction above.
IHTH.
Without using something outside of the shell, no.
The usual culprit for math from old school shell scripts is expr:
head -n `expr $a \* $b` m.txt
but if that's just as verboten as bc et al, then you're out of luck. Period.
Yes, but it's not pretty:
% seq 1 1 10 > m.txt
% set a = 2
% set b = 3
% head -n `# tmp = $a * $b ; echo $tmp ; unset tmp` m.txt
1
2
3
4
5
6
Note that this will clobber $tmp if you happen to have a variable of that name, so choose a unique name.
(Though I wonder why bc, perl, and python are not an option.)

Resources