Re: putting ";" as a replacement in the substitution.

2007-01-21 Thread Ovid
--- Michael Alipio <[EMAIL PROTECTED]> wrote:

> It would be pointless to put () in my regexp when testing with if, unless
> I'm grouping something or I want to do something with $1. 

Correct.
 
> if  /(^\w+)\s+/
> 
> But if I am assigning something, like:
> 
> my $captured =~ /^(\w+)\s+/
> 
> I should put it inside parenthesis.

That doesn't make sense.  Since you have 'my $captured', it's a lexical
variable just being declared and it doesn't yet have a value.  You could try
this:

  my $captured;
  if ( $some_var =~ /^(\w+)\s+/ ) {
  $captured = $1;
  }
  if ( $captured ) { ... }

> I also noticed that $capture here will always contain the first catched
> match ($1).

No, it doesn't in your example.  The only way to make that work would be to
use 'list context' and default to matching the '$_' variable:

  my ($capture) = /^(\w+)\s+/;

Note that because we're using parentheses on the left side, that forces list
content and because we're using the assignment operator, '=', instead of the
binding operator, '=~', the regular expression matches against '$_' instead
of the left hand side.  This is probably a bit more of an advanced usage,
though.  You might use it like this:

  while () {
  next if /^#/;  # skip comments
  my ($capture) = /^(\w+)\s+/;   # grab first 'word'
  ...
  }

> The, (?:) as suggested by someone is also good when I want to avoid
> something being stored in $n... I have read about it and a lot
> more(particularly the extended regexp features) in perlre but not quite
> sure what they mean.

Yes, that's correct.  Sometimes you want to group something but not capture
it to a variable:

  if ( $some_var =~ /(?:foo|bar)/ ) {   # matched by not captured
  ...
  }

Hope that helps.

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Pattern Matching

2007-01-21 Thread Dharshana Eswaran

On 1/19/07, John W. Krahn <[EMAIL PROTECTED]> wrote:


Dharshana Eswaran wrote:
> Hi All,

Hello,

> I have a string as shown below:
>
> $string =
>
"{[0]=0x53,[1]=0x65,[2]=0x63,[3]=0x75,[4]=0x72,[5]=0x69,[6]=0x74,[7]=0x79,[8]=0x43,[9]=0x6F,[10]=0x64,[11]=0x65,[12]=0x00}"
>
>
> This is stored as a string in a variable. I need to pull out only the
> numbers and store them in a array. Like:
>
> @array = (53, 65, 63, 75, 72, 69, 74, 79, 43, 6F, 64, 65, 00);
>
> I am unable to get a pattern to try pattern matching and spliting
it.
>
> How do i do this?

$ perl -le'
my $string =

"{[0]=0x53,[1]=0x65,[2]=0x63,[3]=0x75,[4]=0x72,[5]=0x69,[6]=0x74,[7]=0x79,[8]=0x43,[9]=0x6F,[10]=0x64,[11]=0x65,[12]=0x00}";

my @array = $string =~ /[[:xdigit:]]{2}/g;

print "@array";
'
53 65 63 75 72 69 74 79 43 6F 10 64 11 65 12 00




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/





Thank you John.

Thanks and Regards,
Dharshana


Re: Pattern Matching

2007-01-21 Thread Dharshana Eswaran

On 1/19/07, Rob Dixon <[EMAIL PROTECTED]> wrote:


Igor Sutton wrote:
> I have an update:
>
>> my @data = $string =~ m/0x(\d{2})/g;
>
> my @data = $string =~ m/0x(\S{2}),?/g;
>
> Now I think it is right :)

my @data = $string =~ m/=0x(..)/g;

:)

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/





Thanks for the alternative option.

Thanks and Regards,
Dharshana


Re: Pattern Matching

2007-01-21 Thread Dharshana Eswaran

On 1/19/07, Igor Sutton <[EMAIL PROTECTED]> wrote:


I have an update:

> my @data = $string =~ m/0x(\d{2})/g;

my @data = $string =~ m/0x(\S{2}),?/g;

Now I think it is right :)

--
Igor Sutton Lopes <[EMAIL PROTECTED]>



I used the above expression and it worked for me. Thanks you so much.

Thanks and Regards,
Dharshana


Trouble appending into gzipped file using Compress::Zlib (Re: compressing files into tar.gz (which module do you prefer)

2007-01-21 Thread Michael Alipio
Hi,

- Original Message 
From: Rob Dixon <[EMAIL PROTECTED]>
To: Michael Alipio <[EMAIL PROTECTED]>
Cc: beginners@perl.org
Sent: Monday, January 22, 2007 10:46:40 AM
Subject: Re: compressing files into tar.gz (which module do you prefer)

Michael Alipio wrote:
> Hi,
> 
> After parsing a log and writing it into a file, I now, have to compress it
> into tar.gz. Right now, I'm doing a search at CPAN and there where too many
> modules out there with "compress" or "archive" search keyword.
> 
> What do you suggest?

> > Archive::Tar


Right now, I'm playing with Compress::Zlib.

I have a logfile wich contains logs from different devices.
Now I have a sub which extracts and separates each device's logs.

sub extractlog {

my $clientname=shift;
my $log=shift;
my $year=shift;
my $date=shift;

## create the necessary path in "/client_name/year" notation
mkpath ($clientname.'/'.$year,1);

my $datedlog = $clientname.'/'.$year.'/'.$date.'.log';
## the actual log file destination (e.g; client1/2007/2007-01-12.log)

open FH, '>>', $datedlog or die $!;
print FH $log;
close FH;


}

After the all the extraction have been finished, when I looked into a 
particular client's log:

#ls -alsh
22624 -rw-r--r--  1 root  wheel22M Jan 22 11:03 2007-01-17.log

# wc 2007-01-17.log
   44018  268264 23148149 2007-01-17.log

It's around 22 Mb and  44018 lines.


Now, if I change my sub to this:


sub extractlog {

my $clientname=shift;
my $log=shift;
my $year=shift;
my $date=shift;

## create the necessary path in "/client_name/year" notation
mkpath ($clientname.'/'.$year,1);

my $datedlog = $clientname.'/'.$year.'/'.$date.'.log'.'.gz';
## the actual log file destination (e.g; client1/2007/2007-01-12.log.gz)

my $gzlogfile = gzopen($datedlog, "ab9");

## append that log entry into the $datedlog.
gzlogfile->gzwrite($log);
$gzlogfile->gzclose();

}


And surprisingly, when I looked into the same client:

#ls -alsh
14352 -rw-r--r--  1 root  wheel14M Jan 22 11:13 2007-01-17.log.gz

It has grown to 14Mb. The compression is almost only 50% of the original 22Mb 
size.
what's more surprising is that, when I tried to gunzip it to uncompress it:

#ls -lahs
7904 -rw-r--r--  1 root  wheel   7.7M Jan 22 11:13 2007-01-17.log

It's only 7.7Mb.. What has happened here?

#wc 2007-01-17.log
15936   96734 8066422 2007-01-17.log

Seems like I have lost 28,082 log entries for that particular client :-(

Any idea what went wrong?



I was following this example:

open(LOG,"<$log_file1")or print "cld not open $log_file1 for reading \n";
  $HTML_LOG = gzopen("$log_file2", "wb")  or print "cld not open
$log_file2 for writing \n";
  while ()
  {
 $HTML_LOG->gzwrite($_)
  }
  close(LOG);
  $HTML_LOG->gzclose();
But it differs from my case as I am not continuously reading the logfile and 
'gzwriting' it.
In my case, I'm only reading the logfile, then for every line, decide which 
client it belongs, then call throw it to sub extractlog which in turn writes it 
into gzipped file..








> > Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/









 

TV dinner still cooling? 
Check out "Tonight's Picks" on Yahoo! TV.
http://tv.yahoo.com/

Re: compressing files into tar.gz (which module do you prefer)

2007-01-21 Thread Rob Dixon

Michael Alipio wrote:

Hi,

After parsing a log and writing it into a file, I now, have to compress it
into tar.gz. Right now, I'm doing a search at CPAN and there where too many
modules out there with "compress" or "archive" search keyword.

What do you suggest?


Archive::Tar

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Global variable - No of hits counter

2007-01-21 Thread Mumia W.

On 01/21/2007 04:55 PM, tom tom wrote:

Hi,

In my mod_perl (Authentication module). I have global
variable defined as follows

use vars qw( $SESSION_CLEANUP_COUNTER);
$SESSION_CLEANUP_COUNTER=0;


my intention is to count no of times it is getting
executed by clients (no of hits). I am incrementing it
within Authenticate method as follows.
$SESSION_CLEANUP_COUNTER ++,

But everytime it prints 1.

How can I get this going,



I am not an expert in mod_perl.

Mod_perl is good with making program loading more efficient, but my 
experience has been that it's not very good a storing 
constantly-changing data such as the counter you created.


Apache starts several processes to handle requests, and mod_perl is 
active in each process, but each process has its own data, and you can't 
guarantee that the same process that handles *this* request will handle 
the *next* request.


For example, take these four processes:
USERPID  PROGRAM
root5105 apache2
www-data5106 apache2
www-data5107 apache2
www-data5108 apache2

Any requests will be served by either pid 5106 or 5107 or 5108. If 5106 
gets the first HTTP request, the instance of mod_perl in 5106 will set 
$SESSION_CLEANUP_COUNTER in 5106, but 5107 and 5108 will be unaffected.


If the next request goes to 5107, $SESSION_CLEANUP_COUNTER will start 
off with an "undef" value. If you keep quickly hitting "refresh" on the 
page, you should eventually see some incrementing--but it will be uneven 
because each apache2 process will have a different 
$SESSION_CLEANUP_COUNTER, and the assigning of HTTP requests to apache 
sub-processes seems to be semi-random.


Mod_perl is great for making static data and module procedures more 
efficient; however, I think that data that *must* be updated in a linear 
fashion yet shared between several apache processes should be stored 
externally--in a database or a file (on ramdisk?) or in IPC.


Again, this is my €0.0002 from my limited experience with mod_perl and a 
lot of deduction.


Well perhaps there was a little induction too: try this program to prove 
to yourself that each mod_perl process has a different copy of the $sc 
variable:


use strict;
use warnings;
our $sc;
$sc++;

print "Content-Type: text/html\n";
print "\n";

print qq{
 Session Cleanup Test 

 This is a test: $sc . 
PID: $$


};

__END__

Keep hitting refresh. So long as the PID stays the same, $sc increments 
normally, but when apache assigns a new sub-process to deal with 
requests, $sc restarts at the beginning (1).



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




compressing files into tar.gz (which module do you prefer)

2007-01-21 Thread Michael Alipio
Hi,

After parsing a log and writing it into a file, I now, have to compress it into 
tar.gz.
Right now, I'm doing a search at CPAN and there where too many modules out 
there with "compress" or "archive" search keyword.

What do you suggest?

Thanks




 

Be a PS3 game guru.
Get your game face on with the latest PS3 news and previews at Yahoo! Games.
http://videogames.yahoo.com/platform?platform=120121

Re: character classes vs regexp alternatives (using "( )" or "[ ]"

2007-01-21 Thread Dr.Ruud
Rob Dixon schreef:
> Dr.Ruud:
>> Michael Alipio:

>>> $log  = "date=2007-01-12 blah blah";
>>> [...]
>>> ($date) = $log =~
>>> /date=(\S+?)[\s+|,]/;
>>
>>   /date=([0-9]{4}-[0-9]{2}-[0-9]{2})\b/;
>>
>>   /date=([0-9]{4}(?:-[0-9]{2}){2})\b/;
>>
>>   /date=([0-9-]{10})\b/;
>>
>>   /date=([0-9-]+)\b/;
>>
>> (untested)
>
> Why would you not test the code you posted?

No time.


> $log =~ /date=([\d-]+)/;
>
> will do what is required here.

I prefer [0-9] to \d, and to have that \b in there. Why didn't you ask
why? :)

Your "required" is not mine, I prefer to test the full lay out of a date
(see my 1st and 2nd alternative), unless specifically asked not to.

-- 
Affijn, Ruud

"Gewoon is een tijger."


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: putting ";" as a replacement in the substitution.

2007-01-21 Thread Michael Alipio


- Original Message 
From: Ovid <[EMAIL PROTECTED]>
To: beginners@perl.org
Sent: Sunday, January 21, 2007 10:26:20 PM
Subject: Re: putting ";" as a replacement in the substitution.

--- Michael Alipio <[EMAIL PROTECTED]> wrote:



>  No, not correct.  The regular expression is what's being matched.  Period. 
>  The capturing parentheses merely capture some of all of the regular
>  expression into a 'dollar digit' variable ($1, $2, and so on).

>  So for this:

>$var =~ s/foo(bar)/$1/;

>  The 'foo(bar)' is what is being matched and the 'bar' is captured to the $1
>  variable.  For this:

>if ( $var =~ /foo(bar)/ ) { ... }

>  The 'foo(bar)' is *still* what is being matched and the 'bar' is *still* what
>  is being captured to the $1 variable.  

>  The first version is when you want to alter the string you're matching.  The
>  second version is good when you want to take action based upon a match and
>  possibly extract data out of the string.

I see.. Now it's a lot more clearer.

It would be pointless to put () in my regexp when testing with if, unless I'm 
grouping something or I want to do something with $1. 

if  /(^\w+)\s+/

But if I am assigning something, like:

my $captured =~ /^(\w+)\s+/

I should put it inside parenthesis.
I also noticed that $capture here will always contain the first catched match 
($1).

The, (?:) as suggested by someone is also good when I want to avoid something 
being stored in $n... I have read about it and a lot more(particularly the 
extended regexp features) in perlre but not quite sure what they mean. The 
topic on backtracking when using quantifier is also a good read.

Anyway, thanks for your help!
Have a nice day!


>  Cheers,
>  Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/









 

Food fight? Enjoy some healthy debate 
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367

Global variable - No of hits counter

2007-01-21 Thread tom tom
Hi,

In my mod_perl (Authentication module). I have global
variable defined as follows

use vars qw( $SESSION_CLEANUP_COUNTER);
$SESSION_CLEANUP_COUNTER=0;


my intention is to count no of times it is getting
executed by clients (no of hits). I am incrementing it
within Authenticate method as follows.
$SESSION_CLEANUP_COUNTER ++,

But everytime it prints 1.

How can I get this going,



 

Looking for earth-friendly autos? 
Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
http://autos.yahoo.com/green_center/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: character classes vs regexp alternatives (using "( )" or "[ ]"

2007-01-21 Thread Jen Spinney

On 1/20/07, Michael Alipio <[EMAIL PROTECTED]> wrote:

Hi,

I'm a bit confused here:

I have a regexp:

($date) = $log =~ /date=(\S+?)[\s+|,]/;

so if I have:

"date=2007-01-12 blah blah"

or

"date=2007-01-12,blah,blah"

I was able to retrieve "2007-01-12"

However, just recently after reading my notes on perl, I read that I should use 
parenthesis on regexp alternatives.

so this:

[\s+|,]

should be written as

(\s+|)

But if I use that parenthesis my regexp:

($date) = $log =~ /date=(\S+?)[\s+|,]/;

As I remember my regexp notes, If I want to match something, I will have to put it inside parenthesis. And if 
I change those "[ ]" into "( )" I'm afraid that I might also match those \s+ or 
","


Can you shed some light on this?

Thanks.


There have been some regex solutions posted already, but in the
general case, you're right that normal parenthesis both group and
capture.  If you want non-capturing parentheses, you can use
(?:).

So... you could use
($date) = $log =~ /date=(\S+?)(?:\s+|,)/;

See perldoc perlre for details.

- Jen

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Could someone help me with this source code?

2007-01-21 Thread Tom Phoenix

On 1/20/07, Caduceus <[EMAIL PROTECTED]> wrote:


I'm trying to run this perl script called "salter" on activestates komodo.
I hope to use it with Mozilla Thunderbird.  I've read Learning Perl, another
perl book, went to perl.com, perl.org, pm.org, and cpan.com but nothing
seems to help.  I will show you the script.  Any help will be appreciated.


What does it do that it shouldn't, or what does it fail to do that it
should? Can you find a line of code that isn't doing what you think it
should?

Cheers!

--Tom Phoenix

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: putting ";" as a replacement in the substitution.

2007-01-21 Thread Ovid
--- Michael Alipio <[EMAIL PROTECTED]> wrote:

> I see... so in substitutions, all patterns in the left side are those that
> have to be substituted, regardless of which is enclosed in parenthesis.

Well, with a lot of hand-waving, then yes, that's basically correct.

(There are exceptions, but those are generally for advanced features you're
unlikely to encounter right now and that would be a distraction to the
current topic).

> However in a plain regexp look ups, only those inside the parenthesis are
> being matched...

No, not correct.  The regular expression is what's being matched.  Period. 
The capturing parentheses merely capture some of all of the regular
expression into a 'dollar digit' variable ($1, $2, and so on).

So for this:

  $var =~ s/foo(bar)/$1/;

The 'foo(bar)' is what is being matched and the 'bar' is captured to the $1
variable.  For this:

  if ( $var =~ /foo(bar)/ ) { ... }

The 'foo(bar)' is *still* what is being matched and the 'bar' is *still* what
is being captured to the $1 variable.  

The first version is when you want to alter the string you're matching.  The
second version is good when you want to take action based upon a match and
possibly extract data out of the string.

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: character classes vs regexp alternatives (using "( )" or "[ ]"

2007-01-21 Thread Rob Dixon

Dr.Ruud wrote:

Michael Alipio schreef:


$log  = "date=2007-01-12 blah blah";
[...]
($date) = $log =~
/date=(\S+?)[\s+|,]/;


  /date=([0-9]{4}-[0-9]{2}-[0-9]{2})\b/; 

  /date=([0-9]{4}(?:-[0-9]{2}){2})\b/; 


  /date=([0-9-]{10})\b/;

  /date=([0-9-]+)\b/;

(untested)


Why would you not test the code you posted?

$log =~ /date=([\d-]+)/;

will do what is required here.

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Date and time

2007-01-21 Thread Rob Dixon

M. Lewis wrote:
>
> Given the following code, if I were to want $day, $month, $hour, $minute
> & $sec to have a leading zero (ie 01 for Jan rather than 1), is my only
> option to use printf? Or is there a better way.
>
> What I'm searching for here is the *correct* method to get $day, $month,
> etc for uses like naming backup files (databackup-2007-01-21.tar.gz).
>
> Thanks,
> Mike
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my($sec, $min, $hour, $day, $month, $year)=(localtime)[0 .. 5];
>
> print "day=$day\n";
> print "month=".($month+1)."\n";
> print "year=".($year+1900)."\n\n";
> print "hour=$hour\n";
> print "minute=$min\n";
> print "second=$sec\n\n";

Hi Mike

I'm not sure exactly what you're looking for, as your code doesn't address the
question of naming backup files.

I would write a short subroutine to 'fix' the output from localtime, and use
sprintf to build the filename. I hope this is what's wanted.

Rob


use strict;
use warnings;

sub date {
  my @date = localtime;
  $date[5] += 1900;
  $date[4]++;
  @date;
}

my $backup = sprintf 'databackup-%d-%02d-%02d.tar.gz', (date)[5,4,3];

print $backup, "\n";

**OUTPUT**

databackup-2007-01-21.tar.gz

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: putting ";" as a replacement in the substitution.

2007-01-21 Thread Dr.Ruud
Michael Alipio schreef:

> #I have this string:
>
> my $string = 'vd=root,status=';
>
> #Now, I want to transform it into:
>
> 'vd=root;status='
>
> #That is replace the comma(,) between root and status with semicolon
> (;);
>
>
> $string =~ s/vd=\w+(,)/;/;
> print $string,"\n";
>
> #And it prints:
>
> ;status=
>
> Can you tell me why it has ate up vd= as well?
> And how to get around with it..

Because the "vd=\w+" is part of the search-string, it will be replaced.
The () around the comma don't do what you expect.

You can't make the "vd=\w+" a zero-width positive look-behind assertion
yet (see `perldoc perlre`) because the length of \w+ is not fixed. So
you need to capture that part:

  $string =~ s/\b(vd=\w+),/$1;/;

-- 
Affijn, Ruud

"Gewoon is een tijger."


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: putting ";" as a replacement in the substitution.

2007-01-21 Thread D. Bolliger
Michael Alipio am Sonntag, 21. Januar 2007 13:07:
> D. Bolliger <[EMAIL PROTECTED]>
> > Because everything matched - that is: vd=\w+(,) - is replaced with the
> > semicolon.
> >
> > You seem to misunderstand the meaning of the capturing parenthesis '()'
> > on the left part of the substitution: They do not indicate the part of
> > the string that is to be replaced; replaced is what the left side of the
> > substitution matches.
>
> I see... so in substitutions, all patterns in the left side are those that
> have to be substituted, regardless of which is enclosed in parenthesis.

Yes (and the whole left side constitutes *the* search pattern).

> However in a plain regexp look ups, only those inside the parenthesis are
> being matched...
[snipped]

No, only those inside the parenthesis are being *catched* into the $1..$n 
variables.

For the catching taking place, the *whole* regex must match, and what's 
catched is/are part(s) of what matched.

Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Date and time

2007-01-21 Thread Dr.Ruud
"M. Lewis" schreef:

> if I were to want $day, $month, $hour,
> $minute & $sec to have a leading zero (ie 01 for Jan rather than 1),
> is my only option to use printf? Or is there a better way.
> 
> What I'm searching for here is the *correct* method to get $day,
> $month, etc for uses like naming backup files
> (databackup-2007-01-21.tar.gz). 


(s)printf is fine:

  my $i = 0;
  my $fname = sprintf "databackup-%04d-%02d-%02d.tar.gz", 
  map {$_ + (1900,1,0)[$i++]} (localtime)[5,4,3];

-- 
Affijn, Ruud

"Gewoon is een tijger."

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: putting ";" as a replacement in the substitution.

2007-01-21 Thread Michael Alipio


- Original Message 
From: D. Bolliger <[EMAIL PROTECTED]>
To: beginners@perl.org
Sent: Sunday, January 21, 2007 7:43:19 PM
Subject: Re: putting ";" as a replacement in the substitution.

> Because everything matched - that is: vd=\w+(,) - is replaced with the 
> semicolon.

> You seem to misunderstand the meaning of the capturing parenthesis '()' on 
> the 
> left part of the substitution: They do not indicate the part of the string 
> that is to be replaced; replaced is what the left side of the substitution 
> matches.

I see... so in substitutions, all patterns in the left side are those that have 
to be substituted, regardless of which is enclosed in parenthesis.

However in a plain regexp look ups, only those inside the parenthesis are being 
matched...

I tried explaining my experiment here.

http://www.mail-archive.com/beginners%40perl.org/msg82761.html


> And how to get around with it..

One way is:

$string =~ s/(vd=\w+),/$1;/;


It did work, as someone has already suggested.

Thanks.
Now, it's a lot clearer.


> There are several man pages, where also the capturing parenthesis and the $1..
> $n variables are explained:

> perldoc perlre
> perldoc perlretut
> perldoc perlrequick

> Hope this helps!


Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/









 

Looking for earth-friendly autos? 
Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
http://autos.yahoo.com/green_center/

Re: character classes vs regexp alternatives (using "( )" or "[ ]"

2007-01-21 Thread Dr.Ruud
Michael Alipio schreef:

> $log  = "date=2007-01-12 blah blah";
> [...]
> ($date) = $log =~
> /date=(\S+?)[\s+|,]/;

  /date=([0-9]{4}-[0-9]{2}-[0-9]{2})\b/; 

  /date=([0-9]{4}(?:-[0-9]{2}){2})\b/; 

  /date=([0-9-]{10})\b/;


  /date=([0-9-]+)\b/;

(untested)

-- 
Affijn, Ruud

"Gewoon is een tijger."

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: maximum file size for while() loop?

2007-01-21 Thread D. Bolliger
David Moreno Garza am Sonntag, 21. Januar 2007 07:50:
> On Sat, 2007-01-20 at 09:31 +1100, Ken Foskey wrote:
> > > What's exactly the difference between:
> > > ++$lines and $lines++; ?
> >
> > Nothing in this context.
>
> What about other contexts?

Hi David 

#!/usr/bin/perl

use strict;
use warnings;

{  # preincrement
my (%h, $i);
$h{++$i}='hi';
print keys %h, ", $i\n";
}
{  # postincrement
my (%h, $i);
$h{$i++}='hi';
print keys %h, ", $i\n";
}
__END__

1, 1
0, 1

The difference is the order of "read current value" (used as hash key value) 
and "increment current value" (done by ++ operator).

There's no difference between "standalone" ++$lines and $lines++ because only 
increment takes place, and the result value is not used in the same 
expression.

See also perldoc perlop, "Auto-increment and Auto-decrement".

Hope this helps!

Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: putting ";" as a replacement in the substitution.

2007-01-21 Thread D. Bolliger
Michael Alipio am Sonntag, 21. Januar 2007 04:08:
> Hi,

Hi Michael

> my $string = 'vd=root,status=';
>
> #Now, I want to transform it into:
> 'vd=root;status='
> #That is replace the comma(,) between root and status with semicolon (;);
>
> $string =~ s/vd=\w+(,)/;/;
> print $string,"\n";
>
> #And it prints:
>
> ;status=
>
> Can you tell me why it has ate up vd= as well?

Because everything matched - that is: vd=\w+(,) - is replaced with the 
semicolon.

You seem to misunderstand the meaning of the capturing parenthesis '()' on the 
left part of the substitution: They do not indicate the part of the string 
that is to be replaced; replaced is what the left side of the substitution 
matches.


> And how to get around with it..

One way is:

$string =~ s/(vd=\w+),/$1;/;

There are several man pages, where also the capturing parenthesis and the $1..
$n variables are explained:

perldoc perlre
perldoc perlretut
perldoc perlrequick

Hope this helps!

Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Regexp in lookup differs in regexp in substitution (Re: putting ";" as a replacement in the substitution.)

2007-01-21 Thread Michael Alipio


- Original Message 
From: Michael Alipio <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
Cc: begginers perl.org 
Sent: Sunday, January 21, 2007 7:11:47 PM
Subject: Regexp in loopup differs in regexp in substitution (Re: putting ";" as 
a replacement in the substitution.)



- Original Message 
From: Ovid <[EMAIL PROTECTED]>
To: Michael Alipio <[EMAIL PROTECTED]>
Sent: Sunday, January 21, 2007 4:49:41 PM
Subject: Re: putting ";" as a replacement in the substitution.

--- Michael Alipio <[EMAIL PROTECTED]> wrote:

> Hi,
> 
> #I have this string:
> 
> my $string = 'vd=root,status=';
> 
> #Now, I want to transform it into:
> 
> 'vd=root;status='
> 
> #That is replace the comma(,) between root and status with semicolon
> (;);

> 
> $string =~ s/vd=\w+(,)/;/;
> print $string,"\n";
> 
> #And it prints:
> 
> ;status=
> 
> Can you tell me why it has ate up vd= as well?
> And how to get around with it..

> Try:

 > $string =~ s/(vd=\w+),/$1;/;

> The parentheses are for capturing a match and the 'dollar digit'
> variables are for accessing the results of whatever the parentheses
> captured.  However, when using a substitution (s///), the left hand
> side is the regex to match text and the right hand side is what you
> replace the entire *match* with.

> So your substitution "vd=\w+(,)" was matching "vd=root,", capturing the
> comma, ignoring said capture (since you weren't using $1), and then
> replacing the entire match with ';'.

It did work but this is quite confusing. As far as I can understand, 
parenthesis in regexp are used to indicate whatever you are looking for.

and if I am going to do something like:

s/(vd=\w+),/$1;/;


$1 should contain only "vd=\w+" and not including the ",".
So as I understand it, the expression above will result to replacing "vd=\w+" 
with "vd=root;,"

my $string = 'devid=234FG,vd=root,status=ok,logid=235';
print "There is an alphanumeric word at the beginning that is followed by a 
=\n" if $string =~ /^(\w+)=/;
print "My first match contains $1\n";
print "My entire match contains $&\n";


See, in my first match, given the regexp(\w+) which is surrounded by ( ), it 
captures "devid"
And goes without saying the entire match consist of "devid" and "=".

Now, in substitutions:

my $string = 'devid=234FG,vd=root,status=ok,logid=235';

$string =~ s/(vd=\w+),/$1;/;
print "My first match is $1\n";
print "My entire match is $&\n";

How come my $1 which contains only "vd=root" when replaced with "vd=root;", the 
comma in the regexp pattern was also included in $1??






> See 'perldoc perlre' for more information.

> Cheers,
> Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/







 

The fish are biting. 
Get more visitors on your site using Yahoo! Search Marketing.
http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php






 

Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com

Regexp in loopup differs in regexp in substitution (Re: putting ";" as a replacement in the substitution.)

2007-01-21 Thread Michael Alipio


- Original Message 
From: Ovid <[EMAIL PROTECTED]>
To: Michael Alipio <[EMAIL PROTECTED]>
Sent: Sunday, January 21, 2007 4:49:41 PM
Subject: Re: putting ";" as a replacement in the substitution.

--- Michael Alipio <[EMAIL PROTECTED]> wrote:

> Hi,
> 
> #I have this string:
> 
> my $string = 'vd=root,status=';
> 
> #Now, I want to transform it into:
> 
> 'vd=root;status='
> 
> #That is replace the comma(,) between root and status with semicolon
> (;);

> 
> $string =~ s/vd=\w+(,)/;/;
> print $string,"\n";
> 
> #And it prints:
> 
> ;status=
> 
> Can you tell me why it has ate up vd= as well?
> And how to get around with it..

> Try:

 > $string =~ s/(vd=\w+),/$1;/;

> The parentheses are for capturing a match and the 'dollar digit'
> variables are for accessing the results of whatever the parentheses
> captured.  However, when using a substitution (s///), the left hand
> side is the regex to match text and the right hand side is what you
> replace the entire *match* with.

> So your substitution "vd=\w+(,)" was matching "vd=root,", capturing the
> comma, ignoring said capture (since you weren't using $1), and then
> replacing the entire match with ';'.

It did work but this is quite confusing. As far as I can understand, 
parenthesis in regexp are used to indicate whatever you are looking for.

and if I am going to do something like:

s/(vd=\w+),/$1;/;


$1 should contain only "vd=\w+" and not including the ",".
So as I understand it, the expression above will result to replacing "vd=\w+" 
with "vd=root;,"

my $string = 'devid=234FG,vd=root,status=ok,logid=235';
print "There is an alphanumeric word at the beginning that is followed by a 
=\n" if $string =~ /^(\w+)=/;
print "My first match contains $1\n";
print "My entire match contains $&\n";


See, in my first match, given the regexp(\w+) which is surrounded by ( ), it 
captures "devid"
And goes without saying the entire match consist of "devid" and "=".

Now, in substitutions:

my $string = 'devid=234FG,vd=root,status=ok,logid=235';

$string =~ s/(vd=\w+),/$1;/;
print "My first match is $1\n";
print "My entire match is $&\n";

How come my $1 which contains only "vd=root" when replaced with "vd=root;", the 
comma in the regexp pattern was also included in $1??






> See 'perldoc perlre' for more information.

> Cheers,
> Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/







 

The fish are biting. 
Get more visitors on your site using Yahoo! Search Marketing.
http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php

Re: Date and time

2007-01-21 Thread Goksie
M. Lewis wrote:
>
> Given the following code, if I were to want $day, $month, $hour,
> $minute & $sec to have a leading zero (ie 01 for Jan rather than 1),
> is my only option to use printf? Or is there a better way.
>
> What I'm searching for here is the *correct* method to get $day,
> $month, etc for uses like naming backup files
> (databackup-2007-01-21.tar.gz).
>
> Thanks,
> Mike
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my($sec, $min, $hour, $day, $month, $year)=(localtime)[0 .. 5];
>
> print "day=$day\n";
> print "month=".($month+1)."\n";
> print "year=".($year+1900)."\n\n";
> print "hour=$hour\n";
> print "minute=$min\n";
> print "second=$sec\n\n";
>
>
use POSIX 'strftime';

 my $dd = strftime "%Y-%m-%d",localtime;
 my $backup = "databackup-.$dd."tar.gz"";

goksie

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Could someone help me with this source code?

2007-01-21 Thread Caduceus
Hi:

I'm trying to run this perl script called "salter" on activestates komodo.
I hope to use it with Mozilla Thunderbird.  I've read Learning Perl, another
perl book, went to perl.com, perl.org, pm.org, and cpan.com but nothing
seems to help.  I will show you the script.  Any help will be appreciated.
TIA Steve

--
#!/usr/bin/perl -w
# Salter single-threaded email address salter

# (c) 2003, 2004 Julian Haight, http://www.julianhaight.com/
# All Rights Reserved under GPL:http://www.gnu.org/licenses/gpl.txt
# Current version available here:   http://www.julianhaight.com/salter

# Version history
# 7/19/04 V1.2
# added stripsender feature
# fixed missing newline between header & body

# 3/26/04 V1.1
# cleaned up smtp sending code, added envonly mode, added version

# 3/12/04
# give each recipient their own, permanent random virtual sender
# move config to user-dir, not /etc.

# 9/29/03 - changed to use only lowercase-alpha, avoid spam filters
#  Also, added final response after quit (worked without for pine, but not
moz)

use strict; use Socket; use FileHandle; use Digest::MD5;

my($CONFIG) = ($ENV{HOME} . '/.salter');
my($MAPFN) = "$CONFIG/map.txt";
my($EOL) = "\015\012";
my($debug) = 0;
my($SMTPTO) = 10; # 10 second timeout
my($VERSION) = 'V1.2';

my($SAMP) = '

# here is a sample config file:

listenport 2525
listenip 127.0.0.1
sendport 25
sendip your_isps_mailserver.example.com
maxclient 5
# 1 for unsafe but fast!, 0 for slow & steady (not yet available)
buffermode  1
# 1 remaps only envelope, not header, good if you want to filter bad bounces
envonly 0
# 1 strips sender field (for pine or whatever)
stripsender 1

#   From this address   To random @ this domain!
#   -   
remap [EMAIL PROTECTED] salty.you.example.com
remap [EMAIL PROTECTED]   foo.example.com

# to set your identity per-recipient (email or part)
# -  use workplace address for work recipients
hardwireworkplace.example.com  [EMAIL PROTECTED]
# -  use mailing list subscription address when posting to list.
hardwire[EMAIL PROTECTED][EMAIL PROTECTED]

# end sample config!
';

my(%config, %remap, %map, %hardwire);
unless (-e $CONFIG) { mkdir($CONFIG); }
readConfig(); # read the config file into %config
readMap();
listenLoop(); # work 'til you die!
exit 0;

# listen for one connection at a time, and call the proxy for each one.
# die if there are errors
sub listenLoop {
my($cliaddr, $cliip, $cliport);
socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp')) ||
die "Socket: $!";
setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, pack('l', 1)) ||
die "Setsockopt: $!";
bind(SOCK, sockaddr_in($config{'listenport'},
   inet_aton($config{'listenip'}))) ||
   die "bind: $!";
listen(SOCK, $config{'maxclient'}) ||
die "listen: $!";
while ($cliaddr = accept(CLI, SOCK)) {
# print STDERR "got connection\n";
($cliport, $cliip) = (sockaddr_in($cliaddr));
CLI->autoflush(1);
if ($_ = proxyIt(\*CLI)) {
print STDERR "<< 550 Proxy error: $_\n";
print CLI "550 Proxy error: $_\n";
}
close CLI;
}
}

sub proxyIt {
   my($CLI) = @_;
   my($cmds, $head, $body, $cmd);
   $cmds = '';
   unless ($config{buffered}) {
   print $CLI "500 No safe delivery mode yet, sorry!$EOL";
   close($CLI);
   die "No safe mode yet, sorry!";
   }
   # read smtp
   print $CLI "220 localhost SMTP pretender: salter $VERSION $EOL";
   while ($cmd = <$CLI>) {
   $cmds .= $cmd || '';
   if (lc($cmd) eq "data$EOL") { last; }
   if (lc(substr($cmd, 0, 4)) eq 'ehlo') {
   print $CLI "451 EHLO is soo complicated$EOL";
   } else {
   print $CLI "250 Buffering$EOL";
   }
   }
   print $CLI "354 Ready for data$EOL";
   # read head
   while ($cmd = <$CLI>) {
   if ($cmd eq $EOL) { last; }
   if ((!$config{stripsender}) || ($cmd !~ m/^sender:/i)) {
   $head .= $cmd;
   }
   }
   # read body
   while ($cmd = <$CLI>) {
   if ($cmd eq ".$EOL") { last; }
   $body .= $cmd;
   }
   while ($CLI && print $CLI "250 Buffering$EOL") {
   $cmd = <$CLI>;
   $cmds .= $cmd;
   if (lc($cmd) eq "quit$EOL") { last; }
   }
   print $CLI "221 Bye bye, hopefully it'll work!$EOL";
   close $CLI;
   deliverAll($cmds, $head, $body);
}

sub deliverAll {
my($cmds, $head, $body) = @_;
my($recipmap, $message, $line, $remap, $recip, $sender,
   $sremap, $cmd, $val, $S,
   @recips, $from);
#print STDERR "Deliverall:\n$cmds\n==\n$head\n--\n$body\n++\n";
while ($cmds =~ m/([^:\n]*): ?\\n]*[^\s\>])?\>?/g) {
$cmd = lc($1); $val = $2;
# print "cmd: $cmd = $val\n";
if ($cmd eq 'mail from') {
$sender = $val;
} elsif ($cmd eq 'rcpt to') {
$recip = $val;
$remap = getRecipMapping($recip);
# print STDERR "remap $recip to $remap\n";
push(@{$recipmap->{$remap}}, $recip);
}
}
#print STDERR "Done w/commands\n";
while ($_ = smtpOpen(*S

Re: maximum file size for while() loop?

2007-01-21 Thread David Moreno Garza
On Sat, 2007-01-20 at 09:31 +1100, Ken Foskey wrote:
> > What's exactly the difference between:
> > 
> > ++$lines;
> > 
> > and
> > 
> > $lines++; ?
> 
> 
> Nothing in this context.

What about other contexts?

David.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Date and time

2007-01-21 Thread Owen
On Sun, 21 Jan 2007 02:17:08 -0500
"M. Lewis" <[EMAIL PROTECTED]> wrote:

> 
> Given the following code, if I were to want $day, $month, $hour, $minute 
> & $sec to have a leading zero (ie 01 for Jan rather than 1), is my only 
> option to use printf? Or is there a better way.


Look at sprintf

perldoc -f sprintf



Owen





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Date and time

2007-01-21 Thread John W. Krahn
M. Lewis wrote:
> 
> Given the following code, if I were to want $day, $month, $hour, $minute
> & $sec to have a leading zero (ie 01 for Jan rather than 1), is my only
> option to use printf? Or is there a better way.
> 
> What I'm searching for here is the *correct* method to get $day, $month,
> etc for uses like naming backup files (databackup-2007-01-21.tar.gz).
> 
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> my($sec, $min, $hour, $day, $month, $year)=(localtime)[0 .. 5];
> 
> print "day=$day\n";
> print "month=".($month+1)."\n";
> print "year=".($year+1900)."\n\n";
> print "hour=$hour\n";
> print "minute=$min\n";
> print "second=$sec\n\n";

use POSIX 'strftime';

print strftime
"day=%d\nmonth=%m\nyear=%Y\n\nhour=%H\nminute=%M\nsecond=%S\n\n", localtime;




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/