Re: Searching hash if a given value exists

2007-01-19 Thread Igor Sutton

Yes there is a faster way. Use the reverse function:

 my %hash = (dog = 'house', pig = 'barn', bird= 'cage');
 my %rhash = reverse %hash;
 if ($rhash{house}) {
 print Found house.\n;
 }


That's a really good looking idiom, but I see it is less efficient
than the foreach method. According to Benchmark:

~/Perl $ perl reverse-hash-benchmark.pl
Benchmark: timing 100 iterations of foreach_hash, reverse_hash...
foreach_hash:  3 wallclock secs ( 4.35 usr +  0.00 sys =  4.35 CPU) @
229885.06/s (n=100)
reverse_hash:  6 wallclock secs ( 6.81 usr +  0.01 sys =  6.82 CPU) @
146627.57/s (n=100)
Rate reverse_hash foreach_hash
reverse_hash 146628/s   -- -36%
foreach_hash 229885/s  57%   --
~/Perl $

Below is the code I used:

code
#!env perl

use strict;
use warnings;

use Benchmark qw(:all);

sub reverse_hash {
   my %hash = ( dog = 'house', pig = 'barn', bird = 'cage' );
   my %rhash = reverse %hash;
   if ( exists $rhash{house} ) {
   return 1;
   }
}

sub foreach_hash {
   my %hash = ( dog = 'house', pig = 'barn', bird = 'cage' );
   foreach ( keys %hash ) {
   if ( $hash{$_} eq 'house' ) {
   return 1;
   }
   }
}

my $results = timethese(
   100,
   {
   reverse_hash = \reverse_hash,
   foreach_hash = \foreach_hash,
   }
);

cmpthese($results)

/code

Maybe I'm doing the Benchmark in a wrong way, or is really more
expensive reversing a hash than iterating its keys using foreach?
--
Igor Sutton Lopes [EMAIL PROTECTED]

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




Re: Capturing stdout and stderr without redirection

2007-01-19 Thread Peter Scott
On Thu, 18 Jan 2007 20:28:42 -0600, David Moreno Garza wrote:
 On Thu, 2007-01-18 at 07:11 -0800, Peter Scott wrote:
 my $output = `myperlscript.pl 21`;
 send_email($output) if $output;
 
 Isn't send_email($output) in this situation still going to happen? I
 mean, even of there is not output on myperlscript.pl 21, $output is
 going to be set, meaning the validation (send_email if $output) will be
 true.
 
 Shouldn't this be set to something like:
 
 send_email($output) if $output ne '';

Ahem.  What makes you think that '' would be considered a true value?
perldoc perlsyn.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


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




Re: Searching hash if a given value exists

2007-01-19 Thread Igor Sutton

[...]

New benchmarks about the subject:

foreach_hash_keys:  4 wallclock secs ( 4.40 usr +  0.00 sys =  4.40
CPU) @ 227272.73/s (n=100)
foreach_hash_values:  4 wallclock secs ( 3.46 usr +  0.01 sys =  3.47
CPU) @ 288184.44/s (n=100)
reverse_hash:  6 wallclock secs ( 6.85 usr +  0.01 sys =  6.86 CPU) @
145772.59/s (n=100)
   Rate  reverse_hash foreach_hash_keys foreach_hash_values
reverse_hash145773/s--  -36%-49%
foreach_hash_keys   227273/s   56%---21%
foreach_hash_values 288184/s   98%   27%  --

New code:

code
#!env perl

use strict;
use warnings;

use Benchmark qw(:all);

sub reverse_hash {
   my %hash = ( dog = 'house', pig = 'barn', bird = 'cage' );
   my %rhash = reverse %hash;
   if ( exists $rhash{house} ) {
   return 1;
   }
}

sub foreach_hash_keys {
   my %hash = ( dog = 'house', pig = 'barn', bird = 'cage' );
   foreach ( keys %hash ) {
   if ( $hash{$_} eq 'house' ) {
   return 1;
   }
   }
}

sub foreach_hash_values {
   my %hash = ( dog = 'house', pig = 'barn', bird = 'cage' );
   foreach ( values %hash ) {
   if ( $_ eq 'house' ) {
   return 1;
   }
   }
}

my $results = timethese(
   100,
   {
   reverse_hash= \reverse_hash,
   foreach_hash_keys   = \foreach_hash_keys,
   foreach_hash_values = \foreach_hash_values,
   }
);

cmpthese($results)
/code



That's a really good looking idiom, but I see it is less efficient
than the foreach method. According to Benchmark:

~/Perl $ perl reverse-hash-benchmark.pl
Benchmark: timing 100 iterations of foreach_hash, reverse_hash...
foreach_hash:  3 wallclock secs ( 4.35 usr +  0.00 sys =  4.35 CPU) @
229885.06/s (n=100)
reverse_hash:  6 wallclock secs ( 6.81 usr +  0.01 sys =  6.82 CPU) @
146627.57/s (n=100)
 Rate reverse_hash foreach_hash
reverse_hash 146628/s   -- -36%
foreach_hash 229885/s  57%   --
~/Perl $

Below is the code I used:

code
#!env perl

use strict;
use warnings;

use Benchmark qw(:all);

sub reverse_hash {
my %hash = ( dog = 'house', pig = 'barn', bird = 'cage' );
my %rhash = reverse %hash;
if ( exists $rhash{house} ) {
return 1;
}
}

sub foreach_hash {
my %hash = ( dog = 'house', pig = 'barn', bird = 'cage' );
foreach ( keys %hash ) {
if ( $hash{$_} eq 'house' ) {
return 1;
}
}
}

my $results = timethese(
100,
{
reverse_hash = \reverse_hash,
foreach_hash = \foreach_hash,
}
);

cmpthese($results)

/code

Maybe I'm doing the Benchmark in a wrong way, or is really more
expensive reversing a hash than iterating its keys using foreach?
--
Igor Sutton Lopes [EMAIL PROTECTED]




--
Igor Sutton Lopes [EMAIL PROTECTED]

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




Pattern Matching

2007-01-19 Thread Dharshana Eswaran

Hi All,

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?

Thanks and Regards,
Dharshana


Re: Pattern Matching

2007-01-19 Thread Igor Sutton

Hi Dharshana,

2007/1/19, Dharshana Eswaran [EMAIL PROTECTED]:

Hi All,

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've achieved this results with the following code:

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

I don't know if it is the most efficient way to do that.

HTH!

--
Igor Sutton Lopes [EMAIL PROTECTED]

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




Re: Pattern Matching

2007-01-19 Thread Igor Sutton

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]

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




substitute all spaces in a sentence except spaces in the first three words

2007-01-19 Thread Michael Alipio
Hi,

Suppose I have:

my $sentence = 'the quick brown fox jumps over the lazy dog.

Now I want it to become:

'the quick brown:fox:jumps:over:the lazy dog.

That is, to replace the spaces between brown-fox, fox-jumps, jumps-over, 
over-the, with :.

Should I split the sentence and put each in an array then iterate over the 
array?
What do you think will be the quickest way to do this?

Thanks!




 

Now that's room service!  Choose from over 150,000 hotels
in 45,000 destinations on Yahoo! Travel to find your fit.
http://farechase.yahoo.com/promo-generic-14795097

choosing which to join in a given string.

2007-01-19 Thread Michael Alipio
Hi,

my $string = This is a sentence;

Now, I want to join is and a by a dash.

Any idea how do i do that?

using regexp may not be want I want because the real string contains many 
words, so I have to join several words without using too much regexp 
substitutions.

Thanks.




 

Need a quick answer? Get one in minutes from people who know.
Ask your question on www.Answers.yahoo.com

exec and pipe

2007-01-19 Thread Andreas Brillisauer - Hetzner Online AG
Hello,

I'm just writing a script that gets an email from stdin. This mail
should be passed to procmail via ssh. If calling ssh or procmail fails,
the mail should be saved locally.

First I tried to solve this with system or open. But I cannot pipe
the mail to ssh when using system. open can handle a pipe to ssh but
doesn't return the return value of ssh.

So I tried to create a pipe by my own using pipe. After that I call
fork. Then the mail is passed to ssh via the created pipe. But this
doesn't work.

Here is my current version of the script. For debugging reasons I call
cat instead of ssh. But I get no output.

---8-
#!/usr/bin/perl

my $lb = $/;
$/ = undef;
my $mail = STDIN;
$/ = $lb;

my $pid;

pipe(READ, WRITE);

$pid = fork();
if ($pid == 0) {
close(WRITE);
exec('cat');
exit(1);
}
elsif ($pid  0) {
 close(READ);
 my $old_handle = select(WRITE);
 $| = 1;
 print WRITE $mail;
 waitpid($pid, WNOHANG);
 $retval = $?  8;
 select($old_handle);
 print $retval\n;
}
---8-

Thanks,
Andreas


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




Re: exec and pipe

2007-01-19 Thread Igor Sutton

Hi Andreas,

2007/1/19, Andreas Brillisauer - Hetzner Online AG
[EMAIL PROTECTED]:

Hello,

I'm just writing a script that gets an email from stdin. This mail
should be passed to procmail via ssh. If calling ssh or procmail fails,
the mail should be saved locally.

First I tried to solve this with system or open. But I cannot pipe
the mail to ssh when using system. open can handle a pipe to ssh but
doesn't return the return value of ssh.

So I tried to create a pipe by my own using pipe. After that I call
fork. Then the mail is passed to ssh via the created pipe. But this
doesn't work.

Here is my current version of the script. For debugging reasons I call
cat instead of ssh. But I get no output.

---8-
#!/usr/bin/perl

my $lb = $/;
$/ = undef;
my $mail = STDIN;
$/ = $lb;

my $pid;

pipe(READ, WRITE);

$pid = fork();
if ($pid == 0) {
close(WRITE);
exec('cat');
exit(1);
}
elsif ($pid  0) {
 close(READ);
 my $old_handle = select(WRITE);
 $| = 1;
 print WRITE $mail;
 waitpid($pid, WNOHANG);
 $retval = $?  8;
 select($old_handle);
 print $retval\n;
}
---8-


I would suggest you using Net::SFTP[1] to transfer the file to remote
host, and then Net::SSH::Perl[2] to invoke procmail on remote host. I
think it is the best start, then you could improve it.

[1] http://search.cpan.org/~dbrobins/Net-SFTP-0.10/lib/Net/SFTP.pm
[2] http://search.cpan.org/~dbrobins/Net-SSH-Perl-1.30/lib/Net/SSH/Perl.pm

HTH!

--
Igor Sutton Lopes [EMAIL PROTECTED]

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




Cannot modify read-only value in while loop?

2007-01-19 Thread Richard Jones

Help. I've just stumbled upon a new one to me:

my $ref = [ 1..3 ];
push @$ref, 4;

print Dumper $ref as expected gives:

$VAR1 = [
  1,
  2,
  3,
  4
  ];


But in a DBI context:
while ( my $ref = $sth-fetchrow_arrayref() ) { # $ref is arrayref
  push @$ref, 1; # line xx
}

dies with 'Modification of read-only value attempted at .. line xx'

I can't see the difference here between the two $ref arrayrefs, but Perl 
obviously can. Unfortunately 'use diagnostics' didn't help as I think I 
must be missing something obvious.

 --
ra(dot)jones(at)dpw(dot)clara(dot)co(dot)uk


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




maximum file size for while(FILE) loop?

2007-01-19 Thread Bertrand Baesjou

Hi,

I am trying to read data from a file, I do this by using the while 
(FILE){ $line} construction.
However with files with a size of roughly bigger than 430MB it seems to 
crash the script :S Syntax seems all fine (perl -wc - syntax OK).


I was thinking that maybe it was running to the end of a 32 bit counter 
(but that would be 536 MB right?)? Can anybody offer an other solution 
to work with such large files and perl?


Tnx,

   Bertrand

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


Re: Passing hash into sub routines (should i use reference?)

2007-01-19 Thread Richard Jones

Michael Alipio wrote:

Hi,

Suppose I have this code:

#/usr/local/bin/perl use warnings; use strict; use subs 'verify';

my %where=( Gary = Dallas, Lucy = Exeter, Ian =  Reading, 
Samantha = Oregon );


# Then i open a logfile

open FH, '', $logfile or die Can't open $logfile!: $!;

# Now, for every line in the logfile, I will retrive a particular
string, let's say a person's name and verify if it exists in my hash
above:

while (FH){ (my $person) = $_ =~ /person=(\S+)/; my $personstatus =
verify(\%where,$person);

# And do anything if the personstatus is this and that.


# Now,  on my sub verify, I would like to know if the person exists
in my hash.

sub verify { my $where = shift @_; my $person = shift @_; my
$personstatus;

if (exists $$where{$person}){

$personstatus =1; }else { $personstatus = 0; }

$personstatus; }

Am I thinking as this is how it should be done when passing hashes to
a subroutine? My program doesn't work and I know it has something to
do with the my $personstatus line as well as if (exists
$$where{$person} line. Can you show me the right way of passing
hashes to subroutines? Thanks.

I also tried declaring %where as 'our' so that I can use if (exists
%where{$person} instead of if(exists $$where{person}) but i still
can't make it work.


Do you know if I am making any sense here?

Thanks


Why pass a reference to the same hash for every line of the FH? You 
have declared the %where hash at the top so it's already available to 
the sub-routine. Much simpler:


while (FH) {
  my ($person) = $_ =~ /person=(\S+)/; # are you sure this works?
  my $personstatus = verify($person);
}

sub verify {
  local $_ = shift; # $_ contains $person from while loop

  return $where{$_} ? 1 : 0;
}


Or even simpler, dispose of verify() and just do:

while (FH){
  my ($person) = $_ =~ /person=(\S+)/;
  my $personstatus = $where{$person} ? 1 : 0;
}

The correct way to accept a hashref in a sub-routine is:

sub my_subroutine {
  my $hashref = shift;
  my $something_else = shift;

  # OR:
  my ($hashref, $something_else) = @_;
}
--
ra(dot)jones(at)dpw(dot)clara(dot)co(dot)uk


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




Re: maximum file size for while(FILE) loop?

2007-01-19 Thread Ken Foskey
On Fri, 2007-01-19 at 13:16 +0100, Bertrand Baesjou wrote:
 Hi,
 
 I am trying to read data from a file, I do this by using the while 
 (FILE){ $line} construction.
 However with files with a size of roughly bigger than 430MB it seems to 
 crash the script :S Syntax seems all fine (perl -wc - syntax OK).
 
 I was thinking that maybe it was running to the end of a 32 bit counter 
 (but that would be 536 MB right?)? Can anybody offer an other solution 
 to work with such large files and perl?

No idea,  a little script sample might be good.

-- 
Ken Foskey
FOSS developer


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




Re: choosing which to join in a given string.

2007-01-19 Thread Ken Foskey
On Fri, 2007-01-19 at 02:08 -0800, Michael Alipio wrote:
 Hi,
 
 my $string = This is a sentence;
 
 Now, I want to join is and a by a dash.
 
 Any idea how do i do that?
 
 using regexp may not be want I want because the real string contains
 many words, so I have to join several words without using too much
 regexp substitutions.

smells like homework.

Show us what you have tried.

Remember rule 1 is to get something that works and is clear.
Performance is rarely an issue and even then profile working code before
doing anything.

-- 
Ken Foskey
FOSS developer


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




Re: substitute all spaces in a sentence except spaces in the first three words

2007-01-19 Thread Rob Dixon

Michael Alipio wrote:


Suppose I have:

my $sentence = 'the quick brown fox jumps over the lazy dog.

Now I want it to become:

'the quick brown:fox:jumps:over:the lazy dog.

That is, to replace the spaces between brown-fox, fox-jumps, jumps-over, 
over-the, with :.


Should I split the sentence and put each in an array then iterate over the 
array? What do you think will be the quickest way to do this?


Your title says one thing and your example says another. You have replaced all
spaces with a colon except between the first three and the last three words.
What is it you actually want? The code below may help: it strips leading and
trailing whitespace and replaces the space between the third through the seventh
words with a single colon; all other whitespace is replaced with a single space.

If you need anything different from this then please come back to us.

HTH,

Rob



use strict;
use warnings;

my $sentence = 'the quick brown fox jumps over the lazy dog.';

my @words = split ' ', $sentence;
my @centre = splice @words, 2, 5;
splice @words, 2, 0, join ':', @centre;
$sentence = @words\n;

print $sentence, \n;

**OUTPUT**

the quick brown:fox:jumps:over:the lazy dog.

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




Re: choosing which to join in a given string.

2007-01-19 Thread Rob Dixon

Michael Alipio wrote:


my $string = This is a sentence;

Now, I want to join is and a by a dash.

Any idea how do i do that?

using regexp may not be want I want because the real string contains many
words, so I have to join several words without using too much regexp
substitutions.


This is the same question as before with different numbers. It would help if you
posted the real problem instead of making up other problems that were a bit like
it. Anyway, in the spirit of TIMTOWTDI, here is a different solution.

HTH,

Rob


use strict;;
use warnings;

my $string = This is a sentence;

my $n = 0;
$string =~ s/\s+/++$n; $n == 2 ? '-' : ' '/ge;

print $string, \n;

**OUTPUT**

This is-a sentence

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




Re: maximum file size for while(FILE) loop?

2007-01-19 Thread Rob Dixon

Bertrand Baesjou wrote:

Hi,

I am trying to read data from a file, I do this by using the while 
(FILE){ $line} construction.


However with files with a size of roughly bigger than 430MB it seems to 
crash the script :S Syntax seems all fine (perl -wc - syntax OK).


How does your script crash? What are the symptoms?

I was thinking that maybe it was running to the end of a 32 bit counter 
(but that would be 536 MB right?)? Can anybody offer an other solution 
to work with such large files and perl?


People have read files of several gigabytes with Perl. The problem is more
likely to lie with what you do with the data once you have read it. To prove
this for yourself, set this code against the same file:

my $lines = 0;
while (FILE) {
  ++$lines;
}
print $lines;

and I am pretty sure that won't crash.

Then try to simplify your code by removing stuff from the loop until the problem
goes away. The last thing you removed contains the cause of the crash.

If you need to post again please give us comprehensive details of the crash.

HTH,

Rob

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




Re: Pattern Matching

2007-01-19 Thread Rob Dixon

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/




Re: Pattern Matching

2007-01-19 Thread John W. Krahn
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/




Re: substitute all spaces in a sentence except spaces in the first three words

2007-01-19 Thread John W. Krahn
Michael Alipio wrote:
 Hi,

Hello,

 Suppose I have:
 
 my $sentence = 'the quick brown fox jumps over the lazy dog.
 
 Now I want it to become:
 
 'the quick brown:fox:jumps:over:the lazy dog.

One way to do it:

$ perl -le'
my $sentence = the quick brown fox jumps over the lazy dog.;
$sentence =~ s/ +(?=.* +[^ ]+ +)/ ++$x  2 ? : :   /eg;
print $sentence;
'
the quick brown:fox:jumps:over:the lazy dog.



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/




Re: maximum file size for while(FILE) loop? - maybe HASH problem?

2007-01-19 Thread Bertrand Baesjou

Ken Foskey wrote:

On Fri, 2007-01-19 at 13:16 +0100, Bertrand Baesjou wrote:
  

Hi,

I am trying to read data from a file, I do this by using the while 
(FILE){ $line} construction.
However with files with a size of roughly bigger than 430MB it seems to 
crash the script :S Syntax seems all fine (perl -wc - syntax OK).


I was thinking that maybe it was running to the end of a 32 bit counter 
(but that would be 536 MB right?)? Can anybody offer an other solution 
to work with such large files and perl?



No idea,  a little script sample might be good.

  
While running my script it seems to use around a gigabyte of memory 
(there is 1GB of RAM and 1GB of swap in the system), might this be the 
problem?


The script below gives the error:
#!/usr/local/bin/perl
##
#

use POSIX;

my $inputFile = $ARGV[0];   #file we are 
reading from

my $outputFile = ./tmp/overall-memory.dat;
my %somestorage;
my $linecounter = 0;
my $value=40;
my $bool = 0;

open(INFILE, $inputFile) or die(Could not open log 
file.);# open for input

open(OUTFILE, $outputFile);

foreach $line (INFILE) {

   $somestorage{$linecounter}=$value;
   $linecounter++;
}
close(INFILE);
close(OUTFILE);
print $linecounter;
system(pwd) == 0 or die system failed: $?;

#wc -l samples/1169209055.trcxml
5198365 samples/1169209055.trcxml
# ./sample1.pl samples/1169209055.trcxml
5198365system failed: 0 at ./sample1.pl line 22.

Any ideas how to solve my problem?
Tnx!
   Bertrand

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


Re: maximum file size for while(FILE) loop? - maybe HASH problem?

2007-01-19 Thread Paul Johnson
On Fri, Jan 19, 2007 at 03:17:19PM +0100, Bertrand Baesjou wrote:

 foreach $line (INFILE) {

See, this isn't a while loop, as you have in the subject.

That is the cause of your problems.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

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




Re: memory issues?

2007-01-19 Thread Bertrand Baesjou

Paul Johnson wrote:

On Fri, Jan 19, 2007 at 03:17:19PM +0100, Bertrand Baesjou wrote:

  

foreach $line (INFILE) {



See, this isn't a while loop, as you have in the subject.

That is the cause of your problems.
  
Damn, not very awake today I think. I also left an old subject line 
in But if that is the cause, where lies the solution?


Tnx.


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


Re: memory issues?

2007-01-19 Thread Octavian Rasnita

From: Bertrand Baesjou [EMAIL PROTECTED]


Paul Johnson wrote:

On Fri, Jan 19, 2007 at 03:17:19PM +0100, Bertrand Baesjou wrote:

  

foreach $line (INFILE) {



See, this isn't a while loop, as you have in the subject.

That is the cause of your problems.
  
Damn, not very awake today I think. I also left an old subject line 
in But if that is the cause, where lies the solution?




Use while() instead of foreach().

Octavian



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




Re: memory issues?

2007-01-19 Thread Bertrand Baesjou

Octavian Rasnita wrote:

From: Bertrand Baesjou [EMAIL PROTECTED]


Paul Johnson wrote:

On Fri, Jan 19, 2007 at 03:17:19PM +0100, Bertrand Baesjou wrote:

 

foreach $line (INFILE) {



See, this isn't a while loop, as you have in the subject.

That is the cause of your problems.
  
Damn, not very awake today I think. I also left an old subject line 
in But if that is the cause, where lies the solution?




Use while() instead of foreach().

Octavian


Thank you very much, this is indeed the solution.






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


Re: Searching hash if a given value exists

2007-01-19 Thread Dr.Ruud
Igor Sutton schreef:
 [attribution repaired] Mumia W:

 Yes there is a faster way. Use the reverse function:

  my %hash = (dog = 'house', pig = 'barn', bird= 'cage');
  my %rhash = reverse %hash;
  if ($rhash{house}) {
  print Found house.\n;
  }

 That's a really good looking idiom, but I see it is less efficient
 than the foreach method.

The reverse only has to be done once, so it already pays off if you need
to make 2 or more comparisons.
The problem with duplicates remains though. See also `perldoc -q
look.*hash.*value`.

-- 
Affijn, Ruud

Gewoon is een tijger.


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




Re: simple perl script on Windows

2007-01-19 Thread Dr.Ruud
David Moreno Garza schreef:

 open FILE, 'H:\My Music\folderlist.txt';
 open DEST, ' H:\My Music\artists.txt';
 foreach my $line(readline FILE) {
 chomp;
 print DEST $1.\n if $line =~ /DIR\s*(.*)$/;
 }
 close FILE;
 close DEST;

Wouldn't it be great if, especially on this list, such examples would
be written more like:


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

  my $fpath = 'H:/My Music';
  my $fname_in  = $fpath/folderlist.txt;
  my $fname_out = $fpath/artists.txt;

  open my $fh_in,  '', $fname_in
  or die Error opening '$fname_in' for reading: $!;
  open my $fh_out, '', $fname_out
  or die Error opening '$fname_out' for writing: $!;

  while ( $fh_in ) {
  print $fh_out $1\n if /DIR\s+(.+)/;
  }

  close $fh_out or die Error closing '$fname_out': $!;
  close $fh_in  or die Error closing '$fname_in': $!;

__END__

-- 
Affijn, Ruud

Gewoon is een tijger.


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




Re: memory issues?

2007-01-19 Thread Xavier Noria

On Jan 19, 2007, at 5:53 PM, Bertrand Baesjou wrote:


Thank you very much, this is indeed the solution.


The explanation is that when you process lines this way

  foreach my $line (FH) { ... }

the readline operator is evaluated in list context and, thus, the  
file is slurped into a single list with all the lines. So, first the  
file is fully put into memory, and then foreach starts.


In the line-oriented while loop

  while (my $line = FH) { ... }

you are fetching line by line[*], and so memory is kept under control  
(at least as much in control as the length of the lines in that file,  
that wouldn't solve the problem if the file was GBs of data in a  
single line, you see how it works).


-- fxn

[*] Under the hood there are actually some buffers, but that's the idea.


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




Re: Cannot modify read-only value in while loop?

2007-01-19 Thread Xavier Noria

On Jan 19, 2007, at 12:55 PM, Richard Jones wrote:


while ( my $ref = $sth-fetchrow_arrayref() ) { # $ref is arrayref
  push @$ref, 1; # line xx
}

dies with 'Modification of read-only value attempted at .. line xx'

I can't see the difference here between the two $ref arrayrefs, but  
Perl obviously can. Unfortunately 'use diagnostics' didn't help as  
I think I must be missing something obvious.


Yes, that reference has a flag READONLY turned on, this is a dump of  
an example I ran to figure this out (attached below):


SV = RV(0x1823abc) at 0x1856984
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x1856894
  SV = PVAV(0x185848c) at 0x1856894
REFCNT = 2
FLAGS = (READONLY)   -- look here
IV = 0
NV = 0
ARRAY = 0x4d80b0
FILL = 0
MAX = 3
ARYLEN = 0x0
FLAGS = (REAL)
Elt No. 0
SV = NV(0x1813c00) at 0x1856978
  REFCNT = 1
  FLAGS = (NOK,pNOK)
  NV = 1

There are several lines in

  http://search.cpan.org/src/TIMB/DBI-1.53/DBI.xs

with this code:

  SvREADONLY_on(av);

So the answer is that you get a regular arrayref with that flag  
turned on by hand from XS code. That's why it is read-only.


-- fxn

use DBI;

use strict;
use warnings;

use Devel::Peek;

my $dbh = DBI-connect(dbi:SQLite:dbname=foo.db);
$dbh-do(drop table if exists foos);
$dbh-do(create table foos (id integer));
$dbh-do(insert into foos (id) values (1));

my $sth = $dbh-prepare(select id from foos);
$sth-execute;
Dump $sth-fetchrow_arrayref;


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




Re: maximum file size for while(FILE) loop?

2007-01-19 Thread David Moreno Garza
On Fri, 2007-01-19 at 13:24 +, Rob Dixon wrote:
++$lines;

What's exactly the difference between:

++$lines;

and

$lines++; ?

David.


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




Re: maximum file size for while(FILE) loop?

2007-01-19 Thread Ken Foskey
On Fri, 2007-01-19 at 16:21 -0600, David Moreno Garza wrote:
 On Fri, 2007-01-19 at 13:24 +, Rob Dixon wrote:
 ++$lines;
 
 What's exactly the difference between:
 
 ++$lines;
 
 and
 
 $lines++; ?


Nothing in this context.

It does make a difference if you are 'using' the value see sample and
try it:

$lines = 10;
$value = ++$lines;
print first value is it 10 or 11? $value\n
$value = $lines++;
print second value is it 11 or 12? $value\n

-- 
Ken Foskey
FOSS developer


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




Re: maximum file size for while(FILE) loop?

2007-01-19 Thread John W. Krahn
David Moreno Garza wrote:
 On Fri, 2007-01-19 at 13:24 +, Rob Dixon wrote:
   ++$lines;
 
 What's exactly the difference between:
 
 ++$lines;
 
 and
 
 $lines++; ?

In void context they are both the same because perl optimizes $lines++ to
++$lines.



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/




Re: Cannot modify read-only value in while loop?

2007-01-19 Thread Richard Jones

Xavier Noria wrote:

On Jan 19, 2007, at 12:55 PM, Richard Jones wrote:


while ( my $ref = $sth-fetchrow_arrayref() ) { # $ref is arrayref
  push @$ref, 1; # line xx
}

dies with 'Modification of read-only value attempted at .. line xx'


Yes, that reference has a flag READONLY turned on, this is a dump of an 
example I ran to figure this out (attached below):


OK, thanks - good to know it's a DBI thing rather than something I did. 
Pushing onto an array instead of an arrayref works just as well in this 
context so it's not a major problem.



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




Re: Searching hash if a given value exists

2007-01-19 Thread Michael Alipio
Hi,

  - Original Message 
  From: Igor Sutton [EMAIL PROTECTED]
  To: Mumia W. [EMAIL PROTECTED]
  Cc: Beginners List beginners@perl.org
  Sent: Friday, January 19, 2007 4:42:09 PM
  Subject: Re: Searching hash if a given value exists

  [...]

  New benchmarks about the subject:

  foreach_hash_keys:  4 wallclock secs ( 4.40 usr +  0.00 sys =  4.40
  CPU) @ 227272.73/s (n=100)
  foreach_hash_values:  4 wallclock secs ( 3.46 usr +  0.01 sys =  3.47
  CPU) @ 288184.44/s (n=100)
  reverse_hash:  6 wallclock secs ( 6.85 usr +  0.01 sys =  6.86 CPU) @
  145772.59/s (n=100)
Rate  reverse_hash foreach_hash_keys foreach_hash_values
  reverse_hash145773/s--  -36%
 -49%
  foreach_hash_keys   227273/s   56%--
 -21%
  foreach_hash_values 288184/s   98%   27% 
  --


Cool! so the winner is foreach_hash_values??
Never knew I could do a foreach (values %hash), I only use foreach keys for a 
very long time now.
I tried searching the documentation for foreach but no avail.











 

Yahoo! Music Unlimited
Access over 1 million songs.
http://music.yahoo.com/unlimited

Re: Searching hash if a given value exists

2007-01-19 Thread Rob Dixon

Michael Alipio wrote:
 Hi,

 - Original Message  From: Igor Sutton [EMAIL PROTECTED] To:
 Mumia W. [EMAIL PROTECTED] Cc: Beginners List
 beginners@perl.org Sent: Friday, January 19, 2007 4:42:09 PM Subject: Re:
 Searching hash if a given value exists

 [...]

 New benchmarks about the subject:

 foreach_hash_keys:  4 wallclock secs ( 4.40 usr +  0.00 sys =  4.40 CPU) @
 227272.73/s (n=100) foreach_hash_values:  4 wallclock secs ( 3.46 usr +
 0.01 sys =  3.47 CPU) @ 288184.44/s (n=100) reverse_hash:  6 wallclock
 secs ( 6.85 usr +  0.01 sys =  6.86 CPU) @ 145772.59/s (n=100)
 Rate  reverse_hash foreach_hash_keys foreach_hash_values
  reverse_hash145773/s--  -36%
 -49% foreach_hash_keys   227273/s   56%--
 -21% foreach_hash_values 288184/s   98%   27%
 --


 Cool! so the winner is foreach_hash_values??

No. I thought this was going to mislead somebody.

foreach (values %hash) {
  return 1 if $_ eq 'house';
}

is quicker than

foreach (keys %hash) {
  return 1 if $hash{$_} eq 'house';
}

as you would expect, I hope. But using a reverse hash is only slower if you
rebuild it in its entirety every time you need to do a lookup. If you use it
properly it is many many times faster than the very blunt linear search tool.
Look at these benchmarks:

use strict;
use warnings;

use Benchmark qw/cmpthese/;

my %hash = ( dog = 'house', pig = 'barn', bird = 'cage' );
my %rhash = reverse %hash;

cmpthese(5_000_000,
  {
   'reverse hash' = sub {
  return exists $rhash{house};
},

   'foreach hash keys' = sub {
  foreach (keys %hash) {
return 1 if $hash{$_} eq 'house';
  }
  return '';
},

   'foreach hash values' = sub {
  foreach (values %hash) {
return 1 if $_ eq 'house';
  }
  return '';
},
  }
);

**OUTPUT**

 Rate foreach hash keys foreach hash values reverse hash
foreach hash keys503931/s---51% -86%
foreach hash values 1022495/s  103%  -- -71%
reverse hash3516174/s  598%244%   --

So looping over the values is about twice as fast as looping over the keys and
extracting the corresponding values, but accessing a prebuilt reversed hash is
about seven times faster. And this is for an unrepresentatively tiny set of
data; if there was some real content in the sample the reverse hash would be
streets ahead.

In summary, my rule is to start by writing a program so that it does things in
the most obvious way so as to minimise the risk of errors and bugs. Once it is
working, if it also runs quickly enough then the job is done; great. If not,
then we must start to optimise the nice clean code and make it dirtier but
faster. I think it is clear which of the above is the most transparent solution
to checking whether a hash contains a particular value, and in practise it is
actually going to be the fastest, so lucky us.

 Never knew I could do a foreach (values %hash), I only use foreach keys for
 a very long time now. I tried searching the documentation for foreach but no
 avail.

Take a look at

perldoc -f values

and, while you're at it,

perldoc -f each

HTH,

Rob


Oh, and a PS. If you don't need the full reversal of a hash, but just a
derivative hash containing all the first hash's values as keys, then the hash
slice I put in my original solution

my %rhash;
@rhash{values %hash} = ();

is actually faster than

my %rhash = reverse %hash;

as it doesn't have to duplicate all the original hash keys as well: they are
irrelevant.

R


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




Selective splits... (treat this pattern as a delimiter only if it is followed by this pattern)

2007-01-19 Thread Michael Alipio
Hi,

Suppose I have:


my $string = 'Jan 19 11:37:21 firewall date=2007-01-19 time=11:42:15 
devname=TESTfirewall device_id=FGT-602905503304 log_id=0104032006 ty
pe=event subtype=admin pri=information vd=root user=admin ui=GUI(192.168.1.1) 
action=login status=success reason=none msg
=User admin login successfully from GUI(192.168.1.1)
';


That is, on a regular \s+ split, I will produce this list of strings:
Jan
19
11:37:21
firewall
date=2007-01-19
time=11:42:15
devname=TESTfirewall
device_id=FGT-602905503304
log_id=0104032006
type=event
subtype=admin
pri=information
vd=root
user=admin
ui=GUI(192.168.1.1)
action=login
status=success
reason=none
msg=User
admin
login
successfully
from
GUI(192.168.1.1)

However, I only want to split it with \s+ as delimiter, only if that \s+ is 
followed by \w+= so that the: 'msg=User admin login successfully from 
GUI(192.168.1.1) will not be splited.

I tried putting parenthesis in my split pattern:

split/(\s+)\w+=/, $_

So that it will only split the line delimited by space if it is followed by any 
\w+= just like when doing a regexp matching.
But the program treats \w+= as part of the whole pattern so instead of getting:

date=2007-01-19

time=11:42:15

devname=TESTfirewall

device_id=FGT-602905503304


I got:

2007-01-19

11:42:15

TESTfirewall

FGT-602905503304


Any idea how to accomplish my goal?








 

It's here! Your new message!  
Get new email alerts with the free Yahoo! Toolbar.
http://tools.search.yahoo.com/toolbar/features/mail/

Re: Selective splits... (treat this pattern as a delimiter only if it is followed by this pattern)

2007-01-19 Thread I . B .

you can use lookaheads:

my @matched = split /\s+(?=\w+=)/,$string;

cheers,
~i

On 1/19/07, Michael Alipio [EMAIL PROTECTED] wrote:

Hi,

Suppose I have:


my $string = 'Jan 19 11:37:21 firewall date=2007-01-19 time=11:42:15 
devname=TESTfirewall device_id=FGT-602905503304 log_id=0104032006 ty
pe=event subtype=admin pri=information vd=root user=admin ui=GUI(192.168.1.1) 
action=login status=success reason=none msg
=User admin login successfully from GUI(192.168.1.1)
';


That is, on a regular \s+ split, I will produce this list of strings:
Jan
19
11:37:21
firewall
date=2007-01-19
time=11:42:15
devname=TESTfirewall
device_id=FGT-602905503304
log_id=0104032006
type=event
subtype=admin
pri=information
vd=root
user=admin
ui=GUI(192.168.1.1)
action=login
status=success
reason=none
msg=User
admin
login
successfully
from
GUI(192.168.1.1)

However, I only want to split it with \s+ as delimiter, only if that \s+ is followed by 
\w+= so that the: 'msg=User admin login successfully from GUI(192.168.1.1) 
will not be splited.

I tried putting parenthesis in my split pattern:

split/(\s+)\w+=/, $_

So that it will only split the line delimited by space if it is followed by any 
\w+= just like when doing a regexp matching.
But the program treats \w+= as part of the whole pattern so instead of getting:

date=2007-01-19

time=11:42:15

devname=TESTfirewall

device_id=FGT-602905503304


I got:

2007-01-19

11:42:15

TESTfirewall

FGT-602905503304


Any idea how to accomplish my goal?










It's here! Your new message!
Get new email alerts with the free Yahoo! Toolbar.
http://tools.search.yahoo.com/toolbar/features/mail/



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




Re: Selective splits... (treat this pattern as a delimiter only if it is followed by this pattern)

2007-01-19 Thread Michael Alipio


- Original Message 
From: I.B. [EMAIL PROTECTED]
To: begginers perl.org beginners@perl.org
Sent: Saturday, January 20, 2007 1:02:39 PM
Subject: Re: Selective splits... (treat this pattern as a delimiter only if 
it is followed by this pattern)

 you can use lookaheads:

 my @matched = split /\s+(?=\w+=)/,$string;

I've already figured that out, but there's one problem  I encountered:
What I did was to modify the entire line first and substitute all necessary 
\s+. with '*' so that splitting will be easier.

Those \s+ were:

all spaces if the following word is not either date or time and also all spaces 
if the following word is \w+=

My regexp looks like this:

$_ =~ s/\s+(?!(date|time))/*/g ;

Now, having a problem where to put my parenthesis to add the ?=\w+= 
condition..
Can you help me with this? I tried several times but I failed.

Thanks.





 cheers,
 ~i

On 1/19/07, Michael Alipio [EMAIL PROTECTED] wrote:
 Hi,

 Suppose I have:


 my $string = 'Jan 19 11:37:21 firewall date=2007-01-19 time=11:42:15 
 devname=TESTfirewall device_id=FGT-602905503304 log_id=0104032006 ty
 pe=event subtype=admin pri=information vd=root user=admin 
 ui=GUI(192.168.1.1) action=login status=success reason=none msg
 =User admin login successfully from GUI(192.168.1.1)
 ';


 That is, on a regular \s+ split, I will produce this list of strings:
 Jan
 19
 11:37:21
 firewall
 date=2007-01-19
 time=11:42:15
 devname=TESTfirewall
 device_id=FGT-602905503304
 log_id=0104032006
 type=event
 subtype=admin
 pri=information
 vd=root
 user=admin
 ui=GUI(192.168.1.1)
 action=login
 status=success
 reason=none
 msg=User
 admin
 login
 successfully
 from
 GUI(192.168.1.1)

 However, I only want to split it with \s+ as delimiter, only if that \s+ is 
 followed by \w+= so that the: 'msg=User admin login successfully from 
 GUI(192.168.1.1) will not be splited.

 I tried putting parenthesis in my split pattern:

 split/(\s+)\w+=/, $_

 So that it will only split the line delimited by space if it is followed by 
 any \w+= just like when doing a regexp matching.
 But the program treats \w+= as part of the whole pattern so instead of 
 getting:

 date=2007-01-19

 time=11:42:15

 devname=TESTfirewall

 device_id=FGT-602905503304


 I got:

 2007-01-19

 11:42:15

 TESTfirewall

 FGT-602905503304


 Any idea how to accomplish my goal?









 
 It's here! Your new message!
 Get new email alerts with the free Yahoo! Toolbar.
 http://tools.search.yahoo.com/toolbar/features/mail/


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









 

8:00? 8:25? 8:40? Find a flick in no time 
with the Yahoo! Search movie showtime shortcut.
http://tools.search.yahoo.com/shortcuts/#news

character classes vs regexp alternatives (using ( ) or [ ]

2007-01-19 Thread Michael Alipio
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.










 

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

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

2007-01-19 Thread I . B .

[,|\s+] - means one of the following characters: , or | or \s or +

(,|\s+) - means , or \s+

, but yeas alternation will match $2 in :

~ /date=(\S+?)(\s+|,)/;



On 1/19/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.












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



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




Re: Selective splits... (treat this pattern as a delimiter only if it is followed by this pattern)

2007-01-19 Thread I . B .

substitute \s with * to make split easier sounds like a very bad idea.
didn't see people doing that in perl.
problem is solved, why do you add extra complexity?

On 1/19/07, Michael Alipio [EMAIL PROTECTED] wrote:




- Original Message 
From: I.B. [EMAIL PROTECTED]
To: begginers perl.org beginners@perl.org
Sent: Saturday, January 20, 2007 1:02:39 PM
Subject: Re: Selective splits... (treat this pattern as a delimiter only
if it is followed by this pattern)

 you can use lookaheads:

 my @matched = split /\s+(?=\w+=)/,$string;

I've already figured that out, but there's one problem  I encountered:
What I did was to modify the entire line first and substitute all necessary
\s+. with '*' so that splitting will be easier.

Those \s+ were:

all spaces if the following word is not either date or time and also all
spaces if the following word is \w+=

My regexp looks like this:

$_ =~ s/\s+(?!(date|time))/*/g ;

Now, having a problem where to put my parenthesis to add the ?=\w+=
condition..
Can you help me with this? I tried several times but I failed.

Thanks.






 cheers,
 ~i

On 1/19/07, Michael Alipio [EMAIL PROTECTED] wrote:
 Hi,

 Suppose I have:


 my $string = 'Jan 19 11:37:21 firewall date=2007-01-19 time=11:42:15
devname=TESTfirewall device_id=FGT-602905503304 log_id=0104032006 ty
 pe=event subtype=admin pri=information vd=root user=admin
ui=GUI(192.168.1.1) action=login status=success reason=none msg
 =User admin login successfully from GUI(192.168.1.1)
 ';


 That is, on a regular \s+ split, I will produce this list of strings:
 Jan
 19
 11:37:21
 firewall
 date=2007-01-19
 time=11:42:15
 devname=TESTfirewall
 device_id=FGT-602905503304
 log_id=0104032006
 type=event
 subtype=admin
 pri=information
 vd=root
 user=admin
 ui=GUI(192.168.1.1)
 action=login
 status=success
 reason=none
 msg=User
 admin
 login
 successfully
 from
 GUI(192.168.1.1)

 However, I only want to split it with \s+ as delimiter, only if that \s+
is followed by \w+= so that the: 'msg=User admin login successfully from
GUI(192.168.1.1) will not be splited.

 I tried putting parenthesis in my split pattern:

 split/(\s+)\w+=/, $_

 So that it will only split the line delimited by space if it is followed
by any \w+= just like when doing a regexp matching.
 But the program treats \w+= as part of the whole pattern so instead of
getting:

 date=2007-01-19

 time=11:42:15

 devname=TESTfirewall

 device_id=FGT-602905503304


 I got:

 2007-01-19

 11:42:15

 TESTfirewall

 FGT-602905503304


 Any idea how to accomplish my goal?











 It's here! Your new message!
 Get new email alerts with the free Yahoo! Toolbar.
 http://tools.search.yahoo.com/toolbar/features/mail/


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




 
No need to miss a message. Get email on-the-go
with Yahoo! Mail for Mobile. Get started.


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