How to know if the entered letter is a vowel or a consonant - qbasic

CLS
INPUT "enter any letter"; a$
b$ = UCASE$(a$)
SELECT CASE b$
CASE "a", "e", "i", "o", "u"
END SELECT
IF c$ = b$ THEN
PRINT "Vowel"
ELSE
PRINT "consonant"
END IF
END

If you're going to compare with lowercase letters, be sure to use LCASE$ instead of UCASE$
CLS
INPUT "enter any letter"; a$
b$ = LCASE$(a$)
SELECT CASE b$
CASE "a", "e", "i", "o", "u"
PRINT "Vowel"
CASE ELSE
PRINT "consonant"
END SELECT

You can use the INSTR function to find out if the inputted character is in the list of vowels:
CLS
INPUT "enter any letter"; a$
IF INSTR("AEIOUaeiou", a$) THEN
PRINT "Vowel"
ELSE
PRINT "Consonant"
END IF
END
Not better but feasible is using `UCASE$':
CLS
INPUT "enter any letter"; a$
a$ = UCASE$(a$)
IF INSTR("AEIOU", a$) THEN
PRINT "Vowel"
ELSE
PRINT "Consonant"
END IF
END

Related

Extract n words from string using Perl

i have a text containing sentences in each line, and in front of each word its lemmetize form exemple:
he "he" went "go" to "to" school "school" with "with" his "his" freinds "freind"
i would like to extract for example three by three word in in each line. The result seems like this:
he "he" went "go" to "to" \n
went "go" to "to" school "school" \n
to "to" school "school" with "with" \n
school "school" with "with" his "his" \n
with "with" his "his" freinds "freind" \n
I'd like to do this using Perl.
thank you all for your helps, i found a solution, it works, but it's in dirty code i think, that's why i asked this question, to find a better solution, the awk solution seems great but the result not like i look,
This is the solution i fixed the window at 7 words and in front of each word it's POS and it's lemmetized form:
he "he" "PRO" went "go" "V" to "to" "PREP" school "school" "N" ...
open(F,"/home/file.txt")||die "error";
my $string;
while($ligne = <F> ) {
my #val = split(/ /, $ligne);
my $long=$#val;
for($i=0; $i<$long;$i+=3){
$string="$val[$i] $val[$i+1] $val[$i+2] $val[$i+3] $val[$i+4] $val[$i+5] $val[$i+6] $val[$i+7] $val[$i+8] $val[$i+9] $val[$i+10] $val[$i+11] $val[$i+12] $val[$i+13] $val[$i+14] $val[$i+15] $val[$i+16] $val[$i+17] $val[$i+18] $val[$i+19] $val[$i+20]";
my #val2 = split(/ /, $string);
my $long2=$#val2;
if($long2 >19){ #if length superior at 19, (3*7)
print FILEOUT "$string\n";
$string="";
}
}
}
This script starts by reading the entire line as an array of words (#words) and then uses an #aux array as a FIFO... discarding the first 2 elements at each pass and keeping the FIFO size always 6 itens... then, reapeat while there are words in the #words array:
#!/usr/bin/perl
use strict;
my $file = 'file.txt';
open(F,$file)||die "error";
my #aux;
while(<F>) {
my #words = split /\s+/;
while($#words >= 0) {
while($#aux < 5 && $#words >= 0) {
my $a = shift #words;
push #aux, $a;
}
print ((join " ", #aux)."\n");
shift #aux;
shift #aux;
}
}

how to remove special character "," from a string using perl

hi i have some data like below
S_ METHOD m0 : 47|8#0- (1,0) [0|0] ""
S_ CTRL m1 : 15|8#0- (0.01,-200) [0|0] ""
from above 2 lines i am trying to extract that are in curve brackets () i have written a perl script
my #temp_signal = split(":",$line);
my #signal= split(" ",#temp_signal[0]);
my #Factor_temp1 = split (" ",#temp_signal[1]);
my #factor_temp = split ('\(',#Factor_temp1[1]);
my #factor = chop(#factor_temp);
my #offset = split (",",#factor_temp);
print OUTFILE1 "#offset[0]\n";
print OUTFILE1 "$signal[1]\n";
but when am trying to print #offset[1] & #offset[0] its printing some other value which is not even exist in the line how can i get the values as
1 0
0.01 -200
You can use a regular expression match to extract what's inside parentheses separated by a comma:
if ( my #numbers = $line =~ /\((.*),(.*)\)/) {
print "$numbers[0] $numbers[1]\n";
}

Want to extract the first letter of each word

I basically have a variable COUNTRY along with variables SUBJID and TREAT and I want to concatenate it like this ABC002-123 /NZ/ABC.
Suppose if the COUNTRY variable had the value 'New Zealand'. I want to extract the first letter of each word, But I want extract only the first two letters of the value when there is only one word in the COUNTRY variable. I wanted a to know how to simply the below code. If possible in perl programming.
If COUNTW(COUNTRY) GT 1 THEN
CAT_VAR=
UPCASE(SUBJID||"/"||CAT(SUBSTR(SCAN(COUNTRY,1,' '),1,1),
SUBSTR(SCAN(COUNTRY,2,' '),1,1))||"/"||TREAT);
my #COUNTRY = ("New Zealand", "Germany");
# 'NZ', 'GE'
my #two_letters = map {
my #r = /\s/ ? /\b(\w)/g : /(..)/;
uc(join "", #r);
} #COUNTRY;
The SAS Perl Regular Expression solution is to use CALL PRXNEXT along with PRXPOXN or CALL PRXPOSN (or a similar function, if you prefer):
data have;
infile datalines truncover;
input #1 country $20.;
datalines;
New Zealand
Australia
Papua New Guinea
;;;;
run;
data want;
set have;
length country_letter $5.;
prx_1 = prxparse('~(?:\b([a-z])[a-z]*\b)+~io');
length=0;
start=1;
stop = length(country);
position=0;
call prxnext(prx_1,start,stop,country,position,length);
do while (position gt 0);
matchletter = prxposn(prx_1,1,country);
country_letter = cats(country_letter,matchletter);
call prxnext(prx_1,start,stop,country,position,length);
put i= position= start= stop=;
end;
run;
I realize the OP might not be interested in another answer, but for other users browsing this thread and not wanting to use Perl expressions I suggest the following simple solution (for the original COUNTRY variable):
FIRST_LETTERS = compress(propcase(COUNTRY),'','l');
The propcase functions capitalizes the first letters of each word and puts the other ones in lower case. The compress function with 'l' modifier deletes all lower case letters.
COUNTRY may have any number of words.
How about this:
#!/usr/bin/perl
use warnings;
use strict;
my #country = ('New Zealand', 'Germany', 'Tanzania', 'Mozambique', 'Irish Repuublic');
my ($one_word_letters, $two_word_letters, #initials);
foreach (#country){
if ($_ =~ /\s+/){ # Captures CAPs if 'country' contains a space
my ($first_letter, $second_letter) = ($_ =~ /([A-Z])/g);
my ($two_word_letters) = ($first_letter.$second_letter);
push #initials, $two_word_letters; # Add to array for later
}
else { ($one_word_letters) = ($_ =~ /([A-Z][a-z])/); # If 'country' is only one word long, then capture first two letters (CAP+noncap)
push #initials, $one_word_letters; # Add this to the same array
}
}
foreach (#initials){ # Print contents of the capture array:
print "$_\n";
}
Outputs:
NZ
Ge
Ta
Mo
IR
This should do the job provided there really are no 3 word countries. Easily fixed if there are though...
This should do.
#!/usr/bin/perl
$init = &getInitials($ARGV[0]);
if($init)
{
print $init . "\n";
exit 0;
}
else
{
print "invalid name\n";
exit 1;
}
1;
sub getInitials {
$name = shift;
$name =~ m/(^(\S)\S*?\s+(\S)\S*?$)|(^(\S\S)\S*?$)/ig;
if( defined($1) and $1 ne '' ) {
return uc($2.$3);
} elsif( defined($4) and $4 ne '' ) {
return uc($5);
} else {
return 0;
}
}

Exact pattern match using perl index() function

I am trying to use the index() function and I want to find the position of a word inside a string, only when it is an exact match. For example:
My string is STRING="CATALOG SCATTER CAT CATHARSIS"
And my search string is KEY=CAT
I want to say something like index($STRING, $KEY) and check match for CAT, and not CATALOG. How do I accomplish this? The documentation says
The index function searches for one string within another, but without the wildcard-like behavior of a full regular-expression pattern match.
which makes me think that it may not be that straight-forward, but my perl skills are limited :). Is it possible to do what I am trying to do?
Hopefully, I was able to articulate my question well. Thanks in advance for your help!
How about:
my $str = "CATALOG SCATTER CAT CATHARSIS";
my $key = "CAT";
if ($str =~ /\b$key\b/) {
say "match at char ",$-[0];;
} else {
say "no match";
}
output:
match at char 16
You need to learn about Regular Expressions in Perl. Perl didn't invent Regular Expressions, but tremendously expanded upon the concept. In fact, many other programming languages talk specifically about using Perl Regular Expressions.
A regular expression matches a specific word pattern. For example, /cat/ matches the sequence cat in a string.
if ( $string =~ /cat/ ) {
print "String contains the letters 'cat' in a row\n";
}
In many ways, this does the same thing as:
my $location = index ( $string, "cat" );
if ( $location =! -1 ) { # index returns -1 when substring isn't found
print "String contains the letters 'cat' in a row\n";
}
But, both of these would match:
"Don't let the cat out of the bag"
"The Sears catalog arrived in the mail"
You don't want to match the last. So, you could do this:
my $location = index $string, " cat ";
Now, index $string, " cat " won't match the word catalog. Case closed! Or is it? What about:
"cat and dog it doth rain."
Maybe you could check and say things are okay if a sentence starts with "cat":
if ( (index ($string, " cat ") != -1) or (index ($string, "cat") = 0) ) {
print "String contains the letters 'cat' in a row\n";
}
But, what about these?
"The word CAT in all uppercase"
"Stupid cat"
"Cat! Here Cat! Common Cat!": Punctuation after the word "cat"
"Don't let the 'cat' out of the 'bag'": Quotation Marks around "cat"
It could take dozens of lines to specify each and every one of these conditions.
However:
if ( $string =~ /\bcat\b/i ) {
print "String contains the word 'cat' in it\n";
}
Specifies each and every one -- and then some. The \b says this is a word boundary. This could be a space, a tab, a quote, the beginning or ending of a line. Thus /\bcat\b/ specifies that this should be the word cat and not catalog. The i on the end tells your regular expression to ignore case when matching, so you'll find Cat, cat, CAT, cAt, and all other possible combinations.
In fact, Perl's regular expressions is what made Perl such a popular language to begin with.
Fortunately, Perl comes with not one, but two tutorials on Regular Expressions:
perlretut: Perl Regular Expression Tutorial
perlrequick: Perl Regular Expression Quick Start.
Hope this helps.
That's (partial) solution of this problem with index:
use warnings;
use strict;
my $test = 'CATALOG SCATTER CAT CATHARSIS';
my $key = 'CAT';
my $k_length = length $key;
my $s_length = (length $test) - $k_length;
my $pos = -1;
while (($pos = index $test, $key, $pos + 1) > -1) {
if ($pos > 0) {
my $prev_char = substr $test, $pos - 1, 1;
### print "Previous character: '$prev_char'\n";
next if $prev_char ge 'A' && $prev_char le 'Z'
|| $prev_char ge 'a' && $prev_char le 'z';
}
if ($pos < $s_length) {
my $next_char = substr $test, $pos + $k_length, 1;
### print "Next character: '$next_char'\n";
next if $next_char ge 'A' && $next_char le 'Z'
|| $next_char ge 'a' && $next_char le 'z';
}
print "Word '$key' found at " . $pos + 1 . "th position.\n";
}
As you see, it's kinda wordy, because it uses basic Perl string functions - index and substr - only. Checking whether the substring found is indeed a word is done via checking its next and previous characters (if they exist): if they belong to either A-Z or a-z range, it's not a word.
You can simplify it a bit by trying to lowercase these characters (with lc), then check against the single character range only:
my $lc_prev_char = lc( substr $test, $pos - 1, 1 );
next if $lc_prev_char ge 'a' && $lc_prev_char le 'z';
... but then again, it's quite a minor improvement (if improvement at all).
Now consider this:
my $test = 'CATALOG SCATTER CAT CATHARSIS CAT';
my $key = 'CAT';
while ($test =~ /(?<![A-Za-z])$key(?![A-Za-z])/g) {
print "Word '$key' found at " . ($-[0] + 1) . "th position.\n";
}
... and that's it! The pattern literally tests the string given ($test) for the substring given ($key) not being either preceded with or followed by the symbol of A-Za-z range, and supporting Perl regex magic (this variable, in particular) makes it easy to get the starting position of such substring.
The bottom line: use regexes to do the regexes' work.
Regular expressions allow for the search to contain word boundaries as well as distinct characters. While
my $string = "CATALOG SCATTER CAT CATHARSIS";
index($string, 'CAT');
will return zero or greater if $string contains the characters CAT, a regular expression like
$string =~ /\bCAT\b/;
will return false as $string doesn't contain CAT preceded and followed by a word boundary. (A word boundary is either the beginning or end of the string, or between an word character and a non-word character. A word character is any alphanumeric character or an underscore.)
use \E value.
so :
#!usr/bin/perl
my $string ="Little Tony";
my $check = "Ton";
if($string =~ m/$check\E/g)
{
print "match";
}
else
{
die("No Match");
}

Here's an old school IF statement for you, but there is a problem

I have an IF statement in QBASIC... yes... QBASIC...
I have been teaching someone to program (I decided this would be nice and easy to see
how the syntax works).
...Anyway, I have this code:
CLS
start:
INPUT ">>", a$
PRINT a$
IF (INSTR(a$, "do you")) THEN
IF (INSTR(a$, "like")) THEN
IF (INSTR(a$, "cheese")) THEN PRINT "Yep, I like cheese":
IF (INSTR(a$, "music")) THEN PRINT "Depends, which genre?": GOTO musicGenre
ELSE IF (INSTR(a$, "hate")) THEN
IF (INSTR(a$, "cheese")) THEN PRINT "No, I like cheese"
END IF
END IF
END IF
musicGenre:
INPUT ">>", m$
SELECT CASE (m$)
CASE "pop"
PRINT "..pop! lol, baa baa"
CASE "rock"
PRINT "Rock is ok"
END SELECT
GOTO start
But when I type "do you like cheese?" it seems to only reply "Yep, I like cheese" every other time...
Could anyone shed some light on this?
note:
"do you like music?" works every time...
note 2:
Screenshot of the output:
Your code you provided appears correct.
Try one of the following:
If possible, send us a larger code sample. I'm guessing the error is outside the code you provided.
Output the input (a$) before the first IF to confirm your code will be working with the expected input.
In most languages, FALSE is zero and true is anything else. However, you may want to be more explicit with the following IF (INSTR(a$) > 0).
EDIT: You should put a goto start on any cheese result. Otherwise, it's going to the musicGenre code.
CLS
start:
INPUT ">>", a$
IF (INSTR(1, a$, "do you")) THEN
IF (INSTR(1, a$, "like")) THEN
IF (INSTR(1, a$, "cheese")) THEN PRINT "Yep, I like cheese"
IF (INSTR(1, a$, "music")) THEN PRINT "Depends, which genre?": GOSUB musicGenre
END IF
IF (INSTR(1, a$, "hate")) THEN
IF (INSTR(1, a$, "cheese")) THEN PRINT "No, I like cheese"
END IF
END IF
GOTO start
musicGenre:
INPUT ">>", m$
SELECT CASE (m$)
CASE "pop"
PRINT "..pop! lol, baa baa"
CASE "rock"
PRINT "Rock is ok"
END SELECT
RETURN
This program demonstrates parsing input and gosubs in Basic.
REM Cheese progran source:
CLS
DO
INPUT ">>", a$
a$ = LCASE$(a$)
PRINT a$
IF INSTR(a$, "do you") THEN
IF INSTR(a$, "like") THEN
IF INSTR(a$, "cheese") THEN
PRINT "Yep, I like cheese":
END IF
IF INSTR(a$, "music") THEN
PRINT "Depends, which genre?"
GOSUB MusicGenre
END IF
ELSE
IF INSTR(a$, "hate") THEN
IF INSTR(a$, "cheese") THEN
PRINT "No, I like cheese"
END IF
END IF
END IF
END IF
LOOP
END
MusicGenre:
INPUT ">>>", m$
a$ = LCASE$(a$)
SELECT CASE (m$)
CASE "pop"
PRINT "..pop! lol, baa baa"
CASE "rock"
PRINT "Rock is ok"
END SELECT
RETURN