Re: behavior regex code to count no. of alphabets in a scalar variable?

2008-04-13 Thread Chas. Owens
On Mon, Apr 14, 2008 at 1:06 AM, sanket vaidya <[EMAIL PROTECTED]> wrote: snip > my $scalar = "aaaba" =~ /a/g; snip > my $count = $scalar =~ tr/a//; snip > Count=0 > Scalar = 1 > > Please explain this behavior i.e. why the values of count & scalar are 0 & 1 > respectively? snip $scalar holds

Re: trying to use IO::Handle

2008-04-13 Thread John W. Krahn
Richard Lee wrote: John W. Krahn wrote: Or you could do something like this: use POSIX (); use constant ONE_HOUR => 60 * 60; my $file_name = '/var/log/server.log'; my @bad = do { my $start = qr/@{[ POSIX::strftime( '%b %e %H:', localtime time - ONE_HOUR ) ]}/; open my $SOURCE, '<

behavior regex code to count no. of alphabets in a scalar variable?

2008-04-13 Thread sanket vaidya
Go through the below codes. use strict; use warnings; my $scalar = "aaaba" =~ /a/g; my @list = "aaaba" =~ /a/g; my $list_then_scalar =()= "aaaba" =~ /a/g; print "scalar: $scalar\nlist: @list\nlist then scalar: $list_then_scalar\n"; $scalar="aaaba"; my $count = $scalar =~ tr/a//; print "\n\nCou

Re: trying to use IO::Handle

2008-04-13 Thread Chas. Owens
On Mon, Apr 14, 2008 at 12:50 AM, Richard Lee <[EMAIL PROTECTED]> wrote: snip > >my $start = qr/@{[ POSIX::strftime( '%b %e %H:', localtime time - > > ONE_HOUR ) ]}/; snip > I will have to try out your POSIX solution as I have particular in adding > space if date is single digits (April 3 get

Re: trying to use IO::Handle

2008-04-13 Thread Richard Lee
John W. Krahn wrote: Richard Lee wrote: John W. Krahn wrote: Richard Lee wrote: John W. Krahn wrote: Richard Lee wrote: John W. Krahn wrote: You are not returning the contents of @bad and you are not passing in the name of the file to open so I assume that this is in a subroutine becaus

Re: trying to use IO::Handle

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 11:13 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: > Chas. Owens wrote: > > > On Sun, Apr 13, 2008 at 10:44 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: > > snip > > > > does subroutine introduce additional delay? > > > All non-inlined subroutines introduce some delay, alth

Re: Perl comparison function for binary search

2008-04-13 Thread Nelson Castillo
On Sun, Apr 13, 2008 at 10:17 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: > > Nelson Castillo wrote: (cut) > That won't work correctly unless the numbers are sorted correctly: > > $ perl -le' print for sort { $a cmp $b } 0, 2, 3, 11, 12' > 0 > 11 > 12 > 2 > 3 Hi. I wanted to stress that wi

Re: Perl comparison function for binary search

2008-04-13 Thread John W. Krahn
Nelson Castillo wrote: On Sun, Apr 13, 2008 at 3:10 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: (cut) my $c = &$cmpf($arr->[$mid], $value); That is usually written as: my $c = $cmpf->($arr->[$mid], $value); Thanks Chas. and John for your feedback. I think I'm happy with this ve

Re: trying to use IO::Handle

2008-04-13 Thread John W. Krahn
Chas. Owens wrote: On Sun, Apr 13, 2008 at 10:44 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: snip does subroutine introduce additional delay? All non-inlined subroutines introduce some delay, although that is not your problem as you are only calling it once. snip Perl has inlined subroutin

Re: trying to use IO::Handle

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 10:44 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: snip > > does subroutine introduce additional delay? > > > > All non-inlined subroutines introduce some delay, although that is not your > problem as you are only calling it once. snip Perl has inlined subroutines now? Wh

Re: trying to use IO::Handle

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 9:21 PM, Richard Lee <[EMAIL PROTECTED]> wrote: snip > does subroutine introduce additional delay? snip Yes, there is a non-trivial amount of work done setting up a function call; however, since you are only calling it once that isn't a big deal. My rule of thumb for crea

Re: trying to use IO::Handle

2008-04-13 Thread John W. Krahn
Richard Lee wrote: John W. Krahn wrote: Richard Lee wrote: John W. Krahn wrote: Richard Lee wrote: John W. Krahn wrote: You are not returning the contents of @bad and you are not passing in the name of the file to open so I assume that this is in a subroutine because you are calling it m

Re: [PBML] Setting arbitrary-depth hash from file

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 5:45 PM, Randal L. Schwartz <[EMAIL PROTECTED]> wrote: > > "Kelly" == Kelly Jones <[EMAIL PROTECTED]> writes: > > Kelly> I have a file with this in it: > Kelly> a.b = 10 > Kelly> c.d.e = 11 > Kelly> f.g.h.i.j.k = 12 > > Kelly> Based on that, I want to set: > > Kell

Re: Perl comparison function for binary search

2008-04-13 Thread Nelson Castillo
On Sun, Apr 13, 2008 at 3:10 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: (cut) > >my $c = &$cmpf($arr->[$mid], $value); > > > > That is usually written as: > > > my $c = $cmpf->($arr->[$mid], $value); Thanks Chas. and John for your feedback. I think I'm happy with this version: #!/us

Re: [PBML] Setting arbitrary-depth hash from file

2008-04-13 Thread Randal L. Schwartz
> "Richard" == Richard Lee <[EMAIL PROTECTED]> writes: Richard> I am still a newbie.. and I used monks for a while. It's extremely Richard> helpful just like this mailing list. However, I personally prefer Richard> mailing list as it's easy for me to follow my own question and it's Richard> b

Re: [PBML] Setting arbitrary-depth hash from file

2008-04-13 Thread Richard Lee
Randal L. Schwartz wrote: "Kelly" == Kelly Jones <[EMAIL PROTECTED]> writes: Kelly> I have a file with this in it: Kelly> a.b = 10 Kelly> c.d.e = 11 Kelly> f.g.h.i.j.k = 12 Kelly> Based on that, I want to set: Kelly> $HASH{a}{b} = 10; Kelly> $HASH{c}{d}{e} = 11; Kelly> $HASH{f}{g

Re: trying to use IO::Handle

2008-04-13 Thread Richard Lee
John W. Krahn wrote: Richard Lee wrote: John W. Krahn wrote: Richard Lee wrote: John W. Krahn wrote: You are not returning the contents of @bad and you are not passing in the name of the file to open so I assume that this is in a subroutine because you are calling it many times in your pro

Re: trying to use IO::Handle

2008-04-13 Thread John W. Krahn
Richard Lee wrote: John W. Krahn wrote: Richard Lee wrote: John W. Krahn wrote: You are not returning the contents of @bad and you are not passing in the name of the file to open so I assume that this is in a subroutine because you are calling it many times in your program? Yes, it is sub

Re: trying to use IO::Handle

2008-04-13 Thread Richard Lee
John W. Krahn wrote: Richard Lee wrote: John W. Krahn wrote: Richard Lee wrote: sub shove_it { my $start = time_this(scalar localtime( ( time() - ( 60 * 60 ) ) )); What does the time_this() function do? sub time_this { my $chunli = join(' ', ( split( /\s+/, $_[0]))[1,2,3]);

Re: [PBML] Setting arbitrary-depth hash from file

2008-04-13 Thread Randal L. Schwartz
> "Kelly" == Kelly Jones <[EMAIL PROTECTED]> writes: Kelly> I have a file with this in it: Kelly> a.b = 10 Kelly> c.d.e = 11 Kelly> f.g.h.i.j.k = 12 Kelly> Based on that, I want to set: Kelly> $HASH{a}{b} = 10; Kelly> $HASH{c}{d}{e} = 11; Kelly> $HASH{f}{g}{h}{i}{j}{k} = 12; Kelly> This is

Re: trying to use IO::Handle

2008-04-13 Thread John W. Krahn
Richard Lee wrote: John W. Krahn wrote: Richard Lee wrote: sub shove_it { my $start = time_this(scalar localtime( ( time() - ( 60 * 60 ) ) )); What does the time_this() function do? sub time_this { my $chunli = join(' ', ( split( /\s+/, $_[0]))[1,2,3]); if ($chunli =~ s

Re: Setting arbitrary-depth hash from file

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 4:27 PM, Kelly Jones <[EMAIL PROTECTED]> wrote: > I have a file with this in it: > > a.b = 10 > c.d.e = 11 > f.g.h.i.j.k = 12 > > Based on that, I want to set: > > $HASH{a}{b} = 10; > $HASH{c}{d}{e} = 11; > $HASH{f}{g}{h}{i}{j}{k} = 12; > > This is easy using eval(),

Setting arbitrary-depth hash from file

2008-04-13 Thread Kelly Jones
I have a file with this in it: a.b = 10 c.d.e = 11 f.g.h.i.j.k = 12 Based on that, I want to set: $HASH{a}{b} = 10; $HASH{c}{d}{e} = 11; $HASH{f}{g}{h}{i}{j}{k} = 12; This is easy using eval(), but is there a better way? I realize that entries like "a.b = 10" and "a.b.c = 13" would conflict, s

Re: trying to use IO::Handle

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 4:12 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: snip > > > push @bad, "$3_$5_$4_$6_$2_$1"; > > > > > snip > > > > I believe they would come out roughly the same in terms of > > performance, but, in general, I don't trust $1, $2, etc outside of a > > substitution. I have

Re: trying to use IO::Handle

2008-04-13 Thread John W. Krahn
Chas. Owens wrote: On Sun, Apr 13, 2008 at 3:20 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: snip Why not just: snip push @bad, "$3_$5_$4_$6_$2_$1"; snip I believe they would come out roughly the same in terms of performance, but, in general, I don't trust $1, $2, etc outside of a substitu

Re: Perl comparison function for binary search

2008-04-13 Thread John W. Krahn
Nelson Castillo wrote: Hi :-) Hello, I wrote this binary search function. I wrote it so that I could pass a comparison function as the last parameter. But I have to write "sub" and I noticed that the built in sort function doesn't need it. So I have to write: sub { shift <=> shift} instead

Re: trying to use IO::Handle

2008-04-13 Thread Richard Lee
John W. Krahn wrote: Richard Lee wrote: sub shove_it { my $start = time_this(scalar localtime( ( time() - ( 60 * 60 ) ) )); What does the time_this() function do? sub time_this { my $chunli = join(' ', ( split( /\s+/, $_[0]))[1,2,3]); if ($chunli =~ s/^(\S\S\S)(\s)(\d\d\

Re: trying to use IO::Handle

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 3:20 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: snip > Why not just: snip > push @bad, "$3_$5_$4_$6_$2_$1"; snip I believe they would come out roughly the same in terms of performance, but, in general, I don't trust $1, $2, etc outside of a substitution. I have been bi

Re: trying to use IO::Handle

2008-04-13 Thread John W. Krahn
Chas. Owens wrote: On Sun, Apr 13, 2008 at 2:32 PM, Richard Lee <[EMAIL PROTECTED]> wrote: snip But I am not 100% sure what you are saying on " Why are you making sure the data is right and then pulling the data you want out? " ? I need to move onto next data if it doesn't conform to 2nd regex

Re: trying to use IO::Handle

2008-04-13 Thread John W. Krahn
Chas. Owens wrote: On Sun, Apr 13, 2008 at 2:32 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: snip Or better, use the qr// operator to compile $start. perldoc -q /o snip I thought about suggesting qr//, but the source of $start is my $start = time_this(scalar localtime( ( time() - ( 60 * 6

Re: trying to use IO::Handle

2008-04-13 Thread John W. Krahn
Richard Lee wrote: sub shove_it { my $start = time_this(scalar localtime( ( time() - ( 60 * 60 ) ) )); What does the time_this() function do? open my $source, "<", "/var/log/server.log" or die "Could NOT open /var/log/server.log: $!"; while ( <$source> )

Re: Perl comparison function for binary search

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 2:38 PM, Nelson Castillo <[EMAIL PROTECTED]> wrote: > Hi :-) > > I wrote this binary search function. I wrote it so that I could pass > a comparison function as the last parameter. But I have to write > "sub" and I noticed that the built in sort function doesn't need it.

Re: trying to use IO::Handle

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 2:32 PM, Richard Lee <[EMAIL PROTECTED]> wrote: snip > But I am not 100% sure what you are saying on " Why are you making sure the > data is right and then pulling the data you want out? " ? > I need to move onto next data if it doesn't conform to 2nd regex and then I > am

Perl comparison function for binary search

2008-04-13 Thread Nelson Castillo
Hi :-) I wrote this binary search function. I wrote it so that I could pass a comparison function as the last parameter. But I have to write "sub" and I noticed that the built in sort function doesn't need it. So I have to write: sub { shift <=> shift} instead of: {$a <=> b}. This might be a

Re: trying to use IO::Handle

2008-04-13 Thread Richard Lee
Chas. Owens wrote: On Sun, Apr 13, 2008 at 2:05 PM, Richard Lee <[EMAIL PROTECTED]> wrote: snip next if ! /^$start/; snip You should say next unless /^$start/o; The o makes a promise that $start won't change, so the regex only gets compiled once (it is recompiling the rege

Re: trying to use IO::Handle

2008-04-13 Thread John W. Krahn
Chas. Owens wrote: On Sun, Apr 13, 2008 at 2:05 PM, Richard Lee <[EMAIL PROTECTED]> wrote: snip next if ! /^$start/; snip You should say next unless /^$start/o; The o makes a promise that $start won't change, so the regex only gets compiled once (it is recompiling the regex each t

Re: trying to use IO::Handle

2008-04-13 Thread John W. Krahn
Richard Lee wrote: why does below fail? - where it's failing - open my $source, "<", "/tmp/server.log" or die "Could NOT open /var/log/server.log: $!"; $source is an opened filehandle. my $file = new IO::Handle; $file is an IO::Handle object but it is not rela

Re: trying to use IO::Handle

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 2:16 PM, Chas. Owens <[EMAIL PROTECTED]> wrote: snip > >next if ! m{ > >.+\s+D\s+ > > udp\s+ > > \d+\.\d+\.\d+\.\d+\s+ > > \d+\.\d+\.\d+\.\d+\s+ > >

Re: trying to use IO::Handle

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 2:05 PM, Richard Lee <[EMAIL PROTECTED]> wrote: snip >next if ! /^$start/; snip You should say next unless /^$start/o; The o makes a promise that $start won't change, so the regex only gets compiled once (it is recompiling the regex each time through the loop

Re: trying to use IO::Handle

2008-04-13 Thread Richard Lee
Rob Dixon wrote: Richard Lee wrote: why does below fail? - where it's failing - open my $source, "<", "/tmp/server.log" or die "Could NOT open /var/log/server.log: $!"; my $file = new IO::Handle; while ($_ = $file->getline($source) ) { --

Re: trying to use IO::Handle

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 1:35 PM, Richard Lee <[EMAIL PROTECTED]> wrote: > why does below fail? > > - where it's failing - > open my $source, "<", "/tmp/server.log" > or die "Could NOT open /var/log/server.log: $!"; > my $file = new IO::Handle; > while ($_ = $fil

Re: trying to use IO::Handle

2008-04-13 Thread Rob Dixon
Richard Lee wrote: > > why does below fail? > > - where it's failing - > open my $source, "<", "/tmp/server.log" > or die "Could NOT open /var/log/server.log: $!"; > my $file = new IO::Handle; > while ($_ = $file->getline($source) ) { > ---

trying to use IO::Handle

2008-04-13 Thread Richard Lee
why does below fail? - where it's failing - open my $source, "<", "/tmp/server.log" or die "Could NOT open /var/log/server.log: $!"; my $file = new IO::Handle; while ($_ = $file->getline($source) ) { Uncaught exception from user cod

Re: questions from Learning Perl

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 11:58 AM, brian d foy <[EMAIL PROTECTED]> wrote: snip > > First off, don't call functions with &*. > > The is _Learning Perl_, and he hasn't got to the point where we tell > the reader they don't need the & in front of subroutine calls. Most of > the progression goes fro

Re: how to construct regex to count no. of alphabets in a scalar variable

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 12:05 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: snip > > In scalar context it returns true if it matches at all and in list > > context it returns a list of matches. Now, normally a list in scalar > > context returns the last element of the list, > > > > That is the com

Re: how to construct regex to count no. of alphabets in a scalar variable

2008-04-13 Thread John W. Krahn
Chas. Owens wrote: On Sun, Apr 13, 2008 at 4:03 AM, <[EMAIL PROTECTED]> wrote: my $count = () = $str =~ /a/g; Thanks Chas. Owens, I need some explanation on the above on how the above regex count the number of 'a' in a string. With my limited understanding, this is what I thought:- $coun

Re: questions from Learning Perl

2008-04-13 Thread brian d foy
In article <[EMAIL PROTECTED]>, Chas. Owens <[EMAIL PROTECTED]> wrote: > On Sun, Apr 6, 2008 at 5:36 AM, <[EMAIL PROTECTED]> wrote: > snip > > my $user_total = &total(); > snip > > First off, don't call functions with &*. The is _Learning Perl_, and he hasn't got to the point where we tell t

Re: questions from Learning Perl

2008-04-13 Thread brian d foy
In article <[EMAIL PROTECTED]>, Gunnar Hjalmarsson <[EMAIL PROTECTED]> wrote: > I don't understand what you mean. However, the code below confuses me > for another reason. > > > use strict; > > use warnings; > > my @fred = qw/1 3 5 7 9/; > > my $fred_total = &total(@fred); > > print "The total o

Re: how to construct regex to count no. of alphabets in a scalar variable

2008-04-13 Thread Chas. Owens
On Sun, Apr 13, 2008 at 4:03 AM, <[EMAIL PROTECTED]> wrote: > > > my $count = () = $str =~ /a/g; > > > Thanks Chas. Owens, > I need some explanation on the above on how the above regex count the > number of 'a' in a string. > > With my limited understanding, this is what I thought:- > $count =

Re: how to construct regex to count no. of alphabets in a scalar variable

2008-04-13 Thread itshardtogetone
my $count = () = $str =~ /a/g; Thanks Chas. Owens, I need some explanation on the above on how the above regex count the number of 'a' in a string. With my limited understanding, this is what I thought:- $count = () ; #To me it means $count is assigned with a undefined value which is then ass