Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This is what's in my file I'm reading from:
child bit 0
child bit 1
child bit 2
child bit 3
child bit 4
child bit 5
child bit 6

This is what prints out to my output file:
child bit
child bit
child bi
child b
child
child
chil

for some reason it's decrementing my string. Some of my
strings have a comma within them:

"Parent for Ground Discretes, with mix of multi-bit monitors"

It only prints up until the comma.
I'm just trying to read the data. This is how my code looks:

if ($descr <= 2000)
{
$descr =~ tr/",//d;
print ("$descr;");
}
else
{
print ("Description exceeds bounds of 2000 Characters;");
}
  • Comment on Can I read a text of characters and numbers as one string?

Replies are listed 'Best First'.
Re: Can I read a text of characters and numbers as one string?
by BrowserUk (Patriarch) on May 29, 2003 at 22:54 UTC

    Are you using chop somewhere in your code?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: Can I read a text of characters and numbers as one string?
by graff (Chancellor) on May 30, 2003 at 01:49 UTC
    You can check the Site How To section to learn how to post code correctly (putting "<code>" and "</code>" around it).

    You are using the wrong sort of test in the snippet of code that you provided. Given that "$descr" stores a string of some length, and you want to test whether or not it's longer than 2000 characters, you need to do it this way:

    if ( length( $descr ) <= 2000 ) { $descr =~ tr/",//d; # remove double-quotes and commas print $descr; } else { print "Description is too long (over 2000 characters)\n"; }
    Of course, that has nothing to do with the symptom of the shrinking string value. You haven't posted the part of the code relevant to that problem, but my guess is you might have a loop that looks something like this:
    $descr = <>; while (<>) { chop $descr; print $descr; }
    The problem here is that while (<>) reads a line of input into $_, not into $descr; but then inside the loop, you are truncating and printing $descr, which ends up being the same string, getting shorter at each iteration until chop has nothing left to work on and/or the while loop reaches the end of the input file. You should either use $_ throughout (instead of $descr) or else you should use:   while ( $descr = <> )
Re: Can I read a text of characters and numbers as one string?
by Zaxo (Archbishop) on May 29, 2003 at 22:41 UTC

    You need to show how you're reading the file and otherwise handling the data. Nothing you show will truncate lines like that.

    You have an error in the code you show that does not cause your problem. Your conditional probably should read if (length( $descr) <= 2000) { ... } .

    After Compline,
    Zaxo