title: Changing the value in a map in Groovy
body: <p>This is about a very basic program I'm writing in Groovy.</p>
  
  <p>I have defined a map inside a method:</p>
  
  <pre><code>  def addItem() 
    {
      print("Enter the item name: ")
      def itemName = reader.readLine()
      print("Enter price : ")
      def price = reader.readLine()
      print("Enter barcode : ")
      def barcode = reader.readLine()
      data[itemName] = ['price' : price, 'barcode' : barcode]
    }
  </code></pre>
  
  <p>The problem is I don't know how to update just one value inside a different method. Here's what I tried:</p>
  
  <pre><code>  def updatePrice() 
    {
      print("Enter the item name: ")
      def itemName = reader.readLine()
      print("Enter new price : ")
      def price = reader.readLine()
      data[itemName] = ['price' : price]
    }
  </code></pre>
  
  <p>This sort of works. It changes the value of price but it also changes the barcode value to 'null' presumably because it's being overwritten with...nothing.</p>
  
  <p>Basically I need the code to change the price but leave the barcode as it is. Any ideas on how I can do this?</p>
  
  <p>Sorry if this is a ridiculously elementary question but I'm still very much a newbie at programming.</p>