From your pasted snippet, the biggest problem was spaces - between the $
and the RE var name and a space after the RE string - this works:
my $dateST = qr{\d{4} \d{2} \d{2}};
my $dateEND = qr{\d{2} \d{2} \d{4}};
my $line;
while (DATA)
{
s#[-/]# #g;
print D: $_;
my $line = $_;
You're supposed to use the \1 format to match a current match,
like a duplicated word
$ echo her here hear hear hop hip hip ho! | perl \
-pe 's/(\w+)\s+\1\s+/double ${1}s /g;'
Barry B wrote:
I am confused about this. I thought that a back-reference looks like
$1, not \1. Is there a
Sorry, I really don't do the concept justice - a more comprehensive answer
is to say - if you want help w/ REs, get Mastering Regular Expressions
by J. Friedl
http://oreilly.com/catalog/9780596528126/
It's it a great, great book, up there w/ the Camel and Perl Best
Practices. It covers more
$ cat test.txt |perl -pe 's/(\w+)([A-Z])/\1\. \2/g'
made. Style
facilitated. One
Anti-magnetic. Quality
RE pedanticism: \1 et alia are only supposed to be used on the LHS of the
subst cmd. You'd want:
cat test.txt |perl -pe 's/(\w+)([A-Z])/\1\. \2/g'
or no need for cat (ye olde pipeline debate
The part of the code below does not do this, saying no such path or
directory.
Thanks
opendir DIR, $dat0 or die opendir $dat0: $! ($^E);
while ($_ = readdir DIR) {
next if -d $_;
print unlink $_;
unlink $_, or warn unlink $_' failed: $! ($^E);
}
closedir DIR;
Rember, readdir does
use warnings;
%parts = {};
$parts{'ms51957-101'} = 1207;
$parts{'ms51957-102'} = 17;
$parts{'ms51957-103'} = 1;
print Number of Records in hash: , scalar keys %parts, \n;
Number of Records in hash: 4
To my surprise, this is wrong; there are only 3 records in the
You can build your own sendmail msg and MIME sections, depending on the
attachment content. If it's a binary (jpg, exe etc) you'll have to find a
way to Base64 (or some such) encode it but otherwise, you can just build up
a msg and header and open up a sendmail -t and write your header and msg
Yeah, not sure 'map' is going to give you a trick here, unless there is
some other source that has the list of init values. You could try:
map { $$_ = 'init' } qw(a b c d e f);
but that's just wrong - map in a void context *and* the ref var name
thingee (
Can't use string (a) as a SCALAR ref
Yes. There are 2 hours each year when this formula is wrong.
No, there are 2 hours each year where the 'time' fields wouldn't be exactly
24 hours earlier, but the date should be skookum
More seriously, isn't this code subtracting a day twice?
my ($mday, $mon, $year) = (localtime(time() - 60
This occurs on the 23th hour in the fall and the 0th hour in the spring.
This program shows a brute force way
to find these that might convince you [1].
Slick! I'm convinced. Hmm, so if you need it to be ever accurate (and want
to stay w/ localtime() you need to track changes in the DST
now I'm confused ...
my ($isdst) = ( localtime() ) [8];
my ($mday, $mon, $year, $prev_isdst) = (localtime(time - 60 * 60 * 24) )[
3,4,5, 8];
printf(%d/%02d/%02d\n, $year + 1900, $mon + 1,
$isdst == $prev_isdst ? $mday
: $isdst $prev_isdst ? (localtime($t - 60 * 60 * 23) )[ 3]
:
What i am trying to do is,i have to run multiple perl programmes all at
once
to change the contents of a text file. and the text file would contain 2
things. Test name and status.
Yeah, I'd say this is more a computer science/methodology question, rather
than a perl code question. Using a
Just some standard pointer first - dont' say PERL. It's not an acronymn.
Standardly capitalized Perl when speaking of the language in general,
and lc perl when talking about specific code or script. But never PERL.
Thanks. You can google the backstory.
Next - it is important to use
# The following gives an error that is_running.al is missing.
# Tried threads-list (threads::running) to search and compare: gave
# bareword error.
Tried the code on linux, got the same error so activeperl is off the hook.
list should be:
$thr-list()
The perldocs show no is_running method,
Brian:
Starting Thread Test
This is the child thread (0 sec)
This is the main thread (1 sec)
This is the main thread (2 sec)
This is the child thread (2 sec)
This is the main thread (3 sec)
This is the main thread (4 sec)
This is the child thread (4 sec)
This is the main thread (5 sec)
This is the
I'd be looking at the server's http error log. anything the script kicked
out, msg-wise will be there. You should also put some info in your 'open'
stmt's die clause:
open(FILE,/home/server/file.txt) or die;
s/b:
open(FILE,/home/server/file.txt)
or die Can't write home/server/file.txt: $!;
if ($buffer =~ /MARKER/)
{
$buffer = $
}
Not that I'm an RE guru or anything but my code could look like
something like this:
# consider the marker to be '#'
$buffer = $1 if ($buffer =~ /(.*)#/);
You might also look at using /g and, say, a while loop:
while ( $buffer =~
didyou expect the comments to be removed automatically? Hmm (note the
@ary/@data change)
my %hash = map { split /\s\|/ } grep { /\|/ } @data; # line 76
works:
000: Somewhere/something/thisplace/someplace/Things/here//Value
001: Somewhere/something/thisplace/someplace/Things/here//Value
Is there any kind of Perl scheduling module/system which will allow me
to
abstract the scheduling from the tasks to be done?
May be overkill but there's the WebGui kit:
http://www.webgui.org/
Much much more than a scheduler but ...
a
Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
my %foo = ( '01' = 'abc/SID/def',
'02' = 'bcd/SID/fgh',
'03' = 'hjk/SID/opq' );
to
$foo{'01'} = 'abc/12345/def';
$foo{'02'} = 'bcd/12345/fgh';
$foo{'03'} = 'hjk/12345/opq';
$foo{$_} =~ s/SID/12345/ for keys %foo;
a
Andy Bach
Systems
English to perl translation:
s/SID/12345/ for values %foo;
Nice - this takes advantage of the fact that $_ here is a reference to the
actual value of the hash so that the subst. works directly on the hash.
The same fact works for arrays too
@foo = ('abc/SID/def', 'bcd/SID/fgh',
Is there a Perl function that returns the index of a given element in an
array? For example:
my @list = q( apple banana pear grapefruit);
my $look4 = banana;
my $ndx = somefunc( $look4, @list ); # sets $ndx to 1
$look4 = monkeywrench;
$ndx = somefunc( $look4, @list); # sets $ndx to -1,
Also note that a foreach loop doesn?t have a guaranteed order.
Er, for an array, a foreach loop will go from front to back (or back to
front if you 'reverse' it). Hashes or hash keys will come out in a
non-determinable (or close enough) order but arrays darn well better come
out in order or
On Windows Active State perl whenever I try to run the program
print(Hello World.\n);;
command line scripts on winx is a pain. It doesn't handle quoting very
well. Note here you've got the script enclosing dbl quotes in the script
itself. Even on *nix you'll have troubles w/ that. On
Is it possible for a subroutine to return both a scalar (an integer,
specifically) *and* a reference to an array of arrays?
sub add_row {
my ($counter, @ary) = @_;
# do stuff to @ary...
# add scalar to @ary:
push( @ary, ++$counter );
return( [EMAIL PROTECTED] );
}
#
The OOP ones are ... well. Not sure what you want here - perl does OOP
(one of the best books out there is D. Conway's Object Oriented Perl
(Manning)) as well as you want it, as long as you recognize Larry bias
that enforced encapsulation (by the language) is wrong - a paraphrase of
his
[ is this DNA related?]
Searching CPAN for DNA (or Genetics) there's a whole bunch of stuff for it
- and via the Perl Journal, I recall a major genome mapping project
having been completed *only* on the power of Perl. So if you're doing
something ... I dunno, lab or genome related, you may
$temp='196818OVA_Chlre2_kg.scaffold_4000234YEE1\'conservedmembraneproteinSequence
:196818OVA_Chlre2_kg.scaffold_4000234YEE1\'conservedmembraneprotein';
#$name = $1;
$temp = ~s/'//g;
print $temp;
its gives me: 4294967295 i dont know what this is...
the space is giving you an
$temp='196818OVA_Chlre2_kg.scaffold_4000234YEE1\'conservedmembraneproteinSequence
:196818OVA_Chlre2_kg.scaffold_4000234YEE1\'conservedmembraneprotein';
#$name = $1;
$temp = ~s/'//g;
print $temp;
its gives me: 4294967295 i dont know what this is...
I misspoke, slightly:
Well, some of it depends upon how consistent your markers are:
$temp= AAABBBCCCBBBVBBB
I need to write a regex for filterin out the string between.
AAA
BBB
CCC
so in the above case i should have the output as:
AAAZBBB
BBBSSCCC
CCCBBB
BBBVBBB
meaning all
Hi,
my $foo = 'http://10.20.30.40/gargle';
my ($fee) = $foo =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g;
Note the /g at the end.
Note that it is the list context (i.e. 'my ($fee) =') that causes the
pattern match to return the matching sub-expressions. The g modifier is
redundant in
Deane grumbled:
I've not seen this done, so I don't know if it can be, but... Is there
a way to get $1 from a pair of parens in a regexp into a variable in one
line? That is...
my $foo = 'http://10.20.30.40/gargle';
$foo =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;
my $fee = $1;
What's a quick way to get the name of the newest file in a directory?
ls -ltr
and it'd be the last file listed (-ltr Long listing, by Time, in Reverse
order). I don't know of a way to make opendir/readdir use a different
order (by name, case sensitive as your OS) but a trick to get the
open(SENDMAIL, |/usr/lib/sendmail -t)
I believe the author is trying to read the file sendmail in /usr/bin ...
1. but what does the option -t do???
2. Also, the pipe | is also used ... y??
-t tells send mail to process the msg from STDIN, reading To:/From: fields
from the header. All you
$line=~s/\[*\]//g; but it doesn't work.
try this (note the dot before the *):
$line=~s/ \[ .* \] //xg;
dot star is always a concern, as it'll take everything including your
close square brackets, if there's more than one. If you want to be sure
that doesn't happen, the more general
Can this address be unsubbed from ActivePerl
- Forwarded by Andy Bach/WIWB/07/USCOURTS on 08/07/2006 02:21 PM -
[EMAIL PROTECTED]
08/07/2006 01:17 PM
To
[EMAIL PROTECTED]
cc
Subject
Please contact [EMAIL PROTECTED]
This email address is no longer active and your message was not
So what's the latest status on Perl 6 ?
If you really want fun, laughter and a chance to read Damian Conway, Larry
Wall and others (referred to as @Larry - those in charge of Perl/Perl 6)
live and unedited, you should subscribe to one of the perl 6 lists (
perl6-language is good - that and
Top posting means that the order of posts being added remains clear and
that the most recent addition is that the top - useful is the post is
being followed. I have Outlook set up to add to previous messages which
works fine if everyone posts plain text messages (surely breaking this
rule is
The soon to be indispensable Perl Best Practices (D. Conway,
O'Reilly.com -
http://www.oreilly.com/catalog/perlbp/
) puts it quite simply in Chapter 9 (coincidently on-line!) Don't use
subroutine prototypes.
While the discussion goes on for a page and, to a point, could be saying
don't use
Deane,
Parens are needed for capturing. If you need grouping, you should use
wait for it non-capturing parens:
/^(?:this|that)$/
The diff is, no memory overhead in storing the capture and no mumble in
doing the actual capture. Just grouping (needed here, I add, perhaps
unnecessarily) to
Just to rub it in, er, belabor the point: the actual syntax is:
(?[modifier]:patter)
where modifier can be one or more of i,m,s or x (like the /i etc
modifiers). Doesn't capture. Then comes:
(?pattern)
ditto but doesn't allow backtracking (see: Freidl Mastering Regular
Expressions for
if ($LogLinePrefix =~ /[][^$_*?.|(){}\\]*/)
{
$LoglinePrefix =~ s/([][^$_*?.|(){}\\]*)/\\{$1}/g;
}
Just a point - no need to do this twice. Either:
$LoglinePrefix =~ s/([][^$_*?.|(){}\\]*)/\\{$1}/g;
or, if you want to do something else if it matches:
if ($LogLinePrefix
I have a config file that can have three formats:
[alpha] [alpha], [alpha]: [alpha] or [alpha] *[alpha]
Something like:
/\s*([\w-_\.]+)\s*[ \:\*]\s*([\w-_\.]+)/
would load your alphas into $1 and $2 regardless of the format of the
line.
In general, it a good idea to check your matching
# dup.pl - Monitor a network interface in promiscious mode looking for
SNMP-Traps
# destined for given IP and returning to one or more destination IPs.
# The generated packages can be used with the crossed chain OUTPUT of
# Netfilter with later DNAT for the original IP (-A OUTPUT -t nat -s ip
-j
I was trying to think of how/why this would come about and ended up
thinking hmm, maybe a dir full of data files, w/ each line of data being
delimited somehow, so:
use warnings;
use strict;
my @GAR;
my $file_count = 0;
my @current_array;
while () {
chomp;
my @current_data = split(/\,/);
2 things: this:
if ($title =~ m/@zoo/ ) # here I don't know what to insert
won't really work - you could do:
my $zoo = join(|,@zoo);
if ( $title =~ /$zoo/ ) {
however, an empty entry (i.e. push(@zoo, ); ) will make it always match.
Try a hash:
my $verbose = 0;
my (%zoo, @row);
#
As has been mentioned before, the safest is to check your matching:
my $everything_to_first_hyphen;
if ( $line=~/^([^-]*)-/ ) {
$everything_to_first_hyphen=$1;
} else {
die Failed to find hypen in line: $line\n;
}# if line =~ /^([^-]*)-/
I missed the first part (so the above may be
if ($line =~ /!-- begin body-content --(.*?)\/p/i) {
if ($line =~ /!-- begin body-content --(.*?)\/p/is) {
The difference is 's' allows the '.' to match newlines - you probably
have one or two before the first /p tag. Thist
$line =~ s/[\r\n]+//g;
will help in case your running platform has
Using a 25k line dictionary file:
Benchmark: timing 100 iterations of array, assign, eof...
array: 40 wallclock secs (39.07 usr + 0.42 sys = 39.49 CPU) @ 2.53/s
(n=100)
assign: 20 wallclock secs (19.63 usr + 0.25 sys = 19.88 CPU) @ 5.03/s
(n=100)
eof: 19 wallclock secs (18.13
Dunno if I missed it but:
File::ReadBackwards
This module reads a file backwards line by line. It is simple to use,
memory efficient and fast. It supports both an object and a tied handle
interface
seems to be just the ticket. It does something like the seek eof, work
back for line endings
open(TMP, $filename ) or die Cannot open $filename $!\n;
@tmp = TMP;
close( TMP);
($requiredword)=(split(/\s+/,pop(@tmp)))[0];
Seems, if you really just want the very last line (but how likely is that
to last?), something like:
my $filename = shift || $0;
open(TMP, $filename ) or die Cannot
First off the '/sg' switches aren't needed. 's' is 'single' mode (so
saith Freidl/Owl), changing the behaviour of '.' to allow it to match
newlines - that's it! 'g' means repeat for every instance in the match.
The first part, allowing parens to work in a pattern string wasn't so bad,
but I
You might do better to make an altenation:
$keywords = join(|, @keywords);
if ( $wholefile =~ /^\s*($keywords)\s*$/mo ) { # do you want 'i' ?
rather than the loop. Worth a benchmark, but one search for multiple
matches is probably faster than multiple searches for some sized files.
You also
On other (final?) note, supposedly:
use constant LOG_LEVEL = 10;
print LOG something
if LOG_LEVEL 8;
will allow perl to optomize the print stmt away, if the constant part of
the if clause is false:
print LOG not seen
if LOG_LEVEL 20;
This is from (I believe) a TPJ article a while back.
Just 'cause this bumps one of my pet rants:
2) Temporary DEBUG statements - you want the statements to stand out
against the rest of the code so that you can scan for them. This
includes cut+paste of things like 'use Data::Dumper; print
Dump...'.
99.8% of the time, there are no
Unless you're only looking at:$str = "((a=1 )or(a =2))and(((a= 3)and(a = 4))or( (a= 5)and(a= 6)))";and not arbitrary strings of ands and ors and parens, I don't think this is the best way to go about it. I gather:a=1is equivialant to perl's:a == 1 ? 1 : 0;and you've got the value of 'a' somewhere
no use strict, no use warning and, last, and in this case, the important
one ;-
no
use bigint;
You're overflowing integers. You also are quoting numbers, which makes
them strings, leave that to perl:
#!/usr/bin/perl -w
use strict;
use bigint;
my $var1=-123456789012.34;
my $var2=-45.;
my
I think a standard usage (besides use warnings/use strict):
getopts(...
or die $Usage;
(remember, if the string passed to die ends in a \n, die *won't* add 'at
line xx' to the output. So:
my $Usage = Usage: $0 this way;
will get 'at line xx', while:
my $Usage = Usage: $0 this way
;
won't)
Its good time to do further validations too:
die *** ERROR *** File location is required \n $USAGE \n unless $opt{F}
and -f $opt{F};
die *** ERROR *** Some number is required \n $USAGE \n unless $opt{n}
and $opt{n} =~ /(\d+)/;
my $number_value = $1;
or, to be more specific:
die *** ERROR ***
But you're still going about it backwards. If TOTOIDS2 is the big file,
you only want to read it once, if events is small (you read it to an
array) make it a hash, massage the TOTIDS2 input and check it that way:
#!/usr/bin/perl -w
use strict; # uncomment after running script several
You don't need the 'x' option (white space used in RE for comments) or the
'o' option (compile it - this is used when there is a variable in the RE,
e.g.
$name = Name:
while () {
if ( /$name/o ) {
the colon (:) is not a special char and does not need an escape slash.
The '1' you get now is
my $cnt = 1;
while (DATA) {
next unless /\w/;
s/\n/\t\t/ if ($cnt++ % 2);
print;
}
__DATA__
More homework?
a
Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738 FAX 264-5030
... even if you're mediocre/decent at perl [the cmecf]
And, w/ doskey F8 uses what you've type as a search back through the
command history. There's a couple other Function keys too.
a
Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738 FAX 264-5030
Life would be so much easier if we could just look at the source code.
63 matches
Mail list logo