How to join two array attributes if the second is != "" in Ruby - arrays

I have an array with two elements below:
if params["location"]
params["location"]["street"] =[
params["location"].delete("address1"),
params["location"].delete("address2")
].compact.join(", ")
l = ::Location.create!(street: params["location"]["street"],
city: params["location"]["city"],
state: params["location"]["state"],
zip: params["location"]["postal"],
country: params["location"]["country"])
What I am trying to do is join the two together sperated by a ", " only if address2 is an empty string/nil.
Example 1:
address1 = "56 West Gay Street"
address2 = "Apt. 211"
Actual: "56 West Gay Street, Apt.211"
Expected: "56 West Gay Street, Apt.211"
Example 2:
address1 = "56 West Gay Street"
address2 = ""
Actual: "56 West Gay Street, "
Expected: "56 West Gay Street"

The problem is that params['location']['address2'] is not empty, but an empty string. You can use present? to select only strings that are not blank.
if params['location']
street = [
params['location'].delete('address1'),
params['location'].delete('address2')
].select(&:present?).join(', ')
l = ::Location.create!(params['location'].merge('street' => street))
end

Assuming a and b are your two string:
[a,b].select(&:present?).join(", ")

Related

Loop Until Condition Is Met

I am extremely new to coding so please bear with me.
I am trying to create a loop until a condition is met, namely, an email address. This is what I have so far:
first = input("Please enter your first name: ")
surname = input("Please enter your surname: ")
email = input("Please enter your email address: ")
emailconfirm = input("Please confirm email address: ")
if email != emailconfirm:
input ("Emails are different. Please kindly confirm email address: ")
cell = input("Please enter cell number: ")
How do I code to enter a cell/ mobile number without it printing until the emailconfirm == email condition is met?
I know this may be simple to many but it is VERY early days for me. Many thanks for the assistance.
Graeme
Tried tutorials but I just cannot get it right :(
You need to use a while loop and update the value of emailconfirm in each iteration
email = input("Please enter your email address: ")
emailconfirm = input("Please confirm email address: ")
while email != emailconfirm:
emailconfirm = input("Emails are different. Please kindly confirm email address: ")

count the distance between to strings in an array Ruby

i have an array
line_one = ["flinders street", "richmond", "east richmond", "burnley", "hawthorn", "glenferrie"]
user_input1 = "flinders street"
user_input2 = "glenferrie"
how could I count the distance between the two strings?
expected output 5.
The first thing that comes to mind:
line_one = ["flinders street", "richmond", "east richmond", "burnley", "hawthorn", "glenferrie"]
user_input1 = "flinders street"
user_input2 = "glenferrie"
(line_one.find_index(user_input1) - line_one.find_index(user_input2)).abs
#=> 5
line_one = ["flinders street", "richmond", "east richmond", "burnley", "hawthorn", "glenferrie"]
Code
p (line_one.index("flinders street")...line_one.index("glenferrie")).count
output
5

How to select data from array of json object in snowflake

Data I have:
ID
Value
1
[{"code": "SM", "place": "San Mateo, CA, USA"},{"code": "IND", "place": "Indianapolis, IN, USA"}]
What I want it to be transformed as:
ID
Value
1
San Mateo, CA, USA; Indianapolis, IN, USA
This is as far as I can get (table is just a sample)
SELECT
INDEX,
PARSE_JSON(f.THIS),
ARRAY_TO_STRING(PARSE_JSON(f.THIS),';')
FROM TABLE(FLATTEN(input => parse_json('[{"code": "SM","place": "San Mateo, CA, USA"},{"code": "IND","place": "Indianapolis, IN, USA"}]'))) f LIMIT 1;
You're close:
SELECT
seq,
listagg(f.value:place, '; ')
FROM TABLE(FLATTEN(input => parse_json('[{"code": "SM","place": "San Mateo, CA, USA"},{"code": "IND","place": "Indianapolis, IN, USA"}]'))) f
group by seq
-- San Mateo, CA, USA; Indianapolis, IN, USA

Scala extracting values from arrays and nested arrays

I have an array: Array[(String, (Array[String], Int))]
I need to extract:
first element
last element; and 3rd and 4th elements elements of the inside array[Strings]
data from 3 records: I want to display only the bolded items
Array((27120,(Array(27120, 2011-12-01 09:59:17.0, 2013-09-07 08:29:37.0, Dale, Hanson, 2578 Ingram Road, Inglewood, CA,
90309),8)),
(92694,(Array(92694, 2013-01-25 04:11:10.0, 2013-12-04 02:31:35.0, Stacy, Allbritton, 4990 Clearview Drive,
Sacramento, CA, 94215),2))
(40581,(Array(40581, 2012-04-03 17:53:32.0, 2013-12-10 22:46:16.0, Norman, Scanlon, 312 Ocala Street, Sacramento, CA,
95761),2))
)
data should look like:
27120 8 Dale Hanson
92694 2 Stacy Allbritton
40581 2 Norman Scanlon
Thanks!
Given your data in in format, Array[(Int, (Array[String], Int))], you will have to map over the array and extract the information you need.
your data structure looks like
Tuple(integer, Tuple(Array, integer)))
| | | |
| | | |
_1 _2 | |
_2._1 _2._2
example,
scala> val data = Array((27120,(Array("27120", "2011-12-01 09:59:17.0", "2013-09-07 08:29:37.0", "Dale, Hanson", "2578 Ingram Road, Inglewood, CA, 90309"),8)),
(92694,(Array("92694", "2013-01-25 04:11:10.0", "2013-12-04 02:31:35.0", "Stacy, Allbritton", "4990 Clearview Drive, Sacramento, CA, 94215"),2)),
(40581,(Array("40581", "2012-04-03 17:53:32.0", "2013-12-10 22:46:16.0", "Norman, Scanlon", "312 Ocala Street, Sacramento, CA, 95761"),2)))
data: Array[(Int, (Array[String], Int))] = Array((27120,(Array(27120, 2011-12-01 09:59:17.0, 2013-09-07 08:29:37.0, Dale, Hanson, 2578 Ingram Road, Inglewood, CA, 90309),8)), (92694,(Array(92694, 2013-01-25 04:11:10.0, 2013-12-04 02:31:35.0, Stacy, Allbritton, 4990 Clearview Drive, Sacramento, CA, 94215),2)), (40581,(Array(40581, 2012-04-03 17:53:32.0, 2013-12-10 22:46:16.0, Norman, Scanlon, 312 Ocala Street, Sacramento, CA, 95761),2)))
scala> data.map(tuple => (tuple._1, tuple._2._2, tuple._2._1(3)))
res20: Array[(Int, Int, String)] = Array((27120,8,Dale, Hanson), (92694,2,Stacy, Allbritton), (40581,2,Norman, Scanlon))

Printing the records one below the other

I want to print data like this :
FirstName FamilyName (33 characters), Tel: xxxxxxxxxx, Age: xx, Level: xx, Salary: xxxxx.xx
For example
John Dilbert Tel: 6135202600, Age: 58, Level: 13, Salary: 450.34
Jane Smith Tel: 6135202600, Age: 47, Level: 10, Salary: 133450.00
Main problem am facing is how to put Tel: one before the other . Also how to format the floating salary such that decimal comes one below the other.
Currently my output look like this :
John Dilbert Tel: 6135202600, Age: 58, Level: 13, Salary: 450.34
Jane Smith Tel: 6135202600, Age: 47, Level: 10, Salary: 133450.00
Code :
void printData(struct person currentPerson){
printf("%s %-33sTel: %10s",currentPerson.firstName,currentPerson.familyName,currentPerson.telephone);
}
void printStudent(struct Student currentStudent){
printf("GPA:%3d, Courses:%3d, Tuition: %5f\n",currentStudent.GPA,currentStudent.coursesCount,currentStudent.tuitionFees);
}
In main:
printData(person[i]);
printf(", ");
printStudent(person[i].student);
But still result is wrong. Why ? Please help
If you want to control the width of your data, then you could use the width sub-specifiers in the printf format string. Eg. :
printf("%5d", 2);

Resources