Operating System - OpenVMS
1828623 Members
1345 Online
109983 Solutions
New Discussion

Re: ENTER A STRING AND RETURN AN INT

 
Martin Devek
New Member

ENTER A STRING AND RETURN AN INT

I want to know how can I enter 12 strings:
1.- "C"
2.- "C#"
3.- "D"
4.- "D#"
5.- "E"
6.- "F"
7.- "F#"
8.- "G"
9.- "G#"
10.- "A"
11.- "A#"
12.- "B"

And return a number for each one:
if it is "C" returns 0.
If it is "C#" returns 1.
If it is "D" returns 2.

and so on...

Could you help me?
17 REPLIES 17
Arch_Muthiah
Honored Contributor

Re: ENTER A STRING AND RETURN AN INT

Martin,
if you want it to be done inside DCL,
You can use f$element lexical function

I have pasted one example of using f$element lexical ...., you can go thru this and modify as per your rek.

months="JAN/FEB/MAR/APR/MAY/JUN/JUL/AUG/SEP/OCT/NOV/DEC"
$ i=0
$LOOP:
$ IF F$ELEMENT(I, months) .EQS. mm THEN GOTO ENDLOOP
$ IF I .LE. 11 THEN
$ I = I+1
$ GOTO LOOP
$ ELSE
$ WRITE SYS$OUTPUT "Invalid month"
$ GOTO ENDLOOP
$ ENDIF
$ ENDLOOP

Archunan


Regards
Archie
Lawrence Czlapinski
Trusted Contributor

Re: ENTER A STRING AND RETURN AN INT

Martin:
If you make all the list strings the same number of characters you can use lexical F$LOCATE.
length=F$LENGTH(original_string)
IF length .eq. 1
THEN
string=original_string+" "
ELSE
string=original_string
ENDIF
list="C C#D D#E F F# G G# A A#B "
integer_var=f$locate(string,list)/2)
Lawrence
Lawrence Czlapinski
Trusted Contributor

Re: ENTER A STRING AND RETURN AN INT

Martin:
$!
$ If integer_var .eq. 24
$ THEN
$! Process error
.
.
.
$ ENDIF
Lawrence
David B Sneddon
Honored Contributor

Re: ENTER A STRING AND RETURN AN INT

Martin,

You may get more relevant responses if you specify
what platform you are using and what language you
are trying to use...

Dave
Hein van den Heuvel
Honored Contributor

Re: ENTER A STRING AND RETURN AN INT

I wouldn't use a lookup loop with DCL, but a simple calculation:


----------- keys.com -----------

$ keys = ",C ,C#,D ,D#,E ,F ,F#,G ,G#,A ,A#,B "
$ if p1.eqs."" then exit
$ key = f$loc(","+p1, keys)/3
$ if key .lt. 12
$ then write sys$output "key ''p1' = ''key'"
$ else write sys$output "key ''p1' is invalid"
$endif

----- keys.p ----------

%keys = qw (C 0 C# 1 D 2 D# 3 E 4 F 5 F# 6 G 7 G# 8 A 9 A# 10 B 11);
# foreach (keys %keys) { print $i++, " $_ $keys{$_}\n"};
$key_name = uc (shift @ARGV);
$key = $keys{$key_name};
if (defined $key) {
print "key $key_name = $key\n";
} else {
print "key $key_name = invalid\n";
}


Solution example with DCL:

$ @keys c
key C = 0
$ @keys D#
key D# = 3
$ @tmp x
key X is invalid

Solution example with PERL

$ perl keys.p A#
key A# = 10
$ perl keys.p d
key D = 2
$ perl keys.p x
key X = invalid


hth,
Hein.
Hein van den Heuvel
Honored Contributor

Re: ENTER A STRING AND RETURN AN INT


btw Lawrence was on a right track (imho), again I prefer to calculate rather then decide. For example:

$original_string = p1
$string = f$extr(0,2, original_string+" ")
$list = "C C#D D#E F F# G G# A A#B "
$integer_var=f$locate(string,list)/2
$show symb integer_var

of course this solution suffers from 'false positive's.
Looking for a bad string like #E will result in the value for D#.

The pre and/or post-pending of a known string will solve that. For further robustness add an F$EDIT with UPCASE and/or TRIM

fwiw,
Hein.



Martin Devek
New Member

Re: ENTER A STRING AND RETURN AN INT

I am so sorry. That-s to remind me! DAVE...
I am using C.

Thank you.
Kris Clippeleyr
Honored Contributor

Re: ENTER A STRING AND RETURN AN INT

Martin,
Have a look at the C-function "strstr" or the RTL STR$POSITION.
Regards,
Kris (aka Qkcl)
I'm gonna hit the highway like a battering ram on a silver-black phantom bike...
Steven Schweda
Honored Contributor

Re: ENTER A STRING AND RETURN AN INT

I thought that this was a lot more fun when
everyone was still assuming that he wanted a
DCL solution. Then DS spoiled everything.
Hein van den Heuvel
Honored Contributor

Re: ENTER A STRING AND RETURN AN INT


Yeah well... where the heck is the VMS component in this question.
Suddenly the whole problem sounds like a middle school home work excercise.

Oh well,
Regards,

Hein.
Lawrence Czlapinski
Trusted Contributor

Re: ENTER A STRING AND RETURN AN INT

Hein: My solution with a one correction catches false positives. I was checking for the wrong end value. Better to use f$length.
CONSHY» string="#F"
CONSHY» original_string="#F"
CONSHY» length=F$LENGTH(original_string)
CONSHY» if length.eq.1 then string=original_string+" "
CONSHY» if length.eq.2 string=original_string
%DCL-W-EXPSYN, invalid expression syntax - check operators and operands
CONSHY» if length.eq.2 then string=original_string
CONSHY» list="C C#D D#E F F#G G#A A#B "
CONSHY» integer_var=f$locate(string,list)/2
CONSHY» sh sym integer_var
INTEGER_VAR = 12 Hex = 0000000C Octal = 00000000014
CONSHY» if integer_var .eq. f$length(list)/2 then write sys$output "string error
on ",string
string error on #F
CONSHY»
Lawrence
Jan van den Ende
Honored Contributor

Re: ENTER A STRING AND RETURN AN INT

Martin,

Let me do the honor that the previous posters omitted:

Welcome to the VMS forum.

And in case Hein is right (excuses when he and me are wrong!)

since you happened to trip on the worlds MOST stable, MOST secure (and regrettably, best kept-secret) OS, you are cordially invited to get some better aquaintance with it.
Be warned: the elegance, consistency, security, stability, & scalability of it, will forever ruin your positive view of any other OS!

Once again: Welcome.

Proost.

Have one on me.

jpe
Don't rust yours pelled jacker to fine doll missed aches.
Lawrence Czlapinski
Trusted Contributor

Re: ENTER A STRING AND RETURN AN INT

Hein: You were right. I used the wrong input value. So it is better to use a delimiter.
Lawrence
John Gillings
Honored Contributor

Re: ENTER A STRING AND RETURN AN INT

Don't forget your enharmonic synonyms!

flats
C Db E Eb E...

double sharps
B+C#C+D#D+...

double flats
DbbDbEbbDbFb...

Upper casing might not be such a good idea if you use "b" for "flat".
A crucible of informative mistakes
Martin Devek
New Member

Re: ENTER A STRING AND RETURN AN INT

strcmp
David B Sneddon
Honored Contributor

Re: ENTER A STRING AND RETURN AN INT

Martin,

To borrow some text from Jan...

from your Forum Profile:


I have assigned points to 0 of 13 responses to my questions.


Maybe you can find some time to do some assigning?

http://forums1.itrc.hp.com/service/forums/helptips.do?#33

It is fully up to _YOU_ to decide how many points you
assign. If you consider an answer is not deserving any
points, you can assign 0 points, and then that answer
will no longer be counted as unassigned.
Consider, that every poster took at least the trouble
of posting for you!

To easily find your streams with unassigned points,
click your own name somewhere, this will bring up your
profile. Near the bottom of that page, under the caption
"My Question(s)" you will find "questions or topics with
unassigned points", clicking that will give all, and
only, your questions that still have unassigned postings.

Thanks on behalf of your Forum colleagues.

PS. Nothing personal in this. Jan tries to post it to
everyone with this kind of assignment ratio in this forum.
If you have received a posting like this before, please do
not take offence, none is intended!

Proost.

Have one on Jan.

Dave
Arch_Muthiah
Honored Contributor

Re: ENTER A STRING AND RETURN AN INT

Jan,

This thread has a sign of "this question has at least one answer that has received a rating of 8 points or better" when Martin does not assign ponits to any of the answers.

Archunan
Regards
Archie