in reply to Re: sequence of positions to binary sequence
in thread sequence of positions to binary sequence

Sorry, I forgot to mention the length of the array. Lets say for the example it is 15. My first thought was to create an array of 15 zeros, then insert the 1's at the positions given in @pos. Simple, but I dont know how to simply create the array of zeros. Thanks
  • Comment on Re^2: sequence of positions to binary sequence

Replies are listed 'Best First'.
Re^3: sequence of positions to binary sequence
by citromatik (Curate) on Apr 16, 2009 at 08:25 UTC
    but I dont know how to simply create the array of zeros

    Simple enough, see the "x" operator at perlop

    my @arr = (0) x 15

    citromatik

Re^3: sequence of positions to binary sequence
by Corion (Patriarch) on Apr 16, 2009 at 08:27 UTC

    I can think of many ways to fill an array with zeroes. For example, you could start with an empty array and put a zero at the first position. Once you have an array of zeroes of a given length, you can get an array with twice as many zeroes by appending the array to itself.

    What ideas have you come up with and what code have you written? This is not a site where we will do your homework for you. Most likely your course materials cover the knowledge necessary to master this task.

      Not all people who ask simple questions are looking for easy homework answers. At the risk of being naughty I did not supply my ugly code, apologies for not following protocol.

      If available, course materials are indeed useful but often lack the variety of ways of answering the problem. I am always amazed of the variety of solutions presented in forums, thanks to all who gave positive feedback.