if (1=1)
then print "sdfsdfs" and print "sdfsdfsdfsdf"
else print "sdfsdf";
This gives an error. I want to do two things if the condition matches. How can I do that?
Parenthesize the commands and separate them with a semicolon:
if 1=1
then (print "sdfsdfs"; print "sdfsdfsdfsdf")
else print "sdfsdf"
Related
I want the while loop to run as long as the name is not "1234". And if the name is only spaces, I want to print "name can not be only spaces" and ask for a name again. Else I want it to just ask for a name again.
name=input("")
while (name!="1234"):
if(name.isspace):
print ("name can not be only spaces")
name=input("")
else:
name=input("")
The problem I have is that it prints "name can not only be spaces" for whatever string I give it. Even if the string does not have spaces it prints the if statement. What is going on?
You might just be missing the parentheses of the isspace function.
So turn isspace into isspace() and you should be fine.
As Mat has already mentioned, the problem you're facing is simply because name.ispace does not return a boolean like you need in your program.
Also, I think you could simplify your code a bit -
name=input("")
while (name!="1234"):
if(name.isspace()):
print ("name can not be only spaces")
name=input("")
I wanted to search a string that matched exact times in another string, while I met some problem.
use strict;
use warnings;
my $test="abc1234abc5678abcdef910";
my $cut_seq="abc";
print $test,"\tone time\n" if($test=~/$cut_seq{1}/);
print $test,"\tmore than one times\n" if($test=~/$cut_seq{1,}/);
I expected the result:
abc1234abc5678abcdef910 more than one times
But the result showed as:
abc1234abc5678abcdef910 one time
abc1234abc5678abcdef910 more than one times
I also tried this:
print $test,"\tone time\n" if($test=~/$cut_seq{0,1}/);
print $test,"\tone time\n" if($test=~/$cut_seq{1,1}/);
print $test,"\tmore than one times\n" if($test=~/$cut_seq{1,}/);
But nothing changed. I just wonder why it can't match exact times. If something matches two times it will also match one time, then what's the difference of {1}, {1,}, {1,1}, {1,2}. I don't get the point to create these different forms.
If something matches two times it also matches one time. That's why your "one time" match always kicks in.
The easiest approach, I think, is to simply split at your $cut_seq and check the number of splitted elements.
my $test="abc1234abc5678abcdef910";
my $cut_seq="abc";
my #elts= split /$cut_seq/, $test;
print scalar(#elts)-1," times\n";
P.S. This does not count '$cut_seq` at the end of the string - sorry! You'll have to append something which will not be part of your sequence like:
my $test="abc1234abc5678abcdef910";
my $cut_seq="abc";
my #elts= split /$cut_seq/, $test . chr(0);
print scalar(#elts)-1," times\n";
I have the following exercise:
Two words are anagrams if you can rearrange the letters from one to spell the other.
Write a function called is_anagram that takes two strings and returns True if they are anagrams.
I have developed a function, but it is not working properly and I cannot find out why. Could anybody tell me what I am doing wrong? Thank you very much.
def isa(s,t):
if len(s)!=len(t):
print "impossible"
if len(s)==len(t):
i=0
while i<len(s)-1:
for i in s:
if i in t:
print "yay"
print "NO"
import collections
def is_anagram(s,t):
return collections.Counter(s) == collections.Counter(t)
I have some gene sequence (see below), and I want to find all open reading frame (start with ATG and stop TAG).
I have tried this:
my $file = ('ACCCTGCCCAAAATCCCCCCGATCGATAGAGCTAAATGGCCCATGATGCATCGACTAGCTAGCTAAAATGTCGATCGATACAGCTAATAG');
while($file =~ /(ATG\w+?TAG)/g){
print $1;
}
but it only gives
ATGGCCCATGATGCATCGACTAGATGTCGATCGATACAGCTAATAG
how can i get every one?
The trick to find all occurences is to use a zero-width assertion, this will prevent "the eating" of our characters: (?=ATG\w+?TAG).
The problem with this is that we'll get empty matches, so the solution is to use a group:
(?=(ATG\w+?TAG)). You will find all occurences in group 1.
Group 1 output:
ATGGCCCATGATGCATCGACTAG
ATGATGCATCGACTAG
ATGCATCGACTAG
ATGTCGATCGATACAGCTAATAG
Online demo
Result is ok, simply separate them in output:
print "$1\n";
You are getting two matches. To see them, I suggest you print some separator between them:
print "$1\n";
Then we get the output:
ATGGCCCATGATGCATCGACTAG
ATGTCGATCGATACAGCTAATAG
If you want to find frames that also occur inside another, then you must make sure to not consume too many characters. Work around that via a looahead:
/ATG(?=([ACTG]*+TAG))/g;
Then print "ATG$1\n", Output:
ATGGCCCATGATGCATCGACTAG
ATGATGCATCGACTAG
ATGCATCGACTAG
ATGTCGATCGATACAGCTAATAG
If you want to have the start and stop codons in the same frame don't forget to filter the results to the only ones with a length multiple of 3:
print "ATG$1\n" if (length($1)%3) == 0 ;
If you want to check the six frames available in one sequence, don't forget to check also the complementary chain:
$comp_chain = reverse($chain) ;
$comp_chain =~ tr/ATCG/TAGC/ ;
You will then obtain the open reading frames from the six reading frames available in a single sequence.
I am trying to find out if basket has apple [simplified version of a big problem]
$check_fruit = "\$fruit =~ \/has\/apple\/";
$fruit="basket/has/mango/";
if ($check_fruit) {
print "apple found\n";
}
check_fruit variable is holding the statement of evaluating the regexp.
However it check_fruit variable always becomes true and shows apple found :(
Can somebody help me here If I am missing something.
Goal to accomplish:
Okay so let me explain:
I have a file with a pattern clause defined on eachline similar to:
Line1: $fruit_origin=~/europe\\/finland/ && $fruit_taste=~/sweet/
Line2: similar stuff that can contain ~10 pattern checks seprated by && or || with metacharacters too
2.I have another a list of fruit attributes from a perl hash containing many such fruits
3 I want to categorize each fruit to see how many fruits fall into category defined by each line of the file seprately.
Sort of fruit count /profile per line Is there an easier way to accomplish this ? Thanks a lot
if ($check_fruit) returns true because $check_fruit is defined, not empty and not zero. If you want to evaluate its content, use eval. But a subroutine would serve better:
sub check_fruit {
my $fruit = shift;
return $fruit =~ m(has/apple);
}
if (check_fruit($fruit)) {
print "Apple found\n";
}
Why is there a need to store the statement in a variable? If you're sure the value isn't set by a user, then you can do
if (eval $check_fruit) {
but this isn't safe if the user can set anything in that expression.
Put the pattern (and only the pattern) into the variable, use the variable inside the regular expression matching delimiters m/.../. If you don't know the pattern in advance then use quotemeta for escaping any meta characters.
It should look like this:
my $check_fruit = '/has/apple/'; # here no quotemeta is needed
my $fruit = 'basket/has/mango/';
if ($fruit =~ m/$check_fruit/) {
# do stuff!
}
$check_fruit is nothing but a variable holding string data. If you want to execute the code it contains, you have to use eval.
There were also some other errors in your code related to string quoting/escaping. This fixes that as well:
use strict;
use warnings;
my $check_fruit = '$apple =~ m|/has/mango|';
my $apple="basket/has/mango/";
if (eval $check_fruit) {
print "apple found\n";
}
However, this is not usually a good design. At the very least, it makes for confusing code. It is also a huge security hole if $check_fruit is coming from the user. You can put a regex into a variable, which is preferable:
Edit: note that a regex that comes from user input can be a security problem as well, but it is more limited in scope.
my $check_fruit = qr|/has/mango|;
my $apple="basket/has/mango/";
if ($apple =~ /$check_fruit/) {
print "apple found\n";
}
There are other things you can do to make your Perl code more dynamic, as well. The best approach would depend on what you are trying to accomplish.