# How can I access struct fields

**URL:** <https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259>\
**Category:** New to Julia\
**Tags:** dates, argument, function, vector\
**Created:** [October 4, 2022, 10:08pm UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259 "2022-10-04T22:08:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali1](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@ali1](https://discourse.julialang.org/u/ali1)\
**Post date:** [October 4, 2022, 10:08pm UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/1 "2022-10-04T22:08:49Z")

</div>

Hello Everyone! I am new to Julia and I am trying to create a function that checks if it is possible to insert a request in between 2 reservations. First I want to check the (Time) interval between the reservations, and if it is less than the length of the request, only then i can insert. I have the following;

using Dates

mutable struct Request  
arrival::DateTime  
departure::DateTime  
numGuest::Number  
end

mutable struct Reservation  
checkIn::DateTime  
checkOut::DateTime  
extras::Number  
end

function insertRequestInBetweenReservation(reservation1::Reservation, reservation2::Reservation, request::Request)  
reservation\_interval = Time(reservation2.checkIn - reservation1.checkOut).value  
request\_duration = Time(request.departure - request.arrival).value #get the duration of the request  
if(request\_duration \> booking\_interval) #check if the duration for the request can fit into the interval between the Reservations  
return nothing  
else  
#some code  
end  
return reservation\_interval  
end

Is it possible to use reservations::Vector{Reservation} instead of reservation1 and 2? if so, how can I check the time interval in this way? I am thinking of something of the following…

function insertRequestInBetweenReservation(request::Request, reservations::Vector{Reservation})  
left\_hand\_booking = 1 #the first reservation  
right\_hand\_booking = left\_hand\_booking +1 #the next reservation and I want to check if it is possible to insert in between  
reservation\_interval = reservations[left\_hand\_booking]  
end

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 4, 2022, 11:26pm UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/2 "2022-10-04T23:26:22Z")

</div>

Hi. A few things are unclear. What is an example of a request, a dinner reservation? What is an example of a reservation, a room reservation?  
Why do you need to check the time interval between reservations, as a reservation has its own span `check_out - check_in` – probably I am misinterpreting some of your intent. It would be helpful if you were to clarify what is intended with words rather than code.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [October 4, 2022, 11:26pm UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/3 "2022-10-04T23:26:55Z")

</div>

Something like this

```julia
function insertRequestInBetweenReservation(request::Request, reservations::Vector{Reservation}_
  left_hand_booking = 1 #the first reservation
  right_hand_booking = left_hand_booking + 1
  reservation_interval = Time(reservations[right_hand_booking].checkIn - reservations[left_hand_booking].checkOut).value
  request_duration = Time(request.departure - request.arrival).value #get the duration of the request
  if(request_duration > reservation_interval) #check if the duration for the request can fit into the interval between the Reservations
     nothing
  else
     insert!(reservations,left_hand_booking + 1, newreservation)
  end
  return reservation_interval
end

```

By the way, if you wrap your code between triple backticks it will be formatted nicely!

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [October 4, 2022, 11:36pm UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/4 "2022-10-04T23:36:08Z")

</div>

Sounds like a hotel reservation. So you can only fit a reservation between to preexisting ones if the duration of the new one fits between the checkout of the first reservation and the checkin of the next one, no?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 4, 2022, 11:53pm UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/5 "2022-10-04T23:53:53Z")

</div>

I have never asked to reserve a second hotel room in the hotel where I stayed while staying there. I do not follow the intent. Part of the difficulty is that the request struct and the reservation struct seem much the same. Why both then?  
@ali1 we are looking to be helpful … please provide a brief description of what you want to accommodate.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [October 5, 2022, 12:01am UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/6 "2022-10-05T00:01:42Z")

</div>

Not the original poster but if you are a hotel manager and someone asks for a reservation for Thursday, you will have to check your rooms. If you have a room that is reserved for Wednesday and Friday, then you can offer that one.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 5, 2022, 12:02am UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/7 "2022-10-05T00:02:35Z")

</div>

so this is a database query and insert sort of thing? just without the database.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 5, 2022, 12:18am UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/8 "2022-10-05T00:18:35Z")

</div>

@aramirezreyes Thank you for walking me through. I follow the intent.

---

<div class="post-metadata">

**Author:** ![ali1](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@ali1](https://discourse.julialang.org/u/ali1)\
**Post date:** [October 5, 2022, 11:47am UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/9 "2022-10-05T11:47:25Z")

</div>

> [@aramirezreyes](#):
>
> triple backticks

Thanks, this helped a lot

---

<div class="post-metadata">

**Author:** ![ali1](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@ali1](https://discourse.julialang.org/u/ali1)\
**Post date:** [October 5, 2022, 12:03pm UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/10 "2022-10-05T12:03:56Z")

</div>

> [@ali1](#):
>
> > [@aramirezreyes](#):
> >
> > triple backticks

The reservation struct holds a list of requests that were accepted. New users can send a request, and that is what the request struct is for (like @aramirezreyes explained, thanks). I understand it is much easier to check for overlapping (i.e if the request overlaps with existing reservation). But I have in mind a plan this scenario to EV car-sharing in which apart from the overlapping, I also have to check if the SoC (state of charge) will enough for the 3 reservation (left\_hand\_booking, new request that is inserted and right\_hand\_booking) without needing to recharge.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 5, 2022, 12:21pm UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/11 "2022-10-05T12:21:55Z")

</div>

Have you worked with a relational database?  
This is the sort of application which will tend to overexercise simple datastructures as the number of cars, drivers, passengers, passengers requesting pickup increase (let alone the calendar interdependencies).

We have support for the more common databases at [JuliaDatabases](https://github.com/JuliaDatabases?type=source). SQLite is reasonably nonthreatening – however jumping into relational tables if you have not any prior familiarity may be simple or may be not simple (as works for you, or not).

Otherwise, take a long look and study DataFrames.jl. You have many people on Discourse who would help you with questions as they arise, and there are tutorials [here is one](https://github.com/bkamins/Julia-DataFrames-Tutorial).

---

<div class="post-metadata">

**Author:** ![ali1](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@ali1](https://discourse.julialang.org/u/ali1)\
**Post date:** [October 5, 2022, 12:53pm UTC](https://discourse.julialang.org/t/how-can-i-access-struct-fields/88259/12 "2022-10-05T12:53:37Z")

</div>

Thank you.
