in reply to sequence of positions to binary sequence

To help us help you better, please show what code you've already written and how it fails to achieve your goal. Also, it helps if you describe to us in plain English what approach you try to create the "binary array".

A simple approach to a solution would be to recognize that the "binary array" contains only zeroes except at the places given in @pos. As Perl doesn't conveniently handle infinite lists in most places, you have to find an upper bound for the "binary array" list after which it will only ever contain zeroes, to effectively convert the infinite list into a finite list.

Replies are listed 'Best First'.
Re^2: sequence of positions to binary sequence
by coldy (Scribe) on Apr 16, 2009 at 08:20 UTC
    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
      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

      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.