in reply to Re^2: foreach type of deal?
in thread foreach type of deal?

Using my $line = <FH> means $_ never gets set so your next line needs to now be $line=substr($line,1);

Replies are listed 'Best First'.
Re^4: foreach type of deal?
by hoagies (Initiate) on Mar 15, 2017 at 20:56 UTC

    Thanks! I just ran into that little bit :D.

    However, I read the perldoc about substr, and I get the idea. Now my question is, what modifies the string? substr just locates the particular part of the string I want to single out, then what?

    #!/usr/bin/perl use strict; use warnings; main(@ARGV); sub main { open(FH, "gtype.txt"); while( my $line = <FH> ) { my $line = substr($line,1); printf $line; } } close FH;

    It works!! now what can I use to replace the printf, to use to delete?

      Subject to huck's well placed concerns about moving files on lists that haven't been manually inspected, look up rename or File::Copy. It's very easy to use these powerful tools to torpedo yourself.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      now what can I use to replace the printf, to use to delete?

      You have now entered a situation i dont like. You seem to want to want to replace the original file. for the most part i dont like to do that (at first). I dont like to modify my only copy of the original data. What i do instead is write a new file, then use the new file in later processing.

      use strict; use warnings; main(@ARGV); sub main { open(my $in ,'<', "gtype.txt"); open(my $out ,'>', "fixed-gtype.txt"); while( my $line = <$in> ) { my $line = substr($line,1); print $out $line; } } close $in; close $out;
      Note also the change from printf to print. You should read about what printf does in regards to the first argument being a format. What you wanted instead was print.

        Wow, I'm thinking I have a lot to learn. I was with you until the in, out deal. I never even considered that I had to use something like that. You're right, I shouldn't have been thinking about making a change to the original copy.

        I tried the code, and it works, but it will only work if I comment out use strict mode. Otherwise, I'm getting errors on declarations. I'm currently trying to figure out how to declare this code correctly. As I said, the code works like a charm, but I want to see if I can at least get the declarations correct. :/.

        I learned a few things with this, but I wouldn't have come close to figuring this out without you guys. Thanks!