473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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:
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!
Dec 29 '07 #1
4 1660
KevinADC
4,059 Expert 2GB
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);
Dec 29 '07 #2
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. }
Dec 30 '07 #3
eWish
971 Expert 512MB
t3chn0n3rd,

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

Regards,

Kevin
Dec 30 '07 #4
KevinADC
4,059 Expert 2GB
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.
Dec 30 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Giulio Belrango | last post by:
Hi I need someones help I'm working in an IBM 390 batch environment. What I'm trying to do is to call from a non DB2 COBOL program a DB2 COBOL program that will access a table and perform an...
3
by: SV | last post by:
Dear all, In my application I have a lot of hidden fields. I want to make them invisible for the users though for debugging reasons I want to make them visible. So I want to add these objects to...
7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
2
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! ...
3
by: IamIan | last post by:
I am using os.spawnv in Python 2.1 to do some geoprocessing in a subroutine/process. Everything works great, except when the processing is done the subroutine just waits for a couple minutes before...
3
by: Ameen | last post by:
I am very new to programming and VB.net, and I wrote the code below, but it seem to go on an infinite loop. Please tell me what I am doing wrong. Private Sub search(ByVal indexstart) Pos1 =...
10
by: nasau | last post by:
Perl, I have a main program which calls two subroutines (depending upon the report names).In the subroutine I am printing the data from CSV file using the format variable, Format_top prints the...
4
by: otterbyte | last post by:
Hi, I have a bit of code which is confusing me to no end. Here are the basics: 1) The class module is being used in the module of a form. 2) There is an instance of the object declared at the...
3
by: Heggr | last post by:
I am working on creating a new ASP page that needs to populate a Select object after three other fields have been populated. I am calling a VBScript subroutine to do this from the OnClick for the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.