This question already has answers here:
Adding a unit to a number in Sass
(2 answers)
Closed 7 years ago.
I am having an issue with a Sass loop. I want to make the animation .1 seconds, .2 seconds etc...Here is the loop:
#for $i from 1 through 6 {
#elem span:nth-child(#{$i}) {
animation-delay: .#{$i}s;
}
}
The issue seems to be with the period here: .#{$i}s
If I remove it it works fine, but I get my animation in seconds and not fractions of a second as I would like it.
You could wrap the .#{$i}s by quotes and then use unquote() string function to fix the issue, as follows:
#for $i from 1 through 6 {
#elem span:nth-child(#{$i}) {
animation-delay: unquote(".#{$i}s");
}
}
From the doc:
unquote($string) Removes quotes from a string. If the string is
already unquoted, this will return it unmodified.
Related
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
#!/bin/bash
START_SLEEP_TIME=2
LOOP_SLEEP_TIME=2
OK="false"
#need to wait for another proc first
sleep $START_SLEEP_TIME
RET=
check () {
echo "---1--"
echo "---2--"
return 1
}
# isn't even called but just making sure it's not a syntax issue
finalize () {
OK="true"
}
for i in {0..100}
do
echo "check"
VAL=check
sleep $LOOP_SLEEP_TIME
done
echo "Checks terminated. $OK"
The script is supposed to do more stuff, but I am trying to strip it down to the essential to see why it is not working.
The above for me just always prints
check
check
check
after sleeping but it never prints --1-- or --2--.
The function is supposed to be doing more stuff but if I can't even get this to work there's no point.
VAL=check assigns the value check to VAL.
If you want to call the function check and assign the output from the function to VAL, make it:
VAL=$(check)
If you instead want the function's exit value, make it:
check
VAL=$?
Just call your check function, i.e. write is as a command. The returned value is then in $?.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm a newbie in perl, how I can make this better and faster?
(SPOJ task)
until($i>99){
$a=<>;
if($a%5==0){
if($a%3==0){
print"SPOKOKOKO\n";
}else{
print"SPOKO\n";
}
}elsif($a%3==0){
print"KOKO\n";
}else{
print"$a";
}
$i++;
}
Something which is divisible by 5 and 3 is divisible by 15, so your logic is more straight forward as...
Divisible by 15
Divisible by 5
Divisible by 3
Not divisible by 5 nor 3
I'd also take advantage of say.
I'd also replace the awkward until with a for and not bother with $i.
Avoid using $a and $b because these are special variables used by sort.
Turn on strict and warnings.
Strip the newline off $a. Technically not necessary if you're going to use it as a number, but if you don't printing it will have an extra newline.
A non-number % anything is 0. Unless we validate the input, if you input nothing or "bananas" you'll get SPOKOKOKO. We can check if the input looks like something Perl considers a number with Scalar::Util::looks_like_number. If it isn't a number, throw an exception; it isn't the function's job to figure out what to do with a non-number. Perl doesn't really have exceptions, but we can get close with a die and eval.
And put the logic inside a subroutine. This separates the details of the input and output from the logic. The logic can now be named, documented, tested, and reused. This also avoids the redundant say.
use strict;
use warnings;
use Scalar::Util qw(looks_like_number);
use v5.10;
sub whatever_this_is_doing {
my $num = shift;
# Validate input early to keep the rest of the logic simple.
if( !looks_like_number($num) ) {
die "'$num' is not a number";
}
if($num % 15 == 0) {
"SPOKOKOKO";
}
elsif($num % 5 == 0) {
"SPOKO";
}
elsif($num % 3 == 0) {
"KOKO";
}
else {
$num; # Note there's no need to quote a single variable.
}
}
for(1..99) {
my $num = <>;
chomp($num);
eval {
say whatever_this_is_doing($num);
} or say "'$num' is not a number.";
}
I'm sure there's some very clever math thing one can do to reduce the number of calls to %, but % is extremely unlikely to be a bottleneck, especially in Perl, so I'd leave it there.
100 rows? That will be so fast that I don't understand why you are asking for optimization. Nevertheless...
The biggest two costs are reading 100 rows and writing 100 rows.
Then come the ifs and modulo checks -- each of these is insignificant.
One thing to consider is to turn it inside out. I'm suggesting 1 print with a messy expression inside the loop
print ( ($a%5 == 0) ?
( ($a%3 == 0) ? "SPOKOKOKO\n" : "SPOKO\n" ) :
( ($a%3 == 0) ? "KOKO\n" : $a );
A lot fewer lines (3 vs 10); probably easier to read (especially if columns are aligned); perhaps faster.
Easier to digest. (I had not noticed that the %3 was s the same.) Oh, and now it is obvious that \n is missing from the last case; maybe a bug?
Perhaps some of the parentheses can be removed, but I don't trust Perl to 'do the right thing with nested uses of ?:.
This question already has answers here:
How to iterate for loop in reverse order in swift?
(16 answers)
Closed 6 years ago.
What is the syntax of a decrementing for() loop?
I would like to do something like the following without getting a compilation error:
for ndx in 5...4 {
print("do something")
}
This prints the value of i in reverse order:
for i in (1...10).reversed() {
print(i)
}
This question already has answers here:
How to return a string value from a Bash function
(20 answers)
Closed 7 years ago.
Bash functions are just statements and they don't return values. Can anyone share best practice on writing functions that return values in bash?
Let's say I've a function that joins two strings:
function JoinStrings {
returnValue="$1$2"
}
How do I reuse this function in my code? How do I get returnValue to be returned to caller? Should I just use it as a global after this function call? That leads to many errors with global variables everywhere... How to achieve code reuse in bash?
You can use echo to "return" an arbitrary value:
join_strings() {
echo "$1$2"
}
cat="cat"
dog="dog"
catdog=$(join_strings $cat $dog)
echo $catdog
# catdog
Just output the value.
function JoinStrings {
echo "$1$2"
}
JoinStrings a b #ab
x=$(JoinStrings a bc) #x is now abc
This question already has answers here:
How to convert a number to a percentage with SASS? [duplicate]
(2 answers)
Closed 9 years ago.
I try to create a flexiable Table grid which is inside a for loop
Its works perfectly fine on PX based but I cant get the % added it gives me an error.
$fg-column: 20px;
$$fg-max-columns:41;
width:(#{($fg-column /$fg-max-columns)}#{"%"}) * $i);
.table-grid-layout{
&.is-fluid{
#include fill-parent;
#for $i from 1 through 41 {
.column-#{$i} {
width:(#{($fg-column /$fg-max-columns)}#{"%"}) * $i);
}
}
}
&.not-fluid{
#include outer-container;
#for $i from 1 through 41 {
.column-#{$i} {
width: 20px * $i;
}
}
}
}
Simply adding a percentage sign as a string to the pixel value will not achieve the result you are after. If you want to get the percentage value, you need to divide the number of
columns for that iteration on the total number of columns:
.column-#{$i} {
width: percentage($i / $fg-max-columns);
}
Also, please mote that this approach is not semantic since you end up with classes that describe the layout itself, not the content.
Is it possible that the percent sign is actually trying to return the remainder of the equation?
Check out the paragraph from this source:
http://learn.shayhowe.com/advanced-html-css/preprocessors
"Multiplication is completed with the asterisk sign, *, however only one of the numbers, if any, may include a unit of measurement. Using the percent sign, %, will return the remainder of the two numbers upon being divided, and as with multiplication, only allows one number, if any, to have a unit."
Also, would it be possible to utilize the Sass percentage function? See this previous question on Stack: Calculate a percent with SCSS/SASS