Ruby: find what was replaced with .sub - ruby

Given the code
thisString.sub! /piece\_[0-9]{1,4}.ts/, "piece_#{i}.ts"
where thisString could be anything from piece_1.ts to piece_9999.ts
How can I get the number that was replaced?

You can use a numbered capturing group:
thisString.sub! /piece\_([0-9]{1,4}).ts/, "piece_#{i}.ts"
Now $1 will give you the value as a string and you can do $1.to_i.
You can read more about how this works here.

As a stopgap, I ended up getting the number first, then doing the sub:
foundStartPiece = Integer(firstline[/piece\_[0-9]{1,4}.ts/][/[0-9]{1,4}/]) # Returns the number as an integer
Edit: After that, I did move to using #ndn's answer

Related

How to implement substring function?

I have used substring function in composer and the output should be the number extracted from the given value. Here the condition reaches to false branch.
By following the doc I have used below steps but I am not getting the output. It will be very helpful if somebody provides the solution.
I have used "= substring('${dialog.Str}', 4, 7) " as well.
I want 1234 only intger part to be stored in a property.
Thanks...
use like this ${substring(dialog.str, 4,4)} .. you will get the answers 1234 ..
substring - (string, starting Idx, total length)

How to efficiently generate a unique combination of first and last name given an array of full names

I'm having a bit of difficulty with a problem I'm having. I have an array of names like so:
[Brutananadilewski, Carl]
[Crews, Xander]
[Cartman, Eric]
[Rubio, Daniel]
[Daniels, Julie]
etc. etc.
What I need to do is to create a list of unique names from this list without having first and last names repeated. So I would have the following as a result:
[Brutananadilewski, Daniel]
[Crews, Erix]
[Cartman, Xander]
[Rubio, Carl]
[Jill, Daniels]
The problem I'm having is trying to do this efficiently. My first instint was to use permutation and here is a snippet from the ruby docs
a.permutation(2).to_a #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
The problem is having the following from that example [[1,2],[1,3]]
Theoretically if this was a first/last name this wouldnt work. I couldn't have this:
[Rubio Daniel, Rubio Julie, Rubio Eric]
Has anyone dealt with this before? I'm having an awfully hard time with efficiency and just getting it to work. Help would be appreciated thank you.
You can use transpose as mentioned by 3limin4tor, then shuffle and zip as mentioned by Dave Newton:
surnames, forenames = names.transpose
shuffled_forenames = forenames.shuffle
shuffled_names = shuffled_forenames.zip(surnames)
The desired outcome isn't entirely clear from the question, but if you're trying to create all combinations of first/last names and get a subset of those, you could also use product and uniq to get all the uniq combinations:
names = [
%w(Brutananadilewski Carl),
%w(Crews Xander),
%w(Cartman Eric),
%w(Rubio Daniel),
%w(Daniels Julie)
]
surnames, forenames = names.transpose
all_name_combos = forenames.product(surnames).uniq
You could then use shuffle and sample(INTEGER) to get a subset of those name combinations
all_name_combos.shuffle.sample(5)

Return the count of courses using satisfies contains XQuery

I have been struggling to return the count of courses from this XML file that contain "Cross-listed" as their description. The problem I encounter is because I am using for, it iterates and gives me "1 1" instead of "2". When I try using let instead I get 13 which means it counts all without condition even when I point return count($c["Cross-listed"]. What am I doing wrong and how can I fix it? Thanks in advance
for $c in doc("courses.xml")//Department/Course
where some $desc in $c/Description
satisfies contains($desc, "Cross-listed")
return count($c)
The problem I encounter is because I am using for
You are quite correct. You don't need to process items individually in order to count them.
You've made things much too difficult. You want
count(doc("courses.xml")//Department/Course[Description[contains(., "Cross-listed"]])
The key thing here is: you want a count, so call the count() function, and give it an argument which selects the set of things you want to include in the count.

Creating a variable to measure string length

So I'm curious as to what I'm missing here. I have a program for school and part of the program requires that I measure the length of the input string. I have it laid out as "if String==6" which you can see in my code below. My professor would rather it be stored in a variable and that I use the .length method to measure it. His exact words are as follows, "To see if the ticket number is greater than six characters, you need to store it in a variable. Then, on line 19, you can check it by using ticket.length == 6."
I tried using his method and I put "ticket_number.length==6." but that returns an error. Im not sure why, isnt "ticket_number" the variable that needs measured? Or do I need to create another variable just for ticket length? I'm sure there is an easy answer, I just cant seem to find it. Thanks in advance for any and all help!
begin
print "Please enter your six-digit ticket number."
ticket_number=gets.chomp.to_i
ones_digit=ticket_number%10
truncated_number=ticket_number/10.floor
remainder=truncated_number%7
if String=6 and ones_digit==remainder and ticket_number>0
print "Your ticket number is valid."
else
print "Your ticket number is invalid."
end
end while ticket_number>0
There's a couple of problems here but the biggest one is that converting to an integer means you've forfeited your opportunity to test vs. length:
ticket_number = gets.chomp
if (ticket_number.length != 6)
puts "Your ticket number must be six digits"
next
end
You can convert after the fact:
ticket_number = ticket_number.to_i
Then do your math.
Ideally you'd wrap this up in a function that, given a ticket number, will return true or false depending on validity. This de-couples it from your display and looping logic, simplifying things.

VBA:Compare string with find function value

How to compare with case sensitive in find function?
Like
stringtxt = Range("A1:A10").Find(What:=stringtxt , Lookat:=xlWhole,MatchCase:=False)
Here suppose stringtxt="ABCD (abcd)"
AND in range one of the cells may have value like "ABCD (ABCD)"
I have to compare this with contents in range specified.
I tried adding like "UCase".Didn`t work.MatchCase i tried with both true and false values.Didnt work though.
Suggest me some better answers to compare.
One of the parameters to the Find method is a MatchCase parameter, as follows:
Think that should help you solve this problem.

Resources