in reply to Re: Removing characters
in thread SOLVED Removing characters

Thanks for the help so far everybody!

I tried this, $mystring seems to return 31 instead of the formatted string.

open (READFILE, "<poem.txt") || die "Couldn't open file: $!"; $buffer = ""; $lines = 0; while(<READFILE>) { $chars += length($_); $lines++ ; my @word = split " ", $_; $word_cnt += @word; chomp($mystring); $mystring = $mystring += $_=~ s/\W//g; } print ("Lines: $lines\n"); print ("Words: $word_cnt\n"); print ("Characters: $chars\n"); print lc("$mystring\n");

Replies are listed 'Best First'.
Re^3: Removing characters
by Fletch (Bishop) on Jan 22, 2008 at 15:31 UTC

    Because despite being told that s modifies the string it's working on in place you persist in attempting to use its return value (the number of substitutions made) as something meaningful ($_ contains the converted string; you would want to append the contents of $_ to your accumulated buffer instead). Not to mention you're still trying to use + as a concatenation operator rather than the correct . operator.

    Update: Oop, you're using s/// instead of tr/// but the point's the same (that it modifies the string in place and doesn't have a meaningfull (in this instance) return value). Wording changed accordingly.

    And it's just been pointed out to me that that line wasn't yours, which only partly excuses you in that you cargo cultly included it without understanding that it was doing the very thing you'd already been warned about.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Fletch++, yep, I was wrong on that one... I only tested the s/.../...stuff on the command-line without really checking what the whole line looked like.
      Sorry to have confused.
      Regards,
      svenXY
Re^3: Removing characters
by svenXY (Deacon) on Jan 22, 2008 at 16:13 UTC
    Hi,
    almost there...
    #!/usr/bin/perl use strict; use warnings; my ($chars, $lines, @word, $word_cnt, $mystring, $bla ); $lines = 0; while(<DATA>) { $chars += length($_); $lines++ ; my @word = split " ", $_; $word_cnt += @word; ($bla = $_) =~ s/\W//g; # assign the converted string of $_ to $bl +a $mystring .= lc($bla); # concatenate lowercase $bla to $mystring } print ("Lines: $lines\n"); print ("Words: $word_cnt\n"); print ("Characters: $chars\n"); print("Total String: $mystring \n"); __DATA__ Hey, diddle, diddle, The cat and the fiddle.

    Regards,
    svenXY