I'm trying to use a variable and assign an expression to it in one step:
The given (sample) code
my @l=<a b c d e f g h i j k>;
my $i=0;
while $i < 7 {
say @l[$i];
$i= ($i+1) * 2;
}
# Output:
# a
# c
# g
The desired functionality:
my @l=<a b c d e f g h i j k>;
my $i=0;
say @l[$i =~ ($i+1) * 2] while $i < 7;
# Here, first the @l[$i] must be evaluated
# then $i must be assigned to the expression
# ($i+1) * 2
# (The =~ operator is selected just as an example)
# Output:
# The same output as above should come, that is:
# a
# c
# g
After the variable $i
is used, the (sample) expression ($i+1) * 2
should be assigned to it in one step and that should take place solely inside the array index @l[$i =~ ($i+1) * 2]
i.e. the argument of while
should not be changed.
Here I took the Regex equation operator =~
(check and assign operator, AFAIK) just as an example. In this context of course, it did not work.
I need to Are there any operators or some workaround to achieve that functionality? Thank you.
$i
) with string operations (lt
). 3. you seem to misunderstand what the postfix++
does ($i =~ $i++
). Could you at least provide the required output? Thank you! – Elizabeth Mattijsen Nov 24 at 13:06lt
is not suitable fro integers (I've noticed it after taking a look at the Perl 6 docs) As for the=~
operator, it's there jsut as an example operator. I've chosen it because it does the closest thing I want to achieve: It checks a Regex and then assigns a alue to it in one step. For instance insay $same if ($same =~ m/^$re$/g)
(Perl 5)$same
is both checked and if check returns True, it's assiged a value. – Lars Malmsteen Nov 24 at 15:51$same
to be assigned a value in any circumstances via the codesay $same if ($same =~ m/^$re$/g)
. And sure enough, running that code in tio leaves it unchanged despite the check being true. Please consider explaining specific circumstances in which$same
can ever be assigned a value with the line of code you've quoted. TIA. – raiph Nov 24 at 19:06$same
I have put it in a hurry to depict the kind of functionality I was trying to explain. There I was actually trying to tell the so-called 'Capture Group' method in which the substrings fulfilling the regex being assigned to variables$1
,$2
(using that same operator=~
) but I erroneously talked about the$same
instead of those$1
variable. When you think about this Capture Groups method, it sort of depicts the 'use & assign' mechanism (albeit very roughly) – Lars Malmsteen Nov 24 at 21:33