Ask a Question related to Ruby, Design and Development.

  1. #1

    Default array -> list

    Is there a way to convert an array to a (parameter-)list?

    I often find myself doing things like:

    year,mon,day,hour,minute = ParseDate.parse(datestring)
    printf("%04d-%02d-%02d %02d:%02d\n",year,mon,day,hour,minute)

    while it would be a lot nicer to do:

    printf("%04d-%02d-%02d %02d:%02d\n", ParseDate.parse(datestring)

    It is of course perfectly possible to redefine printf and make it check
    whether it is offered an array or a parameter list, but it would be a lot
    easier when ruby did so automatically.

    Alternatively, there could be something like Array#flatten which flattens
    an array to a list.

    --
    Wybo

    Wybo Dekker Guest

  2. Similar Questions and Discussions

    1. List from array .as
      in this code ... the list is reading from Array ... but i can not display the name of each of them... ... if you click the button, and then...
    2. Creating a 2dm array from a list
      I have a list of hrefs from three different sites which I have then stored into a 1dm array. There are around 40 elements (href's)in the 1dm array. ...
    3. list ($d, $row) = each ($array) by reference?
      Hi ! I always thought that while (list($d,$row) = each($array)){ $row = 5; } would operate by reference, so that $array is filled with 5...
    4. [PHP] Array to List
      All, Thanks for your help, but this shouldn't be so freaking hard to do. <sigh> 1) I have a multiple select input on one page that is populated...
    5. Array to List
      Hello, Coming from ColdFusion, this is difficult. CF has an ArrayToList() function. I can't find anything similar in PHP. I'm building a list...
  3. #2

    Default Re: array -> list

    >>>>> "W" == Wybo Dekker <wybo@servalys.nl> writes:

    W> printf("%04d-%02d-%02d %02d:%02d\n", ParseDate.parse(datestring)

    svg% ruby -rparsedate \
    > -e 'printf("%04d-%02d-%02d %02d:%02d\n", *ParseDate.parsedate(Time.now.to_s))'
    2003-11-15 13:31
    svg%


    Guy Decoux

    ts Guest

  4. #3

    Default Re: array -> list

    il Sat, 15 Nov 2003 21:23:09 +0900, Wybo Dekker <wybo@servalys.nl> ha
    scritto::
    >Is there a way to convert an array to a (parameter-)list?
    >
    >I often find myself doing things like:
    >
    > year,mon,day,hour,minute = ParseDate.parse(datestring)
    > printf("%04d-%02d-%02d %02d:%02d\n",year,mon,day,hour,minute)
    >
    >while it would be a lot nicer to do:
    >
    > printf("%04d-%02d-%02d %02d:%02d\n", ParseDate.parse(datestring)
    just use a * just before an array to make it a list of parameters.

    Btw, maybe you could just use
    print Date.strftime(yourformat)

    gabriele renzi Guest

  5. #4

    Default Re: array -> list

    Hi Wybo

    Wybo Dekker schrieb:
    >
    > Is there a way to convert an array to a (parameter-)list?
    Do you mean *anArray ?
    > I often find myself doing things like:
    >
    > year,mon,day,hour,minute = ParseDate.parse(datestring)
    > printf("%04d-%02d-%02d %02d:%02d\n",year,mon,day,hour,minute)
    >
    > while it would be a lot nicer to do:
    >
    > printf("%04d-%02d-%02d %02d:%02d\n", ParseDate.parse(datestring)
    I tested the following
    ---------------------------------------------------
    dateArray = [1973,11,9,11,30]
    printf("%04d-%02d-%02d %02d:%02d\n", *dateArray)
    ---------------------------------------------------
    It works.

    I don't have a class ParseDate with a method parse,
    but if it returns an Array, I think

    printf("%04d-%02d-%02d %02d:%02d\n", *ParseDate.parse(datestring))
    ^ ^

    will work.

    I hope, I understand your problem.

    Regards

    Fred from Wuppertal, Germany

    Fred Werne Guest

  6. #5

    Default Re: array -> list

    On Sat, 15 Nov 2003, ts wrote:
    > ruby -rparsedate \
    > -e 'printf("%04d-%02d-%02d %02d:%02d\n",
    > *ParseDate.parsedate(Time.now.to_s))'
    > 2003-11-15 13:31
    Great! That's just what I was looking for;
    I now could even find it in the book; thanks!

    --
    Wybo

    Wybo Dekker Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139