Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: foreach type of deal?

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


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

#!/usr/bin/perl use strict; use warnings; main(@ARGV); sub main { open(FH, "gtype.txt"); foreach( my $line = <FH> ) { s/^g//; #toolic's idea. } } close FH; #and kennethk's idea #!/usr/bin/perl use strict; use warnings; main(@ARGV); sub main { open(FH, "gtype.txt"); while( my $line = <FH> ){ substr($_,1); # Start with the second letter } }

So, using kennethk's input, I kind of hacked this up...? I can kind of see how it works...

I looked at the perldocs linked in the replies, and I'm still working through them trying to understand all the various differences.

the file I'm working with looks something like this:

gOrd_3342.png

gOrd_3343.png

500k of those going down a file. I need to leave all 500k of those with "Ord_xxxx.png. I have yet to try either of mentioned options, but my idea here is to figure this out and learn at the same time, and not simply get an answer I can plug in and call it quits. Thanks.

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

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

      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?

        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.

        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://1184767]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found