Subroutine in a loop problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashashi87
    New Member
    • Dec 2007
    • 1

    #1

    Subroutine in a loop problem

    I'm extremely new to perl. I decided to pick it up recently and was writing some elementary code just to get the hang of things. In doing so I've come across a stumbling block for which I couldn't find a solution on google.

    I am working on a program to convert a string to its binary representation.
    I'm picking an inefficient way to do this for the sake of learning perl;
    I have a subroutine to convert individual characters to 8 bit binary strings.
    I am calling this subroutine inside a loop that extracts each character of a string. The binary representation of each character is then appended to an array. This array would then contain the binary representation of the input string.
    My problem is, the output of the conversion subroutine within the loop includes the output from the last call to that subroutine. I can't seem to find a way to resolve this. Is this some sort of buffering issue?

    Here is my code:
    Code:
    sub str2bin
    {
      my($tst) = @_;
      use integer;
      {
           while($tst != 0)
           {
              $str1 = $tst%2;
              $str = $str1.$str;
              $tst = $tst/2;
           }
      }
    
      $j = 8-length($str);
      for($i = 0;$i<$j;$i++)
      {
          $str = "0".$str;
      }
      return $str;
    }
    
    sub convert
    {
      my($phi) = @_;
    
      $len = length($phi);
    
      for($k = 0;$k<$len;$k++)
      {
        $new = str2bin(ord(substr($phi,$k,1)));
        @chlist = (@chlist,$new);
      }
      
      return @chlist;
    }
    Any help would be greatly appreciated.

    Thanks!
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    this line is probably the problem:

    @chlist = (@chlist,$new);

    you assign the current value of the list to the list with $new on the end. You probably mean to use the push function instead to add only $new to the end of @chlist:

    push (@chlist,$new);

    Comment

    • t3chn0n3rd
      New Member
      • Dec 2007
      • 19

      #3
      [code=perl]sub str2bin
      {
      my($tst) = @_;
      use integer;
      {
      while($tst != 0)
      {
      $str1 = $tst%2;
      $str = $str1.$str;
      $tst = $tst/2;
      }
      }

      $j = 8-length($str);
      for($i = 0;$i<$j;$i++)
      {
      $str = "0".$str;
      }
      return $str;
      }

      sub convert
      {
      my($phi) = @_;

      $len = length($phi);

      for($k = 0;$k<$len;$k++)
      {
      $new = str2bin(ord(sub str($phi,$k,1)) );
      @chlist = (@chlist,$new);
      }

      return @chlist;
      }[/code]
      Last edited by eWish; Dec 30 '07, 03:31 PM. Reason: Added Code Tags

      Comment

      • eWish
        Recognized Expert Contributor
        • Jul 2007
        • 973

        #4
        t3chn0n3rd,

        When posting code samples please use the &#91;CODE]&#91;/CODE] tags. This will preserve the whitespace and make the code more readable.

        Regards,

        Kevin

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Originally posted by eWish
          t3chn0n3rd,

          When posting code samples please use the tags. This will preserve the whitespace and make the code more readable.

          Regards,

          Kevin

          What was the purpose of his post? Did he change something in the code? Seems to have the same problem I noted in my post.

          Comment

          Working...