| | |
substr problem
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I'm having a problem with the substr function. I want it to be dynamic in a for loop as follows:
You can probably guess what this code is meant to do. I want to read each character in the string separately and operate on it in the for loop. The problem is that this code doesn't do what I want it to do. Is there a way to use substr for this? Is there a better way to do what I'm trying to do, possibly without substr? Thanks in advance for any help.
perl Syntax (Toggle Plain Text)
for ($i = substr $bin, $sub, 1 ; $sub < $len2 ; $sub++){
use split instead:
Perl Syntax (Toggle Plain Text)
my $var = 'abcdefghijklmnop'; my @chars = split(//,$var); foreach my $char (@chars) { #do something with $char }
Thanks, that worked. I was making a binary to decimal converter. Here's the code if anyone's interested:
perl Syntax (Toggle Plain Text)
#!usr/bin/perl -w print "Binary number: \n"; $bin = <STDIN>; chomp $bin; $dec = 0; $exp = (length($bin)) - 1; @bin = split(//,$bin); chomp @bin; foreach $bit (@bin){ $dec += $bit * (2 ** $exp); $exp--; } print "$dec\n";
![]() |
Similar Threads
- substr problem (C++)
- image verification problem (PHP)
- csv problem (C++)
- Serious problem here.. Warning: mysql_query(): 2 is not a valid............ (MySQL)
- Preview Upload Image Problem (PHP)
- int problem (C++)
- Problem of sorting words of each string using pointers (C++)
- search and results problem (PHP)
Other Threads in the Perl Forum
- Previous Thread: Using this perl code behind a router: this one is definitely a puzzle
- Next Thread: New to Perl HELP.
Views: 845 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Perl





