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

Certainly learning

by monk2b (Monk)
on Oct 11, 2000 at 03:41 UTC ( #36157=perlquestion: print w/replies, xml ) Need Help??

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

The first perl scripts that I wrote started out:
print "Enter blah blah blah\n"; $device_id = <STDIN>;
Which was okay because I didn't have very many devices to keep track of.Then I learned:
$device_id = shift(@ARGV);
And this was much better ,I could call the script and input the argument at the same time.I now have even more devices and I need to automate the process even more.Can someone point out a better way to do this.What I need to do is read from a file where each line will contain 10 digits and nothing else. Then I will place each 10 digit number in as an argument.I hope I have explained my question well enough Thanks in adance for all of the help


Replies are listed 'Best First'.
Re: Certainly learning
by Adam (Vicar) on Oct 11, 2000 at 04:00 UTC
    I think you already understand the tools, so I will just point you in the right direction. You want to open the file and read each line from it. Do this thusly:
    use strict; # ALWAYS! my $filename = 'devices.txt'; # Hardcode the filename. # you could get the filename off the # command line using shift, but be # aware of the consequences of your # actions. open FILEHANDLE, $filename or die "Failed to open '$filename', $!"; while( <FILEHANDLE> ) # Read the file one line at a time. { chomp; m/\d{10}/ or warn "Unknown arg '$_', skipping\n" and next; # Process the device numbered $_ } close FILEHANDLE;
    Code is untested, but that is okay. It is meant to point you in the right direction, not solve everything... thats what they pay you to do.
Re: Certainly learning
by mirod (Canon) on Oct 11, 2000 at 04:00 UTC

    This is really basic stuff, and you should really grab a book (Learning Perl or Programming Perl for example) or even have a look at the docs.

    Now for the task at hand just use:

    #!/bin/perl -w use strict; my $file= shift or die "usage $0 <file>"; open( FILE, "<$file") or die "cannot open $file: $!"; while( my $device_id= <FILE>) { chomp $device_id; # removes the end of line unless( $device_id=~ /^\d{10}$/) { die "invalid device id: $device_id"; } # do your stuff here }

    Just make sure you understand what the code does before and why you should write it along those lines before using it.

    Update: Oh my god! There is no review for Programming Perl in the review section, link removed. Somebody _please_ post a review!

      Update: Oh my god! There is no review for Programming Perl in the review section, link removed. Somebody _please_ post a review!


Re: Certainly learning
by redcloud (Parson) on Oct 11, 2000 at 03:52 UTC
    Pass the filename on the command line, and then do
    while(<>) { # Inside the loop, $_ will contain each ten-digit number in turn }
Re: Certainly learning
by monk2b (Monk) on Oct 11, 2000 at 04:02 UTC
      More like:
      open(BLAH_HANDLE, blah_file) or die blah blah blah; while(<BLAH_HANDLE>) { my $device_id = $_; system "blah blah blah $device_id"; } close(BLAH_HANDLE);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://36157]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2020-08-14 23:43 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (77 votes). Check out past polls.