the classic which is the fastest way to sort question

2009-11-09 Thread Michael Alipio
Hi,

i'm planning to sort an input file (which was File::Slurp'ed, most likely 
megabyte-sized file) in various ways. I did some readings and learned several 
methods
that people have come up with in recent years. So to summarize, the default 
sort is fast (uses quick sort), explicit (using sub) is a bit slower, other 
method
that uses caching is faster. Then there's Schwartzian Transform and a packed 
version by Guttman. Seems like everything is clear. Guttman is the fastest,
until I went to cpan. Found Sort::Key, which claims to be the fastest, even 
faster that ST, GRT. Now, before someone says, why not try each one and
see for yourself (which doing such could be another subject for me to learn), 
my question is this: if such faster sorting algorithms exist, why don't they 
just replace the default sort function in Perl?

And for the classical question, given my situation (in combination with 
File::Slurp), which is fastest sort method? (I hope somebody includes this in 
perlfaq in the future).


  

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




Re: the classic which is the fastest way to sort question

2009-11-09 Thread Michael Alipio
Hi,

 

  Do you need the fastest possible sort? 

I'm not even sure if I really need to worry about all these 
sorting techniques. My program just reads a text file 
(wordlist). It might be megabyte-sized or probably few 
gigabytes (i might also add size checking on this to be
safe with File::Slurp). Then I will give the user an option
of sorting it in various ways, like length, alphabetical,
numerical, frequency of letters, etc. 

I see, so it all boils down to how expensive the 
comparison you're going to implement to fully benefit 
from these techniques. Now comes another question 
for me to find the answer to, how expensive the 
comparisons in my sorting function would be... I 
guess there's no other way for me to find this out 
than to try it out  myself. What's worse is that 
there's also a depends on the system factor to 
consider as well. Sometimes I wish perl's motto 
is there's only one best way to do it so everyone
would just agree on one way of doing something, 
so everyone would have the same beautiful
and efficient code. For now, I will probably just stick
to using the built-in sort (just for sorting length, 
numbers, and letters), until I have gained enough 
knowledge about why it's necessary to use the 
other techniques, or how to do the benchmark 
myself.


 Philip

 PS your email client has a very long line length, causing my quoting
  above to go somewhat haywire. I'd recommend setting it to something
 like 74.





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




must satisfy _unknown_ number of regexp patterns

2009-11-05 Thread Michael Alipio
Hi,

if I have a script that accepts any combination of the 5 or maybe even more 
options, say, option1, option2, option3...


Now, after collecting the options, for each option, there is a corresponding 
regexp pattern. I will then build an if statement, where the test should be, 
all the options entered must match () otherwise, return false.
I'm thinking this can only be done by nested if's:

if ($word =~ /$option1/  $word =~ /$option2){
  if ($word =~ /$option3/  $word =~ /$option4){
 if ($word =~ /$optionN/){
print All pattern matched!\n;
 }
  }
}


Now I'm thinking, it is quite impossible to dynamically create all those if 
tests. Perhaps I can just open a file for writing, write a new perl script 
which will have those codes, and execute it at the end. 

Is there a better way of doing this?









  

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




Re: compact my wordlist generator

2009-10-26 Thread Michael Alipio
hi,

I have this code, mentioned below:

my @word = qw(a b c );
my $length = @word;
my $char1, $char2, $char3;

if ($length == 3){
for ($char1 = 0; $char1$len;$char1++){
 for ($char2 = 0; $char2$len;$char2++){
  for ($char3 = 0; $char3$len;$char3++){
print $word[$char1]$word[$char2]$word[$char3]\n;
}}}
}elsif($length == 2){
 for ($char2 = 0; $char2$len;$char2++){
  for ($char3 = 0; $char3$len;$char3++){
print $word[$char1]$word[$char2]\n;
}elsif($length == n){


and so on...
}


Then as suggested (see far below), a recursive function will do what I want.. 
After some googling I found this:


permutate(0,2);

sub permutate($$){

  my ($cur,$max) = @_;
  
  if ($cur=$max){
 print $result\n;
 return;
  }

  for(@word){
substr($result,$cur,1)=$_;
perm($cur+1,$max);
  }
}


Can anyone tell me how the code above works? My original program must deal with 
arbitrary length and generate all the possible combinations (even repeating) of 
a particular set. What could take me gazillions of for loops for that, somebody 
just came up with less than ten lines. 
I can just copy and paste the code I found above but I want to learn how it 
works. How could someone write this code so easily but when I tried even 
writing just a pseudocode for it, my head almost exploded. 

I want to be a good programmer, but at this rate, I don't think I can be one if 
I will just keep copying and pasting someone else's code or downloading modules 
from CPAN.

Can anyone teach me how my own code (the one with gazillion for loops), can be 
converted into a pseudocode then eventually into a working sub procedure?




--- On Sun, 10/25/09, Gabor Szabo szab...@gmail.com wrote:

  Hi,
 
   I'm trying to write a word list generator which can
   generate all possible combinations of n characters,
 within n
   set of characters.
 
 
   So far, this is what I have come up. The only input
 is the
   lenght of the password the user wants.
 



 What about keeping the characters in an array @char
 so you will have
 $char[0], $char[1] etc. a
 
 nd the loops could be replaced by a recursive function
 call. Something
 like this:
 
 do_something_for_char($k)
 
 sub do_something_for_char {
   my ($k) = @_;
   return if $k = $n;
    do_something_for_char($n+1);
 }
 
 
 Gabor
 





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




Re: AW: compact my wordlist generator

2009-10-26 Thread Michael Alipio
Thanks for the advice. Forgive me if I sounded like someone who's frustrated, 
couldn't do his homework asking somebody else for help.

When I was learning C programming, I read that learning those difficult 
algorithms such as bubble sort, quick sort, binary search, is something that 
only programming students have to deal with. 

I have also learned that the best way to learn is by looking at other peoples 
code. So far I managed to write a number of useful perl programs through this. 
I just got frustrated with this particular task I had to accomplish. I've done 
some readings before about those math topics you've mentioned. As I understood, 
permutation is producing all combinations without repetition. A part of my code 
uses the module Algorithm::Permute for this purpose and for the other part 
which produces all possible (repeating) combinations, I have to write this 
code. Somebody gave me an example pseudocode and I tried to convert my code 
into it but I just couldn't do it.

I knew I needed a recursive function, I just didn't know how to start.
What confused me more is that the code I found is a lot different from what I 
was initially thinking. He used the builtin function substr in the code in a 
way that I couldn't figure how it worked.

See below:

my $result = '';

perm(0,2);

sub perm($$){
my ($cur,$max) = @_;

if ($cur=$max){
 print $result\n;
return;
}

for(@word){
substr($result,$cur,1)=$_;
perm($cur+1,$max);
}
}

What's the use of double ($$) after sub perm?? Does it have anything to do with 
process IDs (perldoc perlvar says so)?

This line is also confusing:
substr($result,$cur,1)= $_;
 
Substr returns a list, right? The first parameter is the expression, 2nd is the 
offset, and last is the length.
The above line is too cryptic for me. What confused me more is that the return 
value of substr was assigned the $_.


If someone can help me decipher each line, i'll be very happy.



--- On Mon, 10/26/09, Thomas Bätzler t.baetz...@bringe.com wrote:

 From: Thomas Bätzler t.baetz...@bringe.com
 Subject: AW: compact my wordlist generator
 To: begginers perl.org beginners@perl.org
 Cc: Michael Alipio daem0n...@yahoo.com
 Date: Monday, October 26, 2009, 10:40 PM
 Michael Alipio daem0n...@yahoo.com
 wrote:
  Can anyone tell me how the code above works? My
 original program must
  deal with arbitrary length and generate all the
 possible combinations
  (even repeating) of a particular set. What could take
 me gazillions of
  for loops for that, somebody just came up with less
 than ten lines.
  I can just copy and paste the code I found above but I
 want to learn how
  it works. How could someone write this code so easily
 but when I tried
  even writing just a pseudocode for it, my head almost
 exploded.
 
 It probably helps if you have some understanding of the
 underlying mathematics of the problem domain, i.e. in this
 case combinatorics.
 
 This would also clear up potential misunderstandings viz
 the definition of a permutation (randomly pick n of m
 elements without putting them back) vs. that of a
 combination (randomly pick n of m elements while putting
 them back).
 
  I want to be a good programmer, but at this rate, I
 don't think I can be
  one if I will just keep copying and pasting someone
 else's code or
  downloading modules from CPAN.
 
 Actually, taking somebody else's code and trying to figure
 out how it works is a good exercise ;-)
  
  Can anyone teach me how my own code (the one with
 gazillion for loops),
  can be converted into a pseudocode then eventually
 into a working sub
  procedure?
 
 No, probably not right away. But the good news is that most
 people don't come up with elegant solutions off the top of
 their heads - instead, they will have learned a number of
 basic patterns and/or algorithms for solving common
 problems, and they will have the experience to adapt what
 they know to new problems.
 
 So, in order to become a better programmer you should look
 at stuff people have been doing before you. Go to your local
 library and borrow a book on Algorithms. Try to implement
 them in Perl. Have a look at the Perl cookbook. If you want
 a challenge, then treat yourself to a copy of Higher Order
 Perl by Mark Jason Dominus.
 
 HTH,
 Thomas
 
 





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




compact my wordlist generator

2009-10-25 Thread Michael Alipio
Hi,

 I'm trying to write a word list generator which can
 generate all possible combinations of n characters, within n
 set of characters.
 
 
 So far, this is what I have come up. The only input is the
 lenght of the password the user wants.
 
 my @set = qw(a b c d e f g h i j k l m n o p q r s t u v w
 x y z);
 my $len = scalar @set;
 my $char1;
 my $char2;
 my $char3;
 my $char4;
 my $char5;
 my $char6;
 my $char7;
 my $char8;
 
 if ($pwlen == 8){
 
 for ($char1=0;$char1$len;$char1++){
   for ($char2=0;$char2$len;$char2++){
     for ($char3=0;$char3$len;$char3++){
 
     ... upto
 
     for ($char8=0;$char8$len;$char8++){
 
 print
 
$set[$char1]$set[$char2]$set[$char3]$set[$char4]$set[$char5]$set[$char6]set[$char7]$set[$char8]\n;
 
 
 
 } elseif ( $pwlen == 7){
 
 for ($char2=0;$char2$len;$char1++){
   for ($char3=0;$char3$len;$char2++){
     for ($char4=0;$char4$len;$char3++){
 
     ... upto
 
     for ($char8=0;$char8$len;$char8++){
 
 print
 
$set[$char2]$set[$char3]$set[$char4]$set[$char5]$set[$char6]$set[$char7]set[$char8]\n;
 
 }}}
 
 }
 
 
 The problem with the code above is that the length of words
 is hard coded. Only 8 maximum. I'm looking for ways on how
 to make my code flexible (length can be whatever the user
 wants ).
 
 My code is limited to 8 chars maximum length plus if I want
 to increase it, I have to add another set of for loops plus
 another variable to use, e.g., $char9, char10.. and so
 on...
 
 
 Any idea?
 
 
       






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




Re: compact my wordlist generator

2009-10-25 Thread Michael Alipio
I was thinking about this recursive thing... thanks for the tip.. will try this 
out.. I hope I can accomplish it.

--- On Sun, 10/25/09, Gabor Szabo szab...@gmail.com wrote:

 From: Gabor Szabo szab...@gmail.com
 Subject: Re: compact my wordlist generator
 To: Michael Alipio daem0n...@yahoo.com
 Cc: begginers perl.org beginners@perl.org
 Date: Sunday, October 25, 2009, 5:06 PM
 2009/10/25 Michael Alipio daem0n...@yahoo.com:
  Hi,
 
   I'm trying to write a word list generator which can
   generate all possible combinations of n characters,
 within n
   set of characters.
 
 
   So far, this is what I have come up. The only input
 is the
   lenght of the password the user wants.
 
   my @set = qw(a b c d e f g h i j k l m n o p q r s t
 u v w
   x y z);
   my $len = scalar @set;
   my $char1;
   my $char2;
   my $char3;
   my $char4;
   my $char5;
   my $char6;
   my $char7;
   my $char8;
 
   if ($pwlen == 8){
 
   for ($char1=0;$char1$len;$char1++){
     for ($char2=0;$char2$len;$char2++){
       for ($char3=0;$char3$len;$char3++){
 
       ... upto
 
       for ($char8=0;$char8$len;$char8++){
 
   print
 
  $set[$char1]$set[$char2]$set[$char3]$set[$char4]$set[$char5]$set[$char6]set[$char7]$set[$char8]\n;
 
   
 
   } elseif ( $pwlen == 7){
 
   for ($char2=0;$char2$len;$char1++){
     for ($char3=0;$char3$len;$char2++){
       for ($char4=0;$char4$len;$char3++){
 
       ... upto
 
       for ($char8=0;$char8$len;$char8++){
 
   print
 
  $set[$char2]$set[$char3]$set[$char4]$set[$char5]$set[$char6]$set[$char7]set[$char8]\n;
 
   }}}
 
   }
 
 
   The problem with the code above is that the length
 of words
   is hard coded. Only 8 maximum. I'm looking for ways
 on how
   to make my code flexible (length can be whatever the
 user
   wants ).
 
   My code is limited to 8 chars maximum length plus if
 I want
   to increase it, I have to add another set of for
 loops plus
   another variable to use, e.g., $char9, char10.. and
 so
   on...
 
 
   Any idea?
 
 What about keeping the characters in an array @char
 so you will have
 $char[0], $char[1] etc. a
 
 nd the loops could be replaced by a recursive function
 call. Something
 like this:
 
 do_something_for_char($k)
 
 sub do_something_for_char {
   my ($k) = @_;
   return if $k = $n;
    do_something_for_char($n+1);
 }
 
 
 Gabor
 





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




split n characters into n chunks

2009-10-25 Thread Michael Alipio
Hi,

How do I split a word into n subsets?

my $word = thequickbrown


If I want three subsets I should be able to create:

the
heq
equ

upto

own


Using split function with limit of 3 gives me:

t h equickbrown



Any idea how to do this? I'm thinking maybe I can just split the whole string 
and push each character into array, then loop through the array, getting 3 
elements set in the proces..







  


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




complex split (when delimiter appears in one of the fields)

2009-06-12 Thread Michael Alipio

Hi,

I have a string that looks like this:

my $string = 1, 3, 0. 0. 0. 0, 22, Zak',adfk $! mac., ;


Basically, there are seven fields. after the bird, everything up to the last 
comma is the 5th field. 6th field is blank.

Now my problem is splitting it and extracting the 5th field.

If I will do a (split/,/,$string)[-2]) then i will only get adfk $! mac.
I need to get the entire Zak' , adfk $! mac.




  

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




2 minutes and 8 second (how do i convert it to 02:08)??

2009-06-11 Thread Michael Alipio

Hi, 

I have a program that computes the number of elapsed minutes and seconds.


if the outputs are:

2 minutes, and 8 seconds.

How do I print those two values to look like Elapsed time: 02:08

Thanks!


  

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




variables gets shared to child but resets back after exiting fork

2009-05-27 Thread Michael Alipio

Hi,

I have to run an external program but the program does not termination on some 
conditions, e.g, ping, will not exit unless you specify -c or some other 
circumstances.


Now I what I want to do is:

my @array;
die Cannot fork myprog unless (defined my $pid = fork)
if ($pid==0){
open MYPROG, myprog | or die Cant run myprog;
my $timeout = 0;
while (MYPROG){
exit(0) if $timeout == 3;
push @array, $_;
sleep 1;
$timeout++;
}

waitpid($pid, 0);
print @array\n;


The problem with the code above is that @array goes back to its initial state 
after exiting the child. No contents are printed. I even tried references but 
it didn't work as well.

If I don't use fork, I the way I would kill the process is by doing a call to 
pkill. With fork, it would be much easier with exit. However the output of 
external program gets discarded.

Can you think of any workaround for this?











  

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




Re: variables gets shared to child but resets back after exiting fork

2009-05-27 Thread Michael Alipio

Sorry about the indention... must be the mail client i'm using.
I need to fork because the external program I want to run inside the child runs 
infinitely. I want to have a timer running in the parent and after that, kill 
the child. Without forking, I have to do a pkill myprogname
I'm reading the perlipc now.. nothing so far..

--- On Wed, 5/27/09, Chas. Owens chas.ow...@gmail.com wrote:

 From: Chas. Owens chas.ow...@gmail.com
 Subject: Re: variables gets shared to child but resets back after exiting fork
 To: Michael Alipio daem0n...@yahoo.com
 Cc: begginers perl.org beginners@perl.org
 Date: Wednesday, May 27, 2009, 7:15 PM
 On Wed, May 27, 2009 at 05:42,
 Michael Alipio daem0n...@yahoo.com
 wrote:
 
  Hi,
 
  I have to run an external program but the program does
 not termination on some conditions, e.g, ping, will not exit
 unless you specify -c or some other circumstances.
 
 
  Now I what I want to do is:
 
  my @array;
  die Cannot fork myprog unless (defined my $pid =
 fork)
  if ($pid==0){
  open MYPROG, myprog | or die Cant run myprog;
  my $timeout = 0;
  while (MYPROG){
  exit(0) if $timeout == 3;
  push @array, $_;
  sleep 1;
  $timeout++;
  }
 
  waitpid($pid, 0);
  print @array\n;
 
 
  The problem with the code above is that @array goes
 back to its initial state after exiting the child. No
 contents are printed. I even tried references but it didn't
 work as well.
 
  If I don't use fork, I the way I would kill the
 process is by doing a call to pkill. With fork, it would be
 much easier with exit. However the output of external
 program gets discarded.
 
  Can you think of any workaround for this?
 snip
 
 
 Variables are not shared between parent and child. 
 The values of the
 parent's variables are copied to the child at the time of
 the fork.
 You really need to read perldoc perlipc[1].  And you
 need to learn how
 to indent code.  Leaving your code all against the
 left side of the
 screen makes it hard to read.
 
 Of course, the biggest question is why are you bothering to
 fork in
 the first place?  Why not just say
 
 my $program = myprog;
 
 open my $pipe, -|, $program
     or die Cant run $program: $!;
 
 my @array;
 for (1 .. 3) {
     last unless defined(my $line =
 $pipe);
     push @array, $line;
 }
 
 close $pipe;
 
 
 
 1. http://perldoc.perl.org/perlipc.html
 
 -- 
 Chas. Owens
 wonkden.net
 The most important skill a programmer can have is the
 ability to read.
 




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




Re: variables gets shared to child but resets back after exiting fork

2009-05-27 Thread Michael Alipio

hey,

thanks for the reply. The -| was the right option for me. The reason is 
because I only need to get the first 4 lines of output of the external program. 
-| was perfect because I don't have to base the stopping on time but rather 
on the number of output lines. Another problem solved!!!
Till next time!





--- On Wed, 5/27/09, Chas. Owens chas.ow...@gmail.com wrote:

 From: Chas. Owens chas.ow...@gmail.com
 Subject: Re: variables gets shared to child but resets back after exiting fork
 To: Michael Alipio daem0n...@yahoo.com
 Cc: begginers perl.org beginners@perl.org
 Date: Wednesday, May 27, 2009, 7:45 PM
 On Wed, May 27, 2009 at 07:24,
 Michael Alipio daem0n...@yahoo.com
 wrote:
 
  Sorry about the indention... must be the mail client
 i'm using.
  I need to fork because the external program I want to
 run inside the child runs infinitely. I want to have a timer
 running in the parent and after that, kill the child.
 Without forking, I have to do a pkill myprogname
  I'm reading the perlipc now.. nothing so far..
 snip
 
 
 You don't need a fork for that, you need alarm[1] and block
 eval[2]:
 
 eval {
     local $SIG{ALRM} = sub { die timeout\n };
     alarm 3; #die after three seconds
     #operation that needs to die if it is not
 finished in three seconds;
     alarm 0; #turn off the alarm clock
     1; #make the eval return true
 } or {
     #propagate the error unless it is the
 timeout
     die $@ unless $@ eq alarm\n;
 };
 
 
 
 1. http://perldoc.perl.org/functions/alarm.html
 2. http://perldoc.perl.org/functions/eval.html
 
 -- 
 Chas. Owens
 wonkden.net
 The most important skill a programmer can have is the
 ability to read.
 
 -- 
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/
 
 
 




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




script to control web browser (enter captcha text manually)

2009-05-23 Thread Michael Alipio

Hi,

I need to register a few accounts on a website that uses captcha. It's just a 
few accounts but it's a very daunting task because there are so many fields to 
fill up. Is there a way I can do this in perl? What I want is that my script 
will fire up the browser, access that particular registration page, then all I 
have to do is to fill in the captcha verification field, the rest have been 
filled already, after wards, pressing enter, the script will retrieve the 
registration page again obtaining a fresh captcha image in the process.


  

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




two nested hashes, one subroutine, after calling, my second hash disappears

2009-05-21 Thread Michael Alipio

Hi,

I thought I had it all figured out. Hash within hashes really made my code 
cleaner until I came up with this code:

While looping, I was creating a key 'webloginpostdata' with value of anonymous 
hash as seen below;

  $all_ap{$_}-{'webloginpostdata'} = {
'var:main' = 'menu',
'testwebcm' = 'webcm',
'login:command/username' = '',
'login:command/password' = '',
'var:connecting1' = '0'
};



Now following the creation above, i'm creating another key 'webadminpostdata' 
which has a value of an anonymous hash again.

  $all_ap{$_}-{'webadminpostdata'} = {
  'var:main' = 'menu',
  'var:style' = 'style5',

  'settings/username' = $all_ap{$_}-{'webuser'},
  'settings/password' = $all_ap{$_}-{'webpass'},
  'settings/password_confirm' = $all_ap{$_}-{'webpass'},
  'settings/idle_timeout' = '30'
};



Then there's two subroutines. The first one (loginweb) is called. If 
successful, the next one (changewebpass) is called


my $login_status = loginweb($all_ap{$_}-{'webloginurl'}, 
$all_ap{$_}-{'webloginpostdata'});
if ($login_status eq 'success'){

   ## Call next subroutine
   
my $setpassword = changewebpass($all_ap{$_}-{'webadminurl'}, 
$all_ap{$_}-{'webadminpostdata'});
if ($setpassword ne 'success'){
  print Can\'t change password\n;
}else{
  print New login created:\n\t username: $all_ap{$_}-{'webuser'} \n\t 
password: $all_ap{$_}-{'webpass'}\n;
}



loginweb sub is really simple:


sub loginweb{

my $url = shift;
my $postdata = shift;
my $username;
my $password;

open PASSLIST, appasswd.txt or die $!;
while (PASSLIST){
$username = (split / === /, $_)[0];
$password = (split / === /, $_)[1];
chomp ($username, $password);
print \nTrying username: \'$username\'  password: \'$password\';
  $$postdata{'login:command/username'} = ($username eq '')?'':$username;
  $$postdata{'login:command/password'} = ($password eq '')?'':$password;
my $res = $ua-request(POST $url, $postdata);
if ($res-content =~ /Basic\s+Home\s+Menu/){
print  SUCCESS!!!\n;
return 'success';
}else{
next;
}

}



Finally, here's the changewebpass


sub changewebpass{

my $url = shift;
my $postdata = shift;
my $res = $ua-request(POST $url, $postdata);

if ($res-is_success){
 return 'success';
}else{
 return 'failed';
 }

}



When I run my program, I get an error:

Need a field name at (eval 11) line 1


That's all. No mention of line number, or whatever. Perhaps that is because the 
error was not a syntax error but a perl module error.
Further investigation showed that after calling loginweb,
my $all_ap($_}-{'webadminpostdata'} gets emptied.

calling 'changewebaccess', nothing is being assigned to $postdata. That is why 
i'm getting that error.



Any idea where to look into?? In my code before, I only had to declare the 
actual hashes:

my %webloginpostdata = (...);
my %webadminpostdata = (...);

Then pass their references to the sub routines: ie,
loginweb(\%webloginpostdata)


I didn't have any troubles doing so.














  

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




Re: two nested hashes, one subroutine, after calling, my second hash disappears

2009-05-21 Thread Michael Alipio

Hi,


      
    $$postdata{'login:command/username'} =
 ($username eq '')?'':$username;
 
 $$postdata{'login:command/username'} is usually written as
 $postdata-{'login:command/username'}.  You compare
 $username to '' and if it is equal to '' then you return ''
 and if it is not equal to '' you return $username.  The
 test is superfluous, just assign $username.


I tried that one. I ran the code against the web application and proxied the 
requests, the form parameter 

e.g, username=adminpassword=blah=

If I will just assign $username or $password I will end up with a non blank 
value +0x (something like that) and the application would throw an error.

Ex.: the following parameter/value is sent to the web app.

username=adminpassword=+0x

While if I only have username=adminpassword=nextparameter

The web app will respond properly.



 
 From what I understand from reading the documentation the
 second argument to POST should be an array reference but you
 are passing it a hash reference.

I'm not sure about this but the first sub was called and executed properly.
The second sub won't execute because the $postdata in there is empty.



  
  When I run my program, I get an error:
 
 An error or a warning?  An error will usually halt the
 program while a warning will not.


I think it is an error, because the program halts. It is supposed to do some 
more routines after those two.

 
 It is hard to tell from the limited amount of code provided
 what exactly is causing your problem.
 

What else can I do?
What other information can I provide?







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




How does printf able to receive a pointer when passing it a string constant?

2009-05-13 Thread Michael Alipio


I have a c code that looks like this:

#includestdio.h

main (){
char girl[] = anna;
char boy[] = jude;
stringcopy(boy, girl); /* copy boy to girl */
printf(%s, girl); 

}

void stringcopy(char *b, char *g){

while ((*g++ = *b++) != '\0')
;
}


It prints fine...
However if I replace the stringcopy call arguments with jude, anna
it compiles fine but i get segmentation fault when running.


How come printf can accept variable names as well as constant strings such as: 

printf (%s, girl); 

and

printf (Hello World\n);


My stringcopy function only accepts pointers. Shouldn't I be passing pointer to 
the first element of anna when passing the string constant anna?? )


How does printf print a string constant then?



  

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




Cross compiling to mipsel

2009-05-12 Thread Michael Alipio

Hi,

I've searched the web and didn't find anything useful. Is there anyway to cross 
compile my perl code to mipsel? or do i have to learn C?? 


  

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




sleep exactly after n seconds (sleep finishing longer than specified)

2009-04-22 Thread Michael Alipio

Hi,

I have a script that forks a child. at the parent, i have a line that tells it 
to sleep for n seconds. Once the 3 seconds have passed, it will kill the child 
process.

I noticed that most of the time, sleep doesn't count exact seconds.. most of 
the time it's longer. Is there any way to sleep precisely for n seconds?


  

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




Controlling screen output. Print something then afterwards replace it

2009-04-22 Thread Michael Alipio

Hi,

Is it possible to print something and then replace what you have printed with a 
different value?

Say I have a program that watches a logfile and prints the current number of 
lines.

after sleeping for n seconds, it will print the number of lines again. But i 
don't want to print it below the old output. I want to replace it with the new 
output.

That is, if I have the previous output as:

The log contains: 30 lines

After 10 seconds, I don't want to see two lines saying:

The log contains: 30 lines
The log contains: 40 lines

Instead I only want to see:
The log contains: 40 lines

Is this possible without removing all other previous output? I mean Only that 
particular line will change. This is commonly seen on programs which updates 
their output in realtime.














  

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




trouble with nested forks() (keep spawning twin process)

2009-04-16 Thread Michael Alipio

Hi,

I have this code:

die Could not fork command1! unless defined (my $command1_pid = fork);
if ($command1_pid == 0){
  open EXTERNAL_PROG1, external_prog1 | or die Can't run external_prog1;

  while (EXTERNAL_PROG1){
if (/pattern/){
  die Could not fork command2 unless defined (my $command2_pid = fork);
  die Could not fork command3 unless defined (my $command3_pid = fork);

if ($command2_pid == 0){
   `external_prog2`
 }
}
 waitpid($command2_pid,0);

if ($command3_pid == 0){
  `external_prog3`;
} 
 waitpid($command3_pid, 0);

}
}



That is, run external_prog1 and watch the output.. as soon as it sees a 
particular pattern, run two other external commands at the same time. However, 
with the above code, I keep getting duplicated external_prog2 process everytime 
i run the script. I need to track both PIDs of external_prog2 and 3 as I need 
to kill them later in the code. However, i'm having trouble running them at the 
same time.
if I will put a kill 15, $command3_pid right after the first waitpid, i will 
surely kill one of those two command 2 process. I tried several places to put 
die lines and waitpids but I couldn't get it right. It's either they don't 
run at the same time or they will run but command 2 has a twin brother.

Any idea how to solve this chicken and egg problem?




  

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




Re: AW: Turn off $ anchor greedy behavior

2009-04-14 Thread Michael Alipio



 my $string = 'boy, pig, 123, 123:412adbd, d0g,
 lajdlf134_ lkadsf !234,';
 
 if( $string =~ m/,\s*([^,]*),[^,]*$/ ){
   print $1\n;
 }

How could you guys write this so simple? My regexp was twice this long.
the regexp after \s* tells perl to match anything (0 or more) that is not a 
comma, right? how come it did not match pig? pig is also followed by comma, 
right? so pig should be captured by ([^,]*), right? I guess perl really looks 
for a match starting from the end of the line.


The string actually looks like this:

ABCD1:5C, 2009-04-14 13:01:24, 2009-04-14, 5, 23, ABC, , , -1, 187, 0, 1.2.3.4, 
20, lkasd123 as_!23:s @12ff,


My Regexp looks like this:
/\.\s+\d+,\s+\d+,\s+(.*),$/


It matches from the comma at the end of the line up to .4 when you go 
backwards. By going as far as this, I can be assured that perl won't find any 
more match, but the regexp looks ugly.

What is wrong with my version?
I think if in the future, if perl finds a line which is not ending in a pattern 
exactly like my regexp then it will fail, however yours i guess won't.


 
 __END__
 
 Depending on the input data size it might worthwhile to
 look at rindex() and substr() instead of using a RE.
 




 HTH,
 Thomas




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




Turn off $ anchor greedy behavior

2009-04-14 Thread Michael Alipio

Hi,


I have a $string that is separated by , and space;

boy, pig, 123, 123:412adbd, d0g, lajdlf134_ lkadsf !234,


Now I want to capture the string(s) between last two commas. It consists of 
anything upto 32 characters. that is, right after d0g,\s+ up to the last 
character before the last comma at the end of the line.

if I do something like

(my $value) = $_ ~= /,\s+(.*),\s+$/;

$value would start matching from pig because when I used $ and it looked 
back, the first thing it would match is , pig upto the end of the line

I wonder how you could match only the pattern which is nearest to the end of 
the line having used $ anchor.

To get around this, I could split the lines push each comma delimited string 
into an array and finally print the last element which is a lot of work to do.

Is there some sort of turning of greedy behavior of the $ anchor?














  

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




Re: Turn off $ anchor greedy behavior

2009-04-14 Thread Michael Alipio


 
 Or use split and return the last field:
 
 $ perl -le'
 my $string = boy, pig, 123, 123:412adbd, d0g,
 lajdlf134_ lkadsf !234,\n;
 my $value = ( split /,\s+/, $string )[ -1 ];

Another mind bogling example... :-)
I thought I would do:

my @value = ( split /,\s+/, $string );
print $value[6];

How could your example, have printed the last field using [ -1 ]?
Can I also print say, the 3rd field using this trick?






 print $value;
 '
 lajdlf134_ lkadsf !234
 
 
 
 
 
 John
 -- Those people who think they know everything are a great
 annoyance to those of us who do.-- Isaac Asimov
 
 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/


  

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




Re: Turn off $ anchor greedy behavior

2009-04-14 Thread Michael Alipio

Aha, found it.. The split returned a list and you've just sliced it. giving 
[-1] means the list will start running through the elements backwards. 


--- On Tue, 4/14/09, Michael Alipio daem0n...@yahoo.com wrote:

 From: Michael Alipio daem0n...@yahoo.com
 Subject: Re: Turn off $ anchor greedy behavior
 To: Perl Beginners beginners@perl.org, John W. Krahn jwkr...@shaw.ca
 Date: Tuesday, April 14, 2009, 10:02 PM
  
  Or use split and return the last field:
  
  $ perl -le'
  my $string = boy, pig, 123, 123:412adbd, d0g,
  lajdlf134_ lkadsf !234,\n;
  my $value = ( split /,\s+/, $string )[ -1 ];
 
 Another mind bogling example... :-)
 I thought I would do:
 
 my @value = ( split /,\s+/, $string );
 print $value[6];
 
 How could your example, have printed the last field using [
 -1 ]?
 Can I also print say, the 3rd field using this trick?
 
 
 
 
 
 
  print $value;
  '
  lajdlf134_ lkadsf !234
  
  
  
  
  
  John
  -- Those people who think they know everything are a
 great
  annoyance to those of us who do.-- Isaac
 Asimov
  
  -- To unsubscribe, e-mail:
 beginners-unsubscr...@perl.org
  For additional commands, e-mail:
 beginners-h...@perl.org
  http://learn.perl.org/
 
 
   
 
 -- 
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/


  

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




killing a forked process

2009-04-13 Thread Michael Alipio

Hi,


My program looks like this:

my $pid = fork ();
if ($pid == 0){
  exec (top);
}else{
   my $time = 0
   while (1){
 sleep 1;
 $time++;
 if ($time == 10){
 kill 9, $pid;
 }
   }
}


That is, after running the forked top process for 10 seconds, the main 
program will end it.

Any idea why it's not working for me?

Thanks




  

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




Re: killing a forked process (external programs always detach from child process)

2009-04-13 Thread Michael Alipio

Hi,

I'm trying to launch and external program using fork.



my $pid = fork()

if ($pid == 0){

system (top);
exit (0);
}else{
waitpid ($pid, 0)
}

print External Program died!;


The problem is top, or the program i'm actually going to run always detaches 
to the child perl process I have forked. If I open another terminal while my 
script is running and kill the child's PID, (usually the one with higher number 
because there are two perls running), the external program still keeps running..

How do i tell it to stick to the child process so that when i kill the child, 
the external program will die. Is there a way to capture the PID of the 
external program? I tried using open to launch it but open quickly exits the 
program after assigning it to a variable, e.g my $extern_pid = open TOP, top 
| or die $!;


 




--- On Mon, 4/13/09, Michael Alipio daem0n...@yahoo.com wrote:

 From: Michael Alipio daem0n...@yahoo.com
 Subject: killing a forked process
 To: begginers perl.org beginners@perl.org
 Date: Monday, April 13, 2009, 4:49 PM
 Hi,
 
 
 My program looks like this:
 
 my $pid = fork ();
 if ($pid == 0){
   exec (top);
 }else{
my $time = 0
while (1){
  sleep 1;
  $time++;
  if ($time == 10){
  kill 9, $pid;
  }
}
 }
 
 
 That is, after running the forked top process
 for 10 seconds, the main program will end it.
 
 Any idea why it's not working for me?
 
 Thanks
 
 
 
 
   
 
 -- 
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/


  

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




catching outputs of realtime apps (e.g; 'top(8)'

2009-04-12 Thread Michael Alipio

Hi,

I have a program that constantly displays values on the screen. The program 
'ping' for example.

What I'd like to do is watch the output and as soon as i see some values, i 
would do something. For example, as suppose when pinging a host I would 
constantly get a reply. however, if I remove the network cable, of my pc, I 
would start seeing host unreachable or something. As soon as I see this 
message, I would like to do something. This is how I want to do this (executing 
the program once and watching the output). I could probably run ping every one 
second and check the output of each execution, however, that is not how I want 
to do it. The program I will be running has to be run only once, like for 
example the 'top' program.

Any idea how to accomplish this?


Thanks!
-mic


  

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




Re: catching outputs of realtime apps (e.g; 'top(8)'

2009-04-12 Thread Michael Alipio

Hi,

My problem is the program I want to watch the output of doesn't output stream 
of data. instead, data is presented on the screen and values change constantly, 
e.g increasing counters. I think it is more like the top program. How do i tell 
the program to be on logging mode like top?


--- On Mon, 4/13/09, Chas. Owens chas.ow...@gmail.com wrote:

 From: Chas. Owens chas.ow...@gmail.com
 Subject: Re: catching outputs of realtime apps (e.g; 'top(8)'
 To: daem0n...@yahoo.com
 Cc: beginners@perl.org
 Date: Monday, April 13, 2009, 2:28 AM
 On Sun, Apr 12, 2009 at 14:14, Michael Alipio
 daem0n...@yahoo.com wrote:
 
  Hi,
 
  I have a program that constantly displays values on
 the screen. The program 'ping' for example.
 
  What I'd like to do is watch the output and as
 soon as i see some values, i would do something. For
 example, as suppose when pinging a host I would constantly
 get a reply. however, if I remove the network cable, of my
 pc, I would start seeing host unreachable or something. As
 soon as I see this message, I would like to do something.
 This is how I want to do this (executing the program once
 and watching the output). I could probably run ping every
 one second and check the output of each execution, however,
 that is not how I want to do it. The program I will be
 running has to be run only once, like for example the
 'top' program.
 
  Any idea how to accomplish this?
 snip
 
 Programs like ping that output a stream of information are
 easy to work with:
 
 ping wonkden.net | perl -ne 'print I saw
 $_'
 
 Perl defaults to a line buffered mode, so if your the
 program you want
 to watch outputs lines, then you just need a loop watching
 stdin (as
 above).  To use top, you must put it in logging mode
 
 OS X
 
 top -l 0 | perl -ne 'print if /firefox/'
 
 Linux
 
 top -b | perl -ne 'print if /firefox/'
 
 
 -- 
 Chas. Owens
 wonkden.net
 The most important skill a programmer can have is the
 ability to read.
 
 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/


  

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




Re: catching outputs of realtime apps (e.g; 'top(8)'

2009-04-12 Thread Michael Alipio

The problem is, the program is quite big. And it is written in C. I'm still on 
chapter 4 of KR. I don't think it would make any difference even after i 
finished it. I've read that the program writes the final tally of values into a 
text file, perhaps I will just try to parse it.
thanks.


--- On Mon, 4/13/09, Chas. Owens chas.ow...@gmail.com wrote:

 From: Chas. Owens chas.ow...@gmail.com
 Subject: Re: catching outputs of realtime apps (e.g; 'top(8)'
 To: daem0n...@yahoo.com
 Cc: beginners@perl.org
 Date: Monday, April 13, 2009, 7:40 AM
 On Sun, Apr 12, 2009 at 17:02, Michael Alipio
 daem0n...@yahoo.com wrote:
 
  Hi,
 
  My problem is the program I want to watch the output
 of doesn't output
  stream of data. instead, data is presented on the
 screen and values
  change constantly, e.g increasing counters. I think it
 is more like the
  top program. How do i tell the program to be on
 logging mode like top?
 snip
 
 If the program has any documentation you could try reading
 that.  If you
 have access to the source code for the program you could
 also try reading
 it.
 
 -- 
 Chas. Owens
 wonkden.net
 The most important skill a programmer can have is the
 ability to read.
 
 -- 
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/


  

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




Break out of the sub or loop on keypress

2008-08-09 Thread Michael Alipio
Hi,

I have a sub that returns a value and inside it runs a loop that displays 
something on the screen, say a list of numbered items. Now what I want to do is 
if the item i'm interested appears on the screen, the user can hit any key and 
the program will exit the sub and return what ever it is supposed to return.

Any idea how to accomplish that?


  

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




ASP with VIEWSTATE and perl

2008-01-17 Thread Michael Alipio
Hi,

Quick question:

Anyone here knows a module which can handle viewstate
in asp html forms?? I need to write a script that can
submit a form to the server that is using viewstate..

thanks.




  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping

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




Password generator (limit my dictionary to...)

2007-10-22 Thread Michael Alipio
Hi,


I'm trying to create a dictionary file for my
bruteforce program.


I have a huge dictionary file and I want to trim it
down according the requirements.

The output should be a dictionary file that is minimum
6 characters and maximum 15 characters with at least 4
letters and 2 numbers in it.. no special characters
whatsoever.. This should be a simple regex but it's
been a while since i wrote my last regexp program.
Need to refresh my perl basics a little bit..



So far I got this:

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


my $wordlist = shift @ARGV;
#my $newwordlist = shift @ARGV;

open INPUTFILE, $wordlist or die $!;
#open OUTPUTFILE, $output or die $!;

while (INPUTFILE){
next if !(/\b\w\w\w\w\b/);
print;

}









__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




Re: Password generator (limit my dictionary to...)

2007-10-22 Thread Michael Alipio
Hi Paul,

Here's what I came up with:

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


my $wordlist = shift @ARGV;

open INPUTFILE, $wordlist or die $!;



while (INPUTFILE){
# Find all words that are 6-15 characters with at
least 2 digits and 4 letters that can appear anywhere

next unless (/\b\w{6,15}\b\n/  /.*(\d).*\d/ 
/(.*([a-z]|[A-Z]).*){4}/);
print;

}



So far i can say that it is working, however i'm not
one 100% sure of it. I'm not really confident about
the positioning. I have to put '.*' before and after
my main expressions... I'm not sure if i'm doing it
right.


With a list file that looks like this:

abcdefghijklmno
22
abcdef
abcd22
1aniraco
Abcd22
ABCD22
abc2d2
abc1def2ijklmno
22
abcdef
.ask lasdf


I have managed to extract these:

abcd22
Abcd22
ABCD22
abc2d2
abc1def2ijklmno


That is, regardless of the position, it must have at
least 4 letters and 2 numbers and of course it should
fall between 6 to 15 characters.



In the meantime, let me look at your solution...




--- Paul Lalli [EMAIL PROTECTED] wrote:

 On Oct 22, 2:45 am, [EMAIL PROTECTED] (Michael
 Alipio) wrote:
 
  The output should be a dictionary file that is
 minimum
  6 characters and maximum 15 characters with at
 least 4
  letters and 2 numbers in it.. no special
 characters
  whatsoever.. This should be a simple regex but
 it's
  been a while since i wrote my last regexp program.
  Need to refresh my perl basics a little bit..
 
 Don't fall into the trap of trying to express every
 one of your
 requirements as one giant regexp.  There's no reason
 for that.
 
  So far I got this:
 
  #!/usr/bin/perl
  use warnings;
  use strict;
 
  my $wordlist = shift @ARGV;
  #my $newwordlist = shift @ARGV;
 
  open INPUTFILE, $wordlist or die $!;
  #open OUTPUTFILE, $output or die $!;
 
  while (INPUTFILE){
 
 next unless /^[a-z0-9]{6,15}$/;
 next unless tr/a-z// = 4;
 next unless tr/0-9// = 2;
 
  print;
  }
 
 
 Paul Lalli
 
 
 -- 
 To unsubscribe, e-mail:
 [EMAIL PROTECTED]
 For additional commands, e-mail:
 [EMAIL PROTECTED]
 http://learn.perl.org/
 
 
 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




confusion with splitting columns using [-n, -n] (e.g; my ( $country, $bytes ) = ( split )[ -2, -1 ])

2007-01-29 Thread Michael Alipio
Hi,

I have a file that look like this:

  1668   |  172.194.177.182   |  US12679172
10396   |  64.237.148.157 |  PR 12679172
  9318   |  211.187.212.242   |  KR1279172
22291   |  66.215.254.186 |  US 1269172
22291   |  24.176.212.76   |  US 1679172
30225   |  66.147.146.214 |  US 2679172
17676   |  221.34.8.92   |  JP  1267173
17858   |  125.180.111.187   |  KR12679172
  6395   |  67.96.150.40 |  US 12679172
17858   |  125.180.193.124   |  KR 12679175
  3462   |  218.168.176.39 |  TW12679472
  9919   |  218.211.204.195   |  TW12666172
  9318   |  222.235.22.225 |  KR 12672272
  9318   |  222.237.14.160 |  KR 12679142



Six columns including two colums with pipe symbols.
The goal is to add up the values in the last column that belongs to the same 
country. That last column are the bytes received by a particular country. So I 
have to add all bytes received by US, KR, etc.

Someone has given me this code:

open WHOISWITHBYTES, '', whois.bytes or die $!;

my %data;
while ( WHOISWITHBYTES ) {
   my ( $country, $bytes ) = ( split )[ -2, -1 ];
   $data{ $country } += $bytes;
}

 print Country Total Bytes\n;
for my $country ( sort { $data{ $b } = $data{ $a } } keys %dat
a ) {
print $country  $data{ $country }\n;
}

It is working perfectly but now, I need to document this code. Can anyone help 
me out on understanding this code. 

I'm particularly confused with the line: 
my ($country, $bytes) = (split) [-2, -1];

What does this tells? What does -2 and -1 tells? All I know is that split will 
output a list containing two values that will be assigned to $country and 
$bytes for every line of that whois.bytes file. But I'm not sure what those 
-2,-1 means and how it was able to extract column 5 and 6. I tried looking at 
perldoc -f split but cannot seem to find the explanation. Are those the LIMIT 
thing?


Thanks!





 

Any questions? Get answers on any topic at www.Answers.yahoo.com.  Try it now.

trouble with list context assignment for substitution inside File::Find wanted function

2007-01-23 Thread Michael Alipio
Hi,


I have a directory which contains several files.

client1-2006-05-19.log.gz
client1-2006-05-20.log.gz  
client1-2006-07-29.log.gz  
client1-2006-10-05.log.gz
client1-2006-05-21.log.gz


I want strip all of axisglobal- in their filenames.

What I did was:

#!/usr/bin/perl
use warnings;
use strict;
use File::Find;


find (\renamefiles, './');


sub renamefiles{
(my $newname) = $_ =~ s/^\w+-//g;
#rename ($_, $newname);
print $newname;
}

When I try printing the $newname which supposedly will print  only 
2006-N-N.log.gz, it instead prints a scalar value of 1, as if parenthesis 
around my $newname does not exists. And so, uncommenting the rename did do 
anything to my files.

Any explanation to this?
Do you have a perl one-liner to rename all files into their filenames with 
stripped ^\w+... thanks.


Thanks.







 

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

Re: trouble with list context assignment for substitution inside File::Find wanted function

2007-01-23 Thread Michael Alipio


- Original Message 
From: John W. Krahn [EMAIL PROTECTED]
To: Perl Beginners beginners@perl.org
Sent: Wednesday, January 24, 2007 10:57:51 AM
Subject: Re: trouble with list context assignment for substitution inside 
File::Find wanted function



  Yes, the substitution operator (s///) returns true (1) or false ('') in 
 either
  list or scalar context.  To do want you want you have to do the assignment
  first and then do the substitution:

  my $newname = $_;
  $newname =~ s/^\w+-//;

  Or in one statement:

  ( my $newname = $_ ) =~ s/^\w+-//;

I've already figured that one out. However, I want to use variables for my 
regexp pattern. So I can replace axis with whatever I my first program 
argument is.

Changed this:1

find (\renamefiles, './');

sub renamefiles{
  if ($_ =~ /axis/){
my $oldname = $_;
$_ =~ s/\w+-//;
#rename ($oldname, $_)
print $oldname will be renamed to $_\n;
  }
}


To this:

find (\renamefiles, './');
my $name = shift;

sub renamefiles{
  if ($_ =~ /$name/){
my $oldname = $_;
$_ =~ s/\w+-//;
#rename ($oldname, $_)
print $oldname will be renamed to $_\n;
  }
}

And if I do a

#perl rename.pl axis

I got many of this:

Use of uninitialized value in regexp compilation at test.pl line 11.

Even if I specify axis in my $name instead of shift, I'm getting the same 
error

What's wrong with using variables in regexp patterns??



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/









 

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

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: 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 beginners@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

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: 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:

snip

  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 QA.
http://answers.yahoo.com/dir/?link=listsid=396545367

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

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 (LOG)
  {
 $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/

Alternative lookaheads in substitution, is it possible?

2007-01-20 Thread Michael Alipio
Hi,

Suppose I want to match all white spaces if it is followed by \w+= or not 
followed by date or time
:

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

Doesn't seem to do what I want.

Given a string:

Jan 19 11:37:21 firewall date=2007-01-19 time=11:42:15 msg=User admin login


I want it to produce something like:
Jan*19*11:37:21*firewall date=2007-01-19 time=11:42:15*msg=User admin 
login*log=3






 

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

Re: Alternative lookaheads in substitution, is it possible? (SOLVED!!!)

2007-01-20 Thread Michael Alipio
Cool

I got this from approximately 71% perldoc perlre:

 print 5: got $1\n if $x =~ /^(\D*)(?=\d)(?!123)/;

so I don't need || between multiple look ahead assertions...

Sometimes, it's more rewarding to solve you're problem on your own.
You just have to RTFM.. :-)

More power to this helpful commmunity!!


- Original Message 
From: Michael Alipio [EMAIL PROTECTED]
To: begginers perl.org beginners@perl.org
Sent: Saturday, January 20, 2007 7:21:17 PM
Subject: Alternative lookaheads in substitution, is it possible?

Hi,

Suppose I want to match all white spaces if it is followed by \w+= or not 
followed by date or time
:

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

Doesn't seem to do what I want.

Given a string:

Jan 19 11:37:21 firewall date=2007-01-19 time=11:42:15 msg=User admin login


I want it to produce something like:
Jan*19*11:37:21*firewall date=2007-01-19 time=11:42:15*msg=User admin 
login*log=3






 

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






 

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

putting ; as a replacement in the substitution.

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


Thanks!







 

Get your own web address.  
Have a HUGE year through Yahoo! Small Business.
http://smallbusiness.yahoo.com/domains/?p=BESTDEAL

Re: putting ; as a replacement in the substitution.

2007-01-20 Thread Michael Alipio


- Original Message 
From: Bill Jones [EMAIL PROTECTED]
To: begginers perl.org beginners@perl.org
Sent: Sunday, January 21, 2007 1:03:33 PM
Subject: Re: putting ; as a replacement in the substitution.

On 1/20/07, Michael Alipio [EMAIL PROTECTED] wrote:
  my $string = 'vd=root,status=';
 ' vd=root;status='

 $string =~ s[\,][\;]g;

Oops, I only want to match the comma, right after vd=\w+..

My string might be:

'devid=234FB,vd=root,status=ok,logid=1235'

I tried replacing my delimiters with [ ] but still it eats up vd=\w+



-- 
WC (Bill) Jones -- http://youve-reached-the.endoftheinternet.org/
http://pgp.mit.edu:11371/pks/lookup?op=vindexsearch=0x2A46CF06fingerprint=on

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









 

We won't tell. Get more on shows you hate to love 
(and love to hate): Yahoo! TV's Guilty Pleasures list.
http://tv.yahoo.com/collections/265 

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

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

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 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/

Searching hash if a given value exists

2007-01-18 Thread Michael Alipio
Hi,

Suppose I have a hash:

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

Now I want to know if there is already a key with a 'house' value as I do not 
want to create another key with the same value.
Is there any other faster way to do it than doing a

for (keys %hash){
if ($hash{$_} eq 'house'){
  #found 'house'! 
}


What if I got thousands of keys?

Lastly, how fast is the if (exists $hash{$key})? Is it faster than doing a 
'for keys' loop, and testing if a given $_ key will appear?


Thanks.



 

Never miss an email again!
Yahoo! Toolbar alerts you the instant new Mail arrives.
http://tools.search.yahoo.com/toolbar/features/mail/

Re: Searching hash if a given value exists

2007-01-18 Thread Michael Alipio


- Original Message 
From: Jason Roth [EMAIL PROTECTED]
To: Michael Alipio [EMAIL PROTECTED]
Cc: begginers perl.org beginners@perl.org
Sent: Friday, January 19, 2007 9:53:49 AM
Subject: Re: Searching hash if a given value exists

Hi Michael,

 To answer your questions, If you want to know if there is already a
 key with a certain value, you have to look through the entire hash.
 However if you want to see if a given key exists, just do if (exists
 $hash{$key}).  This will take constant time regardless of the number
 of elements in your hash.

I see... so, I really have to do a 

for (keys %hash){
if ($hash{$_} eq 'somevalue'){
   #do something
}
}

If I have hundreds of thousands keys and I want to know which contains the same 
values, will I end up waiting too long?




 Hope this helps.

-Jason

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

 Suppose I have a hash:

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

 Now I want to know if there is already a key with a 'house' value as I do not 
 want to create another key with the same value.
 Is there any other faster way to do it than doing a

 for (keys %hash){
 if ($hash{$_} eq 'house'){
   #found 'house'!
 }


 What if I got thousands of keys?

 Lastly, how fast is the if (exists $hash{$key})? Is it faster than doing a 
 'for keys' loop, and testing if a given $_ key will appear?


 Thanks.




 
 Never miss an email again!
 Yahoo! Toolbar alerts you the instant new Mail arrives.
 http://tools.search.yahoo.com/toolbar/features/mail/








 

Finding fabulous fares is fun.  
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel 
bargains.
http://farechase.yahoo.com/promo-generic-14795097

From syslog to csv format

2007-01-18 Thread Michael Alipio

Hi,

I have a script that parses firewall logs. The firewall can be set to export 
its logs in CSV format, and that is what we needed. However, sometimes, other 
firewalls are being left in a Syslog format, and upon reading the log files, I 
want to make sure I will convert it to CSV if in case it is currently in Syslog 
format.


Syslog format:

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.11) action=login 
status=success reason=none msg=User admin login successfully from 
GUI(192.168.1.1)

CSV format:

Jan 19 11:35:27 firewall date=2007-01-19 
time=11:40:21,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)


Based on my observation, commas are inserted only on space-separated fields 
after time=
Also, a semicolon (;) was inserted between vd=root user

Question:

If you have this kind of objective, how will you handle this?
Just some clues, and I'll be good on my own.

Thanks!









 

Don't pick lemons.
See all the new 2007 cars at Yahoo! Autos.
http://autos.yahoo.com/new_cars.html 

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

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








 

Don't pick lemons.
See all the new 2007 cars at Yahoo! Autos.
http://autos.yahoo.com/new_cars.html 

Re: Storing filehandles(for writing) in hashes doesn't work (Re: whilereach my $variable (FILEHANDLE) )

2007-01-16 Thread Michael Alipio
Hi,


Ok, here's my code:


my $logfile='firewalllog';
my $devicefile='deviceid';

our %log;

open DEVICES, '', $devicefile or die Can't open $devicefile $!;

while (my $device DEVICES){
  ($device) = $device =~ /(\S+)$/;
  open (my $fh, '', $device..log) or die Can't write to $device..log: $!;
  $log{device} = $fh;
}
close DEVICES;

So I should be able to print to at least {$log{any_device_id}} right??

But using this:

print {$log{FWF60A1234566}} testing;


gives me:

Use of uninitialized value in ref-to-glob cast at extractdevice.pl line 20.
Can't use string () as a symbol ref while strict refs in use at 
extractdevice.pl line 20.

Any idea what does the above means? 




- Original Message 
From: Mumia W. [EMAIL PROTECTED]
To: Beginners List beginners@perl.org
Sent: Tuesday, January 16, 2007 3:13:38 PM
Subject: Re: Storing filehandles(for writing) in hashes doesn't work (Re: 
whilereach my $variable (FILEHANDLE) )

On 01/15/2007 11:21 PM, Michael Alipio wrote:
 Hi,
 
 Ok, seems like a pet logs is not a good example.:-)
 
 Let me revise my story:
 
 I have a logfile which contains different clients firewall's logs.
 
 Let's say the log file is: firewall.log
 
 Now each line in the logfile has a $deviceid string that identifies where or 
 which client it came from. 
 What I did was to list down all of these clients in a file named 
 deviceid.conf. 
 Let's say it  contains: ('client_name'  dash  'device_id')
 
 client1 - 293u0sdfj
 client2 - 8325kjsdf
 client3 - kjldas8282
 .
 clientn - sdkfj28350
 
 
 Having said that, my goals are:
 
 1. read the firewall.log line by line.
 if it see a particular device_id in $_, and it knows that that deviceid 
 is for this particular client (using the information found at 
 deviceid.conf) it will write that line into /client1/$date.log or 
 /client2/$date.log etc.
 
 By the way, our logs are being rotated  such that it contains logs from 6:26 
 yesterday, to 6:25 today, so $date on the above was obtained by let's say 
 getting the /date=(\S+)/ on the first line entry of the log, let's say it 
 reads 2007-01-10, so our $date will be 2007-01-10_11, so the logfile for a 
 particular client will be /client1/2007-01-10_11.log
 
 
 Here is an example of a line in the logfile:
 
 Jan 10 06:26:17 210.23.194.86 date=2007-01-10 
 time=06:30:14,devname=sccp_firewall,device_id=FWF60A1234566,log_id=00210100
 01,type=traffic,subtype=allowed,pri=notice,vd=root;SN=14435461,duration=139,user=N/A,group=N/A,policyid=11,proto=6,service=7
 500/tcp,status=accept,src=192.169.1.70,srcname=192.168.1.3,dst=192.169.1.17,dstname=192.169.1.17,src_int=internal,dst_int
 =wan2,sent=144,rcvd=0,sent_pkt=3,rcvd_pkt=0,src_port=2354,dst_port=7500,vpn=N/A,tran_ip=0.0.0.0,tran_port=0,dir_disp=org,tra
 n_disp=noop
 

The device_id in this log entry does not appear in the device id file 
you showed above.


 
 So far, I've been trying to use the code that was given to me but I'm still 
 far from my goal:
 
 
 #!/usr/bin/perl
 use warnings;
 use strict;
 
 my $logfile='firewall.log';
 my $devices='deviceid.conf';
 our %log;
 
 
 ##
 # 1ST PART
 open DEVICES, '', $devices or die Can't open $devices $!;
 
 while ( my $device = DEVICES){
   chomp $device;
   ($device) = $device =~ /(\S+)$/;
   open( my $fh, '', $device.log) or die Can't open $device.log: $!;
   $log{$device} = $fh;
 }
 close DEVICES;
 #
 
 So far I can understand that in the first part, the code will read the 
 deviceid.conf and create a file handle for writing for each device id, and 
 store these filehandles inside %log. But that is what I wanted to do, I want 
 to, as I wanted to write my logs into /clientN/date.log instead of 
 client's_deviceid.log. So I'm still trying to figure out this one.

/(\S+)$/ only matches the device-id; the client-id is thrown away. Don't 
throw it away. Create a regular expression that captures the client id 
and use the client-id to create the log file name.


 
 Next:
 
 
 
 # 2nd PART
 
 my $re = '^\S+\s+(' . join( '|', keys %log ). ')';
 
 open( IN, '', $logfile ) or die Can't open $logfile: $!;
 
 while( my $line = IN ){
   if( $line =~ m/$re/ ){
 print $log{$1} $line;
   }
 }
 
 
 ###
 
 
 The second part is what confuses me, especially the line with my $re, and 
 also the if($line =~ m/$re/)
 
 As far as I can understand, the $re will contain a regexp with ('device_id1 | 
 device_id2 | device_id3 | device_idN'), so that whenever it sees any pattern 
 that match either of those device_ids, it will print it to say 
 $log{device_idN} which points to the file handle that writes to 
 device_idN.log.
 
 But this is not the case.
 The line print $log{$1} $line; doesn't even work as if it cannot decode the 
 $log{$1}
 

You are correct, my $re creates a regular expression with all of the 
device ids, and you are also correct that print $log{$1} $line does 
not work. You must use print { $log{$1} } $line.


 Useless use of a constant in void

Hash variable is not imported?? (Re: Storing File Handles for writing)

2007-01-16 Thread Michael Alipio
Hi Mumia,

I think this one is closer to my goal.

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

my $logfile='fortilog';
my $devicefile='deviceid';
our %clientdevice;

open DEVICES, '', $devicefile or die Can't open $devicefile $!;

while (DEVICES){
  (my $client) = $_ =~ /^(\S+\s+)/;
  (my $device) = $_ =~ /(\S+)$/;
  $clientdevice{$device} = $client;
}
close DEVICES;


open( LOGFILE, '', $logfile ) or die Can't open $logfile: $!;
 while(my $log = LOGFILE ){
  (my $deviceid) = $log =~ /device_id=(\S+?),/;
  if (exists $clientdevice{$deviceid}){
 open FH, '', $clientdevice($deviceid) or die $!;
 print FH $log;
 close FH;
   }
}

The if exist line keeps on complaining:

Variable $clientdevice is not imported at extractdevice.pl line 23.
Global symbol $clientdevice requires explicit package name at 
extractdevice.pl line 23.
syntax error at extractdevice.pl line 23, near $clientdevice(


Do you know what does this tell?

For those unaware about my goal:
I have a logfile that contains each of our client's firewall logs:
A sample line looks like this.

Jan 10 06:26:17 210.23.194.86 date=2007-01-10 
time=06:30:14,devname=sccp_firewall,device_id=FWF60A123456,log_id=002101000
1,type=traffic,subtype=allowed,pri=notice,vd=root;SN=14435461,duration=139,user=N/A,group=N/A,policyid=11,proto=6,service=750
0/tcp,status=accept,src=192.168.16.23,srcname=192..168.23.5,dst=192.168.1.17,dstname=192.168.1.17,src_int=internal,dst_int=wa
n2,sent=144,rcvd=0,sent_pkt=3,rcvd_pkt=0,src_port=2354,dst_port=7500,vpn=N/A,tran_ip=0.0.0.0,tran_port=0,dir_disp=org,tran_di
sp=noop

Then I made a deviceid file which contains:

client1 - FWF60A123456
client2 - FG200A123456
client3 - FS300A123456

The goal is to read the logfile line by line, and if it sees that device_id 
exists in the hash that was created after reading deviceid file, then it will 
write that particular log entry in, let's say client1.log.





 

Finding fabulous fares is fun.  
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel 
bargains.
http://farechase.yahoo.com/promo-generic-14795097

Re: Hash variable is not imported?? (Re: Storing File Handles for writing)

2007-01-16 Thread Michael Alipio
Hi John! 

- Original Message 
 From: John W. Krahn [EMAIL PROTECTED]
 To: Perl Beginners beginners@perl.org
 Sent: Tuesday, January 16, 2007 8:15:36 AM
 Subject: Re: Hash variable is not imported?? (Re: Storing File Handles for 
 writing)

  
  open( LOGFILE, '', $logfile ) or die Can't open $logfile: $!;
   while(my $log = LOGFILE ){
(my $deviceid) = $log =~ /device_id=(\S+?),/;
if (exists $clientdevice{$deviceid}){
   open FH, '', $clientdevice($deviceid) or die $!;

 Change the parentheses () to braces {} there:

Holly cow! Sorry about this one. I shouldn't have done this on a putty 
terminal, my mistake, can't spot the difference between () and {}.

Thank you very much John!
You're a certified perl guru! :-)

   open FH, '', $clientdevice{$deviceid} or die $!;

   print FH $log;
   close FH;
 }
  }
  
  The if exist line keeps on complaining:

 It is actually the line after that one that has the problem.

  Variable $clientdevice is not imported at extractdevice.pl line 23.
  Global symbol $clientdevice requires explicit package name at 
  extractdevice.pl line 23.
  syntax error at extractdevice.pl line 23, near $clientdevice(

 Line 23 is:

   open FH, '', $clientdevice($deviceid) or die $!;



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/









 

Sucker-punch spam with award-winning protection. 
Try the free Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/features_spam.html

whilereach my $variable (FILEHANDLE) ???? (sort of like foreach my $variable (@array)

2007-01-15 Thread Michael Alipio
Hi,

I've been thinking about this for quite some time now.
Suppose I have a text file (mypet.log that contains:

dog
pig
cat

Then I have another text file and I want to read it line by line and extract 
each line that has those contained in the first file. Sort of like running 
while inside while.

myallpetlogs contains:

blabla dog blah
blabla cat blah
blabla dog blah
blabla pig blah
blabla cat blah
blabla pig blah


Now, the goal is to have 3 files which contains all extracted lines in 
myallpetlogs

1. dogs.logs
2. cat.logs
3. pig.logs


How do I assign some variable to what was returned by while loop?

Perhaps the above goal should be written like this:


open MYPETS, '', or die  $!
open PETLOGS, '', or die $!

whileeach my $pets (MYPETS){
  whileeach my $petlogs (PETLOGS){
   read each line extracting $pets and putting it in $pet.log
and after reading the entire PETLOGS again, go to the next pet and...
  read the PETLOGS all over again and extract the next pet??? Oh my, 
I'm lost..
   }

}

Any idea?


Thanks









 

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

Storing filehandles(for writing) in hashes doesn't work (Re: whilereach my $variable (FILEHANDLE) )

2007-01-15 Thread Michael Alipio
Hi,

Ok, seems like a pet logs is not a good example.:-)

Let me revise my story:

I have a logfile which contains different clients firewall's logs.

Let's say the log file is: firewall.log

Now each line in the logfile has a $deviceid string that identifies where or 
which client it came from.
What I did was to list down all of these clients in a file named 
deviceid.conf.
Let's say it  contains: ('client_name'  dash  'device_id')

client1 - 293u0sdfj
client2 - 8325kjsdf
client3 - kjldas8282
.
clientn - sdkfj28350


Having said that, my goals are:

1. read the firewall.log line by line.
if it see a particular device_id in $_, and it knows that that deviceid is 
for this particular client (using the information found at deviceid.conf) it 
will write that line into /client1/$date.log or /client2/$date.log etc.

By the way, our logs are being rotated  such that it contains logs from 6:26 
yesterday, to 6:25 today, so $date on the above was obtained by let's say 
getting the /date=(\S+)/ on the first line entry of the log, let's say it reads 
2007-01-10, so our $date will be 2007-01-10_11, so the logfile for a 
particular client will be /client1/2007-01-10_11.log


Here is an example of a line in the logfile:

Jan 10 06:26:17 210.23.194.86 date=2007-01-10 
time=06:30:14,devname=sccp_firewall,device_id=FWF60A1234566,log_id=00210100
01,type=traffic,subtype=allowed,pri=notice,vd=root;SN=14435461,duration=139,user=N/A,group=N/A,policyid=11,proto=6,service=7
500/tcp,status=accept,src=192.169.1.70,srcname=192.168.1.3,dst=192.169.1.17,dstname=192.169.1.17,src_int=internal,dst_int
=wan2,sent=144,rcvd=0,sent_pkt=3,rcvd_pkt=0,src_port=2354,dst_port=7500,vpn=N/A,tran_ip=0.0.0.0,tran_port=0,dir_disp=org,tra
n_disp=noop


So far, I've been trying to use the code that was given to me but I'm still far 
from my goal:


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

my $logfile='firewall.log';
my $devices='deviceid.conf';
our %log;


##
# 1ST PART
open DEVICES, '', $devices or die Can't open $devices $!;

while ( my $device = DEVICES){
  chomp $device;
  ($device) = $device =~ /(\S+)$/;
  open( my $fh, '', $device.log) or die Can't open $device.log: $!;
  $log{$device} = $fh;
}
close DEVICES;
#

So far I can understand that in the first part, the code will read the 
deviceid.conf and create a file handle for writing for each device id, and 
store these filehandles inside %log. But that is what I wanted to do, I want 
to, as I wanted to write my logs into /clientN/date.log instead of 
client's_deviceid.log. So I'm still trying to figure out this one.

Next:



# 2nd PART

my $re = '^\S+\s+(' . join( '|', keys %log ). ')';

open( IN, '', $logfile ) or die Can't open $logfile: $!;

while( my $line = IN ){
  if( $line =~ m/$re/ ){
print $log{$1} $line;
  }
}


###


The second part is what confuses me, especially the line with my $re, and 
also the if($line =~ m/$re/)

As far as I can understand, the $re will contain a regexp with ('device_id1 | 
device_id2 | device_id3 | device_idN'), so that whenever it sees any pattern 
that match either of those device_ids, it will print it to say $log{device_idN} 
which points to the file handle that writes to device_idN.log.

But this is not the case.
The line print $log{$1} $line; doesn't even work as if it cannot decode the 
$log{$1}

Useless use of a constant in void context at extractdevice.pl line 30.
Scalar found where operator expected at extractdevice.pl line 35, near } $line
(Missing operator before  $line?)
syntax error at extractdevice.pl line 35, near } $line

If I comment those codes inside the while in that second part, the program will 
successfully create emtpy device_id1.log, device_id2.log, etc. etc.)

Any idea what's wrong with this one?











- Original Message 
From: Thomas Bätzler [EMAIL PROTECTED]
To: Perl Beginners beginners@perl.org
Sent: Monday, January 15, 2007 4:24:02 PM
Subject: RE: whilereach my $variable (FILEHANDLE)  (sort of like foreach 
my $variable (@array)

Hi, 

Michael Alipio [EMAIL PROTECTED] asked:
 I've been thinking about this for quite some time now.
 Suppose I have a text file (mypet.log that contains:
 
 dog
 pig
 cat
 
 Then I have another text file and I want to read it line by 
 line and extract each line that has those contained in the 
 first file. Sort of like running while inside while.
 
 myallpetlogs contains:
 
 blabla dog blah
 blabla cat blah
 blabla dog blah
 blabla pig blah
 blabla cat blah
 blabla pig blah
 
 
 Now, the goal is to have 3 files which contains all extracted 
 lines in myallpetlogs

Assuming that you don't have too many log extracts to create, the easy
way would be to read in mypet.log first and open a new logfile for
each entry using a scalar as the filehandle. That scalar can be stored
in a hash using the pet name as a key.

When reading the logfile you determine the pet name using a regular
expression and write the line to the pets

Read a text file starting from the bottom

2007-01-14 Thread Michael Alipio
Hi,

I wanted to extract the start time of a particular logfile (located at the 
beginning of the file, and also the end time which is located at the end of the 
logfile.

Is there any way to do this in perl without using head or tail commands?

Thanks.

By the way, can you point me somewhere perhaps a perl function that lets you 
create directories?







 

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

How can I list down all builtin functions?(Re: Read a text file starting from the bottom)

2007-01-14 Thread Michael Alipio
Hi,

Is there a way to list down all builtin functions so that I may know what to 
perldoc -f?

Thanks.


- Original Message 
From: Michael Alipio [EMAIL PROTECTED]
To: beginners@perl.org
Sent: Monday, January 15, 2007 1:44:22 PM
Subject: Read a text file starting from the bottom

Hi,

I wanted to extract the start time of a particular logfile (located at the 
beginning of the file, and also the end time which is located at the end of the 
logfile.

Is there any way to do this in perl without using head or tail commands?

Thanks.

By the way, can you point me somewhere perhaps a perl function that lets you 
create directories?







 

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






 

Don't get soaked.  Take a quick peak at the forecast
with the Yahoo! Search weather shortcut.
http://tools.search.yahoo.com/shortcuts/#loc_weather

create symlinks to thousands of files using perl

2007-01-03 Thread Michael Alipio
I have a folder named myfolder
Inside myfolder, I have several files named ft1, ft2, ft3, ft4.

I need to create symbolic links to all of those files into my current working 
directory.

I tried creating a shell script:

#!/bin/sh
for i in /myfolder/ft* ; do ln -s ${i} ./; done

But what it did was to create a symbolic link ft* pointing to /myfolder/ft*...

#ls

 0 lrwxrwxrwx  1 root root   9 2007-01-03 15:55 ft* - /myfolder/ft*

When all I wanted was to have:

#ls
  0 lrwxrwxrwx  1 root root   8 2007-01-03 15:58 ft1 - myfolder/ft1
   0 lrwxrwxrwx  1 root root   8 2007-01-03 15:58 ft2 - myfolder/ft2
   0 lrwxrwxrwx  1 root root   8 2007-01-03 15:58 ft3 - myfolder/ft3


Do you know a quick perl script to accomplish this thing.
I shouldn't be needing this script but xargs in this OS(debian) doesn't have 
the -J flags like in BSD..

so far all I got is this: :-)

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




Thank you very much for any help or pointers..







__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Aggregating repeating lines

2006-12-05 Thread Michael Alipio
Hi,

I have a long log file that looks like this:

7491| 210.23.185.123   | PH202597706
7491| 210.23.169.91| PH202594221
7303| 201.252.130.245  | AR201955854
9318| 210.205.6.225| KR201892149
9930| 210.19.229.57| MY201418551
9600| 210.251.253.180  | JP201362230
9929| 210.82.176.84| CN201069109
7491| 210.23.182.102   | PH201006342
9600| 210.251.253.180  | JP201322301
4134| 58.215.76.36 | CN170125144
4844| 210.23.5.177 | SG170067928

It contains 4 columns, 1st and 2nd as well as 2nd and 3rd were separated by a 
pipe,
My goal is to add up the last column belonging to the same Country (column 3) 
display it in descending order, such that I may know what country received the 
biggest amount of bytes. The countries included in the log file are not a fixed 
list. Sometimes it contains PH, JP, KR sometimes, it contains PH and JP only, 
and sometimes it contains even more countries CN, JP, DE, US, PH, JP, KR.

It might look like this:


Country Total Bytes

PH 40
KR30
JP  20
CN10
...
...

And I'm thinking, perhaps I should get a unique list of all countries included 
in this logfile and only then I can aggregate their Bytes by using regexp.

Can you give me some hints on how I can accomplish this goal..

Thanks.


















 

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

Re: Ok, this time Net::SSH2 question :-) (pls ignore )

2006-10-30 Thread Michael Alipio


- Original Message 
From: Michael Alipio [EMAIL PROTECTED]
To: Michael Alipio [EMAIL PROTECTED]; beginners perl beginners@perl.org
Sent: Monday, October 30, 2006 1:25:36 PM
Subject: Re: Ok, this time Net::SSH2 question :-)

Hi,

Do you know if I still need to do something to enable Net::SSH::Perl to use 
Math::GMP, Math::Pari.. 

yeah, install Math::BigInt::GMP and IO::Handler first..  :-)

I just build and installed those two modules, even rebuild and reinstall 
Net::SSH::Perl but the session is still taking so long. I notice that 
Net::SSH::Perl is much extensive and example codes shipped in distribution is 
useful and works.. I guess I must stick with Net::SSH::Perl..

 sure, and better yet, join the 
 https://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users, and RTFA 
 first before posting..






- Original Message 
From: Michael Alipio [EMAIL PROTECTED]
To: beginners perl beginners@perl.org
Sent: Monday, October 30, 2006 10:49:18 AM
Subject: Ok, this time Net::SSH2 question :-)

Hi,

After reading the docs, I ended up with these lines:

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

use Net::SSH2;

my $ssh2 = Net::SSH2-new();
$ssh2-method('HOSTKEY', 'ssh-rsa');
$ssh2-connect('192.168.1.1') or die;
$ssh2-auth_publickey ('admin', '/root/.ssh/id_rsa.pub', '/root/.ssh/identity');
my $cmd = $ssh2-channel();
$cmd-exec('get sys stat');

I'm not really sure what I'm doing here. I can see now that Net::SSH2 is a bit 
faster than that of Net::SSH::Perl even after installing Math::pari I think... 
On the server, I can see a fast scrolling lines, a sign of much faster 
negotiation.

However, after executing my script, nothing happens... Although I know 
authentication was successful.. At the bottom of the server's debug, I can see 
these:

SH: secure_filename: checking '/data/etc/ssh'
SSH: secure_filename: checking '/data/etc'
SSH: secure_filename: checking '/data'
SSH: secure_filename: checking '/'
SSH: matching key found: file /etc/ssh/admin_auth_keys, line 2
SSH: restore_uid: 0/0
SSH: ssh_rsa_verify: signature correct
SSH: userauth_pubkey: authenticated 1 pkalg ssh-rsa
SSH: Entering interactive session for SSH2.
SSH: fd 5 setting O_NONBLOCK
SSH: fd 7 setting O_NONBLOCK
SSH: server_init_dispatch_20
SSH: server_input_channel_open: ctype session rchan 0 win 65536 max 16384
SSH: input_session_request
SSH: channel 0: new [server-session]
SSH: session_new: init
SSH: session_SSH: fd 9 is O_NSSH: Received SIGCHLD.
SSH: channel 0: request [EMAIL PROTECTED]

My original goal is to automate configuring an appliance which requires going 
into a certain modes first before you can issue the actual commands, sort of 
like 'conf t' in cisco.. Of course, aside from having to ssh or telnet to it 
first.

 Can you give me a very short code example.. As of this moment I'm still on 
page 6 of rfc 4251, hoping to find out what's with these channels and what am I 
really doing wrong. :-)

Thanks!





















Ok, this time Net::SSH2 question :-)

2006-10-29 Thread Michael Alipio
Hi,

After reading the docs, I ended up with these lines:

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

use Net::SSH2;

my $ssh2 = Net::SSH2-new();
$ssh2-method('HOSTKEY', 'ssh-rsa');
$ssh2-connect('192.168.1.1') or die;
$ssh2-auth_publickey ('admin', '/root/.ssh/id_rsa.pub', '/root/.ssh/identity');
my $cmd = $ssh2-channel();
$cmd-exec('get sys stat');

I'm not really sure what I'm doing here. I can see now that Net::SSH2 is a bit 
faster than that of Net::SSH::Perl even after installing Math::pari I think... 
On the server, I can see a fast scrolling lines, a sign of much faster 
negotiation.

However, after executing my script, nothing happens... Although I know 
authentication was successful.. At the bottom of the server's debug, I can see 
these:

SH: secure_filename: checking '/data/etc/ssh'
SSH: secure_filename: checking '/data/etc'
SSH: secure_filename: checking '/data'
SSH: secure_filename: checking '/'
SSH: matching key found: file /etc/ssh/admin_auth_keys, line 2
SSH: restore_uid: 0/0
SSH: ssh_rsa_verify: signature correct
SSH: userauth_pubkey: authenticated 1 pkalg ssh-rsa
SSH: Entering interactive session for SSH2.
SSH: fd 5 setting O_NONBLOCK
SSH: fd 7 setting O_NONBLOCK
SSH: server_init_dispatch_20
SSH: server_input_channel_open: ctype session rchan 0 win 65536 max 16384
SSH: input_session_request
SSH: channel 0: new [server-session]
SSH: session_new: init
SSH: session_SSH: fd 9 is O_NSSH: Received SIGCHLD.
SSH: channel 0: request [EMAIL PROTECTED]

My original goal is to automate configuring an appliance which requires going 
into a certain modes first before you can issue the actual commands, sort of 
like 'conf t' in cisco.. Of course, aside from having to ssh or telnet to it 
first.

 Can you give me a very short code example.. As of this moment I'm still on 
page 6 of rfc 4251, hoping to find out what's with these channels and what am I 
really doing wrong. :-)

Thanks!









Re: Ok, this time Net::SSH2 question :-)

2006-10-29 Thread Michael Alipio
Hi,

Do you know if I still need to do something to enable Net::SSH::Perl to use 
Math::GMP, Math::Pari.. I just build and installed those two modules, even 
rebuild and reinstall Net::SSH::Perl but the session is still taking so long. I 
notice that Net::SSH::Perl is much extensive and example codes shipped in 
distribution is useful and works.. I guess I must stick with Net::SSH::Perl..






- Original Message 
From: Michael Alipio [EMAIL PROTECTED]
To: beginners perl beginners@perl.org
Sent: Monday, October 30, 2006 10:49:18 AM
Subject: Ok, this time Net::SSH2 question :-)

Hi,

After reading the docs, I ended up with these lines:

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

use Net::SSH2;

my $ssh2 = Net::SSH2-new();
$ssh2-method('HOSTKEY', 'ssh-rsa');
$ssh2-connect('192.168.1.1') or die;
$ssh2-auth_publickey ('admin', '/root/.ssh/id_rsa.pub', '/root/.ssh/identity');
my $cmd = $ssh2-channel();
$cmd-exec('get sys stat');

I'm not really sure what I'm doing here. I can see now that Net::SSH2 is a bit 
faster than that of Net::SSH::Perl even after installing Math::pari I think... 
On the server, I can see a fast scrolling lines, a sign of much faster 
negotiation.

However, after executing my script, nothing happens... Although I know 
authentication was successful.. At the bottom of the server's debug, I can see 
these:

SH: secure_filename: checking '/data/etc/ssh'
SSH: secure_filename: checking '/data/etc'
SSH: secure_filename: checking '/data'
SSH: secure_filename: checking '/'
SSH: matching key found: file /etc/ssh/admin_auth_keys, line 2
SSH: restore_uid: 0/0
SSH: ssh_rsa_verify: signature correct
SSH: userauth_pubkey: authenticated 1 pkalg ssh-rsa
SSH: Entering interactive session for SSH2.
SSH: fd 5 setting O_NONBLOCK
SSH: fd 7 setting O_NONBLOCK
SSH: server_init_dispatch_20
SSH: server_input_channel_open: ctype session rchan 0 win 65536 max 16384
SSH: input_session_request
SSH: channel 0: new [server-session]
SSH: session_new: init
SSH: session_SSH: fd 9 is O_NSSH: Received SIGCHLD.
SSH: channel 0: request [EMAIL PROTECTED]

My original goal is to automate configuring an appliance which requires going 
into a certain modes first before you can issue the actual commands, sort of 
like 'conf t' in cisco.. Of course, aside from having to ssh or telnet to it 
first.

 Can you give me a very short code example.. As of this moment I'm still on 
page 6 of rfc 4251, hoping to find out what's with these channels and what am I 
really doing wrong. :-)

Thanks!















Help, using script to edit router config (entering different modes automatically)

2006-10-27 Thread Michael Alipio
Hi,

Suppose I have an appliance, it's not cisco but the process of updating 
configuration is almost the same.

1. First you log-in either via ssh or telnet..
2. Then you go into a certain mode by typing a particular command.
3. Only after you have enter that mode, you can issue the actual editing 
command, (e.g; set ip 192.168.1.1)
4. Finally, issue another command to apply the configuration change.

I need to execute these steps on a regular basis via cron.
Step 1 is not a problem I guess because I have discovered that the appliance 
supports ssh public/private key authentication.
Step 2 is also not a problem since I can execute it together with the command I 
use in step 1,

ssh appliance.our.domain conf t

Now, what? I don't know what to do next. Any idea?

Thanks.





Can't access search.cpan.org

2006-10-27 Thread Michael Alipio
Hi,

As of 3:43:18 pm here in Manila, Philippines, I can't access search.cpan.org.. 
Any idea why?





Re: Help, using script to edit router config (entering different modes automatically)

2006-10-27 Thread Michael Alipio


- Original Message 
From: Bjørge Solli [EMAIL PROTECTED]
To: beginners@perl.org
Sent: Friday, October 27, 2006 3:32:06 PM
Subject: Re: Help, using script to edit router config (entering different modes 
automatically)


Tried Net::SSH::Perl?
http://search.cpan.org/~dbrobins/Net-SSH-Perl-1.30/lib/Net/SSH/Perl.pm



Actually, I was looking into using the expect.pm, however, it's docs says it 
will become obsolete in the near future. So, I really should tryp 
net::ssh::perl right?. Unfortunately, I still can't access search.cpan.org as 
of 3:51 pm, Manila, Philippines. :-(










-- 
Bjørge Solli - Office:+47 55205847
Mohn-Sverdrupsenteret, Nansensenteret, Høyteknologisenteret T47
Thormöhlensgate 47, 5006 Bergen, Norway - www.nersc.no
Google Earth: www.nersc.no/GE - TOPAZ: topaz.nersc.no

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










Quick Net::SSH::Perl question

2006-10-27 Thread Michael Alipio
Hi,

My program looks like this:

#!/usr/local/bin/perl
use warnings;
use strict;
use Net::SSH::Perl;

my $host = '192.168.1.1';
my $user = 'user';
my $pass = 'password';

my $ssh = Net::SSH::Perl-new($host);
$ssh-login($user, $pass);
$ssh-shell;

It spawns a shell successfully but takes too long during the:

SSH: kex: client-server 3des-cbc hmac-sha1 none
SSH: mac_init: found hmac-sha1
SSH: kex: server-client 3des-cbc hmac-sha1 none
SSH: dh_gen_key: priv key bits set: 196/384
SSH: bits set: 514/1024
SSH: expecting SSH2_MSG_KEXDH_INIT

I guess this is the process I'm seeing when I ssh into a new server and it 
cannot find that host's fingerprint in my ~/.ssh/known_host..
I tried copying ~/.ssh/known_host ~/.ssh/identity but still takes too long.
I tried copying /etc/ssh/id_rsa ~/.ssh/identity but still takes too long.

The docs says:
 Net::SSH::Perl-new($host, %params)

To set up a new connection, call the new method, which connects to $host and 
returns a Net::SSH::Perl object.


new accepts the following named parameters in %params:




I want to set the debug param to see what is happening behind my script, but 
I'm not really sure how to set this param thing..
Any idea?


Thanks






IO::Scalar Help

2006-10-03 Thread Michael Alipio


My Log file looks like this:
Each session contains at most four lines and then it
is separated by newline. I need to grab each session
and put each key/value pair in hash.


Start=2006-10-03.09:09:51
IP=192.168.0.14
User=alex
End=2006-10-03.09:14:10

Start=2006-10-03.09:52:12
IP=192.168.0.15
End=2006-10-03.09:53:56

Start=2006-10-03.09:55:21
IP=192.168.0.16
User=mike

Start=2006-10-03.09:55:38
IP=192.168.0.17
End=2006-10-03.09:56:20


I'm just following an example given to me.. however at
some point, I failed to make it work.



my $logfile = shift @ARGV;
open LOGFILE, $logfile or die $!;
while (LOGFILE){
chomp;
push @data, $_;

}
$logdata = @data;

local $/ = ;
my $fh = new IO::Scalar \$logdata;
while (my $record = $fh) {
my %extr;
for (qw(Start IP User End)){
$extr{$_} = '';
$extr{$_} = $1 if $record =~ /$_=(.*)/;
}
print Start:$extr{Start} IP:$extr{IP}
User:$extr{User} End:$extr{End};
}


The output is ugly :-(.. I tried trimming my logs to
only two sessions:

Start=2006-10-03.02:51:48
IP=192.168.0.14
User=edison
End=2006-10-03.04:17:36

Start=2006-10-03.02:51:49
IP=192.168.0.15
User=arnel
End=2006-10-03.04:17:37


And I got:

Use of uninitialized value in concatenation (.) or
string at formatlogs.pl line 51.
Use of uninitialized value in concatenation (.) or
string at formatlogs.pl line 51.
Use of uninitialized value in concatenation (.) or
string at formatlogs.pl line 51.
Start:2006-10-03.02:51:48 IP=192.168.0.14 User=edison
End=2006-10-03.04:17:36  Start=2006-10-03.02:51:49
IP=192.168.1.15 User=arnel End=2006-10-03.04:17:37 IP:
User: End:

It's even more uglier.. plus, no matter how I insert
the newline... the entire output is still shown as a
single continues line...

Any idea what I am doing?

Thanks.









__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




regexp inside hashes

2006-10-03 Thread Michael Alipio
Hi,

There was this example given to me:

while ( LOGFILE ) {
my %extr = (
Start = '',
IP= '',
User  = '',
End   = '',
/^(Start|IP|User|End)=(.+)/mg
);
print Start:$extr{Start} IP:$extr{IP}
User:$extr{User} End:$extr{End}\n\n;
}

Reading my logfile in paragraph mode, it has these
lines:

Start: blablablah
IP: blah blah blah
User: blah blah blah
End: blah blah blah

Start: blablablah2
IP: blah blah blah2
User: blah blah blah2
End: blah blah blah2

What the above code does (specifically inside the hash
is to assign the found pattern into the hash keys
using this I guess...
...
   /^(Start|IP|User|End)=(.+)/mg
 );
...

I just wanted to know how fast did it happened..
Any idea?

My new logfile contains these lines (each is one
continuous line):

Jul  1 01:06:33 my.hostname.com
date=2006-07-01,time=01:06:46,device_id=FG200A2105403175,log_id=0101023002,type
=event,subtype=ipsec,pri=notice,vd=root,loc_ip=192.168.0.4,loc_port=4500,rem_ip=192.168.1.14,rem_port=33552,out_if=wan1,vp
n_tunnel=AxisGlobal,action=negotiate,status=success,msg=XAUTH
user: ricky authentication successful

Jul  1 04:45:58 ppp130.dyn242.pacific.net.ph
date=2006-07-01,time=04:46:09,device_id=FG200A2105403175,log_id=0101023002,type
=event,subtype=ipsec,pri=notice,vd=root,loc_ip=192.168.0.5,loc_port=4500,rem_ip=192.16.3.97,rem_port=36036,out_if=wan1
,vpn_tunnel=AxisGlobal,action=negotiate,status=success,msg=XAUTH
user: susan authentication successful


Now, my goal is to adapt that code, particularly
obtaining only Start, IP, User. However, those three
targets are not anymore located at the beginning of a
line.

Start is the date=.time= combination,
IP is found after rem_ip=
User is found after user: 

I'm not really sure how to put my regexp inside my
hash..

while ( LOGFILE ) {
my %extr = (
Start = '',
IP= '',
User  = '',
/what should i put here??/mg
);
print Start:$extr{Start} IP:$extr{IP}
User:$extr{User}\n\n;
}


Hope you can help me..
thanks!
-jay

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




Regexp Basics

2006-10-02 Thread Michael Alipio
Hi,

Suppose I have this:

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

my $string = Theres more than 1 way to do it;
if ($string =~ /\w+$/){
print Hooray! pattern found;
print $1;
}

My goal is to print the last word.
However, it only prints Hooray! pattern found;

Any idea what's wrong with $1??

Thanks





__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




one liner to remove \xa0 characters

2006-09-25 Thread Michael Alipio
Hi,

I have several files with \xa0 characters.. Do you
know how can I remove them?

Thanks.

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




running through regexp matches ($1-$n)

2006-09-15 Thread Michael Alipio
Hi,

A log file contains several of these lines:

session.blablahbla
blablabla proto:6 blablabla srcip:90.0.0.1 blablabla
blablabla srcport:3243 blablabla dstport:23 blablabla

session.blablahbla
blablabla proto:6 blablabla srcip:90.0.0.1 blablabla
blablabla srcport:3243 blablabla dstport:23 blablabla

session.blablahbla
blablabla proto:6 blablabla srcip:90.0.0.1 blablabla
blablabla srcport:3243 blablabla dstport:23 blablabla

basically, for each session, I need to obtain:
srcip, srcport, dstip, dstport, then do something with
it, say put it in a table;

So far here's what I got: :-)


my $sessionlog = shift @ARGV;
my $sessioncounter = '0';
my $start;
my $srcip;
my $srcport;
my $dstip;
my $dstport;

open SESSIONLOGS, $sessionlog or die $!;
while (SESSIONLOGS){
  if (/^session/){
 $start = true;
 ++$sessioncounter;
  }

}

#the logic I am thinking of here is to:
#for every line, if it sees the word session and until
it sees a new one, then mark it as a beggining of a
session with $start. process the following lines, only
until it sees another session keyword. then store
all the corresponding regex it see into:
my $srcip;
my $srcport;
my $dstip;
my $dstport;

the problem is... I'm not sure how to do it. :-)
can you help we with just some few tips?
pleaaase...:-)

Thank you very much
-jay

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




extracting several text from logs using regex

2006-09-14 Thread Michael Alipio
Hi,

A log file contains several of these lines:

session.blablahbla
blablabla proto:6 blablabla srcip:90.0.0.1 blablabla
blablabla srcport:3243 blablabla dstport:23 blablabla

session.blablahbla
blablabla proto:6 blablabla srcip:90.0.0.1 blablabla
blablabla srcport:3243 blablabla dstport:23 blablabla

session.blablahbla
blablabla proto:6 blablabla srcip:90.0.0.1 blablabla
blablabla srcport:3243 blablabla dstport:23 blablabla

basically, for each session, I need to obtain:
srcip, srcport, dstip, dstport, then do something with
it, say put it in a table;

So far here's what I got: :-)


my $sessionlog = shift @ARGV;
my $sessioncounter = '0';
my $start;
my $srcip;
my $srcport;
my $dstip;
my $dstport;

open SESSIONLOGS, $sessionlog or die $!;
while (SESSIONLOGS){
  if (/^session/){
 $start = true;
 ++$sessioncounter;
  }

}

#the logic I am thinking of here is to:
#for every line, if it sees the word session and until
it sees a new one, then mark it as a beggining of a
session with $start. process the following lines, only
until it sees another session keyword. then store
all the corresponding regex it see into:
my $srcip;
my $srcport;
my $dstip;
my $dstport;

the problem is... I'm not sure how to do it. :-)
can you help we with just some few tips?
pleaaase...:-)

Thank you very much
-jay









__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




looping through complicated hashes..

2006-09-07 Thread Michael Alipio
Hi,

I have an input file with 7 columns each line. Words
are separated with 1 or more spaces..
Now, I want each of these lines placed into
HTML::Table's rows.

The HTML::Table has something like this.
each row (with 7 cells) is encolsed with []. I want
each row, populated with each line in my input file.

my $table = new HTML::Table(-data= [['1stRowValue1',
'1stRowValue2', upto '1stRowValue7'], ['2ndRowValue1',
'2ndRowValue2', upto '2ndRowValue7']]

So far, all I can do is open the input file, read it
line by line, split each line and store it in an
array.

However, I'm not sure how to insert my loop inside
HTML::Table declaration section.. 
Any idea?

Thanks!


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




Re: looping through complicated hashes..

2006-09-07 Thread Michael Alipio
ignore this one.. I've already figured out how.
thanks.

--- Michael Alipio [EMAIL PROTECTED] wrote:

 Hi,
 
 I have an input file with 7 columns each line. Words
 are separated with 1 or more spaces..
 Now, I want each of these lines placed into
 HTML::Table's rows.
 
 The HTML::Table has something like this.
 each row (with 7 cells) is encolsed with []. I want
 each row, populated with each line in my input file.
 
 my $table = new HTML::Table(-data=
 [['1stRowValue1',
 '1stRowValue2', upto '1stRowValue7'],
 ['2ndRowValue1',
 '2ndRowValue2', upto '2ndRowValue7']]
 
 So far, all I can do is open the input file, read it
 line by line, split each line and store it in an
 array.
 
 However, I'm not sure how to insert my loop inside
 HTML::Table declaration section.. 
 Any idea?
 
 Thanks!
 
 
 __
 Do You Yahoo!?
 Tired of spam?  Yahoo! Mail has the best spam
 protection around 
 http://mail.yahoo.com 
 
 -- 
 To unsubscribe, e-mail:
 [EMAIL PROTECTED]
 For additional commands, e-mail:
 [EMAIL PROTECTED]
 http://learn.perl.org/
 http://learn.perl.org/first-response
 
 
 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




arithmetic expression while substituting

2006-09-06 Thread Michael Alipio
Hi,

Suppose I have the output of this command
date +%d.%H

which outputs:
06.11

I want to adjust the last two digits to less 1:
such that it becomes  06.10..
how do I do that?

perhaps something like this.
s/\d+$/(regexp being lookup minus 1/


thanks!

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




can't print to STDOUT on 5.8.4

2006-09-05 Thread Michael Alipio
Hi,

I was running this command on my perl program,

open FLOWTOOLS, |/usr/bin/flow-cat $start |
/usr/bin/flow-nfilter -f filter.tmp -F $direction
|/usr/bin/flow-stat -Pf8 | le
ss or die $!;

It does output something on the screen when I run it
on FreeBSD 6.1/perl 5.8.8 (while on csh)

However, when I run it on another machine Linux
version 2.6.8-2-386 Debian 1:3.3.5-13, with This is
perl, v5.8.4 built for i386-linux-thread-multi (while
on bash)

a blank screen appears shortly and then the program
immediately exits..

Any idea why this is happening?

Thanks.


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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