pattern-matching

How does pattern matching work in Perl?

浪尽此生 提交于 2019-12-24 08:13:52
问题 I want to know how pattern matching works in Perl. My code is: my $var = "VP KDC T. 20, pgcet. 5, Ch. 415, Refs %50 Annos"; if($var =~ m/(.*)\,(.*)/sgi) { print "$1\n$2"; } I learnt that the first occurrence of comma should be matched. but here the last occurrence is being matched. The output I got is: VP KDC T. 20, pgcet. 5, Ch. 415 Refs %50 Annos Can someone please explain me how this matching works? 回答1: From docs: By default, a quantified subpattern is "greedy", that is, it will match as

Q: Match rows and Insert return value in SQL (Complex condition)

廉价感情. 提交于 2019-12-24 07:49:48
问题 This is the Problem Table: UNIQUE ID NAME TYPE PRICE PAYMENT METHOD Reference hbg5-5rdw-6ts Bagui RECEIVED 150 MANUAL CREDIT CARD asd4e-4rs-5tg Cams RECEIVED 100 CASH 181088 fg6gh-rfd4-tgv Cams TRANSFER 100 CASH 181088 a3accf-wrf-aw Chels RECEIVED 700 MANUAL COD 1sder-5tg7-gcd Chels SUCCESS 500 CHECK asde-1d-sedc Chels SUCCESS 500 CHECK 1sder-5tgs7-gcd5 Failed ased-asd-sedf Duzy RECEIVED 250 DEBIT 181077 5rt4w-4sd-zsd Duzy TRANSFER 250 DEBIT 181077 4er-445ff-thc Jose RECEIVED 300 CASH 157075

Understanding pattern matching in Elixir function parameters

﹥>﹥吖頭↗ 提交于 2019-12-24 07:45:03
问题 In the book 'Elixir in Action', one of the examples has a function that is tripping up my understanding of pattern matching: def add_entry( %TodoList{entries: entries, auto_id: auto_id} = todo_list, entry ) do entry = Map.put(entry, :id, auto_id) new_entries = HashDict.put(entries, auto_id, entry) %TodoList{todo_list | entries: new_entries, auto_id: auto_id + 1 } end The first parameter, %TodoList{entries: entries, auto_id: auto_id} = todo_list , the book explains "...Furthermore, you keep

Pattern matching against regex strange behaviour scala

冷暖自知 提交于 2019-12-24 06:51:27
问题 Can someone explain to me why is this happening, val p = """[0-1]""".r "1" match { case p => print("ok")} //returns ok, Good result "4dd" match { case p => print("ok")} //returns ok, but why? I also tried: "14dd" match { case p => print("ok") case _ => print("non")} //returns ok with: warning: unreachable code 回答1: You'll find the answer if you try to add a new option: "4dd" match { case p => print("ok") case _ => print("ko") } <console>:24: warning: patterns after a variable pattern cannot

OCaml : as keyword in pattern matching behaving strangely

﹥>﹥吖頭↗ 提交于 2019-12-24 06:49:17
问题 Let's say I write this code : # type t = A of int * int let f = function A (i1, i2) -> print_int i1;; type t = A of int * int val f : t -> unit = <fun> Perfect, it works. Now, let's say I have this wonderful function : # let print_couple (i1, i2) = print_int i1; print_int i2;; val print_couple : int * int -> unit = <fun> So, as you expect, I'd like to write the following # let f = function A (_ as c) -> print_couple c;; Well, I can't Error: The constructor A expects 2 argument(s), but is

How to get substring from a sql table?

倖福魔咒の 提交于 2019-12-24 05:52:11
问题 So I have a column (called account_uri) in a postgres table that looks like this: /randomCharacters/123456/randomNumbers I need to query for the substring in the middle, which is a string of characters between two / symbols. My current attempt looked like this: SELECT REVERSE(SUBSTRING(REVERSE([account_uri]),0,CHARINDEX('/',REVERSE(account_uri)))) FROM exp_logs LIMIT 15 Which selects only the randomNumbers and not the desired numbers. I tried to build on that idea though and used (SUBSTRING

How to get substring from a sql table?

笑着哭i 提交于 2019-12-24 05:52:11
问题 So I have a column (called account_uri) in a postgres table that looks like this: /randomCharacters/123456/randomNumbers I need to query for the substring in the middle, which is a string of characters between two / symbols. My current attempt looked like this: SELECT REVERSE(SUBSTRING(REVERSE([account_uri]),0,CHARINDEX('/',REVERSE(account_uri)))) FROM exp_logs LIMIT 15 Which selects only the randomNumbers and not the desired numbers. I tried to build on that idea though and used (SUBSTRING

Function to search recursively for patterns in vim

笑着哭i 提交于 2019-12-24 05:32:30
问题 I have a text file with simple text lines. I want to create a function for vim (and gvim ) text editor which can be sent a variable number of patterns and it should find lines will all patterns (in any order) and keep only these lines while deleting the rest. I searched the net and found some useful links but none that could do all above: Following will find and delete all lines not containing the pattern: :v/pattern/d Multiple searching and highlighting can be done with scripts as

Using pattern matching for Ordered.compare in Scala

℡╲_俬逩灬. 提交于 2019-12-24 05:23:48
问题 I have the following Scala class: case class Person(firstName: String, lastName: String, age: Int) extends Ordered[Person] { def compare(that: Person): Int = { if (this.lastName < that.lastName) -1 else if (this.lastName > that.lastName) 1 else if (this.firstName < that.firstName) -1 else if (this.firstName > that.firstName) 1 else this.age compare that.age } } to allow sorting by lastName, firstName, and age. How can I write this using pattern matching? I've come up with the following, but