Input is to be taken from a-z or A-Z. We need to have the first and last Capital letters of that input string as the output - emu8086

Input is to be taken from a-z or A-Z and the input ends when we give a star(*). We need to have the first and last Capital letters of that input characters as the output. also, we should show the input we have taken each time.
N.B. We take the inuputs character by character, not as a string.
Test case 1:
input: aAbCcP*
output: AP
Test case 2:
input: ZabCBc*
output: ZB

$test1="aAbCcP*";
$test="ZabCBc*";
$i=0;
$a=[];
$final_string="";
while(!empty($test[$i])){
if(ctype_upper($test[$i])){
$final_string=$test[$i];
array_push($a,$final_string);
}
$i++;
}
$first = reset($a);
$last = end($a);
echo $first. $last;

Related

In PowerShell, how do I copy the last alphabet characters from a string which also has numbers in it to create a variable?

For example if the string is blahblah02baboon - I need to get the "baboon" seperated from the rest and the variable would countain only the characters "baboon". Every string i need to do this with has alphabet characters first then 2 numbers then more alphabet characters, so it should be the same process everytime.
Any advice would be greatly appreciated.
My advice is to learn about regular expressions.
'blahblah02baboon' -replace '\D*\d*(\w*)', '$1'
Or use regex
$MyString = "01baaab01blah02baboon"
# Match any character which is not a digit
$Result = [regex]::matches($MyString, "\D+")
# Take the last result
$LastResult = $Result[$Result.Count-1].Value
# Output
Write-Output "My last result = $LastResult"

How can I preserve the uppercase/lower case of a string in search using perl?

I want to search for "Frequencies" (its first letter in uppercase) in my text files. And my code will print to the output file some columns including "Frequencies". But there are also occurrences of "frequencies" (its first letter in lowercase) in the text files. I am using this part $search_word = qr/Frequencies/; in the code. How can I make the first letter of the word "Frequencies" upper case in the $search_word = qr/Frequencies/; part to eliminate the occurrences of "frequencies" in the search?
In Perl, you have ucfirst to capitalize the first letter. For example:
$a = "freQuEncY";
$a = ucfirst(lc($a)); # $a <-- "Frequency";
Why don't you use regex match to check , like this
if($string_to_be_searched =~ /Frequencies/){
do something; # like print
}
Try this one:
if ( $$test_string[$i] =~ /\b(?i)f(?-i)requencies/ ) {
my $captured = ucfirst($&);
# process $captured
}
Explanation:
The regex matches will be case-insensitive for the first letter of the word frequencies only. (?i) turns on case-insensitive matching at the position it occurs for the remainder of the pattern or until it is revoked by (?-i). This works for other flags too, cf. perldoc section on re.
$& contains the full match
\b denotes a word boundary (perhaps you don't need that but your problem description suggests you do).

How do I find the sum of all numbers in STDIN even if there are non-digit characters?

I have an assignment asking me to enter a sequence of numbers and characters each separated by a space and the sequence in ended by entering in "q" or "Q" followed by a space. Everything except the numbers should be discarded and we are to find the sum. So for example if the input is "1 12 a 2 5 P Q" then we should expect to get "20" as the output.
So far I'm using
$input = <>;
$input =~ tr/0-9//cd;
to get only the numbers but what I want is to split them up and get the sum. Right now the output would be 11225 and I want "1+12+2+5" and get the sum.
perl -ne '$s=0;($line)=/(.*?)[Qq]/;while($line=~/(\d+)/g) {$s+=$1} print "$s\n"'
Explanation:
Strips the trailing part of each line starting with a Q or a q, then scan the remaining part for isolated positive integers and adds these together.
First, strip out all characters that aren't numbers or spaces:
$input =~ s/[^0-9\s]//g;
Then, split on whitespace:
#digits = split(/\s/, $input);
Then you have a list of digits that you can add up.
Preserve spaces in your first step:
$input =~ tr/0-9 //cd;
Then split on spaces:
my #numbers = split ' ', $input;
(this is a special form of split that works like split /\s+/ but also discards empty leading fields).
You probably want to start by getting rid of everything after a Q though:
$input =~ s/Q .*//i;
For what it's worth, I wouldn't have jumped to using tr here; I'd have started by spliting on spaces, then processed fields that were only digits until a Q was reached.

Determine if a string starts with an upper-case character in Perl

I need to make a subroutine in Perl which determines if a string starts with an upper-case character. What I have so far is the following:
sub checkCase {
if ($_[0] =~ /^[A-Z]/) {
return 1;
}
else {
return 0;
}
}
$result1 = $checkCase("Hello");
$result2 = $checkCase("world");
Your code is fine as long as you remove the $ from the front of your subroutine call. A dollar sign indicates a scalar value, and the calls should look like
$result1 = checkCase("Hello");
$result2 = checkCase("world");
Your subroutine is also unnec essarily long. The regex match returns a true/false value, and you are using that value to return different true/false values 1 or 0. Much better to return the result of the regex match directly.
Finally, if you are working outside ASCII characters then you may want to use the Uppercase Letter Unicode category, which is encoded using \p{Lu}.
I hope this variation on your code is useful. I have changed the name of the subroutine slightly because it is standard practice to use just lower case letters and underscores for variable and subroutine identifiers. Upper case letters are reserved for globals like package names.
sub check_case {
$_[0] =~ /^\p{Lu}/
}
print check_case('Hello') ? 'YES' : 'NO', "\n";
print check_case('world') ? 'YES' : 'NO', "\n";
That's almost correct. But [A-Z] might not match uppercase accented characters depending on your locale. Better to use /^[[:upper:]]/.
Also your subroutine invocations shouldn't have the $ character in front of them. I.e. they should be:
$result1 = checkCase("Hello");
$result2 = checkCase("world");

Perl Regex to match words with more than 2 characters

I am new to PERL and working on a regex to match only words with equal to or more than 3 letters . Here is the program I am trying. I tried adding \w{3,} since it should match 3 re more characters. But it is still matching <3 characters in a word. For example If i give "This is a Pattern". I want my $field to match only "This" and "Pattern" and skip "is" and "a".
#!/usr/bin/perl
while (<STDIN>) {
foreach my $reg_part (split(/\s+/, $_)) {
if ($reg_part =~ /([^\w\#\.]*)?([\w{3,}\#\(\)\+\$\.]+)(?::(.+))?/) {
print "reg_part = $reg_part \n";
my ($mod, $field, $pat) = ($1, $2, $3);
print "#$mod#$field#$pat#$negate#\n";
}
}
}
exit(0);
What am I missing?
You have
[\w{3,}...]+
which is the same as
[{},3\w...]+
I think you want
(?:\w{3,}|[\$\#()+.])+
Break your regular expression up.
You know you want three word characters, so specify :-
# Match three word characters.
\w{3}
After that, you don't really care if the word has more characters, but you won't block it either.
# Match 0 or more word characters
\w*
Finally, you want to ensure that you have boundaries to catch the end of words. So, putting it all together. To match a word with at least three word characters, possibly more, use:-
# Word boundaries at start and end
\b\w{3}\w*\b
Note - \w matches alphanumeric - if it's just alpha you need:-
# Alpha only
\b[A-Za-z]{3}[A-Za-z]*\b