Re: one liner for replacing under Windows?

2008-06-14 Thread Xavier Noria
On Sat, Jun 14, 2008 at 9:56 AM, Octavian Rasnita [EMAIL PROTECTED] wrote:

 Is there a one-liner command that can replace a certain text with another in
 more files specified with wildcards like *.html that works under Windows
 cmd?

Since you ask this you probably know the Windows shell does not expand
wildcards. There's a trick for Perl one-liners though (untested):

  perl -e @ARGV = glob(qq($ARGV[0])); s/foo/bar/ while  *.html

You see the idea.

-- fxn

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




Re: one liner for replacing under Windows?

2008-06-14 Thread Xavier Noria
On Sat, Jun 14, 2008 at 1:05 PM, Octavian Rasnita [EMAIL PROTECTED] wrote:

 From: Xavier Noria [EMAIL PROTECTED] On Sat, Jun 14, 2008 at 9:56 AM,
 Octavian Rasnita [EMAIL PROTECTED] wrote:

 Is there a one-liner command that can replace a certain text with another
 in
 more files specified with wildcards like *.html that works under Windows
 cmd?

 Since you ask this you probably know the Windows shell does not expand
 wildcards. There's a trick for Perl one-liners though (untested):

  perl -e @ARGV = glob(qq($ARGV[0])); s/foo/bar/ while 

 You see the idea.


 Thank you but I don't think it works without writing a full perl program in
 a single line that does that.

 If I just replace the string in $_, the files are not updated.

 If I also use -pi.bak parameter in order to do that, it gives an error
 telling that the specified file (*.txt) can't be opened.

 So I can't see another solution than opening the files, making the
 replacement than writing the new content.

Absolutely, try putting a BEGIN block around (again untested):

   perl -pi.bak -e BEGIN { @ARGV = glob(shift) } s/foo/bar/g *.html

-- fxn

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




Re: what is ^M at the end of a line?

2008-05-21 Thread Xavier Noria
On Wed, May 21, 2008 at 4:20 PM, Remy Guo [EMAIL PROTECTED] wrote:

 it's really interesting... then how can i match that ^M using regex?
 i've tried chomp when reading each line but it doesn't work...

That's \r everywhere except in Macs before Mac OS X. Some programs
display \r as ^M but that's just a way to show it, there's really
just one character.

I you'd like to order the ideas about how newlines work have a look at
this article:

   http://www.onlamp.com/pub/a/onlamp/2006/08/17/understanding-newlines.html

-- fxn

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




Re: error message

2008-04-20 Thread Xavier Noria

On Apr 20, 2008, at 21:57 , Levente Kovacs wrote:


Can anyone tell me what this error message mean?

syntax error at ./dbubdate.pl line 181, near }continue


The code around that line in that script is not well-formed. If you  
don't know what to do please send lines 170 to 190 specifying which  
one is line 181.


-- fxn


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




Re: error message

2008-04-20 Thread Xavier Noria

On Apr 20, 2008, at 22:45 , Levente Kovacs wrote:

Thank you very much for your answer. I am sitting here for a day  
seeng what
could be wrong, but I can't find it. I post a link to my code here.  
Thank you

for your help in advance.

http://logonex.eu/cgi-bin/viewvc/viewvc.cgi/scripts4geda/dbubdate.pl?revision=127

http://logonex.eu/cgi-bin/viewvc/viewvc.cgi/scripts4geda/dbubdate.pl?view=log


That script does not seem to contain any occurrence of continue, are  
you sure that's the one?


-- fxn


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




Re: Best way to grab lines n thru x of a file

2008-04-10 Thread Xavier Noria

On Apr 10, 2008, at 18:24 , Jonathan Mast wrote:

Hi, I have a ~125MB file of which I want to read lines n thru n+x  
and write

those into a separate file.  What is the best way to go about this?


Tie::File

--
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: to test maximum consecutive numbers

2007-12-11 Thread Xavier Noria

On Dec 11, 2007, at 9:53 PM, ciwei2103 wrote:


Give a disk sequence know that is incrementing with base 16,  such
as ,

0001
0003
0004
0008
0009
000A
000B
000F
0010
0011

how do I extract the a subset that is have  at least 4 consecutives,
such as  0008 0009 000A 000B


I hope this has to do with backups and not homework:

use strict;
use warnings;

use constant AT_LEAST_THESE_MANY = 4;

my @disks = qw(
0001
0003
0004
0008
0009
000A
000B
000F
0010
0011
);

my @codes = map hex, @disks;

my $start;
my $nsucc = 0;
for (my $i = 0; $i  $#codes; ++$i) {
my $diff = $codes[$i+1] - $codes[$i];
if ($diff == 1  $i + 1  $#codes) {
++$nsucc;
} elsif ($nsucc + 1 = AT_LEAST_THESE_MANY) {
$start = $i - $nsucc;
last;
} else {
$nsucc = 0;
}
}

print @disks[$start..($start+$nsucc)]\n if $start;

-- fxn


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




Re: to test maximum consecutive numbers

2007-12-11 Thread Xavier Noria

On Dec 11, 2007, at 9:53 PM, ciwei2103 wrote:


Give a disk sequence know that is incrementing with base 16,  such
as ,

0001
0003
0004
0008
0009
000A
000B
000F
0010
0011

how do I extract the a subset that is have  at least 4 consecutives,
such as  0008 0009 000A 000B


Sorry, the previous code missed an edge case, I think this one is  
correct:


use strict;
use warnings;

use constant AT_LEAST_THESE_MANY = 4;

my @disks = qw(
   0001
   0003
   0004
   0008
   0009
   000A
   000B
   000F
   0010
   0011
);

my @codes = map hex, @disks;

my $start;
my $nsucc = 0;
for (my $i = 0; $i  $#codes; ++$i) {
my $diff = $codes[$i+1] - $codes[$i];
++$nsucc if $diff == 1;
if ($nsucc + 1 = AT_LEAST_THESE_MANY  ($diff != 1 || $i + 1 ==  
$#codes)) {

$start = $i - $nsucc;
++$start if $diff == 1;
last;
} elsif ($diff != 1) {
$nsucc = 0;
}
}

print @disks[$start..($start+$nsucc)]\n if $start;


-- fxn


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




Re: fixed list combinatorics

2007-11-30 Thread Xavier Noria

On Nov 29, 2007, at 2:06 AM, Xavier Noria wrote:

Indeed, the iterator provided by Algorithm::Combinatorics is faster  
only for lists of sizes = 7. (And gets to be twice as fast for size  
16.)

Certainly there's room for improvement here.


For the archives, I copied the iterator in List::PowerSet and rewrote  
it in XS for Algorithm::Combinatorics, that's up in the 0.25.


-- fxn


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




Re: fixed list combinatorics

2007-11-28 Thread Xavier Noria

On Nov 28, 2007, at 8:58 PM, yitzle wrote:


In a personal email conversation, he realized what he was actually
looking for is the power set.
List::PowerSet
http://search.cpan.org/~nikc/List-PowerSet-0.01/lib/List/PowerSet.pm


If speed is an issue Algorith::Combinatorics provides subsets() in XS:

  http://search.cpan.org/~fxn/Algorithm-Combinatorics-0.24/Combinatorics.pm

-- fxn

Disclaimer: That one is mine but that does not matter.

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




Re: fixed list combinatorics

2007-11-28 Thread Xavier Noria

On Nov 28, 2007, at 9:54 PM, Xavier Noria wrote:


On Nov 28, 2007, at 8:58 PM, yitzle wrote:


In a personal email conversation, he realized what he was actually
looking for is the power set.
List::PowerSet
http://search.cpan.org/~nikc/List-PowerSet-0.01/lib/List/PowerSet.pm


If speed is an issue Algorith::Combinatorics provides subsets() in XS:


I had done some benchmarks which are included in the distribution but  
not this one.


I've found that List::PowerSet outperforms Algorithm::Combinatorics'  
subset(). Time to revise that subroutine.


-- fxn


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




Re: fixed list combinatorics

2007-11-28 Thread Xavier Noria

On Nov 28, 2007, at 10:37 PM, Xavier Noria wrote:


On Nov 28, 2007, at 9:54 PM, Xavier Noria wrote:


On Nov 28, 2007, at 8:58 PM, yitzle wrote:


In a personal email conversation, he realized what he was actually
looking for is the power set.
List::PowerSet
http://search.cpan.org/~nikc/List-PowerSet-0.01/lib/List/PowerSet.pm


If speed is an issue Algorith::Combinatorics provides subsets() in  
XS:


I had done some benchmarks which are included in the distribution  
but not this one.


I've found that List::PowerSet outperforms Algorithm::Combinatorics'  
subset(). Time to revise that subroutine.


Indeed, the iterator provided by Algorithm::Combinatorics is faster  
only for lists of sizes = 7. (And gets to be twice as fast for size  
16.)


The subroutine that gives you all the subsets in one shot has its own  
implementation in List::PowerSet (it is not a wrapper around the  
iterator). It is recursive and fast, and clever! I think it is that  
fast because it actually runs basically in C:


  [ map { [$first, @$_ ], [ @$_] } @$pow ];

given that map is an opcode.

For that call Algorithm::Combinatorics matches and performs slighly  
better than List::PowerSet from size 14 on. I guess that's because it  
creates less intermediate stuff.


Certainly there's room for improvement here.

-- fxn


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




Re: Writing DOS CRLF via Unix Perl

2007-11-09 Thread Xavier Noria

On Nov 8, 2007, at 9:33 PM, C.R. wrote:

I run a script on unix Perl to write a text file. By default, when  
Perl

writes \n it writes a line ending sequence which is native to the
current OS. How do I force this particular script to always write DOS
CRLF line endings?


A good approach is to hard-code CRLF:

  my $CRLF = \015\012;

and then output that by hand the same way you would append \n:

  print foo bar baz$CRLF;

You don't mention the runtime platform. If you are sure the runtime  
platform uses Unix line-ending conventions you're done. If you need  
the script to be portable or don't want to leave there that brittle  
assumption you need to work in binmode.


Otherwise, since CRLF has a LF, on Windows you'd end up having triple  
CRCRLF on disk, because the I/O layer translates LF - CRLF on writing  
no matter the surrounding characters.


Even if I weren't the author :-) I'd add a pointer to this article:

  Understanding Newlines
  http://www.onlamp.com/pub/a/onlamp/2006/08/17/understanding-newlines.html

-- fxn




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




Re: peek next line in file

2007-09-27 Thread Xavier Noria

On Sep 27, 2007, at 1:29 AM, Mahurshi Akilla wrote:


Is there a way in perl to peek the next line in the file while keeping
the line pointer the same?

I want to do something like this:

while (INFILE)
{

//do some stuff

//?? peek next line ?? and enter conditional block//

//do some more stuff
}


You could do this:

my $pos = tell $fh;
my $next_line = $fh;
seek $fh, $pos, 0;

-- fxn


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




Re: replace existing text within multiple text files: How?

2007-09-11 Thread Xavier Noria

On Sep 11, 2007, at 9:43 PM, Gerald Wheeler wrote:


I have about 400 text files I need to replace the contents. All files
are in the current directory as the perl script runs out of

my $newText = This is my new text.. anybody's text goes here;

open(INFILE, $plants)
while ()
{
 print $newText;
}

I need help..


You mean $newText contains the whole content of a single file, and  
the 400 files end up having that very same content (that is $newText)?


-- fxn


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




Re: replace existing text within multiple text files: How?

2007-09-11 Thread Xavier Noria

On Sep 11, 2007, at 10:05 PM, Gerald Wheeler wrote:


Correct


That can be done with a one-liner:

  perl -0777 -pi.bak -e '$_ = q{new text goes here}' *.txt

The options -p, -i, and -e are documented in perlrun. The flag -0777  
has the side-effect of slurping the whole file into $_ (one file at a  
time), it is documented in perlrun as well, under -0.


-- fxn


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




Re: String question

2007-09-07 Thread Xavier Noria


On Sep 7, 2007, at 10:07 AM, Santana wrote:


Hei all,
i'am a newbie in PERL and i find a solution for this problem :

I have a string xxx , i want put ones(1) on left of string, if
this string dont have a length of 20 character.

Example :

if i have th string HELLO and woul like get  this ;

111HELLO


How i can this ???


Is this homework?

-- fxn


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




Re: how to remove ^M character from every line

2007-09-03 Thread Xavier Noria
Those ^Ms are \015s, which is \r practically everywhere. Note  
that line-oriented scripts almost work on CRLF text files on Unix  
because CRLF has a trailing \n by chance. I think this is  
unfortunate because instead of just breaking your program  there's  
the annoying situation where you still read lines but there's  
something spurious at the end you don't completely understand.


If the script is line-oriented, to read CRLF text files under Unix it  
is enough to set


  $/ = \r\n;

and chomp each line as usual.

If you want to preserve newlines instead of chomping then s/\r\n/\n/  
instead.


-- fxn


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




Re: substitution key-value

2007-08-27 Thread Xavier Noria

On Aug 27, 2007, at 10:59 AM, Petra Vide Ogrin wrote:


Hi all,

I have a hash and some prose text and want my perl to identify the  
keys of
the hash in this text and replace them with the corresponding  
values of

the keys.

I tried the following

foreach (keys %expan) {
  if ($sbl =~ m/$_/g) {
$sbl =~ s/$_/$expan{$_}/g;


No need for the previous check, if it can't perform the substitution  
it won't.


That assumes no key is part of another key's value, if it was the  
case you could end up doing double/triple/... substitutions. I would  
switch to a one-shot (untested) substitution:


  my $re = join '|', map quotemeta, keys %expan;
  $sbl =~ s/\b($re)\b/$expan{$1}/g;

I don't know why are you applying that substitution, but smells like  
a use case for some templating system like Text::Template. If it is  
please consider basing the script on it.


-- fxn


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




Re: substitution key-value

2007-08-27 Thread Xavier Noria

On Aug 27, 2007, at 1:29 PM, Chas Owens wrote:


Bad idea*, at least until Perl 5.10 (and maybe not even then).


Well, it may or may not be a bad idea.

On the one hand that performance penalty may be negligible for the OP  
problem and thus it just does not matter. On the other hand your  
solution assumes you can loop through the keys and perform global  
substitutions, working on the last modified string in each iteration.


Assuming you don't want higher-order substitutions (so to speak)  
which is suggested by the fact that the OP worked in a list of keys  
in an unkown order, that solutions needs there are no possible  
clashes between values and keys. And I think you don't need to go to  
the trouble of checking that or even depend on that, the OR-based  
solution is robust to that automatically.


-- fxn


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




Re: Convert int for string

2007-08-26 Thread Xavier Noria

On Aug 26, 2007, at 3:22 AM, Randal L. Schwartz wrote:

Perl is a very strongly typed language.  The problem is that people  
keep
thinking number or string is a type in Perl.  It isn't.  The  
type is
called scalar.  Other types are array and hash and  
filehandle and

dirhandle and built-in object and user-defined object.


In that line, I think Perl is not as dynamically typed as other  
scripting languages, in the sense that sigils do put types in the  
source code somehow. Since Perl tries to give meaning almost to  
anything and context allows to add a scalar to an array, the compiler  
does not complain that much in practice. But from a formal point of  
view there are types in the code.


-- fxn


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




Re: Convert int for string

2007-08-26 Thread Xavier Noria

On Aug 26, 2007, at 3:52 PM, Peter Scott wrote:


The term strong typing is so ill-defined as to make this an
angels-dancing-on-a-pinhead discussion and unlikely to lead to any
enlightenment.  Even the Wikipedia definition says that there *is* no
accepted definition and some of the common usages contradict each  
other.


I remember MJD has a nice slice about it. I don't know whether it's  
online though.


-- fxn


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




Re: Convert from unix newlines to dos newlines

2007-08-25 Thread Xavier Noria

On Aug 25, 2007, at 4:54 AM, Yoyoyo Yoyoyoyo wrote:

I use a mac and I was wondering if there was a way to convert unix  
newlines in a text file to dos newlines.


Yeah, with a Perl one-liner it would be

  perl -pi -we 's/\n/\r\n/' file.txt

or

  perl -pi.bak -we 's/\n/\r\n/' file.txt

if you want a backup of the original file.

-- fxn

PS: For the archives, note those solutions assume the runtime  
platform is Unix.



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




Re: Convert from unix newlines to dos newlines

2007-08-25 Thread Xavier Noria

On Aug 25, 2007, at 4:53 PM, Yoyoyo Yoyoyoyo wrote:

Thanks, but quick question.  If I do it from the command line it  
works fine.  But if I add:


`perl -pi -we 's/\n/\r\n/' ./student.csv`;

to my perl script it doesn't make the change to the file.  Is there  
a reason for this?


Sure, the literal between backquotes has the same semantics as  
strings in double quotes, there's interpolation and \t, \n,  and  
friends are translated to their actual meaning. So, the shell sees a  
hard newline after s/, as if you pressed the return key in the  
command-line.


To fix that double the backslashes, the shell needs to see a \n  
verbatim, so you need to write \\n in that command. (That's regular  
English quotes, not Perl double quotes.)


If you are already in a Perl script you could do that much more  
easily without shelling out, for example using Tie::File:


  use Tie::File;

  tie my @csv, 'Tie::File', 'student.csv' or die $!;
  $_ .= \r for @csv;
  untie @csv;

Shelling out is OK though, Perl's about glueing, even to itself :-),  
you can choose the approach you prefer.


-- fxn


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




Re: How to create a package level variable

2007-08-24 Thread Xavier Noria

On Aug 24, 2007, at 11:58 AM, Sundeep Gupta wrote:

The problem was that this module (XYZ) module uses another module,  
which
again has the use statement to load XYZ. I don't know if the perl  
loads the

module again and might be this caused to reset the value back to 0.
When I commented the use statement of that module, it just works fine.


If you load a module with use(), it will only get loaded once, no  
matter how many use()s for that module are executed throughout the  
source tree. In particular the package variable is not reset:


  [EMAIL PROTECTED]:~/tmp$ cat Foo.pm
  use strict;
  package Foo;
  our $x = 0;
  1;
  [EMAIL PROTECTED]:~/tmp$ perl -MFoo -wle '$Foo::x = 1; use Foo; print  
$Foo::x'

  1

-- fxn


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




Re: How to create a package level variable

2007-08-24 Thread Xavier Noria

On Aug 24, 2007, at 8:13 PM, Dr.Ruud wrote:


Chas Owens schreef:


[$db_initiali.ed]
Unless you have some funky source filter installed that normalizes
spelling variants Perl is going to have a problem.


Yes, I like the idea:

  use autocorect;


I think there's an Acme:: module that did something like that, it  
used some heuristic (edit distance or whatever) and chose an already  
existing indentifier. Something like that. But I can't find it.


-- fxn


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




Re: perl include dependencies?

2007-08-22 Thread Xavier Noria

On Aug 21, 2007, at 3:37 PM, infobank wrote:


I'm trying to embed djabberd (perl5 net-im) onto a m0n0wall base
(FreeBSD). I found scandeps, and ran it on DJAbberd and found some
useful information about the modules it relies upon.

Is there any way to find out which files it relies upon?


You mean the files corresponding to those modules? If yes, why you  
need them?


-- fxn


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




Re: Array modification

2007-08-16 Thread Xavier Noria

On Aug 16, 2007, at 11:47 AM, Sayed, Irfan (Irfan) wrote:


I have one array which stores some data after executing specific
command. Depends on situation , command has different output at
different time. sometime array may store 4 values or it may store 5
values.

Now my req. is that I need to assign no. to those values.

for example:

if array is @array1=(data1,data2,data3,data4);

now i need to assign 4 no. as there are four elements are present in
this array. So my array should look like this.

@array1=(1 data1 2 data2 3 data3 4 data4);

basically, i wanted to do indexing or to create hash.


Since a hash is an option, to understand your question better I would  
like to ask whether the already available builtin array indexing


  $array[3]

is enough. If not, why?

-- fxn


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




Re: One liner to change one line

2007-08-15 Thread Xavier Noria



On Aug 15, 2007, at 6:45 PM, Dennis G. Wicks wrote:

Greetings;

I have, conservatively, dozens of html files to change.

I can find them and pass the file name to perl and
do the usual s/// changes but there is one change I can't
figure out.

There is a line in each file that looks like

H1This-Is-The-Title/H1

of course, they are all different!

How can I change the hyphens to spaces in this line only?

Complicating the task is:

1. I don't know that there is only one such line per file.
   I need to get them all.
2. I don't know that all H1 are upper case.
3. Not all of the H1 lines are the same record in the file.


I think this satisfies those constraints:

  perl -0777 -pi.bak -we 's{(h1)(.*?)(/h1)}{$x = $2; $x =~  
tr:-: :; $1$x$3}geis' *.html


We slurp the file with -0777 to be able to work across lines (I  
undestand (3) that way). Then we capture stuff between H1s case  
insensitive, tr/// what's in between. Note that $n-variables are read- 
only, that's why we copy the capture into a regular variable.


Of course that assumes a simple regexp is enough, you need to judge  
whether that's the case in your data set.


-- fxn




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




Re: One liner to change one line

2007-08-15 Thread Xavier Noria

On Aug 15, 2007, at 7:04 PM, Xavier Noria wrote:

  perl -0777 -pi.bak -we 's{(h1)(.*?)(/h1)}{$x = $2; $x =~  
tr:-: :; $1$x$3}geis' *.html


A small improvement, groups are unnecessary because the elements are  
guaranteed not to have hyphens (in general they could, for instance  
in a class name, but in this case they don't):


  perl -0777 -pi.bak -we 's{h1.*?/h1}{$x = $; $x =~ tr:-: :; $x} 
geis' *.html


-- fxn


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




Re: die and exit

2007-08-13 Thread Xavier Noria

On Aug 13, 2007, at 9:29 AM, Jeff Pang wrote:


Does die call exit when it's excuted?
If so,when I overwrote exit() function in my script,would die call  
this customized

exit?


Do you want some code executed if the program dies? If that's the  
case assign a coderef to $SIG{__DIE__}:


  $SIG{__DIE__} = sub {
  my $message = shift;
  ...
  };

See %SIG in perlvar, and see the bottom of perldoc -f die.

-- fxn


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




Re: die and exit

2007-08-13 Thread Xavier Noria

On Aug 13, 2007, at 1:57 PM, Jeff Pang wrote:


Yes that's fine.
I think when we say 'use Apache qw/exit/' in modperl scripts,it may  
do the same things as you mentioned -- modify the typeglob directly  
-- but I'm not so sure.


I don't know too much XS but looks like this code in src/modules/perl/ 
mod_perl.c does that (the comment says so as well):


  /* *CORE::GLOBAL::exit = \Apache::exit */
  if(gv_stashpv(CORE::GLOBAL, FALSE)) {
  GV *exitgp = gv_fetchpv(CORE::GLOBAL::exit, TRUE, SVt_PVCV);
  GvCV(exitgp) = perl_get_cv(Apache::exit, TRUE);
  GvIMPORTED_CV_on(exitgp);
  }

-- fxn


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




Re: Problem with my code

2007-08-03 Thread Xavier Noria

El Aug 3, 2007, a las 1:45 PM, Mr. Shawn H. Corey escribió:

But the expression $hash{@cdr[2,3,6,7]} is the same as $hash{join 
($,@cdr[2,3,6,7])}  You have just replaced one special variable  
with another and $ is a bad choice since it's default is a space  
character, which may easily appear in one (or all) of @cdr[2,3,6,7]


A better choice is $hash{$cdr[2]}{$cdr[3]}{$cdr[6]}{$cdr[7]} but  
you would need four for-loops to get to the value.


To be able to use arrays as keys there are a couple of modules out  
there. One is mine, based on pack/unpack :


  http://search.cpan.org/~fxn/Hash-MultiKey-0.06/MultiKey.pm

and there's another one that is implemented as nested hashes, but I  
can't remember its name right now, does anybody know it?


-- fxn


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




Re: Convert Scientific Notation to decimal equivalent

2007-07-19 Thread Xavier Noria

El Jul 19, 2007, a las 12:19 AM, Joseph L. Casale escribió:


Interesting,
I see from your regexp you use a \A and \z, from Perldoc this means:
\A  Match only at beginning of string
\z  Match only at end of string

I am not sure I understand this requirement?


^ and $ depend on flags, and $ allows an optional trailing newline.  
When I want to match a complete string exactly, I tend to use \A and  
\z because they convey that intention clearly.


If your code processes line by line and splits on whitespace, \A ...  
\z is equivalent to ^ ... $.



In my case, I am checking an array of 3 scalars. Does this make sense:
next unless @data =~ /$RE {num}{real}/;
Does the regexp know to evaluate each element in the array  
implicitly? Or do I need to tell it this?


Detecting whether something holds in an array is the job of grep:

  my $numbers = grep /\A$RE{num}{real}\z/, @data;
  next unless $numbers == @data;

-- fxn


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




Re: Convert Scientific Notation to decimal equivalent

2007-07-18 Thread Xavier Noria



El Jul 18, 2007, a las 11:19 PM, Joseph L. Casale escribió:
How can I detect this, I have been running some code for a few days  
to develop some files and ran into the situation where I am getting  
the following data for input:


14.95313 14.45312 0
14.95313 1.570813E-015 0
14.95313 -14.45313 0
-14.95313 -28.90625 0
-14.95313 -14.45313 0
-14.95313 1.570813E-015 0
-14.95313 14.45312 0
14.95313 -28.90625 0
0 -28.90625 0
-14.95313 28.90625 0
0 28.90625 0
14.95313 28.90625 0

And my code is skipping some lines as it checks for any erroneous  
data:

next if grep (/[^0-9.-]/, @data);
But that thinks the scientific notation is bad. I searched the net  
and didn't find anything. How can I match this specific pattern and  
convert it?


I am not sure I understand the problem to solve.

You need to filter out lines that contain something that's *not* a  
number? If that's the case, is @data a split on whitespace for each  
line? If that's the case in turn, have a look at


  perldoc -q determine

or delegate the job to Regexp::Common:

  $ perl -MRegexp::Common -wle 'print 1 if 1.570813E-015 =~ /\A$RE 
{num}{real}\z/'

  1

-- fxn


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




Re: interpolation of function reference in a here doc

2007-07-02 Thread Xavier Noria

On Jul 2, 2007, at 2:46 PM, Gabriel Striewe wrote:


Dear List,

I wanted to interpolate a function reference in a here doc.
The following works fine:

my $hello = sub {
 return hello world!;
};

printf hello $s\n, $hello();


In Perl printf is rarely used because double-quote strings allow  
interpolation of scalars and arrays, and because print accepts an  
arbitrary number of arguments.


Interpolation does not understand function calls, though, so you  
either use a multi-argument call like this:


  print hello , $hello-(), \n;

or either use this hack:

  print hello @{[ $hello-() ]}\n;

Here-documents have double-quote semantics unless you put single  
quotes around the terminating string, so that last trick works in  
here-documents as well.


-- fxn


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




Re: Redirecting file handler to STDOUT

2007-06-22 Thread Xavier Noria

On Jun 22, 2007, at 11:57 AM, Ben Edwards wrote:


I am opening a log file:

open( LOGFILE,  cronlog.txt );

This is being written to in lots of places.

I have been asked to change the program so if -m (manual) flag is
passed the stuff that goes to the log file is send to standard out
instead.  Is it possible to change the above command to redirect
LOGFILE to STDOUT (i.e. make the two the same thing.


Given those requirements, a simple solution would be:

  unless (m_flag_given) {
  open my $fh, 'cronlog.txt' or die $!;
  select $fh;
  }

  # the rest of the program uses regular prints

-- fxn


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




Re: regexp problem

2007-06-14 Thread Xavier Noria

On Jun 14, 2007, at 12:04 PM, Jorge Almeida wrote:


I'm missing something about Perl's regexp:

  1 #!/usr/bin/perl -w
  2 use strict;
  3 my $s=STDIN;
  4 $s=~s/\D*//;
  5 $s=~s/\D*//;
  6 print $s\n;

When input is 'a123b', I get '123b', but I expected '123'.


  s/\D+//g;

-- fxn


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




Re: regexp problem

2007-06-14 Thread Xavier Noria

On Jun 14, 2007, at 12:10 PM, Martin Barth wrote:


On Thu, 14 Jun 2007 11:04:51 +0100 (WEST)
Jorge Almeida [EMAIL PROTECTED] wrote:


I'm missing something about Perl's regexp:

   1 #!/usr/bin/perl -w
   2 use strict;
   3 my $s=STDIN;
   4 $s=~s/\D*//;
   5 $s=~s/\D*//;
   6 print $s\n;

When input is 'a123b', I get '123b', but I expected '123'. I know I
can substitute line 4 by '$s=~s/\D*//g;' and comment out line 5.  
It will
work then, but that is not the point. I could also substitute line  
5 by

'$s=~s/\D+//;' and it would also work...



the problem is * in your regex. i guess the  \D* matches on zero non
digits before 123. so nothing will be done.


Not really.

Although + is more concise because you want to delete non-empty  
sequences of non-digits and do not care about empty sequences, * will  
go ahead as much as possible and would work fine. Jorge was just  
missing /g.


-- fxn




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




Re: regexp problem

2007-06-14 Thread Xavier Noria

On Jun 14, 2007, at 12:49 PM, Jorge Almeida wrote:


Martin was right (and I should have seen it from the start). The will
go ahead as much as possible is true only in the sense that the
greatest possible string of non-digits will be selected for deletion.
With '$s=~s/\D*//;' in line 5 that string will be empty. In order to
delete the trailing string, the '/g' is needed (but I knew that, as I
said in the original post).


Oh I am sorry, I replied too quick and didn't read that part. The use  
of two s/// in a row hinted to me that there was a naive use s///  
there missing /g. Clearly that was not the case and the question was  
different.


Sorry!

-- fxn




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




Re: Is there a perl equivalent to PHP variables $_POST and $_GET?

2007-06-11 Thread Xavier Noria

On Jun 10, 2007, at 9:18 PM, On Ali wrote:


Is there a perl equivalent to PHP variables $_POST and $_GET?


Perl is a general-purpose programming language and does not have web  
stuff builtin. To do web programming you need to pick some library/ 
framework like CGI.pm or Catalyst for example.


-- fxn




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




Re: Removing decimal points

2007-06-09 Thread Xavier Noria

On Jun 8, 2007, at 9:52 PM, ash wrote:


I need to remove decimal points from numbers. For eg 1.23 or 1.77
would be just 1. Any suggestion is appreciated. Thank you.


Use de int() function.

-- fxn




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




Re: Declaring constants in perl

2007-06-08 Thread Xavier Noria

On Jun 8, 2007, at 1:33 PM, Nath, Alok (STSD) wrote:


Hi,
What is the convention used to declare constants in perl ?
In C we declare constant type in Capital letters , not sure
how its in perl.


Same convention:

  use constant FOO = 3;

-- fxn




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




Re: Algorithm or module needed

2007-06-01 Thread Xavier Noria

On Jun 1, 2007, at 4:29 PM, Beginner wrote:


Hi,

I am trying to copy some file from a removable HDD to a network
drive. I want replicate the directory structure and omit un-needed
files.

The directory paths a 4-5 levels deep and I am trying to work out an
efficient way to recreate the directory path.

What I've been doing is using File::Find to collect the file and then
in the coderef calling the following subroutine to check the path but
i didn't go too far as my method seemswell not well formed.

The path to a source file looks like:

e:/Dir1/Day 1/AM/01/myfile.jpg


I think you want File::Path's mkpath. File::Path is a standard module.

-- fxn




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




Re: Assign a delimiter variable

2007-05-15 Thread Xavier Noria

On May 15, 2007, at 6:42 PM, Mike Blezien wrote:


Hello,

this one has been driving me nuts and I'm sure it's something very  
simple I maybe overlooking. I need to assign a delimiter variable  
IE: Pipe or Comma delimiter:


my $del = '|'; # use either a '|' or ','
my $dataline  = 0|1|2|3|4|5|6|7|8|9;
my @data = split(/$del/, $dataline);

This does not work, it won't split the file line with the '|'  
delimiter, and get no errors. But if I do this:


my $dataline  = 0|1|2|3|4|5|6|7|8|9;
my @data = split(/\|/, $dataline);

Then it works prefectly, it splits the line as expected. What am I  
missing ??


The actual regexp is what you get _after_ interpolation.

Since the pipe is a metacharacter it is being interpreted as such, as  
if you directly wrote


  split /|/, ...

To prevent this there's quotemeta(), which is available in literals  
as \E:


  my @data = split(/\E$del/, $dataline);

-- fxn


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




Re: Assign a delimiter variable

2007-05-15 Thread Xavier Noria

On May 15, 2007, at 6:49 PM, Xavier Noria wrote:

To prevent this there's quotemeta(), which is available in literals  
as \E:


Oh sorry, I meant \Q there.

-- fxn




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




Re: call system command

2007-05-14 Thread Xavier Noria

On May 14, 2007, at 3:44 PM, Tatiana Lloret Iglesias wrote:

Hi all,

I have to execute this command from perl:

my $status = system(d:\\blast\\bin\\blastall -p blastn -i $file -d  
$patDB

-o $workdir\\blast_$blast_file_id.txt);


but the problem is that $workdir contains spaces  how can I  
make it

work?


Break that into a list of arguments:

  system(d:\\blast\\bin\\blastall, -p, blastn, -i, $file, ...);

-- fxn






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




Re: call system command

2007-05-14 Thread Xavier Noria

On May 14, 2007, at 3:52 PM, Tatiana Lloret Iglesias wrote:


Thank a lot!

Another related question,,, system command can be used also for linux?


Sure. Of course it is unlikely that the arguments themselvels are  
portable in practice, I mean blastall won't probably be located at d: 
\\blast\\bin\\blastall on Linux, but you can certainly rely on  
having system().


-- fxn




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




Re: cpan question

2007-05-14 Thread Xavier Noria

On May 14, 2007, at 3:50 PM, Robert Hicks wrote:

I am working on a Perl 5.6.0 project and went to use cpan at the  
command line and behold it was not there. I download the 5.6.1 tar  
file and behold cpan the command line program wasn't in there  
either.


Did it not come with 5.6.0/5.6.1?

I don't see where I can get it either...


Nope, cpan(1) was added in 5.8.1. You can use the traditional

  perl -MCPAN -e 'install Module'

with that Perl.

-- fxn


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




Re: Detecting whether a script was called via CGI or the command line

2007-05-04 Thread Xavier Noria

On May 4, 2007, at 2:44 PM, Nigel Peck wrote:

Within the script I want to know where it was executed from; CGI or  
command line/cron job. I'm currently checking @ARGV to do this i.e.  
if there is data in @ARGV then it was called from the command line,  
but obviously this means there must be command line arguments and  
I'm also not sure whether there could sometimes be arguments in  
@ARGV when called through CGI?


Is there a better way to check?


The proper way would be

  use constant RUNNING_AS_CGI = exists $ENV{GATEWAY_INTERFACE};

-- fxn


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




Re: basename equivalent?

2007-04-26 Thread Xavier Noria

On Apr 26, 2007, at 2:34 AM, Nishi wrote:


What is the equivalent of basename? Ie if I dont want to use basename
package to get the filename off a path, what reg expr can i use in  
perl?


File::Basename is a standard module, why you don't want to use it?

-- fxn




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




Re: Removing lines

2007-04-16 Thread Xavier Noria

On Apr 15, 2007, at 6:38 PM, Mário Gamito wrote:

I have this site that auto generates an index.html file every 15  
minutes

(it's a blog aggregator).

I need that every time the file is generated, all the contents between
the lines
h4 class=post-titlea
href=http://domain.com/2006/08/bourne-shell.html;Bourne Shell/ 
a/h4


and

 pa href=http://domain.com/2006/08/bourne-shell.html;Sáb, 14 Abr
2007 12:31:07/a/p

to be deleted (including these two ones).

I've tried several ways and googled but i can't do the trick.

Any help would be appreciated.


Have a look at the range operator in scalar context, it is documented  
in perlop. The idea is


  while ()
  print unless /matches initial h4 line/ .. /matches ending P  
line/;

  }

-- fxn


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




Re: Uninstalling perl module

2007-04-14 Thread Xavier Noria

On Apr 13, 2007, at 1:02 PM, Randal L. Schwartz wrote:

No, because the CPAN.pm shell is an installer, not a packager.  It  
does not
maintain a list of which files belong to which installations, and  
will happily
let two installations both write to the same file.  This will be  
problematic

when you want to remove one of those installations later.

If you want package management, you'll have to use a distribution  
(like many
Linux and BSD distributions) that have taken the extra time to  
package the

CPAN distributions.  That has its own downsides, of course.


I am not familiar with module installations, let me ask a bit more to  
understand those limitations.


Wouldn't the problem with file name collisions/rewrites potentially  
happen if packages were managed as in distros? Or do they have some  
sort of policy about unicity of files in packages? Besides that, if  
CPAN.pm cached the MANIFESTs of modules installed by it (which if it  
doesn't, I guess it would be a matter of programming), wouldn't that  
suffice to offer uninstalling?


-- fxn




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




Re: Is there any way to use C header files in perl program?

2007-02-20 Thread Xavier Noria

On Feb 20, 2007, at 7:49 AM, Dharshana Eswaran wrote:


I need to use a C header file in Perl program in Unix.

Is it possible to use the header files from perl in any way? I do  
not want

to use any modules for the same.

I do not want to enable the perl program to call a routine in C  
library

through XS. I just want to use the values rather than any code.


Can you explain what do you mean by use and values?

-- fxn




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




Re: DOS to Windows format tr/\n\r\t/ /

2007-02-11 Thread Xavier Noria

On Feb 11, 2007, at 1:36 AM, [EMAIL PROTECTED] wrote:


I wonder if there is a format that preserves paragraph breaks while it
kills line breaks?


How do you define a paragraph break. Why does the subject mentions  
DOS to Windows.


-- fxn




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




Re: DOS to Windows format tr/\n\r\t/ /

2007-02-11 Thread Xavier Noria

On Feb 11, 2007, at 9:39 PM, [EMAIL PROTECTED] wrote:


Well, here's my problem: I have found a way to wrap a text file down
to a narrower column width. But it looses paragraph formatting.   
What I need

is the same thing as converting a file from DOS (line break after each
line) to Windows (line break after each paragraph) format, no?


I am not sure I understand the problem, but do you know the  
Text::Wrap standard module in any case?


-- fxn




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




Re: Removing file extension

2007-01-24 Thread Xavier Noria

On Jan 23, 2007, at 9:55 AM, Dr.Ruud wrote:


Use an anchor and a negated character set:

  s/\.[^.]*$//


That solution is broken, think /foo/bar.baz/woo. Correct regexps  
and other approaches have already been posted.


-- fxn


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




Re: Removing file extension

2007-01-23 Thread Xavier Noria

On Jan 23, 2007, at 8:22 AM, Saravana Kumar wrote:


Hi list,

I am trying to remove the extension from the a list of filenames and
manipulate the names further.

Tried to doing this:
$file=~ s/\..*//;

The above works fine. I get the result 'filename' if the filename is
filename.ext.

There are some files whose names are like file.name.ext and the  
result i get
for this is 'file' while the desired result is 'file.name'. Is  
there a way

to fix this?


Yes, you need to assert in the regexp dots are not allowed in the  
extension, for example something like this:


  $file =~ s/\.\w+$//;

Note that no captures are needed.

-- fxn


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




Re: memory issues?

2007-01-19 Thread Xavier Noria

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


Thank you very much, this is indeed the solution.


The explanation is that when you process lines this way

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

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


In the line-oriented while loop

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

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


-- fxn

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


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




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

2007-01-19 Thread Xavier Noria

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


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

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

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


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


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

There are several lines in

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

with this code:

  SvREADONLY_on(av);

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


-- fxn

use DBI;

use strict;
use warnings;

use Devel::Peek;

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

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


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




Re: comparing hashes

2007-01-13 Thread Xavier Noria

On Jan 13, 2007, at 6:29 PM, xavier mas wrote:


hello list,

I am trying to find if an element in one primary file (transformed  
to array)
is included in two other different secondary files (transformed to  
arrays,

too); the result is going to be printed as 1 or 0:


According to the code that's 1 or -1.




...
#creating arrays from its text files
@img_array=IMATGES; @dict_array=DICT; @in_array=IN;
#creating hashes from its arrays
foreach $in (@in_array) {chomp($in); $in_hash{$in}= 1;}
foreach $in (@dict_array) {chomp($in);$dict_hash{$in}= 1;}
foreach $in (@img_array) {chomp($in);$img_hash{$in}= 1;}
#searching primary element in secondary hashes
while (($key, $value) = each %in_hash) {
if (exists $dict_hash{$key}) {$dic_flag=1;}else {$dic_flag=-1}
if (exists $img_hash{$key}) {$img_flag=1;}else {$img_flag=-1;}
#printing result
print $img_flag, $dic_flag\n;


A bit of air would improve readability, the code is easy but dense  
just because of lack of layout.



but it seems 'exists' function doesn't fly to do this -the element  
isn't
always found into the secondary hashes. Any suggestion of why it  
doesn't and

how to do it?


Besides de potential mismatch given by the fact that the img hash  
has an img flag, but the dict hash has a dic flag (everything  
seems correct in that snippet anyway), I see no problem. Could you  
please send a minimal, self-contained code with minimal example files  
that let us reproduce the issue?


-- fxn

PS: Please turn strict and warnings on.




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




Re: comparing hashes

2007-01-13 Thread Xavier Noria

On Jan 13, 2007, at 7:43 PM, xavier mas wrote:


Here's an example:
in (file, array and hash) contains: woman, lion, ball
img (file, array and hash) contains: ball, dog, cat, lion.
dict (file, array and hash) contains: house, man, woman, kid,  
kitchen, lion



Comparing in with dict ans img, I'll expect as a result (all  
previous code is

between the while curly braces):
-1, 1
1, 1
1, -1
but the result is, instead:
-1, -1
-1, -1
-1, -1
that means never finds it.

I hope this is enough data.


It is better but not enough. The way you use hashes is apparently  
correct, so perhaps the problem comes from elsewhere. Please send  
self-contained, minimal code that reproduces the issue, not just an  
incomplete subset. Please turn strict and warnings on.


-- fxn




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




Re: Line ending with Gary^M on UNIX

2006-12-08 Thread Xavier Noria

This article explains how this works and has this and other tricks:

  http://www.onlamp.com/pub/a/onlamp/2006/08/17/understanding- 
newlines.html


-- fxn

PS: I am in fact the author but that is irrelevant.


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




Re: calling system

2006-10-21 Thread Xavier Noria

On Oct 21, 2006, at 9:52 AM, xavier mas wrote:


Dear all,


Hi Xavier!

I'm trying to rename a bunch of files to the estination names  
included in a
prepared files calling the system function mv inside a while loop  
(that

reads the destination files one by one).

But when doing this, I get an error saying a destiantion operand is  
missing in

line calling the system. Doesn't matter if I use double (for Perl
interpretaton)  or single quotes (for system interpretation).

If doing same thing on system bash line it works. What could be the  
reason for
that behavior? I need, anyway through Perl programm due to the big  
ammount of

files I have to rename.

...
cont = 0;
while (FILE_IN) {
cont++


I bet the culprit are those barewords, write $cont instead. It is  
good to enable strictness and warnings always, they catch lots of  
potential bugs like that one.



$file_out = $_;
$file_in = $cont;
system mv $file_in $file_out;}


Shelling out has always its risks. Here we are assuming names do not  
contain any space (which may be the case in your script). For the  
same price you can robustely use the builtin rename or the move/mv  
subroutines from the standard module File::Copy.


-- fxn




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




Re: perl built in function for mean

2006-09-09 Thread Xavier Noria

On Sep 9, 2006, at 9:00 PM, chen li wrote:


I want to calculate the mean of an array. I know how
to let the job done by using a loop. But I just wonder
if Perl has the short-cut/built-in function for this.


There are modules, but nothing builtin.



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




Re: perl built in function for mean

2006-09-09 Thread Xavier Noria

On Sep 9, 2006, at 9:48 PM, Randal L. Schwartz wrote:


Xavier == Xavier Noria [EMAIL PROTECTED] writes:


Xavier On Sep 9, 2006, at 9:00 PM, chen li wrote:

I want to calculate the mean of an array. I know how
to let the job done by using a loop. But I just wonder
if Perl has the short-cut/built-in function for this.


Xavier There are modules, but nothing builtin.

List::Util is built-in as of 5.8, and back compatible to 5.5 I  
think.


use List::Util qw(sum);

my $average = sum(@input) / @input;


I know, but that's not a builtin mean().



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




Re: Connecting to Oracle

2006-08-23 Thread Xavier Noria

On Aug 23, 2006, at 7:52 AM, anand kumar wrote:

  Can anyone please explain how to connect to oracle database  
server using perl script which is in separate linux server. Please  
note that currently I am using putty to connect to linux server for  
perl scripting and we have the Oracle database on a windows server.  
I have tried using the DBD::Oracle module from cpan.org but i could  
not install the module because the oracle drivers are accessible to  
Perl on the Linux server.


AFAIK you need to install the Oracle client in the local machine.

-- fxn




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




Re: empty line in REs

2006-08-22 Thread Xavier Noria

On Aug 22, 2006, at 5:36 PM, chen li wrote:


Just a quick question: what is the line code for an
empty line using regular expression in Perl?


Empty lines match /^$/, blank lines do not match /\S/.

-- fxn




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




Re: Help on chomp()

2006-08-21 Thread Xavier Noria

On Aug 21, 2006, at 8:51 PM, Mazhar wrote:


use strict;
use warnings;
my $file_name=$ARGV[0];
open(FILE,$file_name) || die Not been Accessed ;


No quotes needed around $file_name there.


@host_array=FILE;


That variable was not declared before, so that script does not run  
because it does not compile. Perhaps you are running a program which  
is not the one you are editing?


-- fxn




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




Re: Perl on Windows Server.

2006-08-16 Thread Xavier Noria

On Aug 16, 2006, at 2:33 PM, Krishnakumar K P wrote:


Hi,
There is a problem in coding perl in windows and using it in linux.
When you code in windows the file will contain dos/windows new line  
pairs
instead of the Linux/UNIX single New Line character which may cause  
error in

linux server.


That is unlikely, Perl does not care about the newline convention  
used in the source code, newlines of any kind are simply discarded.  
If you hard-code newlines in strings, here-docs, regexps, etc., you'd  
get a different literal, not necessarily get an error. Take into  
account anyway that the portable way to write a nelinwe in a string  
or regexp literal is \n.


Any major version control system like Subversion handles newline  
conventions transparently for you. If you commit a source file edited  
in Windows 2000, and check it out in FreeBSD, each local copy will  
have the native conventions. That's why those systems distinguish  
between binary and text files. Yet another benefit of using a VCS.


-- fxn


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




Re: Perl on Windows Server.

2006-08-16 Thread Xavier Noria

On Aug 16, 2006, at 2:54 PM, Xavier Noria wrote:


On Aug 16, 2006, at 2:33 PM, Krishnakumar K P wrote:


Hi,
There is a problem in coding perl in windows and using it in linux.
When you code in windows the file will contain dos/windows new  
line pairs
instead of the Linux/UNIX single New Line character which may  
cause error in

linux server.


That is unlikely, Perl does not care about the newline convention  
used in the source code, newlines of any kind are simply discarded.


Sorry that's not exact.

Let me add that newlines are the end-of-comment marker. You could get  
parsing errors if the file uses CRs and is being interpreted in a  
Unix machine for instance. If the source code uses either LF or CRLF  
there will be no problem as long as you are not running old Mac  
computers.


Another gotcha has to do with shebangs ending in CRLF, I know no  
shell that understands them (that's not a Perl issue though).


-- fxn


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




Re: How to sort an array which contains a value several times ?

2006-08-15 Thread Xavier Noria

On Aug 15, 2006, at 2:02 PM, SFantar wrote:


Hello everyone

I would like to sort an array so that it does not contain any  
values twice or three times.


Unfortunately, perldoc -q sort does not help me very much.
Here is an example of this array :

@array = ('smith ', 'john ', 'edward ', 'smith ', 'edward ', 'john ');

I would like to have : @array1 = ('smith ', 'john ', 'edward ');

Thanks in advance for your help and especially your explanations.


From the example looks like you want to filter out duplicates but  
preserve the relative order of elements in the original array. A  
possible way to accomplish that is:


feynman% cat ~/tmp/foo.pl
@array = ('smith ', 'john ', 'edward ', 'smith ', 'edward ', 'john ');
%seen = ();
@uniq = grep { $seen{$_} = !exists $seen{$_} } @array;
print @uniq\n;
feynman% perl ~/tmp/foo.pl
smith  john  edward

The code in the block for grep is a bit dense, it can be made more  
explicit if needed.


-- fxn




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




Re: How to sort an array which contains a value several times ?

2006-08-15 Thread Xavier Noria

On Aug 15, 2006, at 2:28 PM, SFantar wrote:


Thank for your quick reply.
Your solution should suit me.
Can you give me more details/ explanations about your code?
What does your code do exactly?


Sure, we are using a hash table as a way to deal with duplicates,  
which is a standard idiom in Perl.


The idea is to use the elements you need to process as keys of a hash  
table. Hash tables held just one entry per key, so if you reassign to  
a key the entry in the hash table is overwritten, so to speak, not  
repeated. That's the way hash tables work, and we leverage that  
property.


For instance this is a way to count how many times each element of an  
array is repeated:


  my %counters = ();
  $counters{$_} += 1 for @array;

After that code the keys of %counters are the distinct elements of  
@array, and the hash associates the number of occurrences of each one  
in @array. Do you understand that example?


In the example I sent before, we used a similar trick but using grep,  
because we want to iterate over @array and filter duplicates out (you  
need to know grep to understand this):


  %seen = ();
  @uniq = grep {
  my $is_new = 0;
  if not exists $seen{$_} {
  $seen{$_} = 1;
  $is_new = 1;
  }
  $is_new;
  } @array;

That code is more explicit. We use a hash table called %seen to  
register seen strings. The way to accomplish that is that we assign  
an arbitrary value to $seen{john} the first time we encounter 'john',  
and so subsequent calls to exists $seen{john} will return true. In  
those cases $is_new remains 0 and grep filters that element out.


I wrote a condensed version of an analogous way to work that out.

-- fxn


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




Re: How to sort an array which contains a value several times ?

2006-08-15 Thread Xavier Noria

On Aug 15, 2006, at 3:42 PM, Xavier Noria wrote:


  %seen = ();
  @uniq = grep {
  my $is_new = 0;
  if not exists $seen{$_} {


Oh sorry I wrote that off the top of my head and forgot the parens.



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




Re: How to sort an array which contains a value several times ?

2006-08-15 Thread Xavier Noria

On Aug 15, 2006, at 5:49 PM, SFantar wrote:

On the contrary, the code written above is not clear yet. What does  
your code do exactly when it comes

to deal with smith, edward which appear twice in @array initially?


What's what you don't understand, do you know grep?

-- fxn




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




Re: How to sort an array which contains a value several times ?

2006-08-15 Thread Xavier Noria

On Aug 15, 2006, at 6:19 PM, SFantar wrote:


I know the grep function.
Normally, grep needs a pattern to match with values found in @array.
All the values found are returned in an array.
Is that right?


Not quite.

The elements of the array are passed to grep's block, one at a time,  
and the block is just expected to return some value to be interpreted  
as boolean. Whether the value is related to the element or not it  
does not matter, grep just takes it an applies its logic: If the  
value returned by the block is true the element passes, otherwise the  
element is filtered out.


So the first time 'smith' is seen the code creates an entry for it in  
%seen and sets $is_new to 1, which is the value returned, and true.  
Therefore, the first time we see 'smith' it passes to @uniq. Next  
time 'smith' is seen in @array $is_new ends up being 0 because exists  
$seen{smith} is true. Now the returned value--- $is_new--- it is  
false, so that second 'smith' does not pass. In subsequent 'smith's  
the same reasoning applies, none of them pass.


Note that %seen is shared among invocations of the block because it  
was declared outside.


-- fxn


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




Re: How to split a file on the CR carriage Return and/or replace the \r with \n ??

2006-07-28 Thread Xavier Noria

On Jul 28, 2006, at 9:50 PM, Joshua Colson wrote:


On Fri, 2006-07-28 at 21:28 +0200, Dr.Ruud wrote:

Robert Citek schreef:


Try a different value for the INPUT_RECORD_SEPARATOR ($/):

$/ = \n ;


(spaces added for clarity)

Is that different from the default?
(see perlvar)


Using ActiveState on Windows it is.


That's wrong.

$/ is eq \n everywhere, and \n has length 1 everywhere. \n is  
eq \012 in all systems except MacOS pre-X where it is eq \015. In  
particular \n is *not* CRLF on Windows.


My article about newlines is moving forward at O'Reilly, I hope I can  
soon provide a pointer to it. Misconceptions about newlines arise too  
often, which was my main motivation to write it.


-- fxn


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




Re: How to split a file on the CR carriage Return and/or replace the \r with \n ??

2006-07-26 Thread Xavier Noria

On Jul 26, 2006, at 21:59, Steve Pittman wrote:


I am using activestate on a windows box ...the files I am parsing are
Unix files...I tried this so far...

open ( IN, $_[0] )||die Can't open DAT source file: $tempFile
$!\n;
while (IN){s/\r/\n/g;@lines = IN;}#
close (IN);

foreach $line (@lines)
{


If the file has Unix conventions a line-oriented loop on Windows  
works out of the box:


  while (my $line = $fh_of_unix_text_file) {
  chomp $line;
  # ...
  }

The reason why this works is that no pair CRLF ever gets into $line  
on Windows working in text mode. That's because when reading the I/O  
layer substitutes them on the fly with simple LFs. The readline  
operator returns $/-separated chunks, and $/ is \n by default,  
which is precisely eq to LF on Windows. As you know, LF is the line  
separator on Unix.


If the file actually has CR conventions (as said in the subject)  
instead of Unix conventions (as said in the body), then just set $/  
accordingly and use a regular line-oriented loop as well:


  {
  local $/ = \015; # tell readline to use CR
  while (my $line = $fh_with_cr_convention) {
  chomp $line;
  # ...
  }
  }

-- fxn




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




Re: split function

2006-07-19 Thread Xavier Noria

On Jul 19, 2006, at 9:57, Sayed, Irfan ((Irfan)) wrote:


I need to split following string

cs_backup_restore_cmvobsvr1mum

the output which i am looking for is

cs_backup_restore and _cmvobsvr1mum


Which is the criteria, everything up to the last underscore?

-- fxn




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




Re: split function

2006-07-19 Thread Xavier Noria

On Jul 19, 2006, at 10:05, Sayed, Irfan ((Irfan)) wrote:


I think criteria shud be _ but I need output in following manner


That criteria is ambiguous becasue there are several _s and you need  
to deal with them differently, that is, ignoring some and splitting  
on some. Can you be more specific?


-- fxn


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




Re: doubt..

2006-07-19 Thread Xavier Noria

On Jul 19, 2006, at 13:59, Ankam, Ramesh Babu wrote:


  Can any one please tell me what this pattern meanss|.*/||  .


Assuming there are no newlines in the string, that s/// means remove  
everything up to, and including, the last slash. It's a typical  
regexp for getting the basename of a filename. If that's the usage  
please delegate to File::Basename instead.


-- fxn




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




Re: Next

2006-07-06 Thread Xavier Noria

On Jul 6, 2006, at 6:26, Geetha Weerasooriya wrote:


Dear all,

When I was reading a Perl code I found the following line. Can u  
please

explain what it means?

!defined($rt_nearest) or $dh$dist or next;


It means

  next unless !defined($rt_nearest) or $dh  $dist;

or, equivalently,

  next if defined($rt_nearest) and $dh = $dist;

The trick in the original code is: the or operator returns as soon as  
some of the operands evaluates to true, if any. So if


  !defined($rt_nearest)

holds nothing to its right is evaluated. Otherwise we go for

  $dh  $dist

If it holds we are done, otherwise we evaluate the following operand

  next

which has the secondary effect of jumping to the next iteration  
somewhere. Some people dislike using boolean operators to control  
flow that way, some people don't.


-- fxn




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




Re: Need help with error from certain ASCII characters in a CSV file

2006-07-06 Thread Xavier Noria

On Jul 6, 2006, at 10:41, Roman Daszczyszak wrote:


Does anyone have a suggestion for how I can handle this, or even where
I can look to solve this issue?  Is there another possibility for
where the error is occurring that I am not seeing?


I would create the filehandle specifying the character encoding in  
the open call (as documented in perldoc -f open). I didn't test this,  
just an idea.


-- fxn




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




Re: newlines on win32, old mac, and unix

2006-06-19 Thread Xavier Noria

On Jun 19, 2006, at 22:45, Anthony Ettinger wrote:


   # order matters
   $raw_text =~ s/\015\012/\n/g;
   $raw_text =~ s/\012/\n/g unless \n eq \012;
   $raw_text =~ s/\015/\n/g unless \n eq \015;



Does it make any difference if I use s/\cM\cJ/cJ/ vs. s/\015\012/\n/ 
g ?


The regexp is OK, the replacement string is not, because \cJ is not  
necessarily eq \n. The latter is portable, the former is not.



Since the newline convention is not necessarily the one in the
runtime platform you cannot write a line-oriented script. If files
are too big to slurp then you'd work on chunks, but need to check by
hand whether a CRLF has been cut in the middle.



I'm reading each line in a while loop, so it should work fine on a  
large file?


The while loops over lines ***as long as they are encoded using the  
conventions of the runtime platform***. The diamond operator uses $/  
as separator, which in turn is \n by default. Since the purpose of  
your script is to deal with *any* newline convention, in general a  
while loop like


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

looks suspicious. The variable should be called $chunk_of_text,  
instead of $line. You don't know whether you'll get a line.  
Suspicious, may signal the programmer does not fully understand  
what's going on.


For instance, TextWrangler is known to use old-Mac conventions by  
default (last time I checked). If you read a file like that with that  
while in either Unix or Windows you'll slurp the entire file in a  
single iteration. That is, $line will contain the whole file.


In general, to be robust to newline conventions you need to to some  
munging by hand before using regular, portable line-oriented idioms.


-- fxn


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




Re: Help understand documentation

2006-06-13 Thread Xavier Noria

On Jun 13, 2006, at 6:14, Vijay Kumar Adhikari wrote:


I understand. My question is that Net::Ping does not use that info at
all. If a host is alive, it will be listed in the output no matter
what port you specify. Is this a bug?


The module uses the port in several subroutines, like this

  $saddr = sockaddr_in($self-{port_num}, $ip);

but I think there is an errata in the SYNOPSIS indeed, it should read

  $p-{port_num} = (getservbyname(http, tcp))[2];

-- fxn



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




Re: newlines on win32, old mac, and unix

2006-06-13 Thread Xavier Noria

On Jun 13, 2006, at 20:26, Anthony Ettinger wrote:


I have to write a simple function which strips out the various
newlines on text files, and replaces them with the standard unix
newline \n


In Perl \n depends on the system, it is eq \012 everywhere except  
in MacOS pre-X, where it is \015. The standard Unix newline is \012.



In text-mode, I open the file, and do the following:

   while (defined(my $line = INFILE))
   {
   my $outline;
   if ($line =~ m/\cM\cJ/)
   {
   print dos\n;
   ($outline = $line) =~ s/\cM\cJ/\cJ/;  
#win32


   } elsif ($line =~ m/\cM(?!\cJ)/) {
   print mac\n;
   ($outline = $line) =~ s/\cM/\cJ/g; #mac
   } else {
   print other\n;
   $outline = $line; #default
   }

   print OUTFILE $outline;
   }

It works fine on unix when I run the unit tests on old mac files, win,
and unix files and do a hexdump -C on themhowever, when I run it
on win32 perl 5.6.1, it is not doing any replacement. Teh lines remain
unchanged.

My understanding is that \n is a reference (depending on which OS your
perl is running on) to CR (mac), CRLF (dos), and LF (unix) in
text-mode STDIO.


That is a common misconception. The string \n has length 1 always,  
everywhere. It is not CRLF on Windows.


To explain this properly I'd need to reproduce here an article I've  
written for Perl.com, not yet published. But to address the problem  
is enough to say that in text mode there is an I/O layer (PerlIO)  
that does some magic back and forth between \n and the native newline  
convention. That's the way portability is accomplished, inherited  
from C.


To be able to deal with any newline convention the way you want in a  
portable way you disable that magic enabling binmode on the  
filehandle. The easiest solution is to slurp the text and s/// it  
like this (written inline):


  binmode $in_fh;
  my $raw_text = do { local $/; $in_fh };

  # order matters
  $raw_text =~ s/\015\012/\n/g;
  $raw_text =~ s/\012/\n/g unless \n eq \012;
  $raw_text =~ s/\015/\n/g unless \n eq \015;

  # $raw_text is normalized here

Since the newline convention is not necessarily the one in the  
runtime platform you cannot write a line-oriented script. If files  
are too big to slurp then you'd work on chunks, but need to check by  
hand whether a CRLF has been cut in the middle.


-- fxn


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




Re: plugins

2006-06-11 Thread Xavier Noria


On Jun 11, 2006, at 11:16, OROSZI Balázs wrote:


Chad Perrin wrote:

On Sat, Jun 10, 2006 at 10:53:30PM +0200, OROSZI Balázs wrote:
Makefiles are for installing modules, not for using them.  If you use
the CPAN module management that is provided with common Perl
distributions, you shouldn't have to deal with makefiles even for
installation, unless you wrote the module yourself and are  
packaging it

for CPAN.


It seems this is REALLY not what I want :)
Now I'm currently using do $file and it is working fine. Maybe  
one day I'll do it better, but for now it is ok for me.


You seem to have some misconception about modules.

That do-based approach is very similar to the suggested solution  
indeed, see perldoc -q require.


-- fxn



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




Re: Help understand documentation

2006-06-11 Thread Xavier Noria

On Jun 11, 2006, at 14:20, Vijay Kumar Adhikari wrote:


This is from http://perldoc.perl.org/Net/Ping.html

   # Like tcp protocol, but with many hosts
   $p = Net::Ping-new(syn);
   $p-{port_num} = getservbyname(http, tcp);
   foreach $host (@host_array) {
 $p-ping($host);
   }
   while (($host,$rtt,$ip) = $p-ack) {
 print HOST: $host [$ip] ACKed in $rtt seconds.\n;
   }

Can some body tell me what the following line does?
   $p-{port_num} = getservbyname(http, tcp);


The object in $p is a blessed hashref. That's Perl object-oriented  
jargon, if you are not familiar with it think $p is a regular  
hashref. In any case,


  $p-{port_num} = $value;

assigns $value to the key port_num of the hash %$p.

That usage is a bit unusual, normally the underlying implementation  
of an object is not public for clients, and access to its  
attributes in the case of hashrefs is normally encapsulated in  
method calls.


Anyway, that's what that code means.

-- fxn



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




Re: Newline error

2006-06-06 Thread Xavier Noria

On Jun 6, 2006, at 12:59, joseph wrote:

All,

Just like to ask for correction on what's wrong with my script it  
gives spit

out this error when i run it.

Unsuccessful open on filename containing newline at disksize.pl  
line 8.

Can't open file No such file or directory

But it runs without this error whenever i feed it up when an  
existing file

output by df-h.
ex:## open(FL,path/toactual/file) or die blabalha;
Does this mean it can't trap the output of  `df-h`?


Dealing with system commands (ls, ps, df, etc.) that way is usually  
much tricky than it looks. Normally one goes to CPAN and checks  
whether someone has done it right already. In that case one installs  
the module and writes an easy and robust script in 5 minutes.


For this particular problem delegate to Sys::Filesystem.

-- fxn




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




Re: Newline error

2006-06-06 Thread Xavier Noria

On Jun 6, 2006, at 14:16, John W. Krahn wrote:


Mr. Shawn H. Corey wrote:


$output_file =~ s/\r//g;
# chomp only removes linefeed characters \n

# BTW, there is no such thing as a newline;
# it is either a linefeed: \n ASCII LF 0x0A
# or a carriage return: \r ASCII CR 0x0D


\n is inherited from the C programming language and is the  
newline escape
sequence.  On Unix and Unix-like systems the newline is equivalent  
to the

ASCII line feed character but on other systems it could be one or more
different characters.


To be more precise, \n in Perl is eq \012 everywhere except in  
Mac OS pre-X, where it is eq \015. In CRLF platforms like Win32  
\n is transparently converted to CRLF on writing and back on  
reading by PerlIO in text mode. Thus, in a regular line-oriented  
script like


  while (my $line = FH) {
  # work with $line
  }

$line ends with \n but does not contain a pair CRLF (assuming  
native conventions in the input). On the other direction, the string  
foo\n has length 4 in all systems. When you print that string into  
a file in text mode on Windows the bytes on disk have an extra  
\015, but that's transparent to the programmer. That's the point of  
using \n as logical/portable newline in Perl.


I have written an article about newlines in Perl not yet published.  
All those fine details are explained there.


-- fxn


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




Re: monitoring file like tail -f in perl

2006-05-29 Thread Xavier Noria

On May 29, 2006, at 18:19, Ken Foskey wrote:

I want to monitor a daemon's logs that updates randomly through the  
day
and night.  Is there a way to open the log file and read the new  
records

as they come in the same way as tail -f does?


Sure, with File::Tail for example.

-- fxn



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




Re: suppressing selected warning messages

2006-05-24 Thread Xavier Noria

On May 24, 2006, at 18:48, Gavin Bowlby wrote:


I can suppress all warning checks by doing a:

no warnings;
if ($a == 7) {
use warnings;

but I would like all other normal Perl warnings to be used for the
statement:

if ($a  == 7)

other than the check for the LHS of the equality check being a numeric
value.

Is this possible?


Warnings have a hierarchy documented in perllexwarn. The category  
each warning belongs to is documented in perldiag. In that case the  
most you can do is to disable the numeric category, which is the  
one that warning belongs according to perldiag:


  {
no warnings qw(numeric);
# issues use of uninitialized ... but not the numeric one
print undef if foo == 0;
  }

-- fxn


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




Re: Converting a string to a filehandle

2006-05-22 Thread Xavier Noria

On May 22, 2006, at 4:44, Wijaya Edward wrote:

Is there a way to do it? Module for it? Suppose I have this large  
string.



my $string = 'foo bar
  qux woo
  etc etc';



I would like to convert that string as if it is stored inside a  
file, and bypassing the file creation step.

For example,

my $filehandle = do_sth_to_convert($string);


Yes, since 5.8.0 that's built-in:

  open my $filehandle, '', \$string;

-- fxn



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




Re: Extracting a bit of a scalar variable

2006-04-21 Thread Xavier Noria

On Apr 21, 2006, at 20:59, Praveena Vittal wrote:

Is there any way to print a particular bit of a digit after  
converting to binary form.


That can be easily done with substr y sprintf, this would take the  
second bit of 7 written in base 2:


  % perl -wle 'print substr sprintf(%b, 7), -2, 1'
  1

That's quite readable, but if efficiency is an issue then pack/unpack  
might be better, it had to be measured in any case.


-- fxn


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




Re: regular expressions

2006-04-21 Thread Xavier Noria


On Apr 21, 2006, at 16:10, Bowen, Bruce wrote:

In perldoc under this topic s is listed as Treat string as a  
single line and m as Treat string as multiples lines.


If I have text that has varying spaces at the begging of each line,  
and I use


$string =~ s/^\s+//; It will remove the spaces from in from of the  
first line but not any other lines.  That is clear to me.


However, it does not clear all of the leading spaces from all of  
the lines if I use


$string =~ m/^\s+//;


Modifiers go to the end:

  $string =~ s/^\s+//m;

-- fxn


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




Re: regex help

2006-04-19 Thread Xavier Noria

On Apr 19, 2006, at 9:13, Murphy, Ged (Bolton) wrote:


Hi all.

I have a log containing strings as follows :

 21259 auditO  72398 Mar 09 00:18 dll/ldr/elf.c

The format is the same throughout with the exception of the 'O', as it
doesn't always appear.
I need to match when the 'O' appears and when it does, I need to save
the file path, i.e. 'dll/ldr/elf.c'

Here is a snippet from my code containing my regex

if (/\sO\s.+([\w\/]+)$/) {
   print found $1\n;


Based on yours, the regexp would be for instance:

\sO\s.+\s(\S+)$

Note that assumes no other field can be O except in those cases,  
you can add restrictions as needed if that does not hold.


You could base that on split as well, it may be more readable:

   my @fields = split;
   print found $fields[-1]\n if $fields[2] eq O;

-- fxn


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




Re: split function in perl

2006-04-17 Thread Xavier Noria

On Apr 17, 2006, at 10:30, Irfan J Sayed wrote:


Hi,

I have a following line stored in one variable $test.

deliver.Admin_Irfan_Project.20060413.212355 . I need to split this  
line

into the words and store the output in array.

words should like this.
deliver
admin
irfan
project
20060413
212355

I am using following code to split this line
$test =~ s/^\s+//;
@array = split(/\W/, $test);


Problem is that _ belongs to \w or, equivalently, it does not  
belong to \W. For that particular example it is enough to include _  
in the regexp:


% perl -wle 'print for split /[\W_]/, deliver.Admin_Irfan_Project. 
20060413.212355'

deliver
Admin
Irfan
Project
20060413
212355

-- fxn


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




Re: Combine similar patterns

2006-03-29 Thread Xavier Noria

On Mar 29, 2006, at 10:28, anand kumar wrote:

Sorry for not posting the question clearly, Please find the  
attachment of the sample file. The matter enclosed in f1…./ 
f1=c, f1=c…../f1=c….f1=c……/f1 are all the foot notes that  
are spanning on various pages, now what I am trying to do is that  
to combine all the related footnotes that are spanned in various  
pages and place them at on place. Here f1 indicates the starting  
of the footnotes, f1=c indicates that the footnote continues from  
previous page, the /f1=c indicates that the foot notes continues  
in next page and /f1 indicates the end of the footnotes


Looks like the code I sent before is a good starting point. The only  
new relevant stuff I see are the fn tags, but you could adapt the  
script easily to deal with that correctly and to do what's needed  
with empty lines. If you understand the flip-flop operator you've got  
the script.


-- fxn



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




Re: [SPAM DETECT] Re: Combine similar patterns

2006-03-29 Thread Xavier Noria

On Mar 29, 2006, at 12:01, anand kumar wrote:

Yes, of course the code u have sent is very helpful and thanks for  
that. But the problem here is that in a single file there may be  
various number of foot notes i.e. f1,f2f3.so on. For all  
these the conventions are the same as told before. I am having  
problem in combining the relevant ones. Please help in this matter


I think we can go back to the simplification. Would you please send  
an analogous simplified example that contains sections f1, f2, f3,  
following the same structure as in the original data?


-- fxn



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




  1   2   3   >