Connecting Tech Pros Worldwide Forums | Help | Site Map

Subroutine in a loop problem

Newbie
 
Join Date: Dec 2007
Posts: 1
#1: Dec 29 '07
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. sub str2bin
  3. {
  4.   my($tst) = @_;
  5.   use integer;
  6.   {
  7.        while($tst != 0)
  8.        {
  9.           $str1 = $tst%2;
  10.           $str = $str1.$str;
  11.           $tst = $tst/2;
  12.        }
  13.   }
  14.  
  15.   $j = 8-length($str);
  16.   for($i = 0;$i<$j;$i++)
  17.   {
  18.       $str = "0".$str;
  19.   }
  20.   return $str;
  21. }
  22.  
  23. sub convert
  24. {
  25.   my($phi) = @_;
  26.  
  27.   $len = length($phi);
  28.  
  29.   for($k = 0;$k<$len;$k++)
  30.   {
  31.     $new = str2bin(ord(substr($phi,$k,1)));
  32.     @chlist = (@chlist,$new);
  33.   }
  34.  
  35.   return @chlist;
  36. }
  37.  
Any help would be greatly appreciated.

Thanks!
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Dec 29 '07

re: Subroutine in a loop problem


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);
Newbie
 
Join Date: Dec 2007
Posts: 19
#3: Dec 30 '07

re: Subroutine in a loop problem


Expand|Select|Wrap|Line Numbers
  1. sub str2bin
  2. {
  3.   my($tst) = @_;
  4.   use integer;
  5.   {
  6.        while($tst != 0)
  7.        {
  8.           $str1 = $tst%2;
  9.           $str = $str1.$str;
  10.           $tst = $tst/2;
  11.        }
  12.   }
  13.  
  14.   $j = 8-length($str);
  15.   for($i = 0;$i<$j;$i++)
  16.   {
  17.       $str = "0".$str;
  18.   }
  19.   return $str;
  20. }
  21.  
  22. sub convert
  23. {
  24.   my($phi) = @_;
  25.  
  26.   $len = length($phi);
  27.  
  28.   for($k = 0;$k<$len;$k++)
  29.   {
  30.     $new = str2bin(ord(substr($phi,$k,1)));
  31.     @chlist = (@chlist,$new);
  32.   }
  33.  
  34.   return @chlist;
  35. }
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#4: Dec 30 '07

re: Subroutine in a loop problem


t3chn0n3rd,

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

Regards,

Kevin
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#5: Dec 30 '07

re: Subroutine in a loop problem


Quote:

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.
Reply