in reply to Length and Chomp ??
The code my $str = <STDIN>; graps "snape\n" - it includes the newline. That's the extra character. It's often helpful to print out your string, but do so in a way that shows hidden whitespace. I usually do it like this: print "str = [$str]\n"; - this puts brackets around it so that I can see if there are spaces or newlines at the end.
If I add chomp after the input for $str, I'm not getting any problem - I think you might be calling it as $str = chomp $str which is wrong. Just call chomp $str; like this:
So close :-)#!/usr/bin/perl -w use strict; use warnings; print "Enter the string ="; my $str = <STDIN>; chomp $str; #### here is the call to chomp print "Enter the size ="; my $n = <STDIN>; print "The Length of the string = ",length($str); if ($n <= length($str)){ my $x = pretty_print($str,$n); print "\nThe substring is = ",$x; } else{ print"\nThe length of the string is shorter than the size "; } sub pretty_print{ my ($dna,$len) = @_; my $substring = substr($dna,0,$len); return $substring; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Length and Chomp ??
by snape (Pilgrim) on Aug 18, 2009 at 21:21 UTC |