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

could someone help me figure this out? I am trying to put a filename that could be any combination of M the date and 4 numbers at the end of it, into a variable. my script will prompt the user to enter the date (so I have this covered) my problem is how do I make it so it will take any number for the last four digits $sched_log="M$date\\****"; # I know this wont work
  • Comment on variable mixing numbers,characters & scalers

Replies are listed 'Best First'.
Re: variable mixing numbers,characters & scalers
by Zaxo (Archbishop) on Mar 29, 2002 at 20:50 UTC

    Like in the shell, say it as "M${date}${num}". You can give $num the form you want with sprintf. Or just use sprintf for the whole thing.

    After Compline,
    Zaxo

Re: variable mixing numbers,characters & scalers
by derby (Abbot) on Mar 29, 2002 at 20:47 UTC
    You could give us a little more info (code) 'cuz you don't say how you're getting the number. For simplicity sake, let's say your using a loop counter - then just use sprintf like so:

    $sched_log = sprintf( "%c%s\\%04d", "M", $date, $loop_counter);

    -derby

      Ok, here is the entire script:
      #!/usr/bin/perl -w print "Post Production Report program \n"; print "Enter the Date (yyyymmdd) for the report: \n"; $date= <STDIN>; chomp ($date); print "Enter report type (d for detail or s for summary): \n"; $type= <STDIN>; chomp ($type); if ($type =~ /^d\b/) { $reptr=-detail } else { $reptr=-summary } $1=~[\d0-9]; $2=~[\d0-9]; $3=~[\d0-9]; $4=~[\d0-9]; system `cd /tivoli/maestro/schedlog`; $sched_log="M$date$1$2$3$4"; `conman "reptr $reptr $sched_log"`;
      Basically the script is prompting you for a date of a report you want. The problem is is that the report name is always Myyyymmdd#### (# meaning any number) I need the code to be able take the date the user puts in and match that up with the file it finds and add the last 4 digits that are in the file name

      Thanks in advance for any help
      Bob
        ddrumguy,

        Okay. So you have a bunch of files in /tivoli/maestro/schedlog and you know the file begins with M$date but you don't know what the last 4 digits are? Correct? Will there only be one file that begins M$date or will there be multiples? Do you run the conman proc against only one at a time or can it accept many?

        I'd drop the $1, $2, $3, $4 stuff all together (I don't think its doing anything for you since $_ is not defined anywere - you should get warnings from these lines).

        What you want is opendir, grep, readdir and closedir.

        #!/usr/bin/perl -w use strict; print "Post Production Report program \n"; print "Enter the Date (yyyymmdd) for the report: \n"; chomp( my $date= <STDIN> ); print "Enter report type (d for detail or s for summary): \n"; chomp( my $type= <STDIN> ); my $reptr = substr( $type, 0, 1 ) eq "d" ? "-detail" : "-summary"; chdir( "tivoli" ) or die "cannot cd to directory"; opendir(DIR, ".") or die "cannot open directory: $!\n"; my @files = grep { /M$date/ } readdir(DIR); closedir DIR; foreach( @files ) { print "Would run: conman reptr $reptr $_\n"; }

        -derby