help in while loop

2008-03-17 Thread Irfan.Sayed
Hi All,

 

Can somebody please let me know the meaning of this line.

 

while ($in)

{

if(/,/) {print before match: $`\t and after match: $'\n\n;}; $x=$';
$y=$`; mysubroutine($x,$y);

 

}

 

I know it is a while loop for the file handle ($in) and it will be
executed till the end of file but not getting the meaning of if loop. 

 

What this if loop actually does. Please help

 

Regards,

Irfan

 



Re: List of hashes

2008-03-17 Thread Krzysztof . Chodak
On 16 Mar, 18:30, [EMAIL PROTECTED] (Yitzle) wrote:
 On Sun, Mar 16, 2008 at 12:54 PM, John W. Krahn [EMAIL PROTECTED] wrote:

    push @records, %record
    I think you want an array of references, not of hashed themselves.

   Actually, the hash is converted to a list and that list is pushed onto
   the array.

   John

 Darn! I tested it using
 $array[0] = %hash;
 in which case it converts to a scalar, not in the list context of push... 
 Sorry!

Are subs always return references or only for anonymous lists/hashes?


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




Variables initialization

2008-03-17 Thread Krzysztof . Chodak
How is it possible to initialize variable using 0 or '' and not having
undef warning later on using them?


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




Re: Variables initialization

2008-03-17 Thread Xavier Noria

On Mar 17, 2008, at 11:51 , [EMAIL PROTECTED] wrote:


How is it possible to initialize variable using 0 or '' and not having
undef warning later on using them?


Not reassigning to undef.

-- fxn


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




RE: help in while loop

2008-03-17 Thread Thomas Bätzler
[EMAIL PROTECTED] asked

 Can somebody please let me know the meaning of this line.
 
  
 
 while ($in)
 
 {
 if(/,/) {print before match: $`\t and after match: 
 $'\n\n;}; $x=$'; $y=$`; mysubroutine($x,$y);
 }

The loop iterates over a filehandle, setting $_ to each
line in turn. If that line contains a comma, then the 
line is split at the comma and mysubroutine() is being
called with the parts before and after the comma as
arguments.

My first try at tidying this up would be:

while( my $line = $in )
{
  if( my( $pre, $post ) = ( $line =~ m/^(.*?),(.*)/ ) ){
print before match: $pre\t and after match: $post\n\n;
mysubroutine($pre,$post)
  }
}

I'm assuming that mysubroutine is only being called if there
actually was a match. Your code above would erroneously call
it for each line.

Using the pre/postmatch captures $' and $` is expensive if
you don't need it all the time (cf. perldoc perlre):

 WARNING: Once Perl sees that you need one of $, $`, or $' anywhere in
 the program, it has to provide them for every pattern match. This may
 substantially slow your program.

Using mysubroutine with arguments would only make sense if
mysubroutine was defined with prototypes and you were trying
to disable that. I'm assuming that in this case it's rather
cargo cult programming.

HTH,
Thomas

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




Re: Variables initialization

2008-03-17 Thread Chas. Owens
On Mon, Mar 17, 2008 at 6:51 AM,  [EMAIL PROTECTED] wrote:
 How is it possible to initialize variable using 0 or '' and not having
  undef warning later on using them?
snip

Well, you can say

my $var = 0;

but that doesn't prevent the variable from being set to undef later,
so if it is possible for the variable to be undef you should guard
against it with the defined function (or, if you have Perl 5.10, the
// operator):

print \$var is , (defined $var ? $var : undef), \n; #Perl 5.8 and below

say \$var is , $var // undef; #Perl 5.10


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

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




xml::twig help

2008-03-17 Thread Ken Foskey

I am extracting addresses from an XML file to process through other
programs using pipe delimiter the following code works but this is going
to get 130,000 records through it it must be very efficient and I cannot
follow the documentation on the best way to do this.

After this simple one is programmed I have to change a much more complex
version of this program.

#!/usr/bin/perl -w
# vi:set sw=4 ts=4 et cin:
# $Id:$

=head1 SYNOPSIS

Extract addresses from an XML file into pipe delimited file.

   usage: address_extract.pl  xml_file

=cut

use warnings;
use strict;

use XML::Twig qw(:strict);

sub no_pipe
{
my $value = shift;

$value =~ s/\|//g;
return $value;
}

if( ! -f $ARGV[0] ) {
print $ARGV[0] is not a filename, requires filename as first
parameter!\n;
}

my $sort;
my $sort_file = $ARGV[0].'.unsorted';
unlink $sort_file; # in case of rerun
open( $sort, '', $sort_file  ) 
or die Unable to open $sort_file for output $!;

my $ref = XML::Twig-new( twig_handlers={mem=\member} ) 
or die Unable to open $ARGV[0] $!;

my $member = 0;

$ref-parsefile( $ARGV[0] );

sub get_value
{
my ($mem_ref, $key) = @_;
my @array = $mem_ref-descendants( $key );
return $array[0]-text();
}

sub member {
my ($t, $mem_ref) = @_;
$member++;

my $mem_no = get_value( $mem_ref, 'member' );
my $add1   = get_value( $mem_ref, 'add1' );
my $add2   = get_value( $mem_ref, 'add2' );
my $add3   = get_value( $mem_ref, 'add3' );
my $suburb = get_value( $mem_ref, 'suburb' );
my $state  = get_value( $mem_ref, 'state' );
my $pcode  = get_value( $mem_ref, 'pcode' );

print $sort join( '|', $member,
 $mem_no,
 no_pipe( $add1 ),
 no_pipe( $add2 ),
 no_pipe( $add3 ),
 no_pipe( $suburb),
 $state,
 $pcode,
) .\n;
return 1;
}



Re: help in while loop

2008-03-17 Thread Telemachus
On Mar 17, 7:40 am, [EMAIL PROTECTED] (Irfan Sayed) wrote:
 Hi All,

 Can somebody please let me know the meaning of this line.

 while ($in)

 {

 if(/,/) {print before match: $`\t and after match: $'\n\n;}; $x=$';
 $y=$`; mysubroutine($x,$y);

 }

 I know it is a while loop for the file handle ($in) and it will be
 executed till the end of file but not getting the meaning of if loop.

 What this if loop actually does. Please help

Try looking at it this way:

while ($in) { ## Go through $in line by line
if (/,/) {## Match a comma
print before match: $`\t and after match: $'\n\n;  ## Print
stuff before match and after using $` and $'
}
$x = $';   ## Assign $' to $x
$y = $`;  ## Assign $` to $y
mysubroutine($y, $x);  ## Run $x and $y through mysubroutine
}

The if block goes through $in, line by line, checking for commas. The
variables $` $ and $' are special: $` = the string before a regular
expression match, $ = the match itself, and $' = the part of the
string after the regular expression match. When it finds a comma, it
prints the strings before and after the match of the comma in the
line.  That is, it assigns whatever in the line precedes the comma to
$` and whatever follows to $'. Then it assigns those matches to $x and
$y and runs those as parameters through a subroutine called
mysubroutine. So you can use these special variables in regular
expression matching (as this code does). But many people avoid them
like the plague since they slow down all matching. See perldoc perlvar
for more.

As to what the subroutine mysubroutine actually *does* to or with
those newly assigned variables, there's no way to say without seeing
that code.

Hope this helps, T


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




Re: List of hashes

2008-03-17 Thread yitzle
On Mon, Mar 17, 2008 at 7:12 AM,  [EMAIL PROTECTED] wrote:
  Are subs always return references or only for anonymous lists/hashes?

perldoc perlsub
The Perl model for function call and return values is simple: all
functions are passed as parameters one single flat list of scalars,
and all functions likewise return to their caller one single flat list
of scalars. Any arrays or hashes in these call and return lists will
collapse, losing their identities--but you may always use
pass-by-reference instead to avoid this. Both call and return lists
may contain as many or as few scalar elements as you'd like. 

perldoc perlref
A reference to an anonymous hash can be created using curly brackets:

$hashref = {
'Adam'  = 'Eve',
'Clyde' = 'Bonnie',
};


So. Anonymous hashes create a reference. So its returning a ref. And
subs can't return hashes in any case. Just scalar lists.

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




Re: help in while loop

2008-03-17 Thread telemachus07
On Mar 17, 7:40 am, [EMAIL PROTECTED] (Irfan Sayed) wrote:
 Hi All,

 Can somebody please let me know the meaning of this line.

 while ($in)

 {

 if(/,/) {print before match: $`\t and after match: $'\n\n;}; $x=$';
 $y=$`; mysubroutine($x,$y);

 }

 I know it is a while loop for the file handle ($in) and it will be
 executed till the end of file but not getting the meaning of if loop.

 What this if loop actually does. Please help

Does it help to see it this way?

while ($in) {
if (/,/) {   # Check each line of $in for a match of ,
(the comma)
print before match: $`\t and after match: $'\n\n;  # Print
strings captured before and after the comma
$x = $';  # Assign $x whatever is in $'
$y = $`; # Assign $y whatever is in $`
mysubroutine($x, $y); # Call the subroutine mysubroutine
with $x and $y as parameters
}
}

The variables $` $ and $' are special. They match (repectively) $` -
the stuff before your match, $ - what you match, and $' - the stuff
left after your match. Many people avoid them like the plague since
they slow down regular expression matching. (Check perldoc perlvar.)
As to what mysubroutine actually *does*, there's no way to know
without seeing the code for that subroutine.

Hope this helps, T


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




diff of two files

2008-03-17 Thread Irfan.Sayed
Hi All,

 

I want to compare the two files in Perl. The requirement is that file 1
has 20 lines and file2 has 25 lines . I want to see only five lines as
output which are there in file2 not in file1 as I know that rest of the
20 lines are same in both the files.

I am using the following commands 

 

Diff /tmp/dev_path_PRS.DB.01.12.000 /tmp/dep_path_D.PRS.DB.01.12.000.8

 

As per my understating I should get 5 lines as output

 

Could you please help in getting exact result.

 

Please help

 

Regards,

Irfan

 



Re: functions: rotate and factorial

2008-03-17 Thread Sharan Basappa
Rob,

Actually you are correct. I was on my way to implement permutation for
a given set of numbers.
In that context, I had designed my own algo as a part of bigger
problem I am trying to solve.
That algo requires rotate and factorial. Rotate to get different
combinations and factorial to limit for loop.
Today, I happened to go through Steinhaus-Johnson-Trotter's algo for the same.
It looks elegant and should be efficient. I plan to use this.
Question: Does anyone know if perl has inuilt function to implement
Steinhaus-Johnson-Trotter's?

Regards,
Sharan

On Fri, Mar 14, 2008 at 8:33 PM, Rob Dixon [EMAIL PROTECTED] wrote:
 Sharan Basappa wrote:
   
   Thanks everybody. I need to use these as a part of algo I am working
   on. I will get back if I have any comments ..

  Rotating an array and calculating a factorial are both likely to absorb
  large amounts of processor time unless your problem is trivial. I'm also
  intrigued to hear of an algorithm that makes use of factorials. Perhaps
  your solution needs looking at before you implement it?

  Rob



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




Re: functions: rotate and factorial

2008-03-17 Thread Chas. Owens
On Mon, Mar 17, 2008 at 10:34 AM, Sharan Basappa
[EMAIL PROTECTED] wrote:
 Rob,

  Actually you are correct. I was on my way to implement permutation for
  a given set of numbers.
  In that context, I had designed my own algo as a part of bigger
  problem I am trying to solve.
  That algo requires rotate and factorial. Rotate to get different
  combinations and factorial to limit for loop.
  Today, I happened to go through Steinhaus-Johnson-Trotter's algo for the 
 same.
  It looks elegant and should be efficient. I plan to use this.
  Question: Does anyone know if perl has inuilt function to implement
  Steinhaus-Johnson-Trotter's?snip
snip

I don't know what algorithm Algorithm::Permute* uses, but it seems fairly fast.

* http://search.cpan.org/dist/Algorithm-Permute/Permute.pm

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

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




RE: Variables initialization

2008-03-17 Thread Thomas Bätzler
[EMAIL PROTECTED] asked;
 How is it possible to initialize variable using 0 or '' and 
 not having undef warning later on using them?

my $variable = 0; # = initial value goes here

HTH,
Thomas


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




RE: help in while loop

2008-03-17 Thread Thomas Bätzler
[EMAIL PROTECTED] asked:
 I have certain doubts. 
 
 What's the meaning of  if
 mysubroutine was defined with prototypes and you were trying 
 to disable that sentence. 
 
 Could you please elaborate that what's the meaning of this???

When declaring a subroutine, you can optionally also declare
a prototype for it - i.e. the number and type of arguments.

This allows for a limited sort of compile time parameter
checking aswell as parameter type coercion.

For example:

#!/usr/bin/perl -w

use strict;

sub prototyped ($) {
  print prototyped args: , join(', ', @_ ), \n;
}

sub unprototyped {
  print unprototyped args: , join(', ', @_ ), \n;
}

my @args = qw( foo baz bar );

prototyped @args;
unprototyped @args;
prototyped @args;

__END__

Output:

prototyped args: 3
unprototyped args: foo, baz, bar
prototyped args: foo, baz, bar

The first sub is declared with a prototype to expect a scalar value as the 
first argument. This declaration forces the argument @args into scalar context, 
so that it evaluates to the number of elements in the array, i.e. 3.

Without the prototype, the array is passed as an array to the subroutine.

If you use the old-style subroutine calling syntax with a prepended , any 
prototypes for the function are disabled.

Please see the perlsub manpage (perldoc perlsub) for the
gory details on prototypes and subroutine calling syntax.

HTH,
Thomas



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




Re: xml::twig help

2008-03-17 Thread Rob Dixon

Ken Foskey wrote:

I am extracting addresses from an XML file to process through other
programs using pipe delimiter the following code works but this is going
to get 130,000 records through it it must be very efficient and I cannot
follow the documentation on the best way to do this.

After this simple one is programmed I have to change a much more complex
version of this program.

#!/usr/bin/perl -w
# vi:set sw=4 ts=4 et cin:
# $Id:$

=head1 SYNOPSIS

Extract addresses from an XML file into pipe delimited file.

   usage: address_extract.pl  xml_file

=cut

use warnings;
use strict;

use XML::Twig qw(:strict);

sub no_pipe
{
my $value = shift;

$value =~ s/\|//g;
return $value;
}

if( ! -f $ARGV[0] ) {
print $ARGV[0] is not a filename, requires filename as first
parameter!\n;
}

my $sort;
my $sort_file = $ARGV[0].'.unsorted';
unlink $sort_file; # in case of rerun
open( $sort, '', $sort_file  ) 
or die Unable to open $sort_file for output $!;


my $ref = XML::Twig-new( twig_handlers={mem=\member} ) 
or die Unable to open $ARGV[0] $!;


my $member = 0;

$ref-parsefile( $ARGV[0] );

sub get_value
{
my ($mem_ref, $key) = @_;
my @array = $mem_ref-descendants( $key );
return $array[0]-text();
}

sub member {
my ($t, $mem_ref) = @_;
$member++;

my $mem_no = get_value( $mem_ref, 'member' );
my $add1   = get_value( $mem_ref, 'add1' );
my $add2   = get_value( $mem_ref, 'add2' );
my $add3   = get_value( $mem_ref, 'add3' );
my $suburb = get_value( $mem_ref, 'suburb' );
my $state  = get_value( $mem_ref, 'state' );
my $pcode  = get_value( $mem_ref, 'pcode' );

print $sort join( '|', $member,
 $mem_no,
 no_pipe( $add1 ),
 no_pipe( $add2 ),
 no_pipe( $add3 ),
 no_pipe( $suburb),
 $state,
 $pcode,
) .\n;
return 1;
}


What is your question? I ran you program against XML data like

root
  mem
membermember/member
add1add1/add1
add2add2/add2
add3add3/add3
suburbsuburb/suburb
statestate/state
pcodepcode/pcode
  /mem
/root

and it seemed to work fine.

I would only change it cosmetically, for instance it would be nicer to
pull the first command-line parameter off into a variable, and you need
to die if there isn't one, not just print a message and carry on.

my $file = shift;

unless ($file and -f $file) {
  die $file is not a filename, requires filename as first parameter!\n;
}

also, you could replace all your calls to get_value with

my $mem_no = $mem_ref-first_child('member')-text;

and so on, but there seems to be no problem with the basic
functionality. Let us know if you need any further help.

HTH,

Rob



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




Re: Hash CSV

2008-03-17 Thread JBallinger
On Mar 14, 3:26 pm, [EMAIL PROTECTED] (Manoj) wrote:
 When using Data: Dumper is taking more time for my 1 lines of CSV file.
 This solved a few queries...and the benchmark was a new value addition for
 me. Thanks
  2)       Is there any optimal method for reading a CSV file and put to
 hash table.

  May have better approaches however this read CSV into Hash

  use Data::Dumper;
  open(INFILE, , sample.csv) or die $!;
  my %hsh;
  %hsh = ( %hsh, (split(/,/, $_))[1,2] ) while ( INFILE );

 That is a *very* inefficient way to populate a hash as you are copying
 the entire hash for every record in the file.  Better to add the keys
 and values individually:

 my %hsh;
 while ( INFILE ) {
      chomp;
      my ( $key, $value ) = split /,/;
      }

  print Dumper \%hsh;

 John

 - Show quoted text -

Suppose my csv file has 5 columns: f id fa mo ge
Will my ($key, $value) = split/,/; still work?
Is there any other mothod that is more efficient?

JB


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




perl warnings

2008-03-17 Thread Kashif Salman
Hello,
I have a CGI script; when it runs the very first time I define some variables
my $action = $q-param('action');

The first time it runs, parameter 'action' isn't defined so that is
how I check that it is running the first time and do my things

if ($aciton eq ) {...}
elsif ($action eq submit) {...}

the elsif runs if I hit a button on a form which has a hidden field
that sets action=submit. My question is that the script produces a
warning on the if statement  Use of uninitialized value in string eq
. How can I get rid of that without using no warnings. I tried 'if
(defined($action))' but that still produces a warning.

Regards,
Kash

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




Re: diff of two files

2008-03-17 Thread Gunnar Hjalmarsson

[EMAIL PROTECTED] wrote:

I want to compare the two files in Perl. The requirement is that file 1
has 20 lines and file2 has 25 lines . I want to see only five lines as
output which are there in file2 not in file1 as I know that rest of the
20 lines are same in both the files.

I am using the following commands 


Diff /tmp/dev_path_PRS.DB.01.12.000 /tmp/dep_path_D.PRS.DB.01.12.000.8


C:\hometype test.pl
use strict;
Diff /tmp/dev_path_PRS.DB.01.12.000
 /tmp/dep_path_D.PRS.DB.01.12.000.8

C:\homeperl test.pl
Bareword Diff not allowed while strict subs in use at test.pl line 2.
Bareword tmp not allowed while strict subs in use at test.pl line 2.
Bareword dev_path_PRS not allowed while strict subs in use at 
test.pl line 2.

Bareword DB not allowed while strict subs in use at test.pl line 2.
Bareword tmp not allowed while strict subs in use at test.pl line 2.
Bareword dep_path_D not allowed while strict subs in use at test.pl 
line 2.

Bareword PRS not allowed while strict subs in use at test.pl line 2.
Bareword DB not allowed while strict subs in use at test.pl line 2.
Execution of test.pl aborted due to compilation errors.

C:\home

The FAQ entry perldoc -q difference.+arrays may be useful.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: perl warnings

2008-03-17 Thread yitzle
I think you want:
if( defined $q-param('action') ) {
} else {
}

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




Re: perl warnings

2008-03-17 Thread Gunnar Hjalmarsson

Kashif Salman wrote:

I have a CGI script; when it runs the very first time I define some variables
my $action = $q-param('action');

The first time it runs, parameter 'action' isn't defined so that is
how I check that it is running the first time and do my things

if ($aciton eq ) {...}
elsif ($action eq submit) {...}

the elsif runs if I hit a button on a form which has a hidden field
that sets action=submit. My question is that the script produces a
warning on the if statement  Use of uninitialized value in string eq
. How can I get rid of that without using no warnings. I tried 'if
(defined($action))' but that still produces a warning.


if ( ! $action ) {...}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: perl warnings

2008-03-17 Thread Kashif Salman
On Mon, Mar 17, 2008 at 11:46 AM, Gunnar Hjalmarsson [EMAIL PROTECTED] wrote:

 Kashif Salman wrote:
   I have a CGI script; when it runs the very first time I define some 
 variables
   my $action = $q-param('action');
  
   The first time it runs, parameter 'action' isn't defined so that is
   how I check that it is running the first time and do my things
  
   if ($aciton eq ) {...}
   elsif ($action eq submit) {...}
  
   the elsif runs if I hit a button on a form which has a hidden field
   that sets action=submit. My question is that the script produces a
   warning on the if statement  Use of uninitialized value in string eq
   . How can I get rid of that without using no warnings. I tried 'if
   (defined($action))' but that still produces a warning.

  if ( ! $action ) {...}

  --


if( defined $q-param('action') )#still produces the warning
if ( ! $action )  #worked

thanks

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




Re: perl warnings

2008-03-17 Thread John W. Krahn

Kashif Salman wrote:

Hello,


Hello,


I have a CGI script; when it runs the very first time I define some variables
my $action = $q-param('action');

The first time it runs, parameter 'action' isn't defined so that is
how I check that it is running the first time and do my things

if ($aciton eq ) {...}
elsif ($action eq submit) {...}

the elsif runs if I hit a button on a form which has a hidden field
that sets action=submit. My question is that the script produces a
warning on the if statement  Use of uninitialized value in string eq
.


$ perl -le'
use warnings;
use strict;

my $action;

if ( $action eq  ) {
print \$action is empty;
}
'
Use of uninitialized value in string eq at -e line 7.
$action is empty



How can I get rid of that without using no warnings. I tried 'if
(defined($action))' but that still produces a warning.


$ perl -le'
use warnings;
use strict;

my $action;

if ( defined $action ) {
print \$action is empty;
}
'
Use of uninitialized value in string at -e line 7.
$action is empty


You are not testing the variable $action.  You are testing a string that 
just happens to contain the variable $action.  defined() needs to test 
the actual variable itself:


if ( defined $action ) {


Also see the FAQ entry:

perldoc -q quoting



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

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




Client-Server Architecture script

2008-03-17 Thread Anirban Adhikary
Dear List
I want to write a script using TCP protocol where will be a single server
which can handle  multiple client request  simultaneously. I am able to
write a script using IO::Socket but In these scripts my server can handle
single client suppose my server is printing the IP address of the client
and print 10 times hello world like this But that is not the
thing which I want . I need  a script which will print the IP
address of the client from which it receives the request and also execute
the request which it is receiving from the clients. Regarding this if I get
u people's help I will be highly obliged.

ThanksRegards in advance
Anirban Adhikary.


Re: perl warnings

2008-03-17 Thread Paul Lalli
On Mar 17, 2:08 pm, [EMAIL PROTECTED] (Kashif Salman) wrote:
 Hello,
 I have a CGI script; when it runs the very first time I define some variables
 my $action = $q-param('action');

 The first time it runs, parameter 'action' isn't defined so that is
 how I check that it is running the first time and do my things

 if ($aciton eq ) {...}
 elsif ($action eq submit) {...}

 the elsif runs if I hit a button on a form which has a hidden field
 that sets action=submit. My question is that the script produces a
 warning on the if statement  Use of uninitialized value in string eq
 . How can I get rid of that without using no warnings. I tried 'if
 (defined($action))' but that still produces a warning.

perldoc -q quoting

Stop double quoting your variables.  The warning is telling you that
you're using an uninitialized value in a string.  That warning is
important and relevant.  It's telling you that it's the *STRING*
you're checking for defined'ness, not the variable within the string.
The string will *always* be defined, regardless of whether or not the
variable is.

if (defined($action)) { ... }
not
if(defined($action)) { ... }


$ perl -wle'
my $foo;
if (defined($foo)) { print 1 yes; } else { print 1 no }
if (defined($foo))   { print 2 yes; } else { print 2 no }
'
Use of uninitialized value in string at -e line 3.
1 yes
2 no

Paul Lalli


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




Re: perl warnings

2008-03-17 Thread Paul Lalli
On Mar 17, 2:46 pm, [EMAIL PROTECTED] (Gunnar Hjalmarsson) wrote:
 Kashif Salman wrote:
  I have a CGI script; when it runs the very first time I define some 
  variables
  my $action = $q-param('action');

  The first time it runs, parameter 'action' isn't defined so that is
  how I check that it is running the first time and do my things

  if ($aciton eq ) {...}
  elsif ($action eq submit) {...}

  the elsif runs if I hit a button on a form which has a hidden field
  that sets action=submit. My question is that the script produces a
  warning on the if statement  Use of uninitialized value in string eq
  . How can I get rid of that without using no warnings. I tried 'if
  (defined($action))' but that still produces a warning.

      if ( ! $action ) {...}

That'll work great until some jackass puts ?action=0 in the URL.

Using defined() is correct.  It's what he's passing to defined()
that's not.

Paul Lalli


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




Re: perl warnings

2008-03-17 Thread Kashif Salman
On Mon, Mar 17, 2008 at 12:04 PM, Paul Lalli [EMAIL PROTECTED] wrote:
 On Mar 17, 2:46 pm, [EMAIL PROTECTED] (Gunnar Hjalmarsson) wrote:
   Kashif Salman wrote:
I have a CGI script; when it runs the very first time I define some 
 variables
my $action = $q-param('action');
  
The first time it runs, parameter 'action' isn't defined so that is
how I check that it is running the first time and do my things
  
if ($aciton eq ) {...}
elsif ($action eq submit) {...}
  
the elsif runs if I hit a button on a form which has a hidden field
that sets action=submit. My question is that the script produces a
warning on the if statement  Use of uninitialized value in string eq
. How can I get rid of that without using no warnings. I tried 'if
(defined($action))' but that still produces a warning.
  
if ( ! $action ) {...}

  That'll work great until some jackass puts ?action=0 in the URL.

  Using defined() is correct.  It's what he's passing to defined()
  that's not.

  Paul Lalli




  --


Thank you

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




Re: xml::twig help

2008-03-17 Thread Jay Savage
On Mon, Mar 17, 2008 at 9:55 AM, Ken Foskey [EMAIL PROTECTED] wrote:

  I am extracting addresses from an XML file to process through other
  programs using pipe delimiter the following code works but this is going
  to get 130,000 records through it it must be very efficient and I cannot
  follow the documentation on the best way to do this.

  After this simple one is programmed I have to change a much more complex
  version of this program.

  #!/usr/bin/perl -w
  # vi:set sw=4 ts=4 et cin:
  # $Id:$

  =head1 SYNOPSIS

  Extract addresses from an XML file into pipe delimited file.

usage: address_extract.pl  xml_file

  =cut

  use warnings;
  use strict;

  use XML::Twig qw(:strict);

  sub no_pipe
  {
 my $value = shift;

 $value =~ s/\|//g;
 return $value;
  }

  if( ! -f $ARGV[0] ) {
 print $ARGV[0] is not a filename, requires filename as first
  parameter!\n;
  }

  my $sort;
  my $sort_file = $ARGV[0].'.unsorted';
  unlink $sort_file; # in case of rerun
  open( $sort, '', $sort_file  )
 or die Unable to open $sort_file for output $!;

  my $ref = XML::Twig-new( twig_handlers={mem=\member} )
 or die Unable to open $ARGV[0] $!;

  my $member = 0;

  $ref-parsefile( $ARGV[0] );

  sub get_value
  {
 my ($mem_ref, $key) = @_;
 my @array = $mem_ref-descendants( $key );
 return $array[0]-text();
  }

  sub member {
 my ($t, $mem_ref) = @_;
 $member++;

 my $mem_no = get_value( $mem_ref, 'member' );
 my $add1   = get_value( $mem_ref, 'add1' );
 my $add2   = get_value( $mem_ref, 'add2' );
 my $add3   = get_value( $mem_ref, 'add3' );
 my $suburb = get_value( $mem_ref, 'suburb' );
 my $state  = get_value( $mem_ref, 'state' );
 my $pcode  = get_value( $mem_ref, 'pcode' );

 print $sort join( '|', $member,
  $mem_no,
  no_pipe( $add1 ),
  no_pipe( $add2 ),
  no_pipe( $add3 ),
  no_pipe( $suburb),
  $state,
  $pcode,
 ) .\n;
 return 1;
  }



Ken,

If you're really worried about performance, then I would say two
places to look first would be all the temporary variables and
subroutine invocations. I don't know the ins and outs of XML::Twig, so
I don't really have any concrete advice--for instance, does
'$mem_ref-descendants( $key )-text()' work? some modules will parse
a structure like that, some won't--but in general, think about
creative ways you might use map to avoid three subroutine invocations
and (5? 6?) temporary variables for each element you process.
Something like the following should get you started down the path:

## Untested!

sub member {
my $t = shift;

print join |,
map { s/\|//g }
map { $_[0]-descendants( $_ )-text() } qw/ member add1 add2 add3
suburb state pcode /;
}

That may not work out of the box depending on how deeply nested
XML::Twig's refs are, but hopefully you an see where I'm headed with
it.

Also, string concatenation is less efficient than adding another term
to prints argument list. i.e.: use ',' instead of '.' in print when
you can. and lastly, efficiency for matching is, IME, largely a matter
of system configuration and specific input data, but it's worth
benchmarking to see if substr() performs better than s/// in your
case. Sometimes you can get significant savings that way. sometimes
you can't, of course.

HTH,

-- jay
--
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org

values of β will give rise to dom!


flock - exclusive file locking

2008-03-17 Thread Dermot
Hi,

I have a cgi script that writes to a disk file. It would be safest if I can
get an exclusive lock on the file. I had a look at the opentut and believe I
have followed that the example there. Here's what I have

   sysopen my $fh, $file_path, O_WRONLY || die can't open
$file_path: $!\n;
flock($fh,LOCK_EX) or die can't lock $file_path: $!\n;
seek($fh, 0, 2);   # Append to file
print $fh $status.\n;
print STDERR $0: $! $?\n;
close($fh);

I am getting the error
Inappropriate ioctl for device 0

The worst part about this problem is that it seems completely erratic.
Sometimes the string is written to the file and sometimes not. Sometimes I
get an error and sometimes not but it never dies and the user will think the
data has been written to file! Does anyone see something wrong with the
snippet above or have any insights on file-clocking they could offer.

Thanx,
Dp.


Re: flock - exclusive file locking

2008-03-17 Thread Dr.Ruud
Dermot schreef:

sysopen my $fh, $file_path, O_WRONLY
|| die can't open $file_path: $!\n;

Change or-operator (or use parenthesis). 

-- 
Affijn, Ruud

Gewoon is een tijger.

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




Re: flock - exclusive file locking

2008-03-17 Thread Gunnar Hjalmarsson

Dermot wrote:

I have a cgi script that writes to a disk file. It would be safest if I can
get an exclusive lock on the file. I had a look at the opentut and believe I
have followed that the example there. Here's what I have

   sysopen my $fh, $file_path, O_WRONLY || die can't open
$file_path: $!\n;
flock($fh,LOCK_EX) or die can't lock $file_path: $!\n;
seek($fh, 0, 2);   # Append to file
print $fh $status.\n;
print STDERR $0: $! $?\n;
close($fh);

I am getting the error
Inappropriate ioctl for device 0


Even if I can't reproduce that error, I see one thing that is not correct.


sysopen my $fh, $file_path, O_WRONLY || die ...


That is interpreted as

sysopen my $fh, $file_path, (O_WRONLY || die ... )

i.e. the script will never die at that line, since the constant O_WRONLY 
is always true. You need to do either


sysopen( my $fh, $file_path, O_WRONLY ) || die ...
---^--^

or

sysopen my $fh, $file_path, O_WRONLY or die ...
-^^

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: xml::twig help

2008-03-17 Thread Jenda Krynicky
From:   Ken Foskey [EMAIL PROTECTED]
 I am extracting addresses from an XML file to process through other
 programs using pipe delimiter the following code works but this is going
 to get 130,000 records through it it must be very efficient and I cannot
 follow the documentation on the best way to do this.
 
 After this simple one is programmed I have to change a much more complex
 version of this program.
 
 snipped

 my $ref = XML::Twig-new( twig_handlers={mem=\member} ) 
 or die Unable to open $ARGV[0] $!;
 
 my $member = 0;
 
 $ref-parsefile( $ARGV[0] );
 
 sub get_value
 {
 my ($mem_ref, $key) = @_;
 my @array = $mem_ref-descendants( $key );
 return $array[0]-text();
 }
 
 sub member {
 my ($t, $mem_ref) = @_;
 $member++;
 
 my $mem_no = get_value( $mem_ref, 'member' );
 my $add1   = get_value( $mem_ref, 'add1' );
 my $add2   = get_value( $mem_ref, 'add2' );
 my $add3   = get_value( $mem_ref, 'add3' );
 my $suburb = get_value( $mem_ref, 'suburb' );
 my $state  = get_value( $mem_ref, 'state' );
 my $pcode  = get_value( $mem_ref, 'pcode' );
 
 print $sort join( '|', $member,
  $mem_no,
  no_pipe( $add1 ),
  no_pipe( $add2 ),
  no_pipe( $add3 ),
  no_pipe( $suburb),
  $state,
  $pcode,
 ) .\n;
 return 1;
 }

Looks like a perfect task for XML::Rules:

#!/usr/bin/perl
use strict;
use XML::Rules;

my $member;
my $parser = XML::Rules-new(
stripspaces = 7,
rules = {
_default = 'content',
mem = sub {
print join( '|', ++$member, map {(my $s = $_[1]-{$_}) 
=~ s/\|//; 
$s} qw(member add1 add2 add3 suburb state pcode)), \n;
return;
}
},
);

$parser-parse(\*DATA);

__DATA__
root
   mem
 membermember/member
 add1add1/add1
 add2add2/add2
 add3add3/add3
 suburbsuburb/suburb
 statestate/state
 pcodepcode/pcode
   /mem
   mem
 memberother/member
 add1ADD1/add1
 add2ADD2/add2
 add3ADD3/add3
 suburbsuburb 2/suburb
 statestate/state
 pcodepcode/pcode
   /mem
/root



I would expect this to be quicker than your XML::Twig solution, 
though I have to leave the benchmarking to you.

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




Re: perl warnings

2008-03-17 Thread Gunnar Hjalmarsson

Paul Lalli wrote:

On Mar 17, 2:46 pm, [EMAIL PROTECTED] (Gunnar Hjalmarsson) wrote:


 if ( ! $action ) {...}


That'll work great until some jackass puts ?action=0 in the URL.


So what? If you put random crap in the URL, you can't reasonably expect 
a meaningful response.


In this case, if I understand it correctly, the default version of the 
page, with a form, would appear. Why would that be a problem for anybody 
but the stupid user? ;-)


That said, I agree that using defined() is more to the point.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: flock - exclusive file locking

2008-03-17 Thread John W. Krahn

Dermot wrote:

Hi,


Hello,


I have a cgi script that writes to a disk file. It would be safest if I can
get an exclusive lock on the file. I had a look at the opentut and believe I
have followed that the example there. Here's what I have

sysopen my $fh, $file_path, O_WRONLY || die can't open $file_path: $!\n;


You are using the high precedence '||' operator which means that 
O_WRONLY is ORed with die() before sysopen() is called and since 
O_WRONLY is probably always true then die() will *never* be called.


You intend to append to the file so you should probably also include 
O_APPEND with the third argument.


You want to either use parentheses:

sysopen( my $fh, $file_path, O_WRONLY | O_APPEND ) || die can't open 
$file_path: $!\n;


Or use the low precedence 'or' operator:

sysopen my $fh, $file_path, O_WRONLY | O_APPEND or die can't open 
$file_path: $!\n;




flock($fh,LOCK_EX) or die can't lock $file_path: $!\n;
seek($fh, 0, 2);   # Append to file


It looks like you are using the Fcntl.pm module to get the LOCK_EX macro 
so you should use it to get the SEEK_END macro as well.  And since you 
are using sysopen() to open the file you should use sysseek() instead of 
seek()


sysseek $fh, 0, SEEK_END or die can't seek on $file_path: $!\n;

Although you don't really need to do this if you use O_APPEND with 
sysopen().




print $fh $status.\n;
print STDERR $0: $! $?\n;


It makes no sense to print the values of $! or $? unless *an* *error* 
*actually* *occured*.  Without an actual error the values in them will 
be meaningless.




close($fh);

I am getting the error
Inappropriate ioctl for device 0


That error message is not listed in perldiag.pod so it is not a Perl 
error/warning and it doesn't match any of the error messages in your 
code above so something other than your Perl program is producing it.


perldoc perldiag



The worst part about this problem is that it seems completely erratic.
Sometimes the string is written to the file and sometimes not. Sometimes I
get an error and sometimes not but it never dies and the user will think the
data has been written to file! Does anyone see something wrong with the
snippet above or have any insights on file-clocking they could offer.


perldoc -q lock



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

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




FW: How to install Date::Manip on cygwin perl?

2008-03-17 Thread Siegfried Heintze (Aditi)

Hmm... I did not see this appear on the list so I'm posting it again. I'm using 
cygwin perl on  windows and as you can see below, cpan seems to choking on the 
path name /cygdrive/c/Documents and settings/a-siehei/My Documents because of 
the spaces.

Surely someone else has a workaround!

My apologies if this appears twice.
Siegfried

From: Siegfried Heintze (Aditi)
Sent: Monday, March 17, 2008 12:42 PM
To: '[EMAIL PROTECTED]'
Subject: How to install Date::Manip on cygwin perl?

It looks like the CPAN program does not anticipate paths with spaces in them. I 
did a google search with no luck. Surely someone has already fixed this 
problem! How do I fix it?

Thanks,
Siegfried

$ perl -MCPAN -e 'install Date::Manip'
CPAN: Storable loaded ok
Going to read /cygdrive/c/Documents and Settings/a-siehei/.cpan/Metadata
  Database was generated on Mon, 17 Mar 2008 10:30:46 GMT
Running install for module Date::Manip
Running make for S/SB/SBECK/Date-Manip-5.48.tar.gz
CPAN: Digest::MD5 loaded ok
CPAN: Compress::Zlib loaded ok
Checksum for /cygdrive/c/Documents and Settings/a-siehei/.cpan/sources/authors/i
d/S/SB/SBECK/Date-Manip-5.48.tar.gz ok
Scanning cache /cygdrive/c/Documents and Settings/a-siehei/.cpan/build for sizes

sh: /cygdrive/c/Documents: No such file or directory
/usr/bin/tar: This does not look like a tar archive
/usr/bin/tar: Error exit delayed from previous errors
Uncompressed /cygdrive/c/Documents and Settings/a-siehei/.cpan/sources/authors/i
d/S/SB/SBECK/Date-Manip-5.48.tar.gz successfully
Using Tar:/usr/bin/tar xvf /cygdrive/c/Documents and Settings/a-siehei/.cpan/sou
rces/authors/id/S/SB/SBECK/Date-Manip-5.48.tar:
/usr/bin/tar: /cygdrive/c/Documents: Cannot open: No such file or directory
/usr/bin/tar: Error is not recoverable: exiting now
Couldn't untar /cygdrive/c/Documents and Settings/a-siehei/.cpan/sources/authors
/id/S/SB/SBECK/Date-Manip-5.48.tar


Re: perl warnings

2008-03-17 Thread Justin Hawkins


On 17 Mar 2008, at 21:58, Gunnar Hjalmarsson wrote:


In this case, if I understand it correctly, the default version of  
the page, with a form, would appear. Why would that be a problem for  
anybody but the stupid user? ;-)


Fundamentally, don't make it possible for your users to do anything to  
surprise you.


It would have been a benign surprise this time, once these bad habits  
become entrenched it will only be a matter of time before you get a  
nasty surprise.


Case in point:

I once wrote some indexing code, things being what they were I had to  
make fairly efficient use of disk space. I decided on a simple bit- 
packing scheme, so each byte could flag the existence of a word in up  
to 8 objects. With sparse files, this was quite efficient indeed.


Somewhere in the code I had a check like this:

if (! $index_byte ) {
  [ code to process the next byte ]
}
else {
  [ process this byte ]
}

Seems pretty bulletproof doesn't it? If the index byte is empty then  
we skip on to the next byte, as we don't have any entries at all for  
those 8 objects.


Testing showed a problem occasionally index entries were missing.  
A lot of digging found the culprit if you have a scalar byte  
holding 8 bits worth of data, it's not just when it holds 0x00 that  
that test is true - it's also when it holds 0x30 - ascii '0'.


ALWAYS make your tests as specific as possible, and care about the  
exceptions.


- Justin

--
Justin Hawkins
[EMAIL PROTECTED]
http://hawkins.id.au/~justin/





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




RE: How to install Date::Manip on cygwin perl?

2008-03-17 Thread Mike Wohlrab
Greetings,

I am not familiar at all with Perl, but from my experiences with MS-DOS, if you
want to have the CLI read something, IE file name or path, then you need to
enclose the filename and path in quotes. So if you could try to enclose the path
in quotes, that may work.

Regards,

Mike Wohlrab
www.mikewohlrab.com

-Original Message-
From: Siegfried Heintze (Aditi) [mailto:[EMAIL PROTECTED] 
Sent: Monday, March 17, 2008 6:34 PM
To: beginners@perl.org
Subject: FW: How to install Date::Manip on cygwin perl?


Hmm... I did not see this appear on the list so I'm posting it again. I'm using
cygwin perl on  windows and as you can see below, cpan seems to choking on the
path name /cygdrive/c/Documents and settings/a-siehei/My Documents because of
the spaces.

Surely someone else has a workaround!

My apologies if this appears twice.
Siegfried

From: Siegfried Heintze (Aditi)
Sent: Monday, March 17, 2008 12:42 PM
To: '[EMAIL PROTECTED]'
Subject: How to install Date::Manip on cygwin perl?

It looks like the CPAN program does not anticipate paths with spaces in them. I
did a google search with no luck. Surely someone has already fixed this problem!
How do I fix it?

Thanks,
Siegfried

$ perl -MCPAN -e 'install Date::Manip'
CPAN: Storable loaded ok
Going to read /cygdrive/c/Documents and Settings/a-siehei/.cpan/Metadata
  Database was generated on Mon, 17 Mar 2008 10:30:46 GMT
Running install for module Date::Manip
Running make for S/SB/SBECK/Date-Manip-5.48.tar.gz
CPAN: Digest::MD5 loaded ok
CPAN: Compress::Zlib loaded ok
Checksum for /cygdrive/c/Documents and Settings/a-siehei/.cpan/sources/authors/i
d/S/SB/SBECK/Date-Manip-5.48.tar.gz ok
Scanning cache /cygdrive/c/Documents and Settings/a-siehei/.cpan/build for sizes

sh: /cygdrive/c/Documents: No such file or directory
/usr/bin/tar: This does not look like a tar archive
/usr/bin/tar: Error exit delayed from previous errors
Uncompressed /cygdrive/c/Documents and Settings/a-siehei/.cpan/sources/authors/i
d/S/SB/SBECK/Date-Manip-5.48.tar.gz successfully
Using Tar:/usr/bin/tar xvf /cygdrive/c/Documents and Settings/a-siehei/.cpan/sou
rces/authors/id/S/SB/SBECK/Date-Manip-5.48.tar:
/usr/bin/tar: /cygdrive/c/Documents: Cannot open: No such file or directory
/usr/bin/tar: Error is not recoverable: exiting now
Couldn't untar /cygdrive/c/Documents and Settings/a-siehei/.cpan/sources/authors
/id/S/SB/SBECK/Date-Manip-5.48.tar


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




Re: while reading 'mastering perl' @+ and @-, not too clear on this

2008-03-17 Thread Richard Lee




Say you have the string abcdefghi.

The positions in the string are:

  a b c d e f g h i
 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
 0 1 2 3 4 5 6 7 8 9

If you have the regular expression:

/(de)/

Then the match starts at position 3, moves forward two characters, and 
ends at position 5, where the next match, if any, will start.


If it stopped at position 4 then it would only match 1 character.

If you want to explore all the gory details of regular expressions 
then get the book _Mastering Regular Expression_ by Jeffrey E. F. Friedl:


http://www.oreilly.com/catalog/regex3/index.html



John

Hi, John

Took your advice and start to read 'Mastering regular expression' by 
Jeffrey E.F.Friedl,


Can you explain below further?

on page, 205

push(@fields, $+) while $text =~ m{
   ([^\\\]*(?:\\.[^\\\]*)*),?   #standard quoted string(with 
possible comma)
|  ([^,]+),? #or up to next comman(with 
possible comma)

   |   ,
}gx;

I am not totally understanding how the first line is matching standard 
quoted string.


I understand  in beginning and end and option ,? at the very end.

now why ---  [^\\\]*  , should read anything except \ and  and \\ ?   
why?


and then followed by pretty much samething(in grouping only option ?:) 
followed by \\ ?? and samething?


How is that suppose to match quoted string? such as  hi, how are you,

Also, how does this work?

defined($1) ? $1 : $3;This ternary reads if $1 is true, then test to 
see if $1 is defined? IF $1 is not true, then $3 is defined?(or ??)



   


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




\Q on an pattern containing double quotes, braces etc.

2008-03-17 Thread R (Chandra) Chandrasekhar

Dear Folks,

I want to comment out certain lines in a file that match a particular pattern. 
The file contains lines with characters like: {, }, ==, and . Specifically, I 
want to replace lines beginning with


ATTRS{idVendor}==07b4, ATTRS{idProduct}==0109,

with

# ATTRS{idVendor}==07b4, ATTRS{idProduct}==0109,

I have been unsuccessful in including the pattern as above and have had to work 
around using just the two number patterns like so:



#!/usr/bin/perl -i.bak -pl
use warnings;
use strict;
m/07b4/ and m/0109/ and s/$_/# $_/;


This works, but leaves me wondering how I could include the full pattern, 
including metacharacters. Using \Q did not help, but perhaps I was doing 
something wrong.


I have included some sample lines that can be used as an input file.


ATTRS{idVendor}==0553, ATTRS{idProduct}==0202, MODE=0660, GROUP=plugdev
ATTRS{idVendor}==06bd, ATTRS{idProduct}==0403, MODE=0660, GROUP=plugdev
ATTRS{idVendor}==07b4, ATTRS{idProduct}==0109, MODE=0660, GROUP=plugdev
ATTRS{idVendor}==06bd, ATTRS{idProduct}==0404, MODE=0660, GROUP=plugdev
ATTRS{idVendor}==04fc, ATTRS{idProduct}==504b, MODE=0660, GROUP=plugdev
ATTRS{idVendor}==07b4, ATTRS{idProduct}==0100, MODE=0660, GROUP=plugdev
ATTRS{idVendor}==07b4, ATTRS{idProduct}==0105, MODE=0660, GROUP=plugdev
ATTRS{idVendor}==07b4, ATTRS{idProduct}==0114, MODE=0660, GROUP=plugdev
ATTRS{idVendor}==07b4, ATTRS{idProduct}==0114, MODE=0660, GROUP=plugdev
ATTRS{idVendor}==07b4, ATTRS{idProduct}==0109, MODE=0660, GROUP=plugdev
ATTRS{idVendor}==07b4, ATTRS{idProduct}==0105, MODE=0660, GROUP=plugdev


Thanks.

Chandra

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




Re: \Q on an pattern containing double quotes, braces etc.

2008-03-17 Thread John W. Krahn

R (Chandra) Chandrasekhar wrote:

Dear Folks,


Hello,

I want to comment out certain lines in a file that match a particular 
pattern. The file contains lines with characters like: {, }, ==, and . 


'{' is only special in a regular expression if it is immediately 
followed by a numerical digit, for example '{7}' is special whereas 
'{abc}' is not.  '}' is not special unless it is part of '{numerical 
digits}'.  '=' and '' are never special in a regular expression.




Specifically, I want to replace lines beginning with

ATTRS{idVendor}==07b4, ATTRS{idProduct}==0109,


There is nothing in that that needs to be escaped.



with

# ATTRS{idVendor}==07b4, ATTRS{idProduct}==0109,

I have been unsuccessful in including the pattern as above and have had 
to work around using just the two number patterns like so:



#!/usr/bin/perl -i.bak -pl
use warnings;
use strict;
m/07b4/ and m/0109/ and s/$_/# $_/;


Use s/^/# /; instead of s/$_/# $_/;.





This works, but leaves me wondering how I could include the full 
pattern, including metacharacters. Using \Q did not help, but perhaps I 
was doing something wrong.


I have included some sample lines that can be used as an input file.


ATTRS{idVendor}==0553, ATTRS{idProduct}==0202, MODE=0660, 
GROUP=plugdev


$ perl -Mre=debug -wle'my $x = qr/ATTRS{idVendor}==0553, 
ATTRS{idProduct}==0202, MODE=0660, GROUP=plugdev/'

Freeing REx: `,'
Compiling REx `ATTRS{idVendor}==0553, ATTRS{idProduct}==0202, 
MODE=0660, GROUP=plugdev'

size 22 Got 180 bytes for offset annotations.
first at 1
   1: EXACT ATTRS{idVendor}==0553, ATTRS{idProduct}==0202, 
MODE=066...(22)

  22: END(0)
anchored ATTRS{idVendor}==0553, ATTRS{idProduct}==0202, 
MODE=0660, GROUP=plugdev at 0 (checking anchored isall) minlen 79

Offsets: [22]
1[79] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 
0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 80[0]
Freeing REx: `ATTRS{idVendor}==\0553\, ATTRS{idProduct}==\0202\, 
MODE..'



Nope, nothing in there that needs to be escaped.


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

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