Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^4: foreach type of deal?

by hoagies (Initiate)
on Mar 15, 2017 at 20:56 UTC ( [id://1184771]=note: print w/replies, xml ) Need Help??


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

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?

Replies are listed 'Best First'.
Re^5: foreach type of deal?
by huck (Prior) on Mar 15, 2017 at 21:19 UTC

    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!

        ug... will "too early in the morning" work at 4pm?

        while( my $line = <$in> ) { $line = substr($line,1); print $out $line; }

Re^5: foreach type of deal?
by kennethk (Abbot) on Mar 15, 2017 at 21:46 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1184771]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 05:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found