Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: REGEX on multiple lines

by igotlongestname (Acolyte)
on Jan 31, 2007 at 15:14 UTC ( [id://597544]=note: print w/replies, xml ) Need Help??


in reply to Re: REGEX on multiple lines
in thread REGEX on multiple lines

Thank you sir, that was exactly what I needed. I attempted to incorporate what Graff said below, but quickly found that it what he said is above my level, at least at the moment. I still am quite new and learning things, so thank you so much for your suggestion and coding. I'll attach what I ended up with here in case you see something "bad" or whatever, I simply added what I wanted before it, and allowed a variable definition in the regex search. I understand what Graff was saying enough to catch that what I'm doing is a bad idea, but in time I'll learn better ways and make do with my limited knowledge for now. Thanks to you and graff both!
#!/usr/local/bin/perl -w use strict; use warnings; print "Enter the output file you would like to analyze: "; chomp (my $filename = <STDIN>); print "Enter the isotope you want to extract (ex: Am-241): "; chomp (my $iso= <STDIN>); open(IN, "<", $filename) or die("Can't read the source:$!"); open(OUT, ">Comp_$filename"); select (OUT); $/ = '0AVERAGE COMPOSITION IN PINS.'; while (<IN>) { next if ! /$iso/; chomp; print "$/$_"; }

Replies are listed 'Best First'.
Re^3: REGEX on multiple lines
by GrandFather (Saint) on Jan 31, 2007 at 19:39 UTC

    Mostly what Graff was suggesting was that you should get your input from the command line parameters rather than prompt the user for them. That makes it easier to use the script in an automated context where you may wish to do several runs with different parameters perhaps. In essence it means replacing:

    print "Enter the output file you would like to analyze: "; chomp (my $filename = <STDIN>); print "Enter the isotope you want to extract (ex: Am-241): "; chomp (my $iso= <STDIN>);

    with something like:

    # Validate parameters @ARGV == 2 or error ("Too few parameters"); $ARGV[0] =~ /[A-Z][a-z]?-\d{1,3}/ or error ("Expected an isotope first +"); -f $ARGV[1] or error ("<$ARGV[1]> is not a file"); # Extract parameters my ($iso, $filename) = @ARGV; ... sub error { my $msg = shift; print <<"USAGE"; Error: $msg. ExtractIso parses a given dibbly report file and extracts the record for a given isotope. Use as: ExtractIso <iso> <filename> <iso> is the isotope to be extracted. For example Am-214. <filename> is the filename (with path as required) of the record file. For example: Extract Am-214 dibbly.dat would extract and print the record for Am-214 from the dibbly.dat file in the current directory. USAGE exit -1; }

    The error sub uses a HEREDOC to provide an error diagnostic and usage information.


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-23 13:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found