Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Modify code help

by jamen_98 (Initiate)
on Mar 26, 2004 at 19:45 UTC ( [id://340126]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that pulls the first line of the file below. The file looks like:

Label Location Pool Name Protection [NFK100L2] [MSL6000 Trinity: 1] MSL6000 01/19/2004 [NFK101L2] [MSL6000 Trinity: 5] MSL6000 01/19/2004

Many times it will only be 1 line long of data but it could be up to three. I need to extract the 1 and 5 and put it into a new file. Currently I only need to pull the first line, but in the near future I need unlimited.

This code works great for one line. I like the idea of just reading the line and then outputting it directly into the file, however when doing two lines, whether I use the \n or not, I only end up with the very last line (ie: 5). The code that works beautifully for one line is:

#!/usr/bin/perl -w use strict; system ("c:\\Perl\\DataPro\\convert.bat"); open(DPSLOTFILE, "< c:\\Perl\\DataPro\\newreport.txt") or die "Can't o +pen file: $!"; open(OUTFILE, "> c:\\Perl\\DataPro\\output.txt") or die "Can't open fi +le: $!"; while ( <DPSLOTFILE> ) { my $dpdrivloc = "MSL6000 Trinity"; if ( /\Q$dpdrivloc\E:\s*(\d+)/ ) { print OUTFILE "$1"; } } close OUTFILE; close DPSLOTFILE; open (OUTFILE, "< c:\\Perl\\DataPro\\output.txt") or die "Can't open f +ile: $!"; my $slotnumber = <OUTFILE>; print "$slotnumber"; system("c:\\Perl\\DataPro\\omnimm -eject \"MSL6000 Trinity\" $slotnumb +er -location \"blahblahblah\""); close OUTFILE;

Any ideas on how to edit this to write out more than one slot number to the out file is appreciated!
Thanks! Ben

Replies are listed 'Best First'.
Re: Modify code help
by tcf22 (Priest) on Mar 26, 2004 at 19:58 UTC
    This works fine for me.
    while ( <DATA> ) { my $dpdrivloc = "MSL6000 Trinity"; if ( /\Q$dpdrivloc\E:\s*(\d+)/ ) { print "$1\n"; } } __DATA__ Label Location Pool Name Protection [NFK100L2] [MSL6000 Trinity: 1] MSL6000 01/19/2004 [NFK101L2] [MSL6000 Trinity: 5] MSL6000 01/19/2004
    What is convert.bat doing? Because the perl looks to work fine.

    - Tom

Re: Modify code help
by dragonchild (Archbishop) on Mar 26, 2004 at 20:41 UTC
    Did you actually change your code after bringing it up the last time? It looks (and smells) awfully similar ...

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      Links to previous SOPW questions about the same problem:

      jamen_98, in general, please try to post only one question about a given topic. If you need help, please try to narrow down your question rather than asking the same one again and again. This is a rather basic Perl question, so perhaps you should buy a copy of the O'Reilly Camel Book (Programming Perl) and the Llama Book (Learning Perl) if we aren't able to answer your question after the first 3 times around. It think some added studying effort on your part is needed, we can't always be here to provide answers. We can love to help if you have a problem, but we need legwork from you -- it's "teaching a man to fish" if that aphorism is present in your neck of the woods.

Re: Modify code help
by Art_XIV (Hermit) on Mar 26, 2004 at 20:46 UTC

    The line 'my $slotnumber = <OUTFILE>;' is only ever going to get you one line from OUTFILE, but you may already know this. I'm kind of suprised that it's giving you a 5 instead of a 1, though, with the sample data that you provided.

    I'm almost afraid to offer advice since I may not fully grasp the issue, but would it not be an straight-forward matter to print each match to a separate line in OUTFILE, and then just read each match in a while loop as you eject?

    Update:
    It's probably not necessary to print anything to another file, unless you're going to need those values for another purpose, so your code could probably be simplified into something like:

    use strict; use warnings; system ("c:\\Perl\\DataPro\\convert.bat"); open(DPSLOTFILE, "< c:\\Perl\\DataPro\\newreport.txt") or die "Can't o +pen file: $!"; while ( <DPSLOTFILE> ) { my $dpdrivloc = "MSL6000 Trinity"; if ( /\Q$dpdrivloc\E:\s*(\d+)/ ) { #use match for system call! system("c:\\Perl\\DataPro\\omnimm -eject \"MSL6000 Trinity\" $1 -l +ocation \"blahblahblah\""); print OUTFILE "$1"; } } close DPSLOTFILE;
    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: Modify code help
by graff (Chancellor) on Mar 27, 2004 at 18:27 UTC
    Since you've been trying so often to get help on this task, and you're still not getting it, let's take a step back and look at the whole task, now that you've posted some code that seems to cover everything (or does it?)...
    • First, you run an ms-dos batch file (convert.bat). Does this take some data file and convert it into some other data file? Why wouldn't you just do that as part of your perl script? (Um, okay, maybe it's because you don't know Perl that well yet... or maybe convert.bat was provided by DataPro to format their more complex log format; nevermind.)
    • Next, you open and read a text file (newreport.txt). (Is this the one that "convert.bat" created, by any chance?) You capture these single digits from each line and save them to another output file (output.txt).
    • Finally, based on your final question, you want to read each line of output.txt and run a system call that will eject the tape corresponding to the saved digit.

    Maybe what you really want, then, is simply to run that last system call while you're reading "newreport.txt" -- this will save you a lot of trouble.

    #!/usr/bin/perl -w use strict; system ("c:\\Perl\\DataPro\\convert.bat"); open(DPSLOTFILE, "< c:\\Perl\\DataPro\\newreport.txt") or die "Can't open file: $!"; while ( <DPSLOTFILE> ) { if ( /MSL6000\s+Trinity\s+(\d+)/ ) { my $tapeid = $1; print "Running omnimm to eject $tapeid\n"; system("c:\\Perl\\DataPro\\omnimm -eject \"MSL6000 Trinity\" $ta +peid -location \"blahblahblah\""); } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-24 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found