Beginners question - directions please... 
Author Message
 Beginners question - directions please...

Hi Guys,

Got a special little project.  I have a specific config file in the format
listed at the  end of this post.

I am looking for some pointers to how the following can be acheived:

I need to read the file into a perl program and process it.

The idea is that the perl program will be started, read in the file
and on the specified week day, at the specified time, start sending the
listed files to an output device, one after the other.  Each file name
is followed by a single digit which denotes the number of times
the file is processed by the output device (Controlled by an
external program) with 1 being = once, 2-9 being = 2 - 9 times
and 0 being infinite (10000 times).

The processing of the files is done by the external program and
perl does not need to deal with the multiple play times specified
after each file name (The external program does this and gives an
exit code when done).
I could set the file to play once always and let perl do the repeats
as needed.

When the last file on a given start time is playing multiple times
(Looping),  the next time (On the same output device) should result
in the looping being aborted and a new set of files being played etc.

I have done some work like getting the day of the week from
the RTC and ensuring that it is in large caps.  I also know how to
get the files sent to the external program with the appropriate switches
to be able to process the files.

My biggest perolem is that I do not know how to get from the
given file format to an array with a suitable format that can be
run, something like:

MON 0 02:00 (/some/path/file.name_1 1, /some/path/file.name_2 1, etc)
MON 1 12:00 (/some/path/file.name1 1, /some/path/file.name_2 1, etc)
TUE 0 15:00 (/some/path/file.name1 1, /some/path/file.name_2 1, etc)

Maybe this is the wrong approach....

It may be better to scan the entire file in a loop matching against the
day of the week, then picking up the output device number, then matching
the play time and finally grabbing the files that follow - I don't know.

Any suggestions.

Please note that I am new to all this but have read a lot on perl,
various tutorials and FAQ's...(Have I started something to big...?)

Best regards

Tony

Begin file format.
======================
MON       # The day of the week to take action...
0         # The output device to use...0 in this case
02:00     # The time to process the listed files    
12:00     # The next time to process the listed files
[...more times, one per line...]     # There may be up to 24 times
/some/path/file.name_1 1     # The first file to send to device 0,
/some/path/file.name_2 2     # The second file to device 0
/some/path/file.name_3 0     # The 3rd file to device 0
[...more files, there may be up to 10 or so...]
MON     # The day of the week to take action...
1        # The output device to use...1 in this case
05:00
16:00
[...several times, one per line...]
/some/path/other_file_1.name 1
/some/path/_other_file_2.name 2
/some/path/other_file_3.name 0
TUE
0
02:00
12:00
[...several times, one per line...]
/some/path/new_file_1.name 1
/some/path/new_file_2.name 2
/some/path/new_file_3.name 0
WED
0
13:00
[...continues like this for the whole week...]

--
------------------------------------------------------------
I can be contacted here: anthony_w  at  bigpond  dot  com
(No spam mail here please)
------------------------------------------------------------



Fri, 07 Oct 2005 01:14:08 GMT  
 Beginners question - directions please...
Dear Tony -

Quote:

> My biggest perolem is that I do not know how to get from the
> given file format to an array with a suitable format that can be
> run,

I think that the crux of your problem is that the file format you're
using is awkward.  You should provide information that describes the
data so you don't have to write code to "recognize" data types.

Therefore change:

MON       # The day of the week to take action...
0         # The output device to use...0 in this case
02:00     # The time to process the listed files    
12:00     # The next time to process the listed files

to:

DAY:MON       # The day of the week to take action...
DEVICE:0         # The output device to use...0 in this case
PTIME:02:00     # The time to process the listed files    
NTIME:12:00     # The next time to process the listed files

Once you can easily recognize lines you can use a regular expression
with the match operator to assign substrings to variables:

if ( m/^DAY/ ) {
        ( $label, $day ) =~ /(\w*):(\w*)/;

Quote:
}

Or to an array:


Even better is to format the config as XML.  XML's whole shtick is
being easy to read (and write) with programs.  perl leverages this
very well with the XML::Simple CPAN module.  All you have to say is
something like:

use XML::Simple;
my $config_href = XMLin('scheduled_dump.xml');

You'll find installing XML::Simple a little challenging it has
extensive pre-requisites, but it's well worth the effort.

You'll also need to create the XML files.  It's just text so however
you were going to create the config file will work.  Personally I'd
create a data structure that contains the information that belongs in
the config file.  Then use XMLout to convert the data structure into
XML.  Then I'd use the CGI module an write up a little web front-end
to eliminate typos in the config files.

     Yours -      Billy

============================================================

                          http://www.goedsole.com:8080      
============================================================

          Lest we forget:

Finesse not force.

                - William Goedicke



Fri, 07 Oct 2005 12:48:46 GMT  
 Beginners question - directions please...
Tony,

While I fully agree with William that a different file format may make
this problem significantly simpler, I also realize that there are
times when you can't do anything about the format of the file you are
parsing and just need a solution. The following code should do the
trick with the help of one fairly complicated regex...

#!/usr/bin/perl

use strict;
use warnings;


undef $/; # slurp entire file, make sure the file isn't so
          # big it takes up all your RAM
my $file = <>;
$file =~ s!\s*(\#.*)?$!!mg;    # Remove any comments and whitespace

while( $file =~ s/
          (MON|TUE|WED|THU|FRI|SAT|SUN)\n    # The day
          (\d+)\n                            # The device
          ((\d{1,2}:\d{1,2}\n)+)            # The times
          ((.+[ \t]+\d+\n)+)                # The files
        //mx )
{

  my ($day, $dev, $t, $f) = ( $1, $2, $3, $5 );

  # Re-format each file
  $f =~ s/\n/, /mg;

  # print "($day; $dev; $t; $f)\n";

  # Parse each time
  while( $t =~ s/
            (\d{1,2}:\d{1,2})\n              # Each time
          //mx )
  {
    # print "$day $dev $1 ($f)\n";


  }

Quote:
}



Quote:
}

> Hi Guys,

> Got a special little project.  I have a specific config file in the format
> listed at the  end of this post.

> I am looking for some pointers to how the following can be acheived:

> I need to read the file into a perl program and process it.

> The idea is that the perl program will be started, read in the file
> and on the specified week day, at the specified time, start sending the
> listed files to an output device, one after the other.  Each file name
> is followed by a single digit which denotes the number of times
> the file is processed by the output device (Controlled by an
> external program) with 1 being = once, 2-9 being = 2 - 9 times
> and 0 being infinite (10000 times).

> The processing of the files is done by the external program and
> perl does not need to deal with the multiple play times specified
> after each file name (The external program does this and gives an
> exit code when done).
> I could set the file to play once always and let perl do the repeats
> as needed.

> When the last file on a given start time is playing multiple times
> (Looping),  the next time (On the same output device) should result
> in the looping being aborted and a new set of files being played etc.

> I have done some work like getting the day of the week from
> the RTC and ensuring that it is in large caps.  I also know how to
> get the files sent to the external program with the appropriate switches
> to be able to process the files.

> My biggest perolem is that I do not know how to get from the
> given file format to an array with a suitable format that can be
> run, something like:

> MON 0 02:00 (/some/path/file.name_1 1, /some/path/file.name_2 1, etc)
> MON 1 12:00 (/some/path/file.name1 1, /some/path/file.name_2 1, etc)
> TUE 0 15:00 (/some/path/file.name1 1, /some/path/file.name_2 1, etc)

> Maybe this is the wrong approach....

> It may be better to scan the entire file in a loop matching against the
> day of the week, then picking up the output device number, then matching
> the play time and finally grabbing the files that follow - I don't know.

> Any suggestions.

> Please note that I am new to all this but have read a lot on perl,
> various tutorials and FAQ's...(Have I started something to big...?)

> Best regards

> Tony

> Begin file format.
> ======================
> MON       # The day of the week to take action...
> 0         # The output device to use...0 in this case
> 02:00     # The time to process the listed files    
> 12:00     # The next time to process the listed files
> [...more times, one per line...]     # There may be up to 24 times
> /some/path/file.name_1 1     # The first file to send to device 0,
> /some/path/file.name_2 2     # The second file to device 0
> /some/path/file.name_3 0     # The 3rd file to device 0
> [...more files, there may be up to 10 or so...]
> MON     # The day of the week to take action...
> 1        # The output device to use...1 in this case
> 05:00
> 16:00
> [...several times, one per line...]
> /some/path/other_file_1.name 1
> /some/path/_other_file_2.name 2
> /some/path/other_file_3.name 0
> TUE
> 0
> 02:00
> 12:00
> [...several times, one per line...]
> /some/path/new_file_1.name 1
> /some/path/new_file_2.name 2
> /some/path/new_file_3.name 0
> WED
> 0
> 13:00
> [...continues like this for the whole week...]



Fri, 07 Oct 2005 19:29:53 GMT  
 Beginners question - directions please...

Hi Mark (And others),

Thank you for your assistance.  I have tried to understand
what goes on here with Perl Beginners books :-) and some
on-line works at "library.perl.org" but can not get a full
understanding of what is happening.

The script does not do anything when run, the cursor simply
just sits there on the next line (No output).

I have added the file format again at the end of the post with a few
more entries.

Please look again and let me know if there is something I am overlooking.

Tony

Here is what I did to get the data from the "config" file:

#!/usr/bin/perl
use strict;
use warnings;

### My part added...

my $movies = "/opt/FTG/movie_schedule";   # The path in my test setup...
open (MOVIE_SCH, $movies) or die "Can not open: $movies: $!\n";  

close(MOVIE_SCH) or die "Can not close: $movies: $!\n";  Play safe :-)



# Your array is above...

### What does this do?
undef $/;    # slurp entire file, make sure the file isn't so
                  # big it takes up all your RAM

### Does this take data line by line and assign it to a scalar?
my $file = <>;
$file =~ s!\s*(\#.*)?$!!mg;    # Remove any comments and whitespace

### Will the scalar   $file  not loose all the info?

### I am a bit lost here...It would seem the program gets
### stuck even before getting here.
while( $file =~ s/
          (MON|TUE|WED|THU|FRI|SAT|SUN)\n    # The day
          (\d+)\n                            # The device
          ((\d{1,2}:\d{1,2}\n)+)            # The times
          ((.+[ \t]+\d+\n)+)                # The files
        //mx )
{

  my ($day, $dev, $t, $f) = ( $1, $2, $3, $5 );

  # Re-format each file
  $f =~ s/\n/, /mg;

  # print "($day; $dev; $t; $f)\n";

  # Parse each time
  while( $t =~ s/
            (\d{1,2}:\d{1,2})\n              # Each time
          //mx )
  {
    # print "$day $dev $1 ($f)\n";


  }

Quote:
}



Quote:
}

### End of script

Quote:

>> My biggest problem is that I do not know how to get from the
>> given file format to an array with a suitable format that can be
>> run, something like:

>> MON 0 02:00 (/some/path/file.name_1 1, /some/path/file.name_2 1, etc)
>> MON 1 12:00 (/some/path/file.name1 1, /some/path/file.name_2 1, etc)
>> TUE 0 15:00 (/some/path/file.name1 1, /some/path/file.name_2 1, etc)

>> Maybe this is the wrong approach....

>> It may be better to scan the entire file in a loop matching against the
>> day of the week, then picking up the output device number, then matching

[...snip...]

Begin file format.
======================
MON
0
00:00
04:00
08:00
12:00
16:00
20:00
/movie/trailer.mpg 1
/advert/buy.mpg 1
/movie/movie.mpg 1
/advert/buy.mpg 1
/movie/previews.mpg 0
MON
1
02:00
06:00
10:00
14:00
18:00
22:00
/movie/trailer.mpg 1
/advert/buy.mpg 1
/movie/movie.mpg 1
/advert/buy.mpg 1
/movie/previews.mpg 0
MON
3
01:00
05:00
09:00
13:00
17:00
21:00
/movie/trailer.mpg 1
/advert/buy.mpg 1
/movie/movie.mpg 1
/advert/buy.mpg 1
/movie/previews.mpg 0
MON
4
03:00
07:00
11:00
15:00
19:00
23:00
/movie/trailer.mpg 1
/advert/buy.mpg 1
/movie/movie.mpg 1
/advert/buy.mpg 1
/movie/previews.mpg 0
TUE
0
00:00
04:00
08:00
12:00
16:00
20:00
/movie/trailer.mpg 1
/advert/buy.mpg 1
/movie/movie.mpg 1
/advert/buy.mpg 1
/movie/previews.mpg 0
TUE
1
02:00
06:00
10:00
14:00
[...continues like this for the whole week...]

--
------------------------------------------------------------
A linux user...(No spam mail here please)
------------------------------------------------------------



Sat, 08 Oct 2005 06:56:03 GMT  
 Beginners question - directions please...

Hi Mark (And others),

Just replying to my own message - I can confirm that
Mark's work does in fact work just fine...it was me that
wrongly added to the code....

I should have put the config file on the command line after the
program file but did not see it until reading up on the
my file = <>; statement :-(

The program works perfectly!

I would like to do:

my $movies = "/opt/FTG/movie_schedule";   # The path in my test setup...
open (MOVIE_SCH, $movies) or die "Can not open: $movies: $!\n";  

close(MOVIE_SCH) or die "Can not close: $movies: $!\n";  # Play safe :-)


be identical to the "movie_schedule" file.

I will be reading up a bit more before the next question....

Quote:

> Hi Mark (And others),

> Thank you for your assistance.  I have tried to understand
> what goes on here with Perl Beginners books :-) and some

[...very big snip...]

--
------------------------------------------------------------
A linux user...(No spam mail here please)
------------------------------------------------------------



Sat, 08 Oct 2005 14:45:54 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Beginner NEEDS DIRECTION

2. Beginners question please help on the verge of throwing PC out the window

3. Please Help with 10 line programm -- beginner questions

4. Please advise on directions

5. Please advise on directions

6. Please Point me in the right direction

7. Please help me with some direction

8. Please please help a beginner...

9. URL re-direction question

10. Please tell me I'm beginner

11. Please help a beginner in PEARL and CGI!

12. PERL For NT: Please Help A Beginner!

 

 
Powered by phpBB® Forum Software