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

I need to be reading a file and only working on the lines which contain a certain character (the character will be selected from the command line) and then do calculations on those lines. The rest of the lines should not be taken into consideration in the calculations. If the user doesn't define anything in the command line, the calculations should done on of the lines of the file. Thanks
  • Comment on Reading a file and working only on certain lines

Replies are listed 'Best First'.
Re: Reading a file and working only on certain lines
by toolic (Bishop) on Nov 11, 2014 at 18:34 UTC
    • Read perlintro.
    • Write some code.
    • Post back here with more detailed questions, if necessary.
Re: Reading a file and working only on certain lines
by wjw (Priest) on Nov 11, 2014 at 19:15 UTC

    A bit more info would be helpful. Read How do I Post a Question Effectively

    or

    My version of the same

    Your problem statement breaks down as follows from my perspective:

    • Get input from user
    • GetOpt::Long
    • Open file for reading
    • open

      If the file is not too large, you might consider reading it into and array(ex. my @lines)

    • Search for line(?s) containing user provided characters
    • grep might work well for you
    • Do calculation on lines found
    • maybe write a sub-routine to do your calculations?
    • Provide output to user(file, screen, web page,...?) Might want to consider this


    ... and always remember to:
    use strict; use warnings; etc...

    Hope that is helpful...

    ...the majority is always wrong, and always the last to know about it...

    Insanity: Doing the same thing over and over again and expecting different results...

    A solution is nothing more than a clearly stated problem...otherwise, the problem is not a problem, it is simply an inconvenient fact

Re: Reading a file and working only on certain lines
by 2teez (Vicar) on Nov 11, 2014 at 19:08 UTC

    Hi help_please
    Welcome to Monastery.
    You might want to try code some of the things you described, then others can help fill in areas where you don't get.
    I think it is also great to check How do I post a question effectively?

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Reading a file and working only on certain lines
by karlgoethebier (Abbot) on Nov 11, 2014 at 20:14 UTC

    Try this:

    #!/usr/bin/env perl + use strict; use warnings; my $char = shift || die $!; my $file = $0; open( my $fh, "<", $file ) || die $!; while (<$fh>) { print if /^o/; # your part goes here } close $fh; __END__

    Please see also strict, warnings, my, shift, die, while, open, print, if, close, perlretut as well as TMTOWTDI.

    For the rest: RTFM ;-)

    Update: Immediate feedback is a good thing. But please note: I really didn't want to be harsh or impolite. And i marked the dubious hint as a joke.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Reading a file and working only on certain lines
by ww (Archbishop) on Nov 12, 2014 at 15:55 UTC

    In addition to advice above re 'perlintro' and 'How do I ask a question,' please note:

    Homework should be labeled as such!

    And now you need to use Super Search to find out what else we expect of those asking homework questions.



    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.