How to understand such this kind of variable to combine _ and other charcters in Perl? - perl

How to understand such this kind of value in Perl?
my %opt = ( _argv => join(" ",#ARGV),_cwd = cwd()).
Are _argv and _cwd both strings?

From the reference:
The => operator (sometimes pronounced "fat comma") is a synonym for the comma except that it causes a word on its left to be interpreted as a string if it begins with a letter or underscore and is composed only of letters, digits and underscores. This includes operands that might otherwise be interpreted as operators, constants, single number v-strings or function calls. If in doubt about this behavior, the left operand can be quoted explicitly.
my %hash = ('a' => 'b', 'c' => 'd');
can be written as
my %hash = (a => 'b', c => 'd');

thanks for everyone, Now I think _argv and _cwd both are just a variable name, equals to "_argv" and "_cwd".

Related

String replacement array of strings in Scala

I'm confused as to how i can create a new string on the basis of another, replacing some values of the original string,
If i have
Array(easy_id, 1_sum(term_invested_points), 1_sum(std_invested_points), 1_sum(std_used_points), 1_sum(term_used_points), 9_sum(term_invested_points))
and want to produce
Array(easy_id, 1_sum_term_invested_points_, 1_sum_std_invested_points_, 1_sum_std_used_points_, 1_sum_term_used_points_, 9_sum_term_invested_points_)
i.e substitute brackets for underscores in my array.
I have tried
array.columns.map{ case "" => "("; case x => x }
However this just produces the original array, why doesn't it work?
You could do something like this:
val arr = Array(
"easy_id",
"1_sum(term_invested_points)",
"1_sum(std_invested_points)",
"1_sum(std_used_points)",
"1_sum(term_used_points)",
"9_sum(term_invested_points)"
)
arr.map(_.replaceAll("\\(|\\)", "_"))
Map inside the array and replace all opening or closing brackets with underscores (brackets need to be escaped with backslashes as they are Regex special characters).

How to strip everything except digits from a string in Scala (quick one liners)

This is driving me nuts... there must be a way to strip out all non-digit characters (or perform other simple filtering) in a String.
Example: I want to turn a phone number ("+72 (93) 2342-7772" or "+1 310-777-2341") into a simple numeric String (not an Int), such as "729323427772" or "13107772341".
I tried "[\\d]+".r.findAllIn(phoneNumber) which returns an Iteratee and then I would have to recombine them into a String somehow... seems horribly wasteful.
I also came up with: phoneNumber.filter("0123456789".contains(_)) but that becomes tedious for other situations. For instance, removing all punctuation... I'm really after something that works with a regular expression so it has wider application than just filtering out digits.
Anyone have a fancy Scala one-liner for this that is more direct?
You can use filter, treating the string as a character sequence and testing the character with isDigit:
"+72 (93) 2342-7772".filter(_.isDigit) // res0: String = 729323427772
You can use replaceAll and Regex.
"+72 (93) 2342-7772".replaceAll("[^0-9]", "") // res1: String = 729323427772
Another approach, define the collection of valid characters, in this case
val d = '0' to '9'
and so for val a = "+72 (93) 2342-7772", filter on collection inclusion for instance with either of these,
for (c <- a if d.contains(c)) yield c
a.filter(d.contains)
a.collect{ case c if d.contains(c) => c }

Trying to understand this code (creating a [char]range)

I have code that works, but I have no idea WHY it works.
This will generate a list containing each letter of the English alphabet:
[char[]]([char]'a'..[char]'z')
However, this will not:
[char]([char]'a'..[char]'z')
and this will actually generate a list of numbers from 97 - 122
([char]'a'..[char]'z')
Could any experts out there explain to me how this works (or doesn't)?
In your second example, you are trying to cast an array of characters to a single character [char]. That won't work. In the third example, the 'a' is considered a string by PowerShell. So casting it to [char] tells PowerShell it is a single char. The .. operator ranges over numbers. Fortunately, PowerShell can convert the character 'a' to its ASCII value 97 and 'z' to 122. So you effectively wind up with 97..122. Then in your first example, the [char[]] converts that array of ints back to an array of characters: a through z.
In Powershell 'a' is a [string] type. [char]'a' is, obviously a [char] type. These are very different things.
$string = 'a'
$char = [char]$string
$string can be cast as a [char] because it is a string, consisting of a single character. If there is more than one character in the string, e.g. 'ab' then you need an array of [chars], which is type [char[]]. The extra set of square brackets designates an array.
$string | get-member
$char | get-member
reveals much different methods for the two types. The [char] type has .toint() methods. If you cast it as [int], it assumes the numeric ASCII code for that character.
[int]$char
returns 97, the ASCII code for the letter 'a'.

How do you compare hashes for equality that contain different key formats (some strings, some symbols) in Ruby?

I'm using ruby 1.9.3 and I need to compare two hashes that have different key formats. For example, I want the equality of the following two hashes to be the true:
hash_1 = {:date => 2011-11-01, :value => 12}
hash_2 = {"date" => 2011-11-01, "value" => 12}
Any ideas on how these two hashes can be compared in one line of code?
Stringify the keys on the hash that has symbols:
> hash_1.stringify_keys
=> {"date"=>"2011-11-01", "value"=>12}
Then compare. So, your answer, in one line, is:
> hash_1.stringify_keys == hash_2
=> true
You could also do it the other way around, symbolizing the string keys in hash_2 instead of stringifying them in hash_1:
> hash_1 == hash_2.symbolize_keys
=> true
If you want the stringification/symbolization to be a permanent change, use the version with the bang !: stringify_keys! or symbolize_keys! respectively
> hash_1.stringify_keys! # <- Permanently changes the keys in hash_1 into Strings
=> {"date"=>"2011-11-01", "value"=>12} # as opposed to temporarily changing them for comparison
Ref: http://as.rubyonrails.org/classes/HashWithIndifferentAccess.html
Also, I'm guessing you meant to put quotes around the dates...
:date => "2011-11-01"
...or, explicitly instantiate them as Date objects?
:date => Date.new("2011-11-01")
The way you have the date written now sets :date to 2011-11-01 These are currently being interpreted as series of integers with subtraction in between them.
That is:
> date = 2011-11-01
=> 1999 # <- integer value of 2011, minus 11, minus 1

my understanding on this Perldata example [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
Perldata gives the following example.
$field = $query->radio_group(
name => 'group_name',
values => ['eenie','meenie','minie'],
default => 'meenie',
linebreak => 'true',
labels => \%labels
);
My understanding for this example is that , “radio-group” is used as a key for hash “query”. This key, “radio-group” , itself is a hashref, including name=’group_name’,etc. Moreover, this hashref, identified by “radio_group” is assigned to $field. If my understanding is wrong, please correct me.
$query->radio_group is a method call.
In this particular example the method is being called on the object instance $query with the hash containing the given key-value pairs.
It's the same as if the following had been written:
my %tmp = (
name => 'group_name',
values => ['eenie','meenie','minie'],
default => 'meenie',
linebreak => 'true',
labels => \%labels
);
$field = $query->radio_group(%tmp);
which should make it clear that the hashed set of parameters is independent of $query itself.
The result of that method call is then assigned to $field.
This is a call on the method "radio_group" from the object $query. There are no hashes there, just a bunch of parameter pairs separated by commas and fat commas (=>). From perlop:
The => operator is a synonym for the comma except that it causes its
left operand to be interpreted as a string if it begins with a letter
or underscore and is composed only of letters, digits and underscores.
This includes operands that might otherwise be interpreted as
operators, constants, single number v-strings or function calls. If in
doubt about this behavior, the left operand can be quoted explicitly.
Otherwise, the => operator behaves exactly as the comma operator or
list argument separator, according to context.
This is equivalent to writing:
my %params = (
name => 'group_name',
values => ['eenie','meenie','minie'],
default => 'meenie',
linebreak => 'true',
labels => \%labels
);
$field = radio_group( $query, %params );
So radio_group is not a hash key at all, but a subroutine/method/function.
The tell-tale signs are:
The absence of [] and {}, used to denote array indices and hash keys
The presence of () parens after radio_group