Shawn H Corey <shawnhco...@gmail.com> writes:

> Harry Putnam wrote:
>> I thought I understood you but when I run a modified script (at the
>> end) I see empty elements where I don't expect them.
>>
>> The actual thing I'm working up to was splitting this input var
>>
>> `ev  100421 4'
>>
>> In reality I want to ditch `ev ' work with 100421 <for that part>
>> and finally use ` 4' for a quite different purpose.  So in the end
>> I'll use all but 'ev '.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use Data::Dumper;
>
> # Make Data::Dumper pretty
> $Data::Dumper::Sortkeys = 1;
> $Data::Dumper::Indent   = 1;
>
> # Set maximum depth for Data::Dumper, zero means unlimited
> $Data::Dumper::Maxdepth = 0;
>
> my $var = 'ev  100421 4';
> my @numbers = $var =~ /(\d+)/g;
> print Dumper \...@numbers;

Nice... that's a nifty way to get some clean numbers.

What I'm working on will eventually be a script that reads an `events'
file and lets me know about events I've entered there.  It's my own
primitive but hopefully effective calendar reminder type of tool.

The format of entries look like this:

cat ~/.events

  ev 100411 4
    Wash behind ears
  ev

  ev 100421 4
    Avoid a beating by taking out the garbage
    this evening.
  ev

 [...]

The multidigit numbers represents YYMMDD,

My script will read this file (from cron).  It has to get a year mnth
and mday out of there.  That date will be converted to epochal for
comparing with current date. (I think I've got that part worked out)

The trailing digit is optional.  If its there, the script will do the
necessary math to see if current time() is withing 4 (or N)
days of the incoming date.

If it is, the script fires off a text message to my phone with the
text body between `ev' and `ev' for the message.

It will do that everyday until the date is past.

Right now I wanted a dependable way to process the incoming
data, extracting the date.

Will this example code be a dependable way to do that, or will it be
touchy and likely to give unpredictable results?

Of course we are depending on the user (me) to keep the `~/.events'
file format within what the scripts regex will be able to use.

-------        ---------       ---=---       ---------      -------- 

#!/usr/local/bin/perl

use strict;
use warnings;

my ($year,$mnth,$mday,$daycnt);
my $line = shift;

print "
Splitting <$line> . . .
We want pairs of numbers:
First we ditch the lead stuff, then 
capture the last number with:

`(\$line = \$line) =~ s/^ev|\\s+|(\\d\\s*\$)//g;'
\$daycnt = \$1\n\n";

($line = $line) =~ s/^ev|\s+|(\d\s*$)//g;
$daycnt = $1;

print "Now we split what's left of \$line ($line) but 
skip the null strings with:

(\$year,\$mnth,\$mday) = (split(/(\\d\\d)/,\$line))[1,3,5];\n";

($year,$mnth,$mday) = (split(/(\d\d)/,$line))[1,3,5];

print "Hopefully we've now got the right pieces:

year=<$year>\nmnth=<$mnth>
mday=<$mday>\ndaycnt=<$daycnt>\n";

-------        ---------       ---=---       ---------      -------- 

output of:  ./split2 'ev 100421 4'

Splitting <ev 100421 4> . . .
We want pairs of numbers:
First we ditch the lead stuff, then 
capture the last number with:

`($line = $line) =~ s/^ev|\s+|(\d\s*$)//g;'
$daycnt = $1

Now we split what's left of $line (100421) but 
skip the null strings with:

($year,$mnth,$mday) = (split(/(\d\d)/,$line))[1,3,5];
Hopefully we've now got the right pieces:

year=<10>
mnth=<04>
mday=<21>
daycnt=<4>



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to