Simultaneous "lower than" and "higher than" in Perl? [duplicate] - perl

This question already has answers here:
Perl equivalent of (Postgre)SQL BETWEEN operator?
(5 answers)
Closed 8 years ago.
Is there a way to say
perl -e "#array=(1..10); foreach (#array){print qq{$_\n} if ($_>3 and $_<6);}"
more simply like
perl -e "#array=(1..10); foreach (#array){print qq{$_\n} if (3<$_<6);}"
(notice the differences inside the if statement).
Edit: running Perl v5.16.3

Not that I'm aware of. Consider you are looking for a ternary operator, which is quite rare in most programming languages. Usually unary and binary operators will suffice for most needs.
The only example I can think of a ternary operator is the conditional operator (cond ? true_statement : false_statement), used as a short-hand for the if-else clause for short statements.

Related

Perl Subroutine Declaration Unexpected Symbols [duplicate]

This question already has answers here:
What do dollar, at-sign and semicolon characters in Perl parameter lists mean?
(2 answers)
Closed 5 years ago.
I have been looking at some Perl code that has some subroutine declarations that make no sense to me. They appear as:
foo($$$$;$);
foo(\$\$\$);
What do the symbols ";" and "\" do or mean in these declarations?
They're "prototypes" - but Perl prototypes aren't like those in other languages, and probably shouldn't be used.
The $ denotes a scalar argument to the function. ;$ denotes an optional additional scalar argument. And \$ denotes a reference argument.
See also: Why are Perl 5's function prototypes bad?

What does ${1+"$#"} mean in a unix shell script? [duplicate]

This question already has answers here:
${1:+"$#"} in /bin/sh
(4 answers)
Closed 7 years ago.
I came across this construct while reviewing some older unix shell scripts, what does it mean, and why is is used?
${1+"$#"}
I found this explanation from unix haters handbook, page 152 of text (page 190 of the pdf). http://web.mit.edu/~simsong/www/ugh.pdf
It’s the way to exactly reproduce the command line arguments in the
/bin/sh family of shells shell script.
It says, “If there is at least one argument ( ${1+ ), then substitute in
all the arguments ( “$#” ) preserving all the spaces, etc. within each
argument.
If we used only “$#” then that would substitute to “” (a null argument)
if there were no invocation arguments, but we want no arguments reproduced in
that case, not “”.
Why not “$*” etc.? From a sh(1) man page:
Inside a pair of double quote marks (“”), parameter and
command substitution occurs and the shell quotes the results to
avoid blank interpretation and file name generation. If $* is
within a pair of double quotes, the positional parameters are
substituted and quoted, separated by quoted spaces (“$1
$2 …”); however, if $# is within a pair of double quotes, the
positional parameters are substituted and quoted, separated by
unquoted spaces (“$1” “$2” …).
I think ${1+“$#”} is portable all the way back to “Version 7 Unix.”
Wow! All the way back to Version 7.
Google should be your best friend! This is a similar questions asked almost 2 years ago.
https://unix.stackexchange.com/questions/68484/what-does-1-mean-in-a-shell-script-and-how-does-it-differ-from

What's the meaning of plain string when using as condition? [duplicate]

This question already has answers here:
How do I use boolean variables in Perl?
(9 answers)
Closed 8 years ago.
Below perl one-liner outputs hello to console, so how is false interpreted here since it is not a variable or literal string?
perl -e"if (false) {print 'hello'}"
In Perl false is true. The only terms that are evaluated as false are:
0, '0', '', (), (''), undef
FALSE/TRUE are not boolean values. They are called barewords and with use strict;, it will not even run.
From http://perldoc.perl.org/perldata.html
Barewords
A word that has no other interpretation in the grammar will be treated
as if it were a quoted string. These are known as "barewords".

Using concat() function, is specific cases [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need concatenate regexp pattern pieces, for this pattern, I use C-style Escape E.
If use concatenation operator ||, works:
E'a{'||2||'}'
does not make much sense, but just interes, how to concatenate same, using concat() function ?
The misunderstanding is this: C-style escapes are just another way to input string literals. When you concatenate strings, be it with the || operator or with the concat() function (Postgres 9.1+), the method how individual strings were input is irrelevant.
In addition to that, literals of other types (like the numeric constant 2 in your example) are coerced to text automatically.
On top of that, your example does not exhibit any characters with a special meaning in escape strings (like \).
SELECT E'a{' || 2 || '}';
SELECT concat(E'a{', 2, '}');
So, the E is totally irrelevant in this particular example.
Since you mention regexp patterns: those tend to have \ in them, which have to be escaped with \ in E'' notation:
SELECT E'\\.' || 2 || '\.';
The modern way is not to use escape strings at all if not necessary. That's why Postgres switched to standard_conforming_strings = ON with PostgreSQL 9.1. That is the setting I tested with.

What does =~ do in Perl? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I guess the tag is a variable, and it is checking for 9eaf - but does this exist in Perl?
What is the "=~" sign doing here and what are the "/" characters before and after 9eaf doing?
if ($tag =~ /9eaf/)
{
# Do something
}
=~ is the operator testing a regular expression match. The expression /9eaf/ is a regular expression (the slashes // are delimiters, the 9eaf is the actual regular expression). In words, the test is saying "If the variable $tag matches the regular expression /9eaf/ ..." and this match occurs if the string stored in $tag contains those characters 9eaf consecutively, in order, at any point. So this will be true for the strings
9eaf
xyz9eaf
9eafxyz
xyz9eafxyz
and many others, but not the strings
9eaxxx
9xexaxfx
and many others. Look up the 'perlre' man page for more information on regular expressions, or google "perl regular expression".
The '=~' operator is a binary binding operator that indicates the following operation will search or modify the scalar on the left.
The default (unspecified) operator is 'm' for match.
The matching operator has a pair of characters that designate where the regular expression begins and ends. Most commonly, this is '//'.
Give Perl Re tutorial a read.
The code is testing whether 9eaf is a substring of the value of $tag.
$tag =~ /9eaf/
is short for
$tag =~ m/9eaf/
where m// is the match operator. It matches the regular expression pattern (regexp) 9eaf against the value bound by =~ (returned by the left hand side of =~).
Operators, including m// and =~, are documented in perlop.
Regular expressions (e.g. 9eaf) are documented in perlre, perlretut.
That checks for a match of the scalar $tag (which is presumably a string) against the regular expression /9eaf/, which merely checks to see if the string "9eaf" is a substring of $tag. Check out perldoc perlretut.