Uploaded image for project: 'Puppet'
  1. Puppet
  2. PUP-2894

Assign multiple variables from an array

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Normal
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: PUP 4.1.0
    • Component/s: Compiler, Docs
    • Labels:
      None
    • Template:
    • Epic Link:
    • Story Points:
      1
    • Sprint:
      Language 2015-04-29
    • Release Notes:
      New Feature

      Description

      when using the future parser, it allows for comma separated statements, and assignment to occur in these.
      However, I would like to "splat" an array, using a similar syntax which currently is not supported

      $input = 'example.org:www:filters:gzip text/html'
      $title, $vhost, $valve, $filter = split_titles($input)
       
      notice("\ntitle: ${title}\nvhost: ${vhost}\nvalve: ${valve}\nfilter: ${filter}")
      

      This produced (with --parser=future) the following output:

      title: main
      vhost: 
      valve: 
      filter: [example.org, www, filters, gzip text_html]
      

      We would like to have a syntax, such as

      $input = 'example.org:www:filters:gzip text/html'
      [ $title, $vhost, $valve, $filter ] = split_titles($input)
       
      notice("\ntitle: ${title}\nvhost: ${vhost}\nvalve: ${valve}\nfilter: ${filter}")
      

      Which produces the following output:

      title: example.org
      vhost: www
      valve: filters
      filter: gzip text_html
      

      FOR DOCS

      Multiple variables can be assigned in turn from an array. The variables on the LHS must be in an Array and there must be an equal number of
      values in the assigned value array. If the value is not an Array it is treated as an Array of one element. Multi variable assignment supports sub-arrays
      of variables to match sub-arrays of values.

      # examples
      [$a, $b, $c] = [1,2,3]      # $a = 1, $b = 2, $c = 3
      [$a, [$b, $c]] = [1,[2,3]]  # $a = 1, $b = 2, $c = 3
      [$a, $b] = [1, [2]]         # $a = 1, $b = [2]
      [$a, [$b]] = [1, [2]]       # $a = 1, $b = 2
      

      QA

      risk: medium
      probability: medium
      severity: medium/low (work arounds available)
      test layer: unit

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                igalic Igor Galić
                QA Contact:
                Kurt Wall
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Zendesk Support