Re: Perl regular expression for implementing infix expression parser?

2016-08-14 Thread Richard Heintze via beginners
Well, I'm thinking of a simple syntax tree that would fit on a one-liner? For 
the first cut, I think I'll ignore unary operators and just accommodate numeric 
integer literals for addition, subtraction, multiplication and division.
Later we can add variables, unary operators, functions like log10, sin, cos 
etc...

See Recursive descent parser - Wikipedia, the free encyclopedia
e for expressionf for factorpm for plus or minusmd for multiply divide
n for numeric literal



here is my attempt so far (that does not work):
.! perl -pe 'BEGIN{ $n=qr{\d+}x; $pm=qr{[-\+]}x; $md=qr{[/\*]}x; 
$e=qr{$t($pm$t)?}x; $f=qr{$n|\($e\)}x; $t=qr{$f($md$f)?}}  s/($t)=( 
*)/"$1=".eval($1)/ge'
Here is my sample data55-23=
It seems to  ignore the 55.ThanksSiegfried 

On Sunday, August 14, 2016 4:26 AM, David Mertens 
<dcmertens.p...@gmail.com> wrote:
 

 Hello siegried,

This is a fun question! In fact, v5.10 introduced "recursive subpatterns" into 
the regex lexicon, so using v5.10 or later, it is possible to implement (rather 
arcane) recursive descent regexes without resorting to eval-based tricks like 
what you have.

But before diving in, a few questions are in order to make sure we're working 
on the right problem. Are you looking for something to cram into a one-liner? 
(Do you have a character limit for your one-liner?) Are you open to solving 
this with a combination of for-loops and regexes, or do you want a purely 
regex-based approach? Are you trying to write something that merely validates 
that an expression as valid mathematics, or do you want to build an abstract 
syntax tree? Are you interested in using modules from CPAN or rolling your own? 
Finally, if your answer to all of these is, "I'm just curious!", then you can 
expect to get a very wide range of answers, not just regex-based.

Thanks! Looking forward to the fun!
David

On Sat, Aug 13, 2016 at 3:45 PM, Richard Heintze via beginners 
<beginners@perl.org> wrote:

I this perl one liner for evaluating infix expressions in VI and emacs/VI 
emulator mode:


.! perl -MPOSIX   -pe ' BEGIN{ $np = qr{ \( (?: (?> [^()]+ ) | (??{ $np }) )* 
\) }x;$funpat = qr/$np/;} s/($funpat)=($funpat|(")[^\"]* 
(")|[0-9\.]+)/"$1=$3".eval($1) ."$4"/ge'
That $np is from the camel book and it is a regular expression that parses 
nested sets of parentheses and then my replace command evaluates the arithmetic 
expression.
Since perl accommodates recursive regular expressions, it ought to be possible 
to implement a recursive decent parser.  
Can someone help me enhance the above code so that instead of just blindly 
looking for balanced parenthesis, the regular expression will recognize (match) 
this:
5+8*(2+8/(3+2))*(2+22/3)=()Thankssiegfried





-- 
 "Debugging is twice as hard as writing the code in the first place.
   Therefore, if you write the code as cleverly as possible, you are,
   by definition, not smart enough to debug it." -- Brian Kernighan


  

Perl regular expression for implementing infix expression parser?

2016-08-13 Thread Richard Heintze via beginners
I this perl one liner for evaluating infix expressions in VI and emacs/VI 
emulator mode:


.! perl -MPOSIX   -pe ' BEGIN{ $np = qr{ \( (?: (?> [^()]+ ) | (??{ $np }) )* 
\) }x;$funpat = qr/$np/;} 
s/($funpat)=($funpat|(")[^\"]*(")|[0-9\.]+)/"$1=$3".eval($1)."$4"/ge'
That $np is from the camel book and it is a regular expression that parses 
nested sets of parentheses and then my replace command evaluates the arithmetic 
expression.
Since perl accommodates recursive regular expressions, it ought to be possible 
to implement a recursive decent parser.  
Can someone help me enhance the above code so that instead of just blindly 
looking for balanced parenthesis, the regular expression will recognize (match) 
this:
5+8*(2+8/(3+2))*(2+22/3)=()Thankssiegfried



Wanted: Help performing Date-Time Arithmetic using emacs/vi and perl

2016-04-18 Thread Richard Heintze via beginners
  I just love using viper mode in emacs to execute perl:
.! perl -MPOSIX -pe ' BEGIN{ $np = qr{ \( (?: (?> [^()]+ ) | (??{ $np }) )* \) 
}x;$funpat = qr/$np/;} s/($funpat)=(.*)$/"$1=$3".eval($1)."$4"/ge'

This command searches for a balanced set of parens (from the camel book) 
followed by an equal sign followed by any trailing garbage and evaluates the 
expression inside the balanced parans.
so if you type
(4*atan(1))=gobblygook
and then execute the above VI/Ex command you get:
4*atan(1))=3.14159265358979


Here is my failed attempt to do date arithmetic:
(use Date::Calc ( ":all" ); use Date::Manip; my ( $date, $yy, $dd, $mm ); $date 
= scalar localtime( ( time() - ( 24 * 60 * 60 ) ) ); $date)=""
Can someone help me figure out how to use the eval function in my perl 
one-liner to evaluate date time arithmetic?
Thanks,Siegfried



Re: Removing text

2015-05-19 Thread Richard Taubo
Hi!


 On 18 May 2015, at 18:27, Richard Taubo o...@bergersen.no wrote:
 
 Hi, 
 
 and thanks to Shawn H Corey, Jing Yu.
 
 I see that I have not been specific enough.
 (And sorry for the top posting).
 
 The full (bash) script with perl parts looks like this:
 [$] top_return=$(top -n1 | head -5)
 [$] io_return=$(printf %s\n $top_return | grep ^Cpu(s))
 [$] io_all=$(printf  %s $io_return | awk '{ printf $9 }' | perl -pe 
 's/\%st//')
 [$] printf %s\n $io_all
 
 Returns =  0.0%st(would want it to be: 0.0 without the '%st' 
 part).
 
 This kind of works:
 [$] io_all=$(printf  %s $io_return | awk '{ printf $9 }' | perl -pe 
 's/\%//')
 [$] printf %s\n $io_all
 Returns =  0.0st (without the % sign)
 
 This too:
 [$] io_all=$(printf  %s $io_return | awk '{ printf $9 }' | perl -pe 
 's/st//')
 [$] printf %s\n $io_all
 Returns =  0.0% (without the st characters)
 
 BUT when i search and replace for %st (as described above), I can’t
 get it to work.
 
 Thanks for any feedback, and thanks again for the answers I have received. :-)

In the end, I ended up with two separate perl commands.
One removing the % part of the text, the other the st part of the text:

io_all=$(printf %s\n $cp_return | awk '{ printf $9 }' | perl -pe 's/%//' | 
perl -pe 's/st//')
Returns =  0.0 (without the %st characters)

That works fine, but looks a little clunky, are there perhaps other ways to 
unite the two
perl commands?

Thanks!

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




Removing text

2015-05-18 Thread Richard Taubo
Hi!

Trying to remove the literal text %st from the command line return value: 
0.0%st
as in:
[$] printf 0.0%st | perl -pe 's/\%st//'

I have also tried: 

[$] printf 0.0%st | perl -pe 's/\Q%st\E//'

Neither works.

Would be happy if someone had any input here! :-)

Thanks!

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




Re: Removing text

2015-05-18 Thread Richard Taubo
Hi, 

and thanks to Shawn H Corey, Jing Yu.

I see that I have not been specific enough.
(And sorry for the top posting).

The full (bash) script with perl parts looks like this:
[$] top_return=$(top -n1 | head -5)
[$] io_return=$(printf %s\n $top_return | grep ^Cpu(s))
[$] io_all=$(printf  %s $io_return | awk '{ printf $9 }' | perl -pe 
's/\%st//')
[$] printf %s\n $io_all

Returns =  0.0%st  (would want it to be: 0.0 without the '%st' 
part).
 
This kind of works:
[$] io_all=$(printf  %s $io_return | awk '{ printf $9 }' | perl -pe 
's/\%//')
[$] printf %s\n $io_all
Returns =  0.0st (without the % sign)

This too:
[$] io_all=$(printf  %s $io_return | awk '{ printf $9 }' | perl -pe 
's/st//')
[$] printf %s\n $io_all
Returns =  0.0% (without the st characters)

BUT when i search and replace for %st (as described above), I can’t
get it to work.

Thanks for any feedback, and thanks again for the answers I have received. :-)

Best regards,
Richard Taubo




 On 18 May 2015, at 17:52, Shawn H Corey shawnhco...@gmail.com wrote:
 
 On Mon, 18 May 2015 17:32:03 +0200
 Richard Taubo o...@bergersen.no wrote:
 
 Hi!
 
 Trying to remove the literal text %st from the command line return
 value: 0.0%st as in:
 [$] printf 0.0%st | perl -pe 's/\%st//'
 
 I have also tried: 
 
 [$] printf 0.0%st | perl -pe 's/\Q%st\E//'
 
 Neither works.
 
 Would be happy if someone had any input here! :-)
 
 Thanks!
 
 Richard Taubo
 
 I'm not sure what you're trying to do but if you want a percent sign in
 a printf specification, use two of them.
 
printf Sale: %d%% off, $percentage_off;
 
 See `perldoc -f sprintf`
 http://perldoc.perl.org/functions/sprintf.html

And:

 On 18 May 2015, at 17:49, Jing Yu logus...@googlemail.com wrote:
 
 Hi Richard,
 
 When you 
printf 0.0%st”
 in the command line, it prints
0.0t
 And that is the string piped to perl. This is perhaps why you didn’t succeed.


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




RE: which module to read configuration file?

2014-08-30 Thread Richard Bailey
There is code available from my wizard at
http://www.rtbaileyphd.com/perlwizard that is doing something fairly similar
to what you described.  Use PerlWizard to generate a simple test program,
run it once, and then examine the contents of the pwiz subirectory, which is
where the config files go.  These are used in conjunction with Getopt::Long
as well.

 

Best Regards,

R. T. Bailey

 

-Original Message-
From: lee [mailto:l...@yun.yagibdah.de] 
Sent: Saturday, August 30, 2014 12:09 PM
To: beginners@perl.org
Subject: Re: which module to read configuration file?

 

lee  mailto:l...@yun.yagibdah.de l...@yun.yagibdah.de writes:

 

 Hi,

 

 is there a module available to read configuration files which are like

 this:

 

 

 arbitrary_denominator-00 | alternate-denominator-00 | 

 alternate-denominator-01 [| ...] {

   itemA = some string

   itemB = some integer

   itemC = some float

   itemD = some CSV

 }

 

Following-up my own question: I found [1] which appears to come close to
what I'm looking for.  Of course, I'd still love to hear your ideas :)

 

 

[1]:  https://metacpan.org/pod/Config::JSON
https://metacpan.org/pod/Config::JSON

 

 

--

Knowledge is volatile and fluid.  Software is power.

 

--

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

 



PerlWizard - A free wizard for automatic Perl software code generation using simple forms

2014-08-25 Thread Richard Bailey
Summary.  PerlWizard http://www.rtbaileyphd.com/perlwizard  quickly
generates front ends for user-friendly Perl scripts for Unix and Windows,
with emphasis on the user interface, managing defaults files, and providing
help.  The generated user interface supplies defaults for unentered options,
validates options, and records options for use as defaults on subsequent
runs of the generated scripts.  This makes PerlWizard scripts much more
interactive and friendly than typical command line programs.  Once
PerlWizard generates the front end, the programmer just needs to go to the
bottom of the generated code and start writing.  No need to worry about how
to set up Getopt::Long http://perldoc.perl.org/Getopt/Long.html  calls,
initialization files, built-in help, log files, etc.  PerlWizard is
available at http://www.rtbaileyphd.com/perlwizard.

Basically, you just need to fill out a few simple forms, click a few
buttons, and you can quickly jump into coding the guts of your script
without bothering with all the boilerplate needed to produce a robust,
user-friendly Perl application.

FYI PerlWizard is a Java application that uses the Freemarker template
engine that reads a Perl template to generate Perl scripts.

 

Best Regards,

R. T. Bailey

 



Re: Propose for LINUX kernel and PERL

2014-01-05 Thread Richard Foley
A useful Monks link here:

http://www.perlmonks.org/?node=339754

and:

http://www.foo.be/docs/tpj/issues/vol5_2/tpj0502-0009.html

-- 
Ciao

Richard Foley

http://www.rfi.net/books.html

On Sun, Jan 05, 2014 at 12:46:00AM -0500, Antti Heikkinen wrote:
 To Dear Perl and LINUX kernel development community:
 
 My propose to you at your list: is possible to write operate system in
 PERL? I am student in university, looked for interest project to
 conclude my study on LINUX kernel.
 
 This semester, I take beginner PERL course and learn power of
 procedural language. I automate many daily task with use of it. Very
 impressive ability to make many thing work, interpret or can compile
 also.
 
 Also about LINUX, I talk to much fellow students and professors, and
 take a operate system course use FreeBSD and LINUX. FreeBSD okay, but
 they say LINUX kernel is too big and bloat, run poor with too many
 developer. And too much quick decision from leader with ego is too big
 and bloat too, kekeke.
 
 LINUX kernel can perform more good if written in not C and C++ but
 Perl? Just certain portion of LINUX kernel to rewrite? For instant,
 schedule or support of multithread? If so, should use Perl5 or Perl6,
 focus to x86 or x86-64? Can you want to join me this my project? But
 to hear your expertise.
 
 Am excited to learn and begin study project. Can you want to join this
 my project? Please direct reply of email to myself.
 
 Much thank to you,
 Antti Heikkinen

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




Richard Fernandez

2013-06-17 Thread Richard Fernandez
wer
http://estetikburuncerrahisi.com/mr/lybibl/cddhmsewykrj/ubpejcbsumykym.php
Richard Fernandez


perl hash loop: keys() vs values()

2013-06-15 Thread richard
Hi

I'm trying to understand the difference between the keys() and values ()
operators. What I'm not getting is why a hash must be reversed to get the
key corresponding to a value? (as stated in perldoc). Can someone explain
this please?

Here is my test script; My perl is freebsd 5.8.9

use warnings;
use strict;

my %hds = ('1', '0C8CB35F', '2', '0C9CB37D');
print rows:  . scalar keys(%hds) . \n\n;

my $value;
my $key;

  foreach $value (values %hds) {
print key: $hds{$value} \n;
print value: $value \n;
  }

print --- \n;

  my %r_hds = reverse %hds;
  foreach $value (values %hds) {
print key: $r_hds{$value} \n;
print value: $value \n;
  }

print --- \n;

  foreach $key   (keys %hds) {
print key: $key \n;
print value: $hds{$key} \n;
  }


Here is the output:

Use of uninitialized value in concatenation (.) or string at h line 11.
key:
value: 0C8CB35F
Use of uninitialized value in concatenation (.) or string at h line 11.
key:
value: 0C9CB37D
---
key: 1
value: 0C8CB35F
key: 2
value: 0C9CB37D
---
key: 1
value: 0C8CB35F
key: 2
value: 0C9CB37D


--
regards, Richard
--
tmqrich...@gmail.com


Re: perl hash loop: keys() vs values()

2013-06-15 Thread richard
Octavian

Thanks for taking the time to write such a thorough reply. My question is
completely answered by this section:

You can't get the key for a certain value. Say you have the following hash:

my %hash = (a = 1, b = 2, c = 3, d = 2, e = 2);

What's the key for the value 2?

I was so preoccupied with values() and keys(), thinking of them as being
mirror-images of one another, that I forgot what a 'value' and a 'key'
were.

Thanks again


regards, Richard
--
tmqrich...@gmail.com


On Sat, Jun 15, 2013 at 2:19 PM, Octavian Rasnita orasn...@gmail.comwrote:

 **
 Perl has 2 types of arrays: common arrays and associative arrays which are
 called hashes.

 In order to get the value of an item from an ordinary array, you need to
 specify  the index of that array, for example:

 my @array = (1, 2, 3);
 print $array[1]; #will print 2 (because the indexes start from 0

 A hash associates a key to any value and if you want to get the value, you
 need to specify its key:
 my %hash = (a = 1, b = 2, c = 3);
 print $hash{b}; #will print 2

 You can't get the key for a certain value. Say you have the following hash:

 my %hash = (a = 1, b = 2, c = 3, d = 2, e = 2);

 What's the key for the value 2?

 The keys in a hash are always unique but the values can have duplicates.

 If you reverse the hash, the keys become values and the values become
 keys, so you will be able to use the value as a  key. But if the values
 have duplicates, your reversed hash will contain less elements because that
 hash will contain unique keys.

 In the example above, there would be just a single 2 key and not 3.

 So, the question is why do you need to access the keys using their values?

 Maybe you want to use an array of arrays like:

 my @array = (
 ['a', 1],
 ['b', 2],
 ['c', '3],
 ['d', 2],
 );

 To get the value for the element with the index 1 you need to do:

 my $value = $array[1][1]; #the value 2

 and to get the key for the element with the index 1, you can do:

  my $key = $array[1][0]; #Will get b

 I don't know what you need to do... that's why I gave the idea of using
 arrays of arrays...

 --Octavian

 - Original Message -
 *From:* richard tmqrich...@gmail.com
 *To:* beginners@perl.org
 *Sent:* Saturday, June 15, 2013 7:12 PM
 *Subject:* perl hash loop: keys() vs values()

 Hi

 I'm trying to understand the difference between the keys() and values ()
 operators. What I'm not getting is why a hash must be reversed to get the
 key corresponding to a value? (as stated in perldoc). Can someone explain
 this please?

 Here is my test script; My perl is freebsd 5.8.9

  use warnings;
 use strict;

 my %hds = ('1', '0C8CB35F', '2', '0C9CB37D');
 print rows:  . scalar keys(%hds) . \n\n;

 my $value;
 my $key;

   foreach $value (values %hds) {
 print key: $hds{$value} \n;
 print value: $value \n;
   }

 print --- \n;

my %r_hds = reverse %hds;
   foreach $value (values %hds) {
 print key: $r_hds{$value} \n;
 print value: $value \n;
   }

 print --- \n;

   foreach $key   (keys %hds) {
 print key: $key \n;
 print value: $hds{$key} \n;
   }


 Here is the output:

  Use of uninitialized value in concatenation (.) or string at h line 11.
 key:
 value: 0C8CB35F
 Use of uninitialized value in concatenation (.) or string at h line 11.
 key:
 value: 0C9CB37D
 ---
 key: 1
 value: 0C8CB35F
 key: 2
 value: 0C9CB37D
 ---
 key: 1
 value: 0C8CB35F
 key: 2
 value: 0C9CB37D


 --
  regards, Richard
 --
 tmqrich...@gmail.com




Re: perl hash loop: keys() vs values()

2013-06-15 Thread richard
Thanks. I looked at this site while searching for solutions but I probably
skipped the section on sorting because I wasn't sorting the hash.

regards, Richard
--
tmqrich...@gmail.com


On Sun, Jun 16, 2013 at 3:17 AM, Peter Gordon pete...@netspace.net.auwrote:

 On Sat, 15 Jun 2013 12:12:56 -0400, richard wrote:
 Hi
 
 I'm trying to understand the difference between the keys() and
 values () operators. What I'm not getting is why a hash must be
 reversed to get the key corresponding to a value? (as stated in
 perldoc). Can someone explain this please?
 
 
 Read http://www.tizag.com/perlT/perlhashes.php
 particularly the section on sort by keys  by values.
 This should give you a good understanding of the
 how to use them.

 --
 Peter Gordon, pete...@netspace.net.au on 06/16/2013



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





string substitution command question

2011-02-26 Thread Richard Green
Hi Perl users, Quick question, I have a one long string with tab delimited
values separated by a newline character (in rows)
Here is a snippet of the the string:

chr1ucscexon226488874   226488906   0.00
-   .   gene_id NM_173083; transcript_id NM_173083;
chr1ucscexon226496810   226497198   0.00
-   .   gene_id NM_173083; transcript_id NM_173083;
chr1ucscexon2005086 2005368 0.00+   .
gene_id NM_001033581; transcript_id NM_001033581;
chr1ucscexon2066701 2066786 0.00+   .
gene_id NM_001033581; transcript_id NM_001033581;

I am trying to perform substitution on some values at the end of each rows,
for example, I'm trying to replace the above string with the following:

chr1ucscexon226488874   226488906   0.00
-   .   gene_id NM_173083:12345; transcript_id NM_173083:12345;
chr1ucscexon226496810   226497198   0.00
-   .   gene_id NM_173083:12345; transcript_id NM_173083:12345;
chr1ucscexon2005086 2005368 0.00+   .
gene_id NM_001033581:12346; transcript_id NM_001033581:12346;
chr1ucscexon2066701 2066786 0.00+   .
gene_id NM_001033581:12346; transcript_id NM_001033581:12346;

Here is the substitution command I am trying to use:

$data_string=~ s/$gene_id\NM_173083\\; transcript_id
\NM_173083\\;/\NM_173083:12345\\; \NM_173083:12345\\;/g;

$data_string=~ s/$gene_id\NM_001033581\\; transcript_id
\NM_001033581\\;/\NM_001033581:12346\\; \NM_001033581:12346\\;/g;

I don't know why I am not able to substitute at the end of each row in the
string.
Any suggestions folks have are muchly appreciated. Thanks -Rich


Re: string substitution command question

2011-02-26 Thread Richard Green

 What is $gene_id?  
 Are you by any chance using '$' at the beginning of your search pattern 
 instead of the end?
I have $ to designate the end of the row
$gene_id 
 
 Why are you escaping the quote marks?
I thought it would be easier to perform substitution without them
 
 Why is there no space after 'gene_id'?
I guess there should be





On Feb 26, 2011, at 12:30 PM, John Delacour johndelac...@gmail.com wrote:

 At 12:06 -0800 26/02/2011, Richard Green wrote:
 
 chr1ucscexon226488874   226488906   0.00
 -   .   gene_id NM_173083:12345; transcript_id NM_173083:12345;
 chr1ucscexon226496810   226497198   0.00
 -   .   gene_id NM_173083:12345; transcript_id NM_173083:12345;
 chr1ucscexon2005086 2005368 0.00+   .
 gene_id NM_001033581:12346; transcript_id NM_001033581:12346;
 chr1ucscexon2066701 2066786 0.00+   .
 gene_id NM_001033581:12346; transcript_id NM_001033581:12346;
 
 Here is the substitution command I am trying to use:
 
 $data_string=~ s/$gene_id\NM_173083\\; transcript_id
 \NM_173083\\;/\NM_173083:12345\\; \NM_173083:12345\\;/g;
 
 $data_string=~ s/$gene_id\NM_001033581\\; transcript_id
 \NM_001033581\\;/\NM_001033581:12346\\; \NM_001033581:12346\\;/g;
 
 I don't know why I am not able to substitute at the end of each row in the
 string.
 
 What is $gene_id?  Are you by any chance using '$' at the beginning of your 
 search pattern instead of the end?
 
 Why are you escaping the quote marks?
 
 Why is there no space after 'gene_id'?
 
 JD
 
 -- 
 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: string substitution command question

2011-02-26 Thread Richard Green
Ok JD thanks 




On Feb 26, 2011, at 3:46 PM, John Delacour johndelac...@gmail.com wrote:

 At 12:57 -0800 26/02/2011, Richard Green wrote:
 
 
  What is $gene_id? 
 Are you by any chance using '$' at the beginning of your search pattern 
 instead of the end?
 I have $ to designate the end of the row
 $gene_id
 
 $gene_id designates $gene_id period.
 
  Why are you escaping the quote marks?
 I thought it would be easier to perform substitution without them
 
 What made you think that?
 
  Why is there no space after 'gene_id'?
 I guess there should be
 
 You can guess as much as you like but Perl Regular Expressions don't care 
 what you think or what you guess.  Read perlvar and pelretut.
 
 JD
 
 
 
 -- 
 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: Removing leading whitespace and removing alternate newlines

2010-10-31 Thread Richard Hug
$string =~ s/^\s+//;

http://oreilly.com/catalog/9781565922433/ page 43

That book is my bible!!!

On Sun, Oct 31, 2010 at 11:01 AM, Brian brian5432...@yahoo.co.uk wrote:

 Hi guys, long time no working with PERL :-)

 I have just installed 5.2.12

 First, I am trying to remove leading tabs  spaces but the following leaves
 a
 few blanks at the beginning of each line, could someone be kind enough to
 point
 out the error of my ways please? :-)

 #!/usr/bin/perl

 local $/=undef;
 open(FILE, smith3.txt) || die (Error\n);
 $string = FILE;
 $string =~ s/\s//; #remove leading spaces

 print $string;



 Secondly, I would like to remove newline from alternate lines, ie I would
 like
 to remove from lines 1,3,5,7 etc.
 What would be the simplest way of getting even line numbers to print on the
 same
 line as odds?

 Thanks muchly
 Brian







-- 
Richard Hug
813-973-3783
cell 813-361-7946


loading and comparing large hashes

2010-09-30 Thread Richard Green
Hi everyone, could use some advice on a perl script I wrote using hashes. I
have three files ( each file is a list of indexes) my program loads these
indexes into hashes and compares the differences and similarities between
them. With smaller files it runs fine. problem is I now files have about 88
million records and the script has been running for days. not sure the best
way to resolve the issue.(pieces of code samples below) One suggestion that
was given to me was to load the first file into a hash and then as I open
the next file I immediately do a comparison of the second hash into the 1st
one for similarities and differences. I can't really get my head around how
to that. Is there a simple way to compare three very large hashes without so
much demand on memory? The program manages to loads the large hashes without
problems and as I mentioned previously smaller files have no issues. Any
suggestions folks have are muchly appreciated.
Thanks R

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


###loading file content into hashes

my $filename = '/tmp/test.txt';
open my $fh,,$filename or die $!;

my %hash = map { /(^ABC*.*?)\n(.*)/} $fh;

# get the hash size
my $hash_size = keys %trim5p;

print The hash contains $hash_size elements.\n;

close $fh or die $!;

##intersect subroutine
sub intersection
{
   my ($hasha, $hashb) = @_;
   my %newhash;
   foreach my $key (keys %{$hasha})
   {
  $newhash{$key} = $$hasha{$key} if (exists $$hashb{$key});
   }

  # dont return %newhash just grab size

my $newhash_size = keys %newhash;

print The intersected hash contains $newhash_size elements.\n;

}

differences between hashes

sub in_one_not_in_both
{
#Find keys from one hash that aren't in both

my ($hash3, $hash4) = @_;
my %newhash2;


foreach my $key2 (keys %{$hash3})
{
$newhash2{$key2} = $$hash3{$key2} unless (exists $$hash4{$key2});

}

my $newhash_size2 = keys %newhash2;

print This is the number of unique values: $newhash_size2 \n;

}


How to remove duplicates from a hash

2010-09-29 Thread Richard Green
What would be the quickest , easiest way to remove duplicates from a hash?
Any suggestions are muchly appreciated. Thanks
-Rich




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




Re: How to remove duplicates from a hash

2010-09-29 Thread Richard Green
These are great suggestions thanks everyone!
-Rich



On Sep 29, 2010, at 2:39 AM, Paul Johnson p...@pjcj.net wrote:

 On Tue, Sep 28, 2010 at 08:10:30AM -0700, Richard Green wrote:
 
 What would be the quickest , easiest way to remove duplicates from a hash?
 Any suggestions are muchly appreciated. Thanks
 
 I presume you mean duplicate values, because hashes don't have duplicate keys.
 Do you care which values get removed?  If not, then:
 
   %x = reverse %x; %x = reverse %x;
 
 will do the trick.
 
 That's the quickest and easiest method according to some criteria, anyway.
 
 -- 
 Paul Johnson - p...@pjcj.net
 http://www.pjcj.net
 
 -- 
 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: How to Extract to a flat file thru Excel an Access DB file

2009-05-12 Thread Richard Loveland
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Wagner, David --- Senior Programmer Analyst --- CFS wrote:
   I have an Excel file that I am copying from a Windows server to Linux 
 box(initially). I then extract each worksheet to their own csv. Within the 
 Excel is detail that is contained within a Access DB. I am getting the 
 necessary worksheets out ok, but am unsure how or if one can extract to a 
 csv, the detail data from Access DB.
 
   Any thoughts or how one would research to do this? I am on 5.8.8 
 ActiveState Build 824.
 
  If you have any questions and/or problems, please let me know.
  Thanks.
  
 Wags ;)
 David R. Wagner
 Senior Programmer Analyst
 FedEx Freight
 1.719.484.2097 TEL
 1.719.484.2419 FAX
 1.408.623.5963 Cell
 http://fedex.com/us 
 

Not Perl, but MDB Tools might be of use. From the FAQ:

1.2 What does MDB Tools do
MDB Tools is an open source suite of libraries and utilities to read
(and soon write) MDB database files.

http://mdbtools.sourceforge.net/faq.html

They say it'll run on Linux.

Regards,
Rich Loveland
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFKCXQS4EG8v4hpG/ERAhJKAKCiUcv7YQ8ba5r81FadB37cEi0u4wCeKd7R
tTsNRdT3wKlNasSI6FNvRsE=
=JeS/
-END PGP SIGNATURE-


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




Re: Perl code for comparing two files

2009-05-08 Thread Richard Loveland
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Mr. Adhikary,

The following will take any number of files as arguments, in the format
you described (I even tested it! :-)). It goes through each line of
those files, stuffing (the relevant part of) each line in a 'seen' hash
(more on that, and other, hash techniques here if you're interested:
http://www.perl.com/pub/a/2006/11/02/all-about-hashes.html).

The code below does not keep track of line numbers as you requested, but
I think the hash technique used here could help you as you approach a
solution to your particular problem.


#!/usr/bin/perl

use strict;
use warnings;
use File::Slurp; # This is where 'read_file' lives

my %seen;

for my $arg ( @ARGV ) {
my @lines = read_file( $arg );
for my $line ( @lines ) {
chomp $line;
my @elems = split / /, $line;
my $value = $elems[1];
$seen{$value}++;
}
}

for my $k ( keys %seen ) {
print $k, \n if $seen{$k}  1;
}


Regards,
Rich Loveland


Anirban Adhikary wrote:
 Hi List
 I am writing a perl code which will takes 2 more files as argument. Then It
 will check the the values of each line of a file with respect with another
 file. If some value matches then it will write the value along with line
 number to another ( say outputfile) file.
 
 The source files are as follow
 
 Contents of abc.txt
 1 2325278241,P0
 2 2296250723,MH
 3 2296250724,MH
 4 2325277178,P0
 5 7067023316,WL
 6 7067023329,WL
 7 2296250759,MH
 8 7067023453,WL
 9 7067023455,WL
 10 555413,EA05
 ###
 Contents of xyz.txt
 1 7067023453,WL
 2 31-DEC-27,2O,7038590671
 3 31-DEC-27,2O,7038596464
 4 31-DEC-27,2O,7038596482
 5 2296250724,MH
 6 31-DEC-27,2O,7038597632
 7 31-DEC-27,2O,7038589511
 8 31-DEC-11,2O,7038590671
 9 7067023455,WL
 10 31-DEC-27,2O,7038555744
 ###
 Contents of pqr.txt
 1 2325278241,P0
 2 7067023316,WL
 3 7067023455,WL
 4 2296250724,MH
 
 
 
 
 
 
 For this requirement I have written the following code which works fine for
 2 input files
 
 use strict;
 use warnings;
 
 use Benchmark;
 
 if(@ARGV  2) {
 print Please enter atleast two or more  .orig file names \n;
 exit 0;
 }
 my @file_names = @ARGV;
 chomp(@file_names);
 my @files_to_process;
 
 for(@file_names) {
 if( -s $_){
 print File $_ exists\n;
 push(@files_to_process,$_);
 }
 elsif( -e $_) {
 print File $_ exists but it has zero byte size\n;
 }
 else {
 print File $_ does not exists \n;
 }
 }
 
 my $count = @files_to_process;
 if( $count  2 ) {
 print Atleast 2 .orig files are required to continue this
 program\n;
 exit 0;
 }
 
 my $output_file = outputfile;
 my $value = 0;
 my $start_time = new Benchmark;
 
 
 if( $count = 2 ) {
 while ($count) {
 my ($files_after_processing_pointer,$return_val) =
 create_itermediate_file (\...@files_to_process,$value);
 my @files_after_processing =
 @$files_after_processing_pointer;
 $count = @files_after_processing;
 $value = $return_val;
 @files_to_process = @files_after_processing;
 
 }
 
 my $end_time = new Benchmark;
 my $difference = timediff($end_time, $start_time);
 print It took , timestr($difference),  to execute the program\n;
 
 }
 
 
 
 
 sub create_itermediate_file {
 my $file_pointer = $_[0];
 my $counter = $_[1];
 my @file_content = @$file_pointer;
 
 if($counter == 0) {
 my($first_file,$second_file) = splice
 (@file_content, 0, 2);
 open my $orig_first, , $first_file
 or die could not open $first_file: $!;
 open my $orig_second, , $second_file
 or die could not open $second_file:
 $!;
 open my $output_fh, , $output_file
 or die could not open $output_file:
 $!;
 
 my %content_first;
 while  (my $line = $orig_first) {
 chomp $line;
 if ($line) {
 
 my($line_num,$value) = split( ,$line);
 
 $content_first{$value} = $line_num;
 }
 }
 
 my %content_second;
 while (my $line = $orig_second) {
 chomp $line;
 if ($line) {
 
 my($line_num,$value) = 

Having problems using HTML::HashTable and HTML::TableContentParser together.

2009-04-22 Thread Richard Cross

Hi,

I'm trying to read an HTML table from JBoss's Destination Manager,  
strip out only the table rows I'm interested in and then write it back  
out as an HTML table.  What I've done so far (see Fig 1 below) is to  
grab every table off the page (although there is in fact only one)  
into $tables.  What I'm not sure of is how to turn that into a hash  
that I can then write out with HTML::HashTable.


I printed the contents of $tables via Data::Dumper and got the  
structure you see in Fig 2.  Out of curiosity, I pasted that into my  
code and assigned it to %table, but then the output of print  
tablify... was:


table border=0
tr
   tdHASH(0x199ca080)/td
   td/td
/tr
/table

Presumably because it couldn't read down into the data structure I  
gave it.


I'm at a bit of a loss as to how to convert one structure into the  
other as I'm finding the dereferencing syntax for anonymous data  
structures somewhat impenetrable. Can anyone help?


Regards,

Richard Cross.

-- Fig 1 - My code so far (I've cut out a whole load of extra CGI  
stuff that isn't relevant to my question) --

#!/usr/bin/perl -w
use strict;
use CGI qw(:standard escapeHTML);
use HTML::HashTable;
use HTML::TableContentParser;

my $HTML = `./twiddle.sh invoke jboss.mq:service=DestinationManager  
listMessageCounter`;

my $parser = HTML::TableContentParser-new;
my $tables = $parser-parse($HTML);

#
#  I need to get the first element of $tables and somehow turn it into  
a hash (%table) that I can use below

#

print tablify({
   BORDER  = 0,
   DATA= \%table,
   SORTBY  = 'key',
   ORDER   = 'desc'}
   );


-- Fig 2 - what is returned from HTML::TableContentParser --$VAR1 = [
{
   'width' = '100%',
   'cellspacing' = '1',
   'cellpadding' = '1',
   'border' = '1',
   'headers' = [ { 'data' = 'Type' },
  { 'data' = 'Name' },
  { 'data' = 'Subscription' },
  { 'data' = 'Durable' },
  { 'data' = 'Count' },
  { 'data' = 'CountDelta' },
  { 'data' = 'Depth' },
  { 'data' = 'DepthDelta' },
  { 'data' = 'Last Add' }
],
   'rows' = [ {},
   { 'cells' = [ { 'rowspan' = '1', 'data' =  
'Queue' },
  { 'rowspan' = '1', 'data' =  
'A' },

  { 'data' = '-' },
  { 'data' = '-' },
  { 'data' = '0' },
  { 'data' = '-' },
  { 'data' = '0' },
  { 'data' = '-' },
  { 'data' = '-' }
],
 'bgcolor' = '#F0F0F0'
   },
   { 'cells' = [ { 'rowspan' = '1', 'data' =  
'Queue' },
  { 'rowspan' = '1', 'data' =  
'B' },

  { 'data' = '-' },
  { 'data' = '-' },
  { 'data' = '0' },
  { 'data' = '-' },
  { 'data' = '0' },
  { 'data' = '-' },
  { 'data' = '-' }
],
 'bgcolor' = '#FF'
   },

 ],
}
];

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




Programming Perl vs perldoc

2009-04-08 Thread Richard Hobson
So, I've done the Learning Perl book, and frustrating myself no end by
trying to write a chess program using just the knowledge contained in
Learning Perl and with no modules.

I thought about getting Intermediate Perl, but I've heard that
Programming Perl is the best next step.

But, what's the advantage of Programming Perl when we have perldoc?
What does the book give me that perldoc does not?

Thanks,
Richard

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




Re: Programming Perl vs perldoc

2009-04-08 Thread Richard Hobson
Thanks Brian

On Wed, 08 Apr 2009 08:37 -0400, Brian J. Miller
 Interesting first choice, but okay... Was there something in particular
 that you are getting hung up on?

Yeah, the complexity of chess! I'm getting there. I do miss a lot of
short-cuts and efficiencies in Perl that would help, but this is a good
exercise to learn Perl.

 I'd disagree and say that your suggestion of Intermediate Perl would be
 the next best step.
 
 A book. Some descriptions are more in depth and having a book with a
 table of contents and index can often be easier to reference when you
 don't know *where* to look in perldoc, but much of the information will
 be the same. (Any number of sites and Google can help with where to
 look.)

OK. I'll probably stick with perldoc and get Intermediate Perl. I'm
currently unemployed, so I can't really fork out for both books right
now - but I have plenty of time to learn Perl!

Cheers,
Richard

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




Re: Programming Perl vs perldoc

2009-04-08 Thread Richard Hobson
Thanks Brian

On Wed, 08 Apr 2009 08:37 -0400, Brian J. Miller
 Interesting first choice, but okay... Was there something in particular
 that you are getting hung up on?

Yeah, the complexity of chess! I'm getting there. I do miss a lot of
short-cuts and efficiencies in Perl that would help, but this is a good
exercise to learn Perl.

 I'd disagree and say that your suggestion of Intermediate Perl would be
 the next best step.
 
 A book. Some descriptions are more in depth and having a book with a
 table of contents and index can often be easier to reference when you
 don't know *where* to look in perldoc, but much of the information will
 be the same. (Any number of sites and Google can help with where to
 look.)

OK. I'll probably stick with perldoc and get Intermediate Perl. I'm
currently unemployed, so I can't really fork out for both books right
now - but I have plenty of time to learn Perl!

Cheers,
Richard

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




Re: Programming Perl vs perldoc

2009-04-08 Thread Richard Hobson
On Wed, 08 Apr 2009 08:50 -0400, Chas. Owens chas.ow...@gmail.com
wrote:
 Your whois information suggests that you live in the US; I am always
 amazed to hear fellow Americans say things like that book is
 expensive.  We have a wonderful lending library system in this
 country, use it.  If your local library does not have the 3rd Edition
 of Programming Perl, then ask you librarian to order a copy.  They
 generally want to help you.

Actually, no. I live in the UK, and price for O'Reilly books here are
significantly higher. Amazon US has Intermediate Perl new for $25,
while Amazon UK has it for $46, excluding postage. Also, I live in a
relatively small town in Scotland, whose libraries tend not to purchase
obscure programming books on demand.

Look, I'm not after pity. I just want advice about which book to get.

Cheers,
Richard

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




Pattern matching question

2009-04-01 Thread Richard Hobson
Hi,

Please be patient with this beginner. I have a subrouting as follows,
that prints out an ASCII representation of chess board

sub display_board {
foreach (0..7) {
my $ref = @_[$_];
foreach (0..7) {
my $piece = $ref-[$_];
$piece =~ /.*(..$)/;
print $pieces{$1};
}
print \n;
}
};

It works, but is there a way of combining these lines:

my $piece = $ref-[$_];
$piece =~ /.*(..$)/;

It feels like this could be done in one step. Is this correct? I'm
finding that I'm doing alright in Perl, but I sense the Perl urge to do
things in as few a number of steps as possible.

Thanks,
Richard Hobson

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




Re: AW: Pattern matching question

2009-04-01 Thread Richard Hobson
Firstly, apologies for the double posting of this question. 

On Wed, 01 Apr 2009 09:49 +0200, Thomas Bätzler
t.baetz...@bringe.com wrote:
 How about (untested):
 
 sub display_board {
   foreach my $ref (@_){
 foreach my $piece ( @$ref ){
   print substr( $piece, -2);
 }
   }
 }
 
 The Perl way is to avoid using indices where you don't need them.

Yes, that works fine, and is far more elegant.

Thanks,
Richard

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




Pattern matching question

2009-03-31 Thread Richard Hobson
Hi,

Please be patient with this beginner. I have a subrouting as follows,
that prints out an ASCII representation of chess board

sub display_board {
foreach (0..7) {
my $ref = @_[$_];
foreach (0..7) {
my $piece = $ref-[$_];
$piece =~ /.*(..$)/;
print $pieces{$1};
}
print \n;
}
};

It works, but is there a way of combining these lines:

my $piece = $ref-[$_];
$piece =~ /.*(..$)/;

It feels like this could be done in one step. Is this correct? I'm
finding that I'm doing alright in Perl, but I sense the Perl urge to do
things in as few a number of steps as possible.

Thanks,
Richard Hobson

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




using (dot) . with printf does not incur the results I expected

2008-12-24 Thread Richard

what's wrong w/ this ?
I used (dot) . to indicate the maximum length of each element in the 
print but last one does not print out in alinged format..

What am i missing?

use warnings;
use strict;

my $yabal = 'truncated';
my $never = 'sai';
my $noway = 'han1';

my %never = qw(hi how are you today fine i am good and everyday12345678 
never12345689);


printf %-.5s %-15s %-.2s\n, $yabal, $never, $noway;

for (keys %never) {
 printf %.2s %.10s\n, $_, $never{$_};
}

r...@myserver ~ ././yahoo2
trunc sai ha
hi how
go and
to fine
ar you
ev never12345
i am


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




Re: using (dot) . with printf does not incur the results I expected

2008-12-24 Thread Richard

Richard wrote:

what's wrong w/ this ?
I used (dot) . to indicate the maximum length of each element in the 
print but last one does not print out in alinged format..

What am i missing?

use warnings;
use strict;

my $yabal = 'truncated';
my $never = 'sai';
my $noway = 'han1';

my %never = qw(hi how are you today fine i am good and 
everyday12345678 never12345689);


printf %-.5s %-15s %-.2s\n, $yabal, $never, $noway;

for (keys %never) {
 printf %.2s %.10s\n, $_, $never{$_};
}

r...@myserver ~ ././yahoo2
trunc sai ha
hi how
go and
to fine
ar you
ev never12345
i am



I guess I was missing the 2.2 option...


my %never = qw(hi how are you today fine i am good and everyday12345678 
never12345689);



for (keys %never) {
 printf %-2.2s %-10.10s\n, $_, $never{$_};
}

r...@myserver  ~ ./!$
././yahoo2
trunc sai ha
hi how  
go and  
to fine 
ar you  
ev never12345
i  am 




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




Re: counter program by using closure

2008-12-15 Thread Richard

John W. Krahn wrote:

Richard wrote:

John W. Krahn wrote:


You want something more like this:

sub counter {
my $count;
my $clear = `clear`;
my $counting = 'EOF';
%s
|   Counting...|
|   %2d |
|  |

EOF
return sub { local $| = 1; printf $counting, $clear, ++$count }
}

my $yeah = counter();
for ( 1 .. 35 ) {
sleep 1;
$yeah-();
}


this is interesting and this also works well.
My question is, how does perl know in this instance that %2d is 
refering to $count.. is it because $clear contains none numeric value 
or because $count contains numeric value?


$counting contains the format string for printf() and the first 
argument $clear is substituted for '%s' in $counting and the second 
argument ++$count is substituted for '%2d' in $counting.  They are 
substituted in the same order as they appear.





John
Just curious, in programming in general, is it possible to do other 
things while counting is going on?

Is this possible in perl?

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




Re: counter program by using closure

2008-12-15 Thread Richard

David Schmidt wrote:

You might want to look into fork
http://perldoc.perl.org/functions/fork.html
http://www.tutorialspoint.com/perl/perl_fork.htm

  


thank you..
I was experimenting w/ forks but I definitely need to understand forking 
better.


thanks!!


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




Re: counter program by using closure

2008-12-14 Thread Richard

John W. Krahn wrote:

Richard wrote:


wanted to draw a box that's counting up for certain time.
Thought I could use the counter but it just displays the box not the 
number..


can anyone point it out for me?

thank you.

use warnings;
use strict;

sub counter {
  my $count;
  my $counting = EOF;

|   
Counting... 
|
|   
++$count;   
|
|
  |


EOF

  return sub { print $counting }
}

my $real_count;
my $yeah = counter();
while ($real_count  '35') {
 ++$real_count;
 system('clear');
 $yeah-();
}


You want something more like this:

sub counter {
my $count;
my $clear = `clear`;
my $counting = 'EOF';
%s
|   Counting...|
|   %2d |
|  |

EOF
return sub { local $| = 1; printf $counting, $clear, ++$count }
}

my $yeah = counter();
for ( 1 .. 35 ) {
sleep 1;
$yeah-();
}



John

this is interesting and this also works well.
My question is, how does perl know in this instance that %2d is refering 
to $count.. is it because $clear contains none numeric value or because 
$count contains numeric value?




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




Re: counter program by using closure

2008-12-14 Thread Richard

John W. Krahn wrote:

Richard wrote:

John W. Krahn wrote:


You want something more like this:

sub counter {
my $count;
my $clear = `clear`;
my $counting = 'EOF';
%s
|   Counting...|
|   %2d |
|  |

EOF
return sub { local $| = 1; printf $counting, $clear, ++$count }
}

my $yeah = counter();
for ( 1 .. 35 ) {
sleep 1;
$yeah-();
}


this is interesting and this also works well.
My question is, how does perl know in this instance that %2d is 
refering to $count.. is it because $clear contains none numeric value 
or because $count contains numeric value?


$counting contains the format string for printf() and the first 
argument $clear is substituted for '%s' in $counting and the second 
argument ++$count is substituted for '%2d' in $counting.  They are 
substituted in the same order as they appear.





John
ah.. did not see the %s... and now that makes perfect sense.. thank you 
very much!!!


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




counter program by using closure

2008-12-13 Thread Richard


wanted to draw a box that's counting up for certain time.
Thought I could use the counter but it just displays the box not the 
number..


can anyone point it out for me?

thank you.

use warnings;
use strict;

sub counter {
  my $count;
  my $counting = EOF;

|   Counting... 
|
|   ++$count;   
|
|
  |


EOF

  return sub { print $counting }
}

my $real_count;
my $yeah = counter();
while ($real_count  '35') {
 ++$real_count;
 system('clear');
 $yeah-();
}

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




Re: counter program by using closure

2008-12-13 Thread Richard

Richard wrote:


wanted to draw a box that's counting up for certain time.
Thought I could use the counter but it just displays the box not the 
number..


can anyone point it out for me?

thank you.

use warnings;
use strict;

sub counter {
  my $count;
  my $counting = EOF;

|   
Counting... 
|
|   
++$count;   
|
|
  |


EOF

  return sub { print $counting }
}

my $real_count;
my $yeah = counter();
while ($real_count  '35') {
 ++$real_count;
 system('clear');
 $yeah-();
}

I am assuming this will not work because above clousure will have no 
access to variable $count.. trying to see if there is another way to get 
this done.


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




Re: counter program by using closure

2008-12-13 Thread Richard

Chas. Owens wrote:

On Sat, Dec 13, 2008 at 21:13, Richard rich.j...@gmail.com wrote:
  

wanted to draw a box that's counting up for certain time.
Thought I could use the counter but it just displays the box not the
number..

can anyone point it out for me?

thank you.

use warnings;
use strict;

sub counter {
 my $count;
 my $counting = EOF;

|   Counting...
   |
|   ++$count;
   |
|
   |

EOF

 return sub { print $counting }
}

my $real_count;
my $yeah = counter();
while ($real_count  '35') {
++$real_count;
system('clear');
$yeah-();
}



The problem is that

my $counting = EOF;

|   Counting...
|
|   ++$count;
|
|
|

EOF

only gets executed once.  Move it into your closure to execute it each
time the closure is called:O

#!/usr/bin/perl

use warnings;
use strict;

sub counter {
my $count;
return sub {
my $l = (length ++$count)/2 - .5;
my $r = (length $count)/2;
print
= x 52, \n,
|,   x 20, Counting...,   x 19, |\n,
|,   x (25 - $l), $count,   x (25 - $r), |\n,
|,   x 50, |\n,
= x 52, \n;
}
}

my $yeah = counter();
for (1 .. 35) {
system('clear');
$yeah-();
select undef, undef, undef, .25;
}


  

WOW..

thank you and I will study this code now... this is great!!



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




Re: not understanding pipe command

2008-12-07 Thread Richard

Raymond Wan wrote:


Hi Richard,

I'm no Perl expert, but it sounds like you are right.


Richard wrote:

still confused by after running this program.

1)when I do  #print while READER; program only outputs

  -- truly done



Let's comment out fibonacci, first.  Then, my understanding of your 
posted code is that the child process calculates the factorial and 
sends it to the parent process.  So, if you comment out print while 
READER, then the parent process doesn't read what it received, and 
just prints the single line.





2)here is my attempt for understanding of this program.

  The parent process keeps one filehandle and closes the other, 
while the child does the opposite. The parent
and child process can now communicate acorss the pipe as they 
work in parallel


   because above statement from the book network programming with 
perl...   my thought is
  a)child choose to write to filehandle WRITER and have all the 
output from factorial and fibonacci go there
  b)parent choose to close WRITER and read from READER because that's 
what it will be able to read since READER will have

 all the output from child's filehandle WRITER

am I interpreting this right way? or close?




Yes, that sounds right.  Again,  ignoring fibonacci, the child does 
the calculations and sends what it has to the parent and exits.  The 
parent reads it in, prints it out using the line mentioned above, and 
finishes it with a truly done message.


That line print while READER is doing two important functions in 
the parent...reading from the input pipe, and then immediately writing 
it out.


So, yes, I don't see anything wrong with what you said.  Oh, just one 
thing -- with the two forks,  there are actually two children if you 
bring fibonacci in.  This phrase helped me -- it is from the 
definition of pipe in the Programming Perl book (i.e., the camel book):


This call [pipe] is almost always used right before a fork, after 
which the pipe's reader should close WRITEHANDLE, and the writer close 
READHANDLE.  (Otherwise the pipe won't indicate EOF to the reader when 
the writer closes it.)  Note that if you set up a loop of piped 
processes, deadlock can occur unless you are very careful.  In 
addition, note that Perl's pipes use standard I/O buffering, so you 
may need to set $| on your WRITEHANDLE to flush after each output 
command, ...


Hope this helps...

Ray





my $arg = shift || '10';

pipe(READER,WRITER) or die Can't open pipe: $!\n;
### pipe(READHANDLE, WRITEHANDLE)  --- first is the name of a 
filehandle to read from
### 2nd is filehandle to write 
to. IF successful, pipe() returns a true result code


if ( fork == '0' ) {
close READER;
select WRITER; $| = '1';
factorial($arg);
print done with factorial\n;
exit 0;
}

if ( fork == '0' ) {
close READER;
select WRITER; $| = '1';
my $result = fibonacci($arg);
print done with fibonacci here\n;
exit 0;
}

#parent process closes WRITER and reads from READER
close WRITER;
print while READER;
print truly done\n;


sub factorial {
  my $target = shift;
  for ( my $result = 1, my $i = 1; $i = $target; $i++ ) {
print hi\n;
   print factorial($i) = , $result *= $i, \n;
  }
}

sub fibonacci {
  my $target = shift;
  my ($a,$b) = (1,0);
  for ( my $i = 1; $i = $target; $i++ ) {
 my $c = $a + $b;
 print hi2\n;
 print fibonacci($i) = $c\n;
 ($a,$b) = ($b,$c);
  }
}







thank you!!

I will explorer this bit further and post a code based on it..

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




Re: not understanding pipe command

2008-12-06 Thread Richard

Raymond Wan wrote:


Hi Richard,


Richard wrote:
while going over below example from the book, I am not understanding 
why/how below program works.

Can someone explain this to me in better way?



Have you tried running it?  does it work like what you expect?



what is READER exactly reading from???
and what does print while READER do ? I thought all the print 
happens during factorial() and fibonacci()



It is reading from a pipe.  Take a look at the description of the pipe 
function and what a pipe is (in case you do not know):


http://perldoc.perl.org/functions/pipe.html
http://en.wikipedia.org/wiki/Pipe_(Unix)

 is reading in from the pipe.  Take a look at:

http://perldoc.perl.org/perlop.html#I%2fO-Operators

So, while there is something left to read, print it out.  Go to the 
above link and look at the first shaded box which says The following 
lines are equivalent.  The line that is confusing you is the last 
line in that box...as it says, all of those are equivalent.


to answer your last question, then, the answer is no, the printing 
isn't happening in just the two functions you named.  Take a look at 
the above links to see if they give you a clue.  And if you are 
wondering what the select line is doing, take a look at this and look 
up the select function:


http://perldoc.perl.org/perlfaq5.html#How-do-I-flush%2funbuffer-an-output-filehandle%3f--Why-must-I-do-this%3f 



And BTW, you can cut out almost half of the code if you just focus on 
either fibonacci or factorial and not both.


Ray




ok so here is my attempt to understand this,

serverX  ~/.sc/net ./pipe.pl 2
hi2
fibonacci(1) = 1
hi2
fibonacci(2) = 1
done with fibonacci here
hi
factorial(1) = 1
hi
factorial(2) = 2
done with factorial
truly done

serverX ~/.sc/net ./pipe.pl 10
hi
factorial(1) = 1
hi
factorial(2) = 2
hi
factorial(3) = 6
hi
factorial(4) = 24
hi
factorial(5) = 120
hi
factorial(6) = 720
hi
factorial(7) = 5040
hi
factorial(8) = 40320
hi
factorial(9) = 362880
hi
factorial(10) = 3628800
done with factorial
hi2
fibonacci(1) = 1
hi2
fibonacci(2) = 1
hi2
fibonacci(3) = 2
hi2
fibonacci(4) = 3
hi2
fibonacci(5) = 5
hi2
fibonacci(6) = 8
hi2
fibonacci(7) = 13
hi2
fibonacci(8) = 21
hi2
fibonacci(9) = 34
hi2
fibonacci(10) = 55
done with fibonacci here
truly done

still confused by after running this program.

1)when I do  #print while READER; program only outputs

  -- truly done

2)here is my attempt for understanding of this program.

  The parent process keeps one filehandle and closes the other, while 
the child does the opposite. The parent
and child process can now communicate acorss the pipe as they work 
in parallel


   because above statement from the book network programming with 
perl...   my thought is
  a)child choose to write to filehandle WRITER and have all the output 
from factorial and fibonacci go there
  b)parent choose to close WRITER and read from READER because that's 
what it will be able to read since READER will have

 all the output from child's filehandle WRITER

am I interpreting this right way? or close?

my $arg = shift || '10';

pipe(READER,WRITER) or die Can't open pipe: $!\n;
### pipe(READHANDLE, WRITEHANDLE)  --- first is the name of a 
filehandle to read from
### 2nd is filehandle to write to. 
IF successful, pipe() returns a true result code


if ( fork == '0' ) {
close READER;
select WRITER; $| = '1';
factorial($arg);
print done with factorial\n;
exit 0;
}

if ( fork == '0' ) {
close READER;
select WRITER; $| = '1';
my $result = fibonacci($arg);
print done with fibonacci here\n;
exit 0;
}

#parent process closes WRITER and reads from READER
close WRITER;
print while READER;
print truly done\n;


sub factorial {
  my $target = shift;
  for ( my $result = 1, my $i = 1; $i = $target; $i++ ) {
print hi\n;
   print factorial($i) = , $result *= $i, \n;
  }
}

sub fibonacci {
  my $target = shift;
  my ($a,$b) = (1,0);
  for ( my $i = 1; $i = $target; $i++ ) {
 my $c = $a + $b;
 print hi2\n;
 print fibonacci($i) = $c\n;
 ($a,$b) = ($b,$c);
  }
}



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




Re: not understanding pipe command

2008-12-04 Thread Richard

Raymond Wan wrote:


Hi Richard,


Richard wrote:
while going over below example from the book, I am not understanding 
why/how below program works.

Can someone explain this to me in better way?



Have you tried running it?  does it work like what you expect?



what is READER exactly reading from???
and what does print while READER do ? I thought all the print 
happens during factorial() and fibonacci()



It is reading from a pipe.  Take a look at the description of the pipe 
function and what a pipe is (in case you do not know):


http://perldoc.perl.org/functions/pipe.html
http://en.wikipedia.org/wiki/Pipe_(Unix)

 is reading in from the pipe.  Take a look at:

http://perldoc.perl.org/perlop.html#I%2fO-Operators

So, while there is something left to read, print it out.  Go to the 
above link and look at the first shaded box which says The following 
lines are equivalent.  The line that is confusing you is the last 
line in that box...as it says, all of those are equivalent.


to answer your last question, then, the answer is no, the printing 
isn't happening in just the two functions you named.  Take a look at 
the above links to see if they give you a clue.  And if you are 
wondering what the select line is doing, take a look at this and look 
up the select function:


http://perldoc.perl.org/perlfaq5.html#How-do-I-flush%2funbuffer-an-output-filehandle%3f--Why-must-I-do-this%3f 



And BTW, you can cut out almost half of the code if you just focus on 
either fibonacci or factorial and not both.


Ray




thanks!! I will study them and let you know.


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




not understanding pipe command

2008-12-03 Thread Richard
while going over below example from the book, I am not understanding 
why/how below program works.

Can someone explain this to me in better way?

what is READER exactly reading from???
and what does print while READER do ? I thought all the print happens 
during factorial() and fibonacci()


use warnings;
use strict;

my $arg = shift || '10';

pipe(READER,WRITER) or die Can't open pipe: $!\n;
### pipe(READHANDLE, WRITEHANDLE)  --- first is the name of a 
filehandle to read from
### 2nd is filehandle to write to. 
IF successful, pipe() returns a true result code


if ( fork == '0' ) {
close READER;
select WRITER; $| = '1';
factorial($arg);
exit 0;
}

if ( fork == '0' ) {
close READER;
select WRITER; $| = '1';
my $result = fibonacci($arg);
exit 0;
}

#parent process closes WRITER and reads from READER
close WRITER;
print while READER;

sub factorial {
  my $target = shift;
  for ( my $result = 1, my $i = 1; $i = $target; $i++ ) {
   print factorial($i) = , $result *= $i, \n;
  }
}

sub fibonacci {
  my $target = shift;
  my ($a,$b) = (1,0);
  for ( my $i = 1; $i = $target; $i++ ) {
 my $c = $a + $b;
 print fibonacci($i) = $c\n;
 ($a,$b) = ($b,$c);
  }
}

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




capturing output from c program inside of perl

2008-11-29 Thread Richard

i have a c program that I want to just run from perl and capture output

I would have thought

my $c_output = `/tmp/c_program`;

would get me the output of the program.

instead it prints out to the screen.

Is there way to redirect STOUT just for that line and put it back to normal?

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





Re: capturing output from c program inside of perl

2008-11-29 Thread Richard

Chas. Owens wrote:

On Sun, Nov 30, 2008 at 00:52, Richard [EMAIL PROTECTED] wrote:
  

i have a c program that I want to just run from perl and capture output

I would have thought

my $c_output = `/tmp/c_program`;

would get me the output of the program.

instead it prints out to the screen.

Is there way to redirect STOUT just for that line and put it back to normal?



Take a closer look at your c program.  Backticks do indeed grab
stdout, so it must be writing to stderr (or worse yet the console).
Try

my $c_output = `/tmp/c_program 21`;


  

thank you guys!!

21 fixed the issue..

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




Re: removing files in unix or linux using glob?

2008-11-21 Thread Richard Lee

Mr. Shawn H. Corey wrote:


See:
  * perldoc -f unlink
  * perldoc -f glob
  * perldoc perlfunc and search for -f under Alphabetical Listing
of Perl Functions

  

one more question on this.. suppose there is 3 files in temp directory

/tmp/yahoo1
/tmp/yahoo2
/tmp/yahoo3

and I wanted to take the last file that was created.. would this work?

my $filename = shift;

my @file_1 = /tmp/$filename*;
my $file_1 = $file_1[-1];
push @files, $file_1;

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




Re: removing files in unix or linux using glob?

2008-11-21 Thread Richard Lee

Richard Lee wrote:

stion on this.. suppose there is 3 files in temp directory

/tmp/yahoo1
/tmp/yahoo2
/tmp/yahoo3

and I wanted to take the last file that was created.. would this work?

my $filename = shift;

my @file_1 = /tmp/$filename*;
my $file_1 = $file_1[-1];
push @files, $file_1;

i am not 100% sure if this logic is correct or not.

my %big_hash;
for (@file_1) {
$big_hash{$_} = (stat($_))[9];
}

my @keys = sort { $big_hash{$a} = $big_hash{b} } keys %big_hash;
my $file_1 = $keys[-1];
push @files, $file_1;


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




Re: removing files in unix or linux using glob?

2008-11-21 Thread Richard Lee

Richard Lee wrote:

Richard Lee wrote:

stion on this.. suppose there is 3 files in temp directory

/tmp/yahoo1
/tmp/yahoo2
/tmp/yahoo3

and I wanted to take the last file that was created.. would this work?

my $filename = shift;

my @file_1 = /tmp/$filename*;
my $file_1 = $file_1[-1];
push @files, $file_1;

i am not 100% sure if this logic is correct or not.

my %big_hash;
for (@file_1) {
$big_hash{$_} = (stat($_))[9];
}

my @keys = sort { $big_hash{$a} = $big_hash{b} } keys %big_hash;
my $file_1 = $keys[-1];
push @files, $file_1;

I am missing a $ in $big_hash{b}.. and i see that this method works.. 
but is there easier and better(correct) way to do this ???


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




removing files in unix or linux using glob?

2008-11-19 Thread Richard Lee

I thought I could do this,

if ( -f q#/tmp/yahoo.* ) {
   system(rm -rf /tmp/yahoo.*);
}

what am i missing?

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




Re: removing files in unix or linux using glob?

2008-11-19 Thread Richard Lee

Mr. Shawn H. Corey wrote:

for my $file ( glob( '/tmp/yahoo.*' ) ){
  unlink $file if -f $file;
}

See:
  * perldoc -f unlink
  * perldoc -f glob
  * perldoc perlfunc and search for -f under Alphabetical Listing
of Perl Functions

  

thanks, I tried

if ( -f q#/tmp/text.*# ) {
system(rm -rf /tmp/text.*);
}

and worked.. but above seems better.. i will read up on those as well.. 
thank you.


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




what was the name of the new perl site which had link to all perl journal published.. in pdf format somebody posted that before here

2008-11-19 Thread Richard Lee

please help..

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




Re: what was the name of the new perl site which had link to all perl journal published.. in pdf format somebody posted that before here

2008-11-19 Thread Richard Lee

Chas. Owens wrote:

On Wed, Nov 19, 2008 at 22:48, Richard Lee [EMAIL PROTECTED] wrote:
  

please help..



Is this what you are looking for?

http://www.perlmonks.org/index.pl?node_id=711609


  

exactly!! thank you

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




can you substitute equation with variable?

2008-11-17 Thread Richard Lee

say something like == or eq ..

Can you sub them w/ varilable like $unknown   ?

Let me be more specific.
Let's say I don't know what the variable will hold

I guess I can say something like,

sub check_unknown {
   my $unknown = shift;   ## it's either digits or letters but both 
will be same kind

   my $unknown1 = shift; ## it's either digits or letters

  my $result = ( $unknown =~ /^\d+$/ ) ? '==' : 'eq';
   if ( $unknown $result $unknown1 ) {
do something...
   }
}

But obviously above dont work.. can someone shed some light on this?

thanks!

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




Re: can you substitute equation with variable?

2008-11-17 Thread Richard Lee

Chas. Owens wrote:

On Mon, Nov 17, 2008 at 04:47, Richard Lee [EMAIL PROTECTED] wrote:
  
but it would only be worthwhile if you used $compare a lot after

setting it once; otherwise it would be simpler to just say

sub compare {
my ($x, $y) = @_;

return looks_like_number($x) ? $x == $y : $x eq $y;
}

  

Hi Chas,

This is nice and I will try it...

but I am suprised that perl doesn't have neutral equator..

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




Re: xml question for xml::twig

2008-11-16 Thread Richard Lee

Richard Lee wrote:

Richard Lee wrote:

Chas. Owens wrote:

my $sabal = new XML::Twig(
 twig_roots  = {
'foo/yahoo'  =
#'[EMAIL PROTECTED]kingtony]'   =
sub {
   my ($yabal, $element ) = @_;
   if ( 
$element-first_child('bayking_list')-first_child('bayking')-att('id') 
eq 'kingtony' ) {

  $element-print;
}
 }
 }
);

I think I made a mistake .. this is now working...

yahoo 
V=bazbay_idvalue1000/valuefactyes/fact/bay_idbay_seenvalue50/valuefactno/fact/bay_seenbay_overall 
value=disabled/bayking_listbayking active=true 
country=Russia id=kingtonybayking type=dictator/bay_usage 
value=none/bayking_originbayking_origin_name emmigrate=no 
value=ohio_usaeconomy_status_previous value=very 
poor//bayking_origin_name/bayking_origin/bayking/bayking_listbayqueen_listbayqueen 
active=true country=japan id=queensarahbayqueen 
type=dictator/bay_usage 
value=none/bayqueen_originbayqueen_origin_name emmigrate=no 
value=ca_usa/economy_status_previous value=very poor/previous 
marriage=no//bayqueen_origin/bayqueen/bayqueen_list/yahoo


Now, I just need good way to put this into hash of hash referernce.
ok so I gave up putting them into has of hash reference because of 
unpredictableness of items..
I can get to all values by doing manual $_-first_child method... but my 
problem is, I don't know how to extract all information on

yahoo V=baz.  how do I get that?  $_-parent?

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




Re: xml question for xml::twig

2008-11-16 Thread Richard Lee

Richard Lee wrote:


I think I made a mistake .. this is now working...

yahoo 
V=bazbay_idvalue1000/valuefactyes/fact/bay_idbay_seenvalue50/valuefactno/fact/bay_seenbay_overall 
value=disabled/bayking_listbayking active=true 
country=Russia id=kingtonybayking type=dictator/bay_usage 
value=none/bayking_originbayking_origin_name emmigrate=no 
value=ohio_usaeconomy_status_previous value=very 
poor//bayking_origin_name/bayking_origin/bayking/bayking_listbayqueen_listbayqueen 
active=true country=japan id=queensarahbayqueen 
type=dictator/bay_usage 
value=none/bayqueen_originbayqueen_origin_name emmigrate=no 
value=ca_usa/economy_status_previous value=very poor/previous 
marriage=no//bayqueen_origin/bayqueen/bayqueen_list/yahoo


Now, I just need good way to put this into hash of hash referernce.
ok so I gave up putting them into has of hash reference because of 
unpredictableness of items..
I can get to all values by doing manual $_-first_child method... but 
my problem is, I don't know how to extract all information on

yahoo V=baz.  how do I get that?  $_-parent?
After I extracted above xml, what is the proper way to loop through them 
and extract all information?

I like my final output to look like,

yahoo V: baz
bay_id value: 1000 fact: yes
bay_seen value: 50 fact: no
.. and so on



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




xml question for xml::twig

2008-11-15 Thread Richard Lee


Hello, I am praticing below XML file.
Based on where I find att id for bayking id 'kingtony' , I wanted to 
print out the entire element/att(and ID) and any text found from

yahoo to /yahoo.(exception of bayqueen_list and its descendatns)...

I am having problem just print out one value... can someone take a look 
please?


use strict;
use warnings;

use XML::Twig;

my $xml = XML;
foo
 yahoo V=bay
 bay_id
 value1/value
 factyes/fact
 /bay_id
 bay_seen
 value10/value
 factno/fact
 /bay_seen
 bay_overall value=disabled/
 bayking_list
 bayking id=kingjames country=usa active=true
 bayking type=dictator/
 bay_usage value=none/
 bayking_origin
   bayking_origin_name value=ohio_usa 
emmigrate=no

  economy_status_previous value=very poor /
   /bayking_origin_name
 /bayking_origin
 /bayking
 /bayking_list
 bayqueen_list
 bayqueen id=queenliz country=france active=true
 bayqueen type=dictator/
 bay_usage value=none/
 bayqueen_origin
   bayqueen_origin_name value=ohio_usa 
emmigrate=no/

 /bayqueen_origin
 /bayqueen
 /bayqueen_list
 /yahoo
 yahoo V=baz
 bay_id
 value1000/value
 factyes/fact
 /bay_id
 bay_seen
 value50/value
 factno/fact
 /bay_seen
 bay_overall value=disabled/
 bayking_list
 bayking id=kingtony country=Russia active=true
 bayking type=dictator/
 bay_usage value=none/
 bayking_origin
   bayking_origin_name value=ohio_usa 
emmigrate=no

  economy_status_previous value=very poor /
   /bayking_origin_name
 /bayking_origin
 /bayking
 /bayking_list
 bayqueen_list
 bayqueen id=queensarah country=japan active=true
 bayqueen type=dictator/
 bay_usage value=none/
 bayqueen_origin
   bayqueen_origin_name value=ca_usa 
emmigrate=no/

  economy_status_previous value=very poor /
  previous marriage=no/
 /bayqueen_origin
 /bayqueen
 /bayqueen_list
 /yahoo
/foo
XML

my $t = XML::Twig-new
(
 twig_handlers = { '/foo/yahoo/bayking_list' = sub
 {
my @bay = $_-children('bayking');
foreach my $bay1 ( @bay ) {
if ( $bay1-att('id' eq 'kingtony' ) ) {
  # print all elements/att/value and text from yahoo to 
/yahoo which contains kingtony but do NOT print out bayqueen

print $bay1-att('id') . \n;
}
}
  }
  }
);

$t-parse ($xml);

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




Re: xml question for xml::twig

2008-11-15 Thread Richard Lee



use strict;
use warnings;

use XML::Twig;

my $xml = XML;
foo
 yahoo V=bay
 bay_id
 value1/value
 factyes/fact
 /bay_id
 bay_seen
 value10/value
 factno/fact
 /bay_seen
 bay_overall value=disabled/
 bayking_list
 bayking id=kingjames country=usa active=true
 bayking type=dictator/
 bay_usage value=none/
 bayking_origin
   bayking_origin_name value=ohio_usa 
emmigrate=no

  economy_status_previous value=very poor /
   /bayking_origin_name
 /bayking_origin
 /bayking
 /bayking_list
 bayqueen_list
 bayqueen id=queenliz country=france active=true
 bayqueen type=dictator/
 bay_usage value=none/
 bayqueen_origin
   bayqueen_origin_name value=ohio_usa 
emmigrate=no/

 /bayqueen_origin
 /bayqueen
 /bayqueen_list
 /yahoo
 yahoo V=baz
 bay_id
 value1000/value
 factyes/fact
 /bay_id
 bay_seen
 value50/value
 factno/fact
 /bay_seen
 bay_overall value=disabled/
 bayking_list
 bayking id=kingtony country=Russia active=true
 bayking type=dictator/
 bay_usage value=none/
 bayking_origin
   bayking_origin_name value=ohio_usa 
emmigrate=no

  economy_status_previous value=very poor /
   /bayking_origin_name
 /bayking_origin
 /bayking
 /bayking_list
 bayqueen_list
 bayqueen id=queensarah country=japan active=true
 bayqueen type=dictator/
 bay_usage value=none/
 bayqueen_origin
   bayqueen_origin_name value=ca_usa 
emmigrate=no/

  economy_status_previous value=very poor /
  previous marriage=no/
 /bayqueen_origin
 /bayqueen
 /bayqueen_list
 /yahoo
/foo
XML

my $sabal = new XML::Twig(
 twig_roots  = {
'[EMAIL PROTECTED]kingtony]'   =
sub {
my ($yabal, $element ) = @_;
 $element-print;
 }
 }
);

above produces,

bayking active=true country=Russia id=kingtonybayking 
type=dictator/bay_usage 
value=none/bayking_originbayking_origin_name emmigrate=no 
value=ohio_usaeconomy_status_previous value=very 
poor//bayking_origin_name/bayking_origin/bayking


which is very close to what i need but I really need it to traverse all 
the way top to yahoo and traverse down back all the way to /yahoo..


I thought sub'ing $element-print to //../../$element-print would work 
but does not


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




Re: xml question for xml::twig

2008-11-15 Thread Richard Lee

Chas. Owens wrote:

On Sat, Nov 15, 2008 at 16:27, Richard Lee [EMAIL PROTECTED] wrote:
snip
  

   if ( $bay1-att('id' eq 'kingtony' ) ) {


snip

I think you mean to say

if ($bay1-att(id) eq kingtony) {

  

yes, that was a typo...

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




Re: xml question for xml::twig

2008-11-15 Thread Richard Lee

Richard Lee wrote:

Chas. Owens wrote:

On Sat, Nov 15, 2008 at 16:27, Richard Lee [EMAIL PROTECTED] wrote:
snip
 

   if ( $bay1-att('id' eq 'kingtony' ) ) {


snip

I think you mean to say

if ($bay1-att(id) eq kingtony) {

  

yes, that was a typo...

I changed to

my $sabal = new XML::Twig(
 twig_roots  = {
'foo/yahoo'  =
#'[EMAIL PROTECTED]kingtony]'   =
sub {
my ($yabal, $element ) = @_;
if ( 
$yabal-first_child('bayking_list')-first_child('bayking')-att('id') 
eq 'kingtony' ) {

$element-print;
}
 }
 }
);

$sabal-parse($xml);

but still no luck..

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




Re: xml question for xml::twig

2008-11-15 Thread Richard Lee

Chas. Owens wrote:




Perhaps I am dense, but what is the desired output from the given XML?

  

Hello Chas,

From xml file, based on attribute value for bayking id, I want to find 
kingtony and then I want to traverse back up to yahoo and print 
everything from

yahoo to /yahoo

I have tried to use xpath //../../bayking but doesn't seem to work.

I am trying to ultimately push below find value into hash of hash 
reference such as


yahoo V=baz  $list{'yahoo'} = { 'V' = 'baz'}

bayking id=kingtony country=Russia active=true   
$list{'bayking'} = { 'id' = 'kingtony' , 'country' = 'Russia', 
'active' = 'true' }


I am able to find what I am looking for 'kingtony' but having tough time 
traversing back to right yahoo element and accessing all data.



yahoo V=baz
bay_id
value1000/value
factyes/fact
/bay_id
bay_seen
value50/value
factno/fact
/bay_seen
bay_overall value=disabled/
bayking_list
bayking id=kingtony country=Russia active=true
bayking type=dictator/
bay_usage value=none/
bayking_origin
  bayking_origin_name value=ohio_usa 
emmigrate=no

 economy_status_previous value=very poor /
  /bayking_origin_name
/bayking_origin
/bayking
/bayking_list

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




Re: xml question for xml::twig

2008-11-15 Thread Richard Lee

Richard Lee wrote:

Chas. Owens wrote:

my $sabal = new XML::Twig(
 twig_roots  = {
'foo/yahoo'  =
#'[EMAIL PROTECTED]kingtony]'   =
sub {
   my ($yabal, $element ) = @_;
   if ( 
$element-first_child('bayking_list')-first_child('bayking')-att('id') 
eq 'kingtony' ) {

  $element-print;
}
 }
 }
);

I think I made a mistake .. this is now working...

yahoo 
V=bazbay_idvalue1000/valuefactyes/fact/bay_idbay_seenvalue50/valuefactno/fact/bay_seenbay_overall 
value=disabled/bayking_listbayking active=true country=Russia 
id=kingtonybayking type=dictator/bay_usage 
value=none/bayking_originbayking_origin_name emmigrate=no 
value=ohio_usaeconomy_status_previous value=very 
poor//bayking_origin_name/bayking_origin/bayking/bayking_listbayqueen_listbayqueen 
active=true country=japan id=queensarahbayqueen 
type=dictator/bay_usage 
value=none/bayqueen_originbayqueen_origin_name emmigrate=no 
value=ca_usa/economy_status_previous value=very poor/previous 
marriage=no//bayqueen_origin/bayqueen/bayqueen_list/yahoo


Now, I just need good way to put this into hash of hash referernce.

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




please explain the anonymous sub usage in one of the article from stonehenge in detail for me

2008-11-07 Thread Richard Lee

http://www.stonehenge.com/merlyn/UnixReview/col08.html

From above article, I am not fully understanding what's going on on 
below code.


1)is there any difference in $next = $non_blank(STDIN) and 
$next=$non_blank( sub{ STDIN })  in terms of funcaitonality?
  I am not fully understanding the difference in anonymous sub vs just 
having regular STDIN in there


2)in non_blank sub, my *guess is { $scanner }() is trying to 
dereference the anonymous sub routine... is () necessary there?


Please kindly explain as it's bit over my head at this point but I do 
enjoy the article immensenly..


thank you in advance.


$next = non_blank(
   sub { STDIN; }
   ); # read from stdin
   $next = non_blank(
   sub { shift @cache; }
   }; # grab from list @cache



 sub non_blank {
   my($scanner) = @_;
   my($return);
   {
   $return = {$scanner}();
   last unless defined $return;
   redo until $return =~ /\S/;
   }
   $return;
   }

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




Re: please explain the anonymous sub usage in one of the article from stonehenge in detail for me

2008-11-07 Thread Richard Lee

Jenda Krynicky wrote:

If we name the subroutine the code would look like this:

sub read_from_stdin {
 return STDIN;
}

...

$next = non_blank(\read_from_stdin);

that is again the non_blank() would receive a reference to a 
subroutine that reads one line from STDIN whenever called.
(actually this is not entirely true, the behaviour of the subroutine 
in both cases depends on the context. In scalar context it reads just 
one line, in list context it reads all remaining lines.)


  
Excellent!!! this is very clear... (I mean I am sure article was very 
clear to regular readers.. i am just bit slow but this is great!!)


   I am not fully understanding the difference in anonymous sub vs just 
having regular STDIN in there


2)in non_blank sub, my *guess is { $scanner }() is trying to 
dereference the anonymous sub routine... is () necessary there?



We need to dereference and thus call the subroutine, but you are 
right that the () is not necessary there. I like this syntax better 
though:


   $scanner-()

but it means the same.
  

thank you for the confirmation!!


Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.

-- Terry Pratchett in Sourcery

  



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




trying to parse out the simple xml file from the book with XML::Parser and I get not well-formed error

2008-11-02 Thread Richard Lee

Not sure why I get this..

As xml file is very very simple one(I even tried to put different 
encoding as well).

Below is all necessary information.

[EMAIL PROTECTED] script]# uname -a
Linux xmen 2.6.25.14-108.fc9.i686 #1 SMP Mon Aug 4 14:08:11 EDT 2008 
i686 i686 i386 GNU/Linux

[EMAIL PROTECTED] script]# cat /etc/issue
Fedora release 9 (Sulphur)
Kernel \r on an \m (\l)
fortune | cowsay

[EMAIL PROTECTED] script]# ./weather.pl weather.xml

not well-formed (invalid token) at line 3, column 26, byte 60 at 
/usr/lib/perl5/vendor_perl/5.10.0/i386-linux-thread-multi/XML/Parser.pm 
line 187


?xml version='1.0' ?
FORECAST
  OUTLOOKPartly Cloudy\OUTLOOK
  TEMPERATURE TYPE=MAX DEGREES=C12/TEMPERATURE
  !-- TEMPERATURE TYPE=MIN DEGREES=C6/TEMPERAURE  --
/FORECAST

use strict;
use warnings;

use XML::Parser;

my %forecast;
my @curr;
my $type;

my $p1 = new XML::Parser(Style = 'Stream');
my $yahoo = shift;
$p1-parsefile($yahoo);

print Outlook: $forecast{outlook}\n;

foreach (keys %forecast) {
   next if /outlook/;
   print $_: $forecast{$_}-{val} $forecast{$_}-{deg}\n;
}

sub StartTag {
   my ($p, $tag) = @_;

   push @curr, $tag;
  
   if ($tag eq 'TEMPERATURE') {

$type = $_{TYPE};
$forecast{$type}-{deg} = $_{DEGREES};
   }
}

sub EndTag {
pop @curr;
}

sub Text {
   my ($p) = shift;

   return unless /\S/;

   s/^\s+//;
   s/^\s+$//;

   if ($curr[-1] eq 'OUTLOOK') {
 $forecast{outlook} .= $_;
   } elsif ( $curr[-1] eq 'TEMPERATURE' ) {
 $forecast{$type}-{val} = $_;
   }
}

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




Re: trying to parse out the simple xml file from the book with XML::Parser and I get not well-formed error

2008-11-02 Thread Richard Lee

Richard Lee wrote:

Not sure why I get this..

As xml file is very very simple one(I even tried to put different 
encoding as well).

Below is all necessary information.

[EMAIL PROTECTED] script]# uname -a
Linux xmen 2.6.25.14-108.fc9.i686 #1 SMP Mon Aug 4 14:08:11 EDT 2008 
i686 i686 i386 GNU/Linux

[EMAIL PROTECTED] script]# cat /etc/issue
Fedora release 9 (Sulphur)
Kernel \r on an \m (\l)
fortune | cowsay

[EMAIL PROTECTED] script]# ./weather.pl weather.xml

not well-formed (invalid token) at line 3, column 26, byte 60 at 
/usr/lib/perl5/vendor_perl/5.10.0/i386-linux-thread-multi/XML/Parser.pm 
line 187


?xml version='1.0' ?
FORECAST
  OUTLOOKPartly Cloudy\OUTLOOK
  TEMPERATURE TYPE=MAX DEGREES=C12/TEMPERATURE
  !-- TEMPERATURE TYPE=MIN DEGREES=C6/TEMPERAURE  --
/FORECAST

use strict;
use warnings;

use XML::Parser;

my %forecast;
my @curr;
my $type;

my $p1 = new XML::Parser(Style = 'Stream');
my $yahoo = shift;
$p1-parsefile($yahoo);

print Outlook: $forecast{outlook}\n;

foreach (keys %forecast) {
   next if /outlook/;
   print $_: $forecast{$_}-{val} $forecast{$_}-{deg}\n;
}

sub StartTag {
   my ($p, $tag) = @_;

   push @curr, $tag;
 if ($tag eq 'TEMPERATURE') {
$type = $_{TYPE};
$forecast{$type}-{deg} = $_{DEGREES};
   }
}

sub EndTag {
pop @curr;
}

sub Text {
   my ($p) = shift;

   return unless /\S/;

   s/^\s+//;
   s/^\s+$//;

   if ($curr[-1] eq 'OUTLOOK') {
 $forecast{outlook} .= $_;
   } elsif ( $curr[-1] eq 'TEMPERATURE' ) {
 $forecast{$type}-{val} = $_;
   }
}

sorry, just saw the typo on xml file

?xml version='1.0' ?
FORECAST
 OUTLOOKPartly Cloudy\OUTLOOK   --- typo here
 TEMPERATURE TYPE=MAX DEGREES=C12/TEMPERATURE
 !-- TEMPERATURE TYPE=MIN DEGREES=C6/TEMPERAURE  --
/FORECAST


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




Re: trying to parse out the simple xml file from the book with XML::Pa

2008-11-02 Thread Richard Lee

Jeff Pang wrote:

I didn't see you assign a value to %forecast but you can loop through it,why?


- Original Message -
From: Richard Lee 
To: Perl Beginners 
Subject: trying to parse out the simple xml file from the book with XML::Parser

 and I get not well-formed error
Date: 2008-11-3 09:48:05

use strict;

use warnings;



use XML::Parser;



my %forecast;

my @curr;

my $type;



my $p1 = new XML::Parser(Style = 'Stream');

my $yahoo = shift;

$p1-parsefile($yahoo);



print Outlook: $forecast{outlook}\n;



foreach (keys %forecast) {

next if /outlook/;

print $_: $forecast{$_}-{val} $forecast{$_}-{deg}\n;

}



---
??(http://space.sina.com.cn/ )
I am not sure if you got the entire code but after the loop through, 
there are more codes which when it encounters tags, it builds %forecast 
hash as below


sub StartTag {
my ($p, $tag) = @_;

push @curr, $tag;

if ($tag eq 'TEMPERATURE') {
$type = $_{TYPE};
$forecast{$type}-{deg} = $_{DEGREES};
}
}

sub EndTag {
pop @curr;
}

sub Text {
my ($p) = shift;

return unless /\S/;

s/^\s+//;
s/^\s+$//;

if ($curr[-1] eq 'OUTLOOK') {
$forecast{outlook} .= $_;
} elsif ( $curr[-1] eq 'TEMPERATURE' ) {
$forecast{$type}-{val} = $_;
}
}

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




confused about reference

2008-10-30 Thread Richard Lee


I was just testing some reference and while trying out below I am trying 
to understand below


@{$yahoo-{yahoo}}...  I can see that this is pointing to 0,1,3 
by running the code.
But I am trying to really understand whether this is trying to say since 
value of 'yahoo' is array put @ at the front?

or @ is there because it's array slice??
Can someone explain this?

Also, %{$base[0]}, trying to understand this.
According to the tutorial,

%base   referenced is %{$base}

what is the difference between  %{$base[0]}  vs %{$base}[0]  
Are we here talking about   first being hash reference which first item 
is array reference vs  second one is hash reference's first item ?


thank you.

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

my $yahoo = { 'yahoo' = [ 0, 1, 3],
 'msn' = [ 12, 32, 44]
   };

print Dumper($yahoo);

print 
\n\n\n\n\n\n;


print ${$yahoo}{yahoo}[0]\n;   # prints 0
print $yahoo-{yahoo}[0]\n;# prints 0
print @{$yahoo-{yahoo}}\n;# prints 0,1,3
print %{$yahoo-{yahoo}}\n;# print %{ARRAY(0x832e8c4)}

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




Re: confused about reference

2008-10-30 Thread Richard Lee

Richard Lee wrote:


I was just testing some reference and while trying out below I am 
trying to understand below


@{$yahoo-{yahoo}}...  I can see that this is pointing to 
0,1,3 by running the code.
But I am trying to really understand whether this is trying to say 
since value of 'yahoo' is array put @ at the front?

or @ is there because it's array slice??
Can someone explain this?
@{$yahoo-{yahoo}}  has @ on it because it's value of yahoo is 
reference. Is this right?


Also, %{$base[0]}, trying to understand this.
According to the tutorial,

%base   referenced is %{$base}

what is the difference between  %{$base[0]}  vs %{$base}[0]  Are we 
here talking about   first being hash reference which first item is 
array reference vs  second one is hash reference's first item ?

This one, I cannot figure it out



thank you.

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

my $yahoo = { 'yahoo' = [ 0, 1, 3],
 'msn' = [ 12, 32, 44]
   };

print Dumper($yahoo);

print 
\n\n\n\n\n\n; 



print ${$yahoo}{yahoo}[0]\n;   # prints 0
print $yahoo-{yahoo}[0]\n;# prints 0
print @{$yahoo-{yahoo}}\n;# prints 0,1,3
print %{$yahoo-{yahoo}}\n;# print %{ARRAY(0x832e8c4)}



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




Re: XML::Simple question

2008-10-22 Thread Richard Lee

Rob Dixon wrote:

Richard Lee wrote:
  

while trying to study the article on perlmonks.org,

http://perlmonks.org/?node_id=490846

regarding XML parsing, I need bit of clarfication.

how do I parse out

image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif;
 width=145 height=190 / 



I tried $book-{image}-{src}... but doesn't work.. I need some 
understanding on how these information is stored.




parsing code

  use XML::Simple qw(:strict);

  my $library  = XMLin($filename,
ForceArray = 1,
KeyAttr= {},
  );

  foreach my $book (@{$library-{book}}) {
print $book-{title}-[0], \n 


  }

XML file

library
book
  titlePerl Best Practices/title
  authorDamian Conway/author
  isbn0596001738/isbn
  pages542/pages
  image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif;
 width=145 height=190 /
/book
book
  titlePerl Cookbook, Second Edition/title
  authorTom Christiansen/author
  authorNathan Torkington/author
  isbn0596003137/isbn
  pages964/pages
  image src=http://www.oreilly.com/catalog/covers/perlckbk2.s.gi
+f
 width=145 height=190 /
/book
book
  titleGuitar for Dummies/title
  authorMark Phillips/author
  authorJohn Chappell/author
  isbn076455106X/isbn
  pages392/pages
  image src=http://media.wiley.com/product_data/coverImage/6X/07
+645510/076455106X.jpg
 width=100 height=125 /
/book
  /library



I agree with Stewart that XML::Simple is far from simple in practice. For the
selection of options for XMLin you have used, you can access the image data like
this:

foreach my $book (@{$library-{book}}) {
  my $title = $book-{title}[0];
  my $image = $book-{image}[0];
  print $title\n;
  print   $image-{src}\n;
  print   $image-{width}\n;
  print   $image-{height}\n;
}

but the structure of the data will change depending on what options are set, and
in general it is very difficult to use XML::Simple without also using
Data::Dumper to inspect the data structure that has actually been generated.

HTH,

Rob
  

thanks all!

Will try few others now.

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




XML::Simple question

2008-10-21 Thread Richard Lee

while trying to study the article on perlmonks.org,

http://perlmonks.org/?node_id=490846

regarding XML parsing, I need bit of clarfication.

how do I parse out

image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif;
width=145 height=190 / 



I tried $book-{image}-{src}... but doesn't work.. I need some 
understanding on how these information is stored.




parsing code

 use XML::Simple qw(:strict);

 my $library  = XMLin($filename,
   ForceArray = 1,
   KeyAttr= {},
 );

 foreach my $book (@{$library-{book}}) {
   print $book-{title}-[0], \n 


 }

XML file

library
   book
 titlePerl Best Practices/title
 authorDamian Conway/author
 isbn0596001738/isbn
 pages542/pages
 image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif;
width=145 height=190 /
   /book
   book
 titlePerl Cookbook, Second Edition/title
 authorTom Christiansen/author
 authorNathan Torkington/author
 isbn0596003137/isbn
 pages964/pages
 image src=http://www.oreilly.com/catalog/covers/perlckbk2.s.gi
+f
width=145 height=190 /
   /book
   book
 titleGuitar for Dummies/title
 authorMark Phillips/author
 authorJohn Chappell/author
 isbn076455106X/isbn
 pages392/pages
 image src=http://media.wiley.com/product_data/coverImage/6X/07
+645510/076455106X.jpg
width=100 height=125 /
   /book
 /library



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




Please help me w/ my sub

2008-10-14 Thread Richard Lee

below sub works fine except the line where key is default.
Instead of printing out PCMU only once, it's printing it out 40 times 
randomly..

Trying to figure out what I did wrong.

Please leave me a feedback.

thank you.

156  time(s) Codec(s)  : unassigned_38
185  time(s) Codec(s)  : G729   
1966 time(s) Codec(s)  : PCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMUPCMU


sub codec_list {
 #my @codec_d = qw/0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
19 20 21 22 23 24 25 26 27

 # 28 29 30 31 32 33 34 35--71 72--76 77--95 96--127/;
 my @codec_d = qw/0 default 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 
17 18 19 20 21 22 23 24 25 26 27

  28 29 30 31 32 33 34 A B C D/;
 my @codec_i = qw/PCMU PCMU Reserved Reserved GSM G723 DVI4 DVI4 
LPC PCMA G722 L16 L16 QCELP CN MAP
  G728 DVI4 DVI4 G729 reserved unassigned 
unassigned unassigned unassigned unassigned
  CelB JPEG unassigned nv unassigned unassigned 
H261 MPV MP2T H263

  unassigned reserved unassigned dynamic/;
 my %codec_r;
 my @return_codec;
 @[EMAIL PROTECTED] = (@codec_i);
 my $number = shift;
 HS: for ( @$number ) {
   my $request_key = $_;
   #next unless $request_key =~ /^\d+$/;
   for my $o ( keys  %codec_r ) {
  if ( $request_key eq $o ) { 
   push @return_codec, $codec_r{$o};

  } elsif ( $request_key eq 'default' ) {
   push @return_codec, $codec_r{'default'};   --- 
problem line

  } elsif ( ($o =~ /A|B|C|D/ ) ) {
   if ( ( $request_key = 35 )  and ( $request_key = 
71 )) {
 push @return_codec, join ('_' , $codec_r{'A'}, 
$request_key );

 next HS;
   } elsif ( ( $request_key = '72' )  and ( 
$request_key = '76' )) {

 push @return_codec, $codec_r{'B'};
 next HS;
   } elsif  ( ( $request_key = '77' )  and ( 
$request_key = '95' )) {

 push @return_codec, $codec_r{'C'};
 next HS;
   } elsif ( ( $request_key = '96' )  and ( 
$request_key = '126' )) {

 push @return_codec, $codec_r{'D'};
 next HS;
   }  
  }  
   }  
 }

 return @return_codec;
}

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




Re: Please help me w/ my sub

2008-10-14 Thread Richard Lee

John, I think this is now fixed.
Still looking to make sure its covering all basis..

sub codec_list {
 #my @codec_d = qw/0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
19 20 21 22 23 24 25 26 27

 # 28 29 30 31 32 33 34 35--71 72--76 77--95 96--127/;
 my @codec_d = qw/0 default 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 
17 18 19 20 21 22 23 24 25 26 27

  28 29 30 31 32 33 34 A B C D/;
 my @codec_i = qw/PCMU PCMU Reserved Reserved GSM G723 DVI4 DVI4 
LPC PCMA G722 L16 L16 QCELP CN MAP
  G728 DVI4 DVI4 G729 reserved unassigned 
unassigned unassigned unassigned unassigned
  CelB JPEG unassigned nv unassigned unassigned 
H261 MPV MP2T H263

  unassigned reserved unassigned dynamic/;
 my %codec_r;
 my @return_codec;
 @[EMAIL PROTECTED] = (@codec_i);
 my $number = shift;
 HS: for ( @$number ) {
   my $request_key = $_;
   #next unless $request_key =~ /^\d+$/;
   for my $o ( keys  %codec_r ) {
  if ( $request_key eq $o ) {  # if  
item from array ref @number matches one of the key in %codec_r

   push @return_codec, $codec_r{$o};
   next HS;
  } elsif ( $request_key eq 'default' ) {
   push @return_codec, $codec_r{'default'};
   next HS;
  } elsif ( ($o =~ /A|B|C|D/ ) ) {
   if ( ( $request_key = 35 )  and ( $request_key = 
71 )) {
 push @return_codec, join ('_' , $codec_r{'A'}, 
$request_key );

 next HS;
   } elsif ( ( $request_key = '72' )  and ( 
$request_key = '76' )) {
 push @return_codec, join ('_' , $codec_r{'B'}, 
$request_key ) ;

 next HS;
   } elsif  ( ( $request_key = '77' )  and ( 
$request_key = '95' )) {
 push @return_codec, join ('_', $codec_r{'C'}, 
$request_key ) ;

 next HS;
   } elsif ( ( $request_key = '96' )  and ( 
$request_key = '126' )) {
 push @return_codec, join ('_', $codec_r{'D'}, 
$request_key ) ;

 next HS;
   }  
  } else {
next;  
  }  
   }  
 }

 return @return_codec;
}



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




Re: Please help me w/ my sub

2008-10-14 Thread Richard Lee

Chas. Owens wrote:

On Tue, Oct 14, 2008 at 02:15, Richard Lee [EMAIL PROTECTED] wrote:
  

below sub works fine except the line where key is default.
Instead of printing out PCMU only once, it's printing it out 40 times
randomly..
Trying to figure out what I did wrong.


snip

I found your code to be very odd.  You do not normally loop over the
keys in a hash to determine if there is a match; you just ask the hash
if a match occurred.  Did your code start out using the arrays at the
top instead of a hash?  I have rewritten your function to make better
use of the hash and provide error handling for out of range requests.
The error was in your looping code (the default case was being
executed for every key in the hash).

#!/usr/bin/perl

use strict;
use warnings;

$|=1;

for my $request (qw/default A B C D/, 0 .. 126) {
print codec_list([$request]), \n,
}

print join(, , codec_list([0 .. 126])), \n;

codec_list([-1, 128]);

sub codec_list {
   my $number = shift;

   #FIXME: this should really be in a file
   my @codec_d = qw/0 default 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 A B C D/;
   my @codec_i = qw/PCMU PCMU Reserved Reserved GSM G723 DVI4 DVI4 LPC
PCMA G722 L16 L16 QCELP CN MAP G728 DVI4 DVI4 G729 reserved unassigned
unassigned unassigned unassigned unassigned CelB JPEG unassigned nv
unassigned unassigned H261 MPV MP2T H263 unassigned reserved
unassigned dynamic/;
   my %codec_r;
   @[EMAIL PROTECTED]  = (@codec_i);
   $codec_r{$_} = $codec_r{A}_$_ for 35 .. 71;
   @codec_r{72 .. 76}  = ($codec_r{B}) x ( 77 - 72);
   @codec_r{77 .. 95}  = ($codec_r{C}) x ( 96 - 77);
   @codec_r{96 .. 126} = ($codec_r{D}) x (127 - 96);

   my $errors = 0;
   my @return_codec;
   for my $request_key ( @$number ) {
   if (exists $codec_r{$request_key}) {
   push @return_codec, $codec_r{$request_key};
   next;
   } else {
   warn $request_key is not valid;
   $errors++;
   }
   }
   die had $errors errors if $errors;
   return @return_codec;
}



--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
  

thank you. I am heading to work.. will review and comment back.

thanks again!

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




Re: Please help me w/ my sub

2008-10-14 Thread Richard Lee

Chas. Owens wrote:

On Tue, Oct 14, 2008 at 02:15, Richard Lee [EMAIL PROTECTED] wrote:
  

below sub works fine except the line where key is default.
Instead of printing out PCMU only once, it's printing it out 40 times
randomly..
Trying to figure out what I did wrong.


snip

I found your code to be very odd.  You do not normally loop over the
keys in a hash to determine if there is a match; you just ask the hash
if a match occurred.  Did your code start out using the arrays at the
top instead of a hash?  I have rewritten your function to make better
use of the hash and provide error handling for out of range requests.
The error was in your looping code (the default case was being
executed for every key in the hash).

#!/usr/bin/perl

use strict;
use warnings;

$|=1;

for my $request (qw/default A B C D/, 0 .. 126) {
print codec_list([$request]), \n,
}

print join(, , codec_list([0 .. 126])), \n;

codec_list([-1, 128]);

sub codec_list {
   my $number = shift;

   #FIXME: this should really be in a file
   my @codec_d = qw/0 default 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 A B C D/;
   my @codec_i = qw/PCMU PCMU Reserved Reserved GSM G723 DVI4 DVI4 LPC
PCMA G722 L16 L16 QCELP CN MAP G728 DVI4 DVI4 G729 reserved unassigned
unassigned unassigned unassigned unassigned CelB JPEG unassigned nv
unassigned unassigned H261 MPV MP2T H263 unassigned reserved
unassigned dynamic/;
   my %codec_r;
   @[EMAIL PROTECTED]  = (@codec_i);
   $codec_r{$_} = $codec_r{A}_$_ for 35 .. 71;
   @codec_r{72 .. 76}  = ($codec_r{B}) x ( 77 - 72);
   @codec_r{77 .. 95}  = ($codec_r{C}) x ( 96 - 77);
   @codec_r{96 .. 126} = ($codec_r{D}) x (127 - 96);

   my $errors = 0;
   my @return_codec;
   for my $request_key ( @$number ) {
   if (exists $codec_r{$request_key}) {
   push @return_codec, $codec_r{$request_key};
   next;
   } else {
   warn $request_key is not valid;
   $errors++;
   }
   }
   die had $errors errors if $errors;
   return @return_codec;
}



--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
  

your work put my code to shame..
But it is excellent and I am reviewing and trying to understand your 
code so that I can re-incorporate them into my program..


will let you know..

thanks.

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




Re: Please help me w/ my sub

2008-10-14 Thread Richard Lee

John W. Krahn wrote:

Richard Lee wrote:

below sub works fine except the line where key is default.
Instead of printing out PCMU only once, it's printing it out 40 times 
randomly..

Trying to figure out what I did wrong.

Please leave me a feedback.

thank you.

156  time(s) Codec(s)  : unassigned_38
185  time(s) Codec(s)  : G729   1966 time(s) Codec(s)  : 
PCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMU
PCMUPCMUPCMUPCMUPCMU


sub codec_list {
 #my @codec_d = qw/0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
19 20 21 22 23 24 25 26 27
 # 28 29 30 31 32 33 34 35--71 72--76 77--95 
96--127/;
 my @codec_d = qw/0 default 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
16 17 18 19 20 21 22 23 24 25 26 27

  28 29 30 31 32 33 34 A B C D/;
 my @codec_i = qw/PCMU PCMU Reserved Reserved GSM G723 DVI4 DVI4 
LPC PCMA G722 L16 L16 QCELP CN MAP
  G728 DVI4 DVI4 G729 reserved unassigned 
unassigned unassigned unassigned unassigned
  CelB JPEG unassigned nv unassigned unassigned 
H261 MPV MP2T H263

  unassigned reserved unassigned dynamic/;
 my %codec_r;
 my @return_codec;
 @[EMAIL PROTECTED] = (@codec_i);
 my $number = shift;
 HS: for ( @$number ) {
   my $request_key = $_;
   #next unless $request_key =~ /^\d+$/;
   for my $o ( keys  %codec_r ) {
  if ( $request_key eq $o ) {push 
@return_codec, $codec_r{$o};

  } elsif ( $request_key eq 'default' ) {


Your problem is that neither $request_key nor 'default' changes in the 
inner loop so $codec_r{'default'} is added for every element of 
@codec_d.  You probably want to put next HS; after the push().



   push @return_codec, $codec_r{'default'};   --- 
problem line

  } elsif ( ($o =~ /A|B|C|D/ ) ) {
   if ( ( $request_key = 35 )  and ( $request_key = 
71 )) {
 push @return_codec, join ('_' , 
$codec_r{'A'}, $request_key );

 next HS;
   } elsif ( ( $request_key = '72' )  and ( 
$request_key = '76' )) {


Why are you using numerical comparison on strings:

 } elsif ( $request_key = 72  $request_key = 
76 ) {




 push @return_codec, $codec_r{'B'};
 next HS;
   } elsif  ( ( $request_key = '77' )  and ( 
$request_key = '95' )) {


 } elsif  ( $request_key = 77  $request_key = 
95 ) {




 push @return_codec, $codec_r{'C'};
 next HS;
   } elsif ( ( $request_key = '96' )  and ( 
$request_key = '126' )) {


 } elsif ( $request_key = 96  $request_key = 
126 ) {




 push @return_codec, $codec_r{'D'};
 next HS;
   }} }   }
 return @return_codec;
}



John

thanks John, that was the problem. (HS solution).

Why are you using numerical comparison on strings:

} elsif ( $request_key = 72  $request_key = 76 ) {


-- I see the flaw in my design.. I am going back to see if I can fix it..



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




array serach for partial value

2008-10-14 Thread Richard
Hello,

I have small Perl project and got stuck on following problem :
There is a zip file with bunch of files in it. I need to search
through it and find if every xxx.txt file has xxx.log pair and list
all of those .txt without pairs. I figured out that I will use
Archive::Zip and iterate through members of the zip then copy all
compressed file names into an array and then search array for those
pairs. But since my knowledge of perl is rather weak i can't figure
out how to search through array for only part of the value (xxx. in
this case...)
Can you help me guys with that problem ?
Thanks a lot...


Richard


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




sub and return question

2008-10-10 Thread Richard Lee

can't you do below??


sub criteria {
  return  qw/ 1,3,5,7,9 /;
}

I was going to do

sub criteria {
 my @array = qw/1,3,5,7,9/;
}

but was wondering if I can do without if i was calling the sub like this


my @array_result = criteria();

??

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




Re: sub and return question

2008-10-10 Thread Richard Lee

Richard Lee wrote:

can't you do below??


sub criteria {
  return  qw/ 1,3,5,7,9 /;
}

I was going to do

sub criteria {
 my @array = qw/1,3,5,7,9/;
}

but was wondering if I can do without if i was calling the sub like this


my @array_result = criteria();

??
actually qw/ 1 3 5 7 9/;  i was wrongly adding commas there.. sorry abou 
thtis post.. disregard please.


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




Re: sub and return question

2008-10-10 Thread Richard Lee

Mr. Shawn H. Corey wrote:

Don't use commas inside a qw

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;
$Data::Dumper::Maxdepth = 0;

sub criteria {
   return  qw/ 1 3 5 7 9 /;
}

my @array_result = criteria();
print Dumper [EMAIL PROTECTED];

__END__

  

yes, thank you!

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




Re: a perl blog (perlbuzz)

2008-10-01 Thread Richard Lee

Jeff Pang wrote:

I don't know if everybody here know it yet, but I just give a recommendation.

http://perlbuzz.com/

It has some good and updated articles there.


Regards,
Jeff.

 Créez votre adresse électronique [EMAIL PROTECTED] 
 1 Go d'espace de stockage, anti-spam et anti-virus intégrés.


  

links to the perl journal alone made this post my day.

thank you!!!

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




reference question

2008-09-29 Thread Richard Lee

one more question on reference,

if say you have sub as below

my @arrayref;

sub do_something {
   my $something = @_;
   open FILE, $something, or die;
   while (FILE) {
my @array = map (split /,/)[1,2,3,5];
push @arrayref, [EMAIL PROTECTED];
   }
  close FILE;
}

my @arrayref_copy = do_something($something);



Let's for a moment forget about my inefficient style and just look at 
the last step.
If I wanted to use @arrayref_copy and pass it into another 
subroutine,should I reference them again?(if file itself was pretty big?)


another_sub([EMAIL PROTECTED]);   -- is this the right thing to do? or is 
this redundant since array is  already collection of reference?


sub another_sub {

}


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




Re: reference question

2008-09-29 Thread Richard Lee

Rob Dixon wrote:

Richard Lee wrote:
  

one more question on reference,

if say you have sub as below



Your code is badly wrong. Please let me correct it first.

  

my @arrayref;

sub do_something {
my $something = @_;



That will set $something to the number of parameters passed in @_

To retrieve the first parameter you should write

  my ($something) = @_;

or

  my $something = shift;

  

open FILE, $something, or die;



You should not quote scalar variables unless you know what it does and have a
reason to do it. Also the it is far better and safer to use lexical file handles
and the three-parameter form of open, and you should incorporate the $! variable
into the die string so that it is clear why the open has failed.

  open my $file, '', $something or die $!;

  

while (FILE) {



  while ($file) {

if you are using my proposed change.

  

 my @array = map (split /,/)[1,2,3,5];



That will probably not compile. You are calling

  map(split /,/)

and map requires either a block or an expression as its first parameter,
followed by a list to map. You mean simply

  my @array = (split /,/)[1,2,3,5];

  

 push @arrayref, [EMAIL PROTECTED];
}
   close FILE;



If you use a lexical file handle $file then it will be closed implicitly when it
goes out of scope at the end of the subroutine. There is no need for an explicit
 call to close.

  

}

my @arrayref_copy = do_something($something);



Let's for a moment forget about my inefficient style and just look at 
the last step. If I wanted to use @arrayref_copy and pass it into another 
subroutine,should I reference them again?(if file itself was pretty big?)


another_sub([EMAIL PROTECTED]);   -- is this the right thing to do? or is 
this redundant since array is  already collection of reference?


sub another_sub {

}



Yes that would be fine. It helps because you're not copying all the references
to a second list. But I would go a step further and pass it back from the first
subroutine as a reference to an array. Like this

  sub read_file {

my $name = shift;
open my $fh, '', $name or die $!;

my @data;

while ($fh) {
  my @record = (split /,/)[1, 2, 3, 5];
  push @data, [EMAIL PROTECTED];
}

return [EMAIL PROTECTED];
  }

 and then you can write

  my $data = read_file('filename');
  my $result = another_sub($data);

and so on.

HTH,

Rob
  
This is great explanation.. thank you. I will re-read this to see if I 
can correct and manipulate my original program.


thank you.

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




from perl review (first article) question on regex

2008-09-28 Thread Richard Lee

I was reading perl magazine and saw

sub readable {
   my $number = shift;
   $matched = $number =~ s{
 (\d+)
 (\d{3})
 (,|$)
  }{$1,$2$3}x;

  } while ($matched);
 return $number;
}

on test driven development article by Denis Kosykh.

I am not sure what

(,|$) is doing...

Can someone please explain?

thank you.

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




can perl program do this?

2008-07-31 Thread Richard Lee

say I have big wireshark file based on remote server.

I want to logon to that server and using Net::Pcap to poke the file and 
only grep out small portion of information which meets my criteria.

Remote server won't have Net::Pcap installed.

I wanted to write this program w/ Expect modules and Net::Pcap module in 
my mind.
But since these things are only located in my local server, will this 
work on remote server?


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




Re: can perl program do this?

2008-07-31 Thread Richard Lee

Rob Coops wrote:
If you can make Net::Pcap connect to a remote server things will work 
fine, I am not sure about using Net::Pcap to do this as I never used 
it and from the description it seems to be a packet capture lib not so 
much a communication one, but as I said if you can make it reach out 
and talk to the other side you should be fine.
 
I think you will end up using somehting like Net::SSH or something 
like that as these are more aimed at communication then Net::Pcap 
seems to be.
 
But in the end as long as the other side is listening on a port and 
you can produce the required packets to get communication going you 
should be fine, there is no need to have perl, or any perl libs on the 
other side all that is needed is an open port that you can communicate 
with.
The thing that is listening on that port is what determins what you 
can do, read email, open and interact with files or just browse a web 
page, perl on your machine cannot change which process is listening on 
the other machine.
 
In short I guess you can if the other machine is listening, but you 
might want to use a more suitable lib for it.
 
Regards,
 
Rob


On Thu, Jul 31, 2008 at 8:41 AM, Richard Lee [EMAIL PROTECTED] 
mailto:[EMAIL PROTECTED] wrote:


say I have big wireshark file based on remote server.

I want to logon to that server and using Net::Pcap to poke the
file and only grep out small portion of information which meets my
criteria.
Remote server won't have Net::Pcap installed.

I wanted to write this program w/ Expect modules and Net::Pcap
module in my mind.
But since these things are only located in my local server, will
this work on remote server?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]

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





Hi,

local server(w/ Perl installed) --- remote server(with 
Pcap file I am interested in poking around)


if I launch a perl script from my local server(using say Net::SSH) and 
logs onto remote server, my perl can poke around that file on remote server
using Net::Pcap ? So essentially in order to do this, I need to use 
2 modules(to logon, and then parsing out the pcap file)


I am thinking about writing this but just not sure if it will work on 
remote server



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




Re: can perl program do this?

2008-07-31 Thread Richard Lee

Rob Coops wrote:

Ah, I see...
 
I think you have been misled by the name of the Net::Pcap lib, the lib 
is meant to produce the file you are trying to poke around in.
 
I would do the follwoing.


* Use Net::SSH or similair to connect to the remote machine.
* On the remote machine use grep or similair to pull out the lines
  you are intrested in and store those in an array/file or well
  what ever storage you like on your side.
* Process the data on your own machine.

The advantage of this is that you only move the data you are intrested 
in over the network and not the whole big file, thus reducing the 
network trafic and maning your network admins happy as well. Because 
you have the data on your side you can then do with it what you want 
without having to rely on any software on the remote machine.
 
I hope this helps a bit,
 
Rob
On Thu, Jul 31, 2008 at 9:06 AM, Richard Lee [EMAIL PROTECTED] 
mailto:[EMAIL PROTECTED] wrote:


Rob Coops wrote:

If you can make Net::Pcap connect to a remote server things
will work fine, I am not sure about using Net::Pcap to do this
as I never used it and from the description it seems to be a
packet capture lib not so much a communication one, but as I
said if you can make it reach out and talk to the other side
you should be fine.
 I think you will end up using somehting like Net::SSH or
something like that as these are more aimed at communication
then Net::Pcap seems to be.
 But in the end as long as the other side is listening on a
port and you can produce the required packets to get
communication going you should be fine, there is no need to
have perl, or any perl libs on the other side all that is
needed is an open port that you can communicate with.
The thing that is listening on that port is what determins
what you can do, read email, open and interact with files or
just browse a web page, perl on your machine cannot change
which process is listening on the other machine.
 In short I guess you can if the other machine is listening,
but you might want to use a more suitable lib for it.
 Regards,
 Rob

On Thu, Jul 31, 2008 at 8:41 AM, Richard Lee
[EMAIL PROTECTED] mailto:[EMAIL PROTECTED]
mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote:

   say I have big wireshark file based on remote server.

   I want to logon to that server and using Net::Pcap to poke the
   file and only grep out small portion of information which
meets my
   criteria.
   Remote server won't have Net::Pcap installed.

   I wanted to write this program w/ Expect modules and Net::Pcap
   module in my mind.
   But since these things are only located in my local server,
will
   this work on remote server?

   --To unsubscribe, e-mail:
[EMAIL PROTECTED]
mailto:[EMAIL PROTECTED]
   mailto:[EMAIL PROTECTED]
mailto:[EMAIL PROTECTED]

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




Hi,

local server(w/ Perl installed) --- remote
server(with Pcap file I am interested in poking around)

if I launch a perl script from my local server(using say Net::SSH)
and logs onto remote server, my perl can poke around that file on
remote server
using Net::Pcap ? So essentially in order to do this, I need
to use 2 modules(to logon, and then parsing out the pcap file)

I am thinking about writing this but just not sure if it will work
on remote server


but I am interested in grepping out the pcap file so I cannot simply 
just do grep


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




Re: Pipe print output into shell in debugger

2008-07-27 Thread Richard Lee

Marek wrote:

Hello all,


while being in the debugger, how do I pipe all printings going to a
OUTputfile like:

print OUT text text text;

into the shell?

I read perldebug and perldebguts, but probably I just overread it?


Thank you for your help



marek


  
I am sure there are other ways to do this but I always wonder how to get 
this done.
Many research and I found that I could have |  (pipe) precede your 
command and output will go to your pager.
I am just not sure how to change the pager on my system(mine is less, 
and if you can change to vi, you should be able to save it).


Let me know if you are able to change your pager.. (I am not sure if 
pager they mean editor or something else in your environment, I use bash ).


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




Re: while trying to learn pack

2008-07-26 Thread Richard Lee

John W. Krahn wrote:

Richard Lee wrote:
I am begining to read bit of low level(assembly) book to just gain 
some knoweldge on inner workings of memory.


My quesiton is, if machine is 32 bit, even if it's accessing string 
'A', it will have to fetch 32 bit (instead of 8 bit that requires to 
make that letter A ) ?


It depends on the CPU.  An Intel compatible CPU has instructions to 
access 8 bit values from memory but some CPUs can only access 32 bit 
aligned data.


I know this is not a mailing list for this but i figure since it's 
closely related to pack, i thought someone would clarify for me.


It is not really related to pack() as pack() is a high level function 
that can hide the details of the CPUs addressing problems.


I am reading step by step assembly language... I am not sure i will 
read the whole thing but i just want to get better inner working of 
memory as my c book didn't do enough justice.



John

thanks guys

Just one more question on the topic,
I am trying to understand how it works in binary world.
So, If let's say I take a pcap file. I am assuming here that 
ethereal/wireshark will take binaries on the wire and then decoding it 
based on pcap standard(? hex? ) and then present them in ascci ?


so which means in order to unpack pcap file, I have to know how ethereal 
pack on their own.. is this something similar to what happens when pcap 
file gets created?


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




Re: while trying to learn pack

2008-07-26 Thread Richard Lee

John W. Krahn wrote:

Richard Lee wrote:
I am begining to read bit of low level(assembly) book to just gain 
some knoweldge on inner workings of memory.


My quesiton is, if machine is 32 bit, even if it's accessing string 
'A', it will have to fetch 32 bit (instead of 8 bit that requires to 
make that letter A ) ?


It depends on the CPU.  An Intel compatible CPU has instructions to 
access 8 bit values from memory but some CPUs can only access 32 bit 
aligned data.


I know this is not a mailing list for this but i figure since it's 
closely related to pack, i thought someone would clarify for me.


It is not really related to pack() as pack() is a high level function 
that can hide the details of the CPUs addressing problems.


I am reading step by step assembly language... I am not sure i will 
read the whole thing but i just want to get better inner working of 
memory as my c book didn't do enough justice.



John

Thanks guys,

One more question, How do I look at binary file using perl? ( I am 
assuming unpack will be able to handle it ).
I am trying to see if I can peak into pcap file w/ unpack... what will 
require? ( I am trying to not use modules to learn )
Idea is to learn the pcap structure and use it to unpack somehow? I am 
not asking for anything specific in order to solve the problem.

I guess just reaching out for general idea. if that's possible.

Meanwhile I will keep reading. thanks for help!!



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




while trying to learn pack

2008-07-25 Thread Richard Lee
I am begining to read bit of low level(assembly) book to just gain some 
knoweldge on inner workings of memory.


My quesiton is, if machine is 32 bit, even if it's accessing string 'A', 
it will have to fetch 32 bit (instead of 8 bit that requires to make 
that letter A ) ?


I know this is not a mailing list for this but i figure since it's 
closely related to pack, i thought someone would clarify for me.


I am reading step by step assembly language... I am not sure i will 
read the whole thing but i just want to get better inner working of 
memory as my c book didn't do enough justice.


Please help.

thanks!

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




perlpacktut ... not sure what I am doing wrong while following the example.. Explanation please

2008-07-19 Thread Richard Lee

just trying to learn pack/unpack function in perl..

http://perldoc.perl.org/perlpacktut.html

I thought i followed pretty much to the teeth from the tutorial itself 
and when I type them into my linux box , it didn't exactly work the way

I expected them to.. What am I doing wrong?

[EMAIL PROTECTED] ~]# cat -A pack_test2.pl
#!/usr/bin/perl$
$
use strict;$
use warnings;$
$
my $tot_income;$
my $tot_expend;$
use POSIX;$
$
my $date = POSIX::strftime(%m/%d/%Y, localtime);$
$
while (DATA) {$
   my($date, $desc, $income, $expend) = unpack(A10 A27 A7 A*, $_);$
   $tot_income += $income;$
   $tot_expend += $expend;$
   print_this($tot_income,$tot_expend);$
}$
$
sub print_this {$
my($tot_income,$tot_expend) = @_;$
$tot_income = sprintf(%.2f, $tot_income); # Get them into $
$tot_expend = sprintf(%.2f, $tot_expend); # financial format$
print pack(A10 A27 A7 A*, $date, Totals, $tot_income, 
$tot_expend);$

}$
$
__END__$
01/24/2001 Ahmed's Camel Emporium  1147.99$
01/28/2001 Flea spray24.99$
01/29/2001 Camel rides to tourists  235.00$

But it's not working out well..

[EMAIL PROTECTED] ~]# ./!$
./././././././pack_test2.pl
Argument  isn't numeric in addition (+) at ./././././././pack_test2.pl 
line 14, DATA line 1.
Argument  isn't numeric in addition (+) at ./././././././pack_test2.pl 
line 14, DATA line 2.
07/19/2008Totals 0.00   
1147.9907/19/2008Totals 0.00   
1172.9807/19/2008Totals 235.00 [EMAIL PROTECTED] ~]#




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




Re: perlpacktut ... not sure what I am doing wrong while following the example.. Explanation please

2008-07-19 Thread Richard Lee

John W. Krahn wrote:

Richard Lee wrote:

just trying to learn pack/unpack function in perl..

http://perldoc.perl.org/perlpacktut.html


You *should* have the documentation for Perl installed on your hard 
drive which will be more relevant to the version of Perl you are using.



I thought i followed pretty much to the teeth from the tutorial 
itself and when I type them into my linux box , it didn't exactly 
work the way

I expected them to.. What am I doing wrong?

[EMAIL PROTECTED] ~]# cat -A pack_test2.pl


cat -A?  You're really trying to make it difficult to just copy and 
run your code?




#!/usr/bin/perl$
$
use strict;$
use warnings;$
$
my $tot_income;$
my $tot_expend;$
use POSIX;$
$
my $date = POSIX::strftime(%m/%d/%Y, localtime);$
$
while (DATA) {$
   my($date, $desc, $income, $expend) = unpack(A10 A27 A7 A*, $_);$


Your unpack() format defines the fields as:

01/24/2001 Ahmed's Camel Emporium  1147.99
01/28/2001 Flea spray24.99
01/29/2001 Camel rides to tourists  235.00
^ ^  ^  ^
123456789012345678901234567890123456789012345678901234567890

The second field includes leading whitespace which you don't need.  
The third field is not long enough and cuts off the decimal portion of 
the number.  The fourth field includes the decimal portion from the 
third field which in numerical context will be interpreted as the 
fourth field instead of the actual fourth field.


You probably want something like this instead:

unpack 'A11 A26 A9 A*', $_;



   $tot_income += $income;$
   $tot_expend += $expend;$


To suppress the isn't numeric warning you can do this:

   $tot_income += $income || 0;
   $tot_expend += $expend || 0;



   print_this($tot_income,$tot_expend);$
}$
$
sub print_this {$
my($tot_income,$tot_expend) = @_;$
$tot_income = sprintf(%.2f, $tot_income); # Get them into $
$tot_expend = sprintf(%.2f, $tot_expend); # financial format$
print pack(A10 A27 A7 A*, $date, Totals, $tot_income, 
$tot_expend);$


And don't forget to add a newline at the end of each output line:

print pack( 'A10 A27 A7 A*', $date, 'Totals', $tot_income, 
$tot_expend ), \n;




}$
$
__END__$
01/24/2001 Ahmed's Camel Emporium  1147.99$
01/28/2001 Flea spray24.99$
01/29/2001 Camel rides to tourists  235.00$

But it's not working out well..

[EMAIL PROTECTED] ~]# ./!$
./././././././pack_test2.pl
Argument  isn't numeric in addition (+) at 
./././././././pack_test2.pl line 14, DATA line 1.
Argument  isn't numeric in addition (+) at 
./././././././pack_test2.pl line 14, DATA line 2.
07/19/2008Totals 0.00   
1147.9907/19/2008Totals 0.00   
1172.9807/19/2008Totals 235.00 [EMAIL PROTECTED] ~]#



John

Thanks John and others for explanation. I understand now.

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




how do you do this in one step instead of two

2008-07-17 Thread Richard Lee

is there way to do this in one step?

 push @array, ($direction, $source);

push @hh, [EMAIL PROTECTED]


push @hh \($direction,$source) doesn't seem to work.. or not the samething

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




what is wrong w/ this program? (getting undefined value.. but I don't understand) please help

2008-07-15 Thread Richard Lee

what is wrong w/ below program???


use warnings;
use strict;
use diagnostics;

#open 'PASSWD', '', '/etc/passwd' or die cannot open: $!\n;
open (PASSWD1, /etc/passwd) or die cannot open: $!\n;
my $line;

while ( chomp($line = PASSWD1)  )  {
  print ---$line---\n if $line =~ /root/;
}

seek(PASSWD1, 0, 0) or die $!\n;

while(PASSWD1) { print if /ellie/;}
close PASSWD1;


Use of uninitialized value in chomp at ./seek_.pl line 11 (#1)
   (W uninitialized) An undefined value was used as if it were already
   defined.  It was interpreted as a  or a 0, but maybe it was a mistake.
   To suppress this warning assign a defined value to your variables.
  
   To help you figure out what was undefined, perl tells you what operation
   you used the undefined value in.  Note, however, that perl optimizes 
your

   program and the operation displayed in the warning may not necessarily
   appear literally in your program.  For example, that $foo is
   usually optimized into that  . $foo, and the warning will refer to
   the concatenation (.) operator, even though there is no . in your
   program.
  


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




Re: what is wrong w/ this program? (getting undefined value.. but I don't understand) please help

2008-07-15 Thread Richard Lee

Richard Lee wrote:

what is wrong w/ below program???


use warnings;
use strict;
use diagnostics;

#open 'PASSWD', '', '/etc/passwd' or die cannot open: $!\n;
open (PASSWD1, /etc/passwd) or die cannot open: $!\n;
my $line;

while ( chomp($line = PASSWD1)  )  {
  print ---$line---\n if $line =~ /root/;
}

seek(PASSWD1, 0, 0) or die $!\n;

while(PASSWD1) { print if /ellie/;}
close PASSWD1;


Use of uninitialized value in chomp at ./seek_.pl line 11 (#1)
   (W uninitialized) An undefined value was used as if it were already
   defined.  It was interpreted as a  or a 0, but maybe it was a 
mistake.

   To suppress this warning assign a defined value to your variables.
 To help you figure out what was undefined, perl tells you what 
operation
   you used the undefined value in.  Note, however, that perl 
optimizes your

   program and the operation displayed in the warning may not necessarily
   appear literally in your program.  For example, that $foo is
   usually optimized into that  . $foo, and the warning will refer to
   the concatenation (.) operator, even though there is no . in your
   program.
  
I found the problem. this new fedora 9 I guess hides the /etc/passwd 
file  is empty file.

Sorry for the trouble guys.



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




Re: what is wrong w/ this program? (getting undefined value.. but I don't understand) please help

2008-07-15 Thread Richard Lee

John W. Krahn wrote:


Or you could do it like this:

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

print '---', join( ':', getpwnam 'root' ), ---\n, join( ':', 
getpwnam 'ellie' ), \n;


__END__



John

Thank you for through explanation John as always!!

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




Re: using Expect.pm

2008-07-10 Thread Richard Lee





was able to make it work but not sure if I am doing the right thing.

use warnings;
use strict;

use Expect;

my $exp = new Expect;
my $password = 'abc123';
my $user = 'userX';
my $host = '10.3.3.1';

# typical regex pattern for end of /bin/sh prompt:
my $shell_prompt = qr/[\$\#]\s*$/;

my $login =
 /usr/bin/ssh [EMAIL PROTECTED];

$exp-spawn($login)
or die
   Can't login to $host as $user: $!\n;

## Enables internal debugging
#$exp-exp_internal( 1 );

$exp-log_file(./log_this.txt);

$exp-expect(5, '-re', '(RSA)');
my @matchlist = $exp-matchlist;

$exp-send(yes\n)
if (  ( $exp-matchlist)[0] );

$exp-expect(5, password: );
$exp-send($password\n);

$exp-expect(2, -re = $shell_prompt);
$exp-send(cat /tmp/file1\n);
my @result = $exp-expect(5, -re = $shell_prompt);

my $ltedge_now = $result[1];
print \n\n\n $ltedge_now\n;

$exp-send(exit\n);
$exp-soft_close();
$exp-log_file(undef);

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




using Expect.pm

2008-07-09 Thread Richard Lee

Hi guys,

I was trying to use Expect.pm but it was just too hard for me to capture 
remote machine's output via below..


#!/usr/bin/perl

use warnings;
use strict;

use Expect;

my $exp = new Expect;
my $password = 'abc123';
my $user = 'userX';
my $host = '10.3.1.2';

my $login =
 /usr/bin/ssh [EMAIL PROTECTED];

$exp-spawn($login)
or die
   Can't login to $host as $user: $!\n;

## Enables internal debugging
$exp-exp_internal( 1 );

$exp-log_file(./log_this.txt);

$exp-expect(5, '-re', '(RSA)');
my @matchlist = $exp-matchlist;

$exp-send(yes\n)
if (  ( $exp-matchlist)[0] );

$exp-expect(5, password: );
$exp-send($password\n);

#my @err = $exp-send(cat /etc/passwd);
#print $_\n for @err;

$exp-send(exit\n);
$exp-log_file(undef);

I thought this would capture the output of cat into log file but it does 
not.
I also thought perhaps I can direclty capture it into the array(I have 
commented them out) but I could not...

can someone lead me to the right path?

thank you.



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




Re: user id validation

2008-07-02 Thread Richard Lee

Randal L. Schwartz wrote:

Rajnikant == Rajnikant  [EMAIL PROTECTED] writes:



Rajnikant Is there any such way to get home directory of user? 


Is there any such way that this might be, perhaps, oh, a FAQ?

Oh, yes, let's see.

$ perldoc -q tilde
Found in /usr/libdata/perl5/pod/perlfaq5.pod
  How can I translate tildes (~) in a filename?

Use the  (glob()) operator, documented in perlfunc. Older versions of
Perl require that you have a shell installed that groks tildes. Recent
perl versions have this feature built in. The File::KGlob module
(available from CPAN) gives more portable glob functionality.

Within Perl, you may use this directly:

$filename =~ s{
  ^ ~ # find a leading tilde
  (   # save this in $1
  [^/]# a non-slash character
* # repeated 0 or more times (0 means me)
  )
}{
  $1
  ? (getpwnam($1))[7]
  : ( $ENV{HOME} || $ENV{LOGDIR} )
}ex;

OK, so you may have not known tilde.  But you should be reading
the FAQ titles at least weekly until you are bored to tears from them,
and probably reading the entire FAQ at least bi-monthly.

  

thanks guys!!

But I guess there is no way to get user id.. ? I will read FAQ more... 
(till I am bored to tears ? :-) )


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




  1   2   3   4   5   6   >