John W. Krahn
Jan 19 2005, 11:22 AM
Brihas Sarathy wrote:
QUOTE |
I learned something new and thought you may be interested.
Regards, Brihas
Problem: I have a text file in UNIX format (i.e. UNIX EOL) to which I would like to do the following: FOR {each blank line} WHERE {the subsequent line starts with a digit (i.e. 1-9)}, {add a % to the blank line} AND {delete the subsequent line (i.e. the one starting with a digit)}
======================================= Solution # 1 perl -i.bak -ne'/S/?print:(($a=<>)=~/^d/)?print"%$_":do{print;$_=$a;redo}' yourfile
My Comments: Works as advertised, but I don't understand what all the code means :( ======================================= Solution # 2
bash programs make heavy use of awk and sed (and other little languages and utilities) to do what they do. bash on it's own is pretty weak but it's power is that it integrates so well with other shell utilities. This could have been done with sed. However, Perl's "s" flag for regular expressions, which tells it to treat new lines just as any other character when matching, makes it easy to do this in Perl. I've attached a little script. This is the heart of it:
1 while $buf =~ s{nn([0-9][^n]+)n}{n%n};
|
The "1 while /pattern/" idiom is great if you have a nested or overlapping
pattern but your example does not. The character class [0-9] could have been
written as d and the character class [^n]+ could have been written as .+.
So to sum up that line could have been written as:
$buf =~ s/(?<=n)n(d.+)/%/g;
QUOTE |
Normally this would be written without the "1 while", but the next match could (and often does) start in the text that was substituted in, so this will try again from the start after each successful match and finally stop after it fails to find any match.
I'm using the { } characters to bracket the matching pattern and the replacement text portitions of the regular expression to make it more readable. This could have been written like s/// instead of s{}{}.
The matching pattern is the newline, a digit (0-9, which could also have been written as d, but in this example written as [0-9]), a span of any number of any character except the newline (written as [^n]+), and then two newlines (nn) in a row which indicates a blank line (the end of one line which probably had something on it and then another line end immediately without anything on it).
When that matching pattern is found, the text n%n (n translates to a newline) is substitued in.
The rest of the program is just reading the first argument, doing file I/O, and error handling (the "or die" clauses). Error handling tries to insure that the original file isn't overwritten if something goes wrong and other bad scenarios I could think of. To be more user friendly, the print statement could have had an "or die" on it to detect a suddenly full disc and other scenarios =)
<--START perl code-- #!/usr/bin/perl
use strict; use warnings;
=begin comment
FOR {each blank line} WHERE {the subsequent line starts with a digit (i.e. 1-9)}, {add a % to the blank line} AND {delete the subsequent line (i.e. the one starting with a digit)}
=cut
my $fn = shift @ARGV; # first command-line argument
open my $f, '<', $fn or die "usage: $0 <infile>"; read $f, my $buf, -s $f or die "could not read from file $fn: $!"; close $f; rename $fn, "$fn.orig" or die "could not rename $fn to $fn.orig: $!"; open $f, '>', $fn or die "could not open $fn for writing: $!";
1 while $buf =~ s{nn([0-9][^n]+)n}{n%n};
print $f $buf; close $f; <--END perl code--
My Comments: Works as advertised, and I got to learn some Perl! :) =======================================
|
Are you related to Gurusamy Sarathy at ActiveState?
John
--
use Perl;
program
fulfillment
John W. Krahn
Jan 19 2005, 11:50 AM
John W. Krahn wrote:
QUOTE |
Brihas Sarathy wrote:
|
Oops, sorry I posted this to the wrong mailing list.
John
--
use Perl;
program
fulfillment
Bob Showalter
Jan 19 2005, 03:07 PM
Mariano Cunietti wrote:
QUOTE |
Hi all, can anyone address me to a Perl module/function to parse gzipped log files without uncompressing them via gzip/gunzip? Thanks
|
This search will turn up some modules:
http://search.cpan.org/search?query=gzip&mode=moduleYou might also just read from the output of 'gunzip -c' or 'zcat'
open F, 'gunzip -c somefile.log.gz|' or die $!;
while (<F>) {
# process log data here
}
Thomas Btzler
Jan 19 2005, 03:15 PM
Mariano Cunietti <[Email Removed]> asked:
QUOTE |
can anyone address me to a Perl module/function to parse gzipped log files without uncompressing them via gzip/gunzip?
|
You have to uncompress them somehow, but you don't have to
uncompress them to a file. I regularly use code like this:
if( $file =~ m/.gz$/ ){
$file = "gzip -d < $file |";
} elsif( $file =~ m/.bz2$/ ){
$file = "bzip2 -d < $file |";
}
if( my $fh = new IO::File $file ){
# do something
} else {
die "Failed to open input file '$file': $!";
}
HTH,
Thomas
Wiggins d Anconia
Jan 19 2005, 07:04 PM
QUOTE |
Hi all, can anyone address me to a Perl module/function to parse gzipped log files without uncompressing them via gzip/gunzip? Thanks
Mariano
|
Compress::Zlib is my standard way to do this. If all you want to do is
grep them then zgrep will be faster as the others have pointed out. I
don't like shelling out, period, so I would avoid the other (somewhat
elegant) solutions offered. The added benefit to using C<gzopen> is that
it will work on zipped/unzipped files similar to how zgrep functions.
Code I have used below, there should be one more check (at least) to
verify that $gz is defined.
http://danconia.org-- Untested --
use Compress::Zlib;
#
# Opens a log file for reading, even if the file is gzipped. gzopen and
# gzreadline will read the data if it is regular or gzipped. Returns an
# array reference of all of the lines from the file.
#
sub _read_log {
my $self = shift;
my $log = shift;
my @lines = ();
unless (-f $log) {
die "Can't open log file '$log' for readingn";
}
my $line;
my $gz = gzopen($log, "rb");
while ($gz->gzreadline($line) > 0) {
chomp $line;
push (@lines, $line);
}
$gz->gzclose();
return @lines;
}
----- Original Message -----
From: "Henry, Mark Patrick" <[Email Removed]>
Date: Thursday, January 20, 2005 0:54 am
Subject: Can't locate loadable object for module
Hello;
QUOTE |
I'm trying to use a perl module that a tool I got, ccmeter.pl, requiresand I can't no matter what I do..
I'm on hpux11.11, and the script was written for 5.005_03 so that's what I'm including at the top of the perl script ( #!/apps/perl-5.005_03/bin/perl) - this exists and *is* that particular version.
The module is Time::HiRes, the file is HiRes.pm
I d/l'd the module from cpan and put it in my user directory... ~mhenry/tools/3rdparty/Time/HiRes.pm The file exists of course, has read bit set across the board.
When I run the script it complains it "Can't locate loadable object for module Time::HiRes in @INC"
If I run 'perl -V', I get (among other things): @INC: /apps/perl-5.005_03/lib/5.00503/PA-RISC1.1 /apps/perl-5.005_03/lib/5.00503 /apps/perl-5.005_03/lib/site_perl/5.005/PA-RISC1.1 /apps/perl-5.005_03/lib/site_perl/5.005
So, in the script I put the following:
use lib '/users/mhenry/tools/3rdparty';
I get: Can't locate loadable object for module Time::HiRes in @INC (@INC contains: /users/mhenry/tools/3rdparty /apps/perl-5.005_03/lib/5.00503/PA-RISC1.1 /apps/perl-5.005_03/lib/5.00503 /apps/perl-5.005_03/lib/site_perl/5.005/PA-RISC1.1 /apps/perl-5.005_03/lib/site_perl/5.005) at ./ccmeter.pl line 221
I changed the line above to:
use lib '/users/mhenry/tools/3rdparty/Time';
I get the same..
I tried putting the HiRes.pm file at every level of the dirs mentioned above in the 'perl -V' return, but nothing!!
It seems to be a simple task but nothing will work.
The 'head' of the module is:
package Time::HiRes;
use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK @EXPORT_FAIL);
require Exporter; require DynaLoader;
@ISA = qw(Exporter DynaLoader);
..which appears to be legit.
Can *anyone* help??! I've tried every google search result out there.. Did you try: |
push @INC , '/users/mhenry/tools/3rdparty/Time';
HTH,
Mark G.
Brian Barto
Jan 20 2005, 05:21 PM
If you figure it out, let me know. I had problems getting it to read the
database. I eventually quit.
-----Original Message-----
From: Jay [mailto:[Email Removed]]
Sent: Thursday, January 20, 2005 12:17 PM
To: Perl Beginners List
Subject: Geo::PostalCode
I have the following code, which dies "no such file or directory" at
line 8. The problem seems to be that the census data file it relies
on is a single .dbf, and the documentation seems to indicate that the
module looks for three separate .db files. The .dbf file, though, is
the file from the working link in the docs, submitted by the author,
and doesn't seem to have changed since 1999, so I'm slightly
suspicious that, whatever the docs say, the error is in the way I'm
invoking it. Does anyone have experience with this module?
__CODE__
#!/usr/bin/perl
use warnings;
use strict;
use Geo::PostalCode;
my $gp = Geo::PostalCode->new(db_dir => ".") or die "can't create
Geo::PostalCode object: $!n" ;
my $record = $gp->lookup_city_state(city => "Jersey City", state =>
"NJ") or die "can't read from file: $!n" ;
my $lat = $record->{lat};
my $lon = $record->{lon};
print "$lat $lonn" ;
my $postal_codes = $gp->nearby_postal_codes(lat => $lat, lon => $lon,
distance => 50);
print map{"$_n"} @$postal_codes ;
__END__
TIA,
--jay
[Email Removed]
--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Mark Patrick Henry
Jan 20 2005, 06:02 PM
Hi Mark,
Thanks..
No that didn't help. I had also previous tried 'shift'ing the same
thing onto INC.
As you can see below, it does show my added path in the INC array but
still no luck..
Cheers,
M
-----Original Message-----
From: [Email Removed] [mailto:[Email Removed]]
Sent: Thursday, January 20, 2005 6:51 AM
To: Henry, Mark Patrick
Cc: [Email Removed]
Subject: Re: Can't locate loadable object for module
----- Original Message -----
From: "Henry, Mark Patrick" <[Email Removed]>
Date: Thursday, January 20, 2005 0:54 am
Subject: Can't locate loadable object for module
Hello;
QUOTE |
I'm trying to use a perl module that a tool I got, ccmeter.pl, requiresand I can't no matter what I do..
I'm on hpux11.11, and the script was written for 5.005_03 so that's what I'm including at the top of the perl script ( #!/apps/perl-5.005_03/bin/perl) - this exists and *is* that particular version.
The module is Time::HiRes, the file is HiRes.pm
I d/l'd the module from cpan and put it in my user directory... ~mhenry/tools/3rdparty/Time/HiRes.pm The file exists of course, has read bit set across the board.
When I run the script it complains it "Can't locate loadable object for module Time::HiRes in @INC"
If I run 'perl -V', I get (among other things): @INC: /apps/perl-5.005_03/lib/5.00503/PA-RISC1.1 /apps/perl-5.005_03/lib/5.00503 /apps/perl-5.005_03/lib/site_perl/5.005/PA-RISC1.1 /apps/perl-5.005_03/lib/site_perl/5.005
So, in the script I put the following:
use lib '/users/mhenry/tools/3rdparty';
I get: Can't locate loadable object for module Time::HiRes in @INC (@INC contains: /users/mhenry/tools/3rdparty /apps/perl-5.005_03/lib/5.00503/PA-RISC1.1 /apps/perl-5.005_03/lib/5.00503 /apps/perl-5.005_03/lib/site_perl/5.005/PA-RISC1.1 /apps/perl-5.005_03/lib/site_perl/5.005) at ./ccmeter.pl line 221
I changed the line above to:
use lib '/users/mhenry/tools/3rdparty/Time';
I get the same..
I tried putting the HiRes.pm file at every level of the dirs mentioned above in the 'perl -V' return, but nothing!!
It seems to be a simple task but nothing will work.
The 'head' of the module is:
package Time::HiRes;
use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK @EXPORT_FAIL);
require Exporter; require DynaLoader;
@ISA = qw(Exporter DynaLoader);
..which appears to be legit.
Can *anyone* help??! I've tried every google search result out there.. Did you try: |
push @INC , '/users/mhenry/tools/3rdparty/Time';
HTH,
Mark G.
FlashMX
Jan 24 2005, 05:13 PM
Hi,
I have a large file that I want to split into smaller chunks based on
a start and end text (<start>...<end>)
My script works only partially. It only saves the first occurence of
my match and then closes the script. How can I get it to keep ripping
throught the file saving into individual files.
My file I'm reading in is called test.txt
Below is just an example.
blah...blah...
blah...blah...
blah...blah...
<start>
....
<end>
blah...blah...
blah...blah...
blah...blah...
<start>
....
<end>
and so on...
Here is my script:
#!/usr/bin/perl -w
use strict;
my $written = 0;
my $index = 1;
my $filename;
if ( open( FH, 'D:test.txt' ) )
{
$filename = sprintf( 'D:out%d.txt', $index );
open( OUT, ">$filename" );
while ( <FH> )
{
if ( m|^<start>$| ... m|^<end>$| )
{
print OUT $_;
$written = 1;
}
else
{
if ( $written )
{
close( OUT );
$index++;
$filename = sprintf( 'C:out%d.txt', $index );
open( OUT, ">$filename" );
$written = 0;
}
}
print "-> $_";
}
close( FH );
}
John W. Krahn
Jan 25 2005, 02:47 AM
FlashMX wrote:
Hello,
QUOTE |
I have a large file that I want to split into smaller chunks based on a start and end text (<start>...<end>)
My script works only partially. It only saves the first occurence of my match and then closes the script. How can I get it to keep ripping throught the file saving into individual files.
My file I'm reading in is called test.txt
Below is just an example.
blah...blah... blah...blah... blah...blah...
<start ... <end
blah...blah... blah...blah... blah...blah...
<start ... <end
and so on...
Here is my script:
#!/usr/bin/perl -w
use strict;
my $written = 0; my $index = 1; my $filename;
if ( open( FH, 'D:test.txt' ) ) { $filename = sprintf( 'D:out%d.txt', $index ); open( OUT, ">$filename" ); while ( <FH> ) { if ( m|^<start>$| ... m|^<end>$| ) { print OUT $_; $written = 1; } else { if ( $written ) { close( OUT ); $index++; $filename = sprintf( 'C:out%d.txt', $index ); open( OUT, ">$filename" ); $written = 0; } }
print "-> $_";
} close( FH ); }
|
If I understand correctly then this should do what you want: (UNTESTED)
if ( open FH, '<', 'D:/test.txt' ) {
while ( <FH> ) {
my $range = /^<start>$/ .. /^<end>$/;
if ( $range == 1 ) {
my $filename = sprintf 'C:/out%d.txt', $index++;
open OUT, '>', $filename or die "Cannot open $filename: $!";
}
if ( $range ) {
print OUT;
}
else {
print "-> $_";
}
}
close FH;
}
John
--
use Perl;
program
fulfillment
Brian McKee
Jan 28 2005, 07:25 PM
QUOTE |
From: "O-Dzin Tridral" <[Email Removed] Date: Fri Jan 28, 2005 8:06:09 AM Canada/Eastern Subject: Converting a text file for printing We have an application which produces a plain ASCII text file which then needs to be printed on a laser printer. Because it gets printed to pre-printed stationery things have to be correctly aligned.
|
We do that here - hundreds a day in fact.
In our case we figured out what the PCL codes were and embedded them in
the text,
sending that directly to the printer. It isn't perl, but I don't see
why it couldn't be.
Dunno how easy that would be on a Windows machine.
Brian
How do you check Perl 5.8.1 is configured for Dynamic Loading?
Dan
Atterbigler Josef
Jan 31 2005, 02:00 PM
Hi,
Many thanks all, for your early reply, it works now fine.
regards
Joe
-----Ursprngliche Nachricht-----
Von: Atterbigler Josef [mailto:[Email Removed]]
Gesendet: Montag, 31. Januar 2005 10:23
An: [Email Removed]
Betreff: dbi:odbc: dont get all rows from database
Hi
I dont get all rows from database with my perlscript, following error displayed: Use of uninitialized value in concatenation (.)
The same query statement on mssql server I get more rows, whats wrong?
my $statement2 = "select Doctyp, Count (*) as count_doctyp from tdoc_kd where archdate between '05/23/2003' and '05/26/2003' group by Doctyp Order by count_doctyp DESC";
#----------------------------------------------------------------
# Databasequery
$sth = $dbh->prepare($statement2);
$sth->execute();
@doctyp = $sth->fetchrow_array;
$sth->finish;
#------------------------------------------------------------------
# Output
print "$doctyp[0] t";
print "$doctyp[1] n";
print "$doctyp[2] t";
print "$doctyp[3] n";
Thanks for your help
regards
Joe
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
Hinweis: Dieses E-mail kann vertrauliche und geschtzte Informationen enthalten.
Sollten Sie nicht der beabsichtigte Empfnger sein, verstndigen Sie bitte den Absender und lschen Sie dieses E-mail dann sofort.
Notice: This e-mail contains information that is confidential and may be privileged.
If you are not the intended recipient, please notify the sender and then delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
Hinweis: Dieses E-mail kann vertrauliche und geschtzte Informationen enthalten.
Sollten Sie nicht der beabsichtigte Empfnger sein, verstndigen Sie bitte den Absender und lschen Sie dieses E-mail dann sofort.
Notice: This e-mail contains information that is confidential and may be privileged.
If you are not the intended recipient, please notify the sender and then delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
Bob Showalter
Jan 31 2005, 02:14 PM
Anish Kumar K. wrote:
QUOTE |
File is not getting created in perl CGI mode...
It is surprising that the file is not created in the CGI....
I tried this from the command line it is getting created there...
|
Most likely explanation is that the web server user does not have write
privilege in the directory you're trying to create the file in, or the file
already exists and the web server user doesn't have write privilege.
Add $! to your error message to see more information.
QUOTE |
I am not able to debug also this issue. Please help in proceeding this....
#!/usr/bin/perl use CGI;
|
use strict;
use warnings;
QUOTE |
my $cgi=new CGI; print $cgi->header(); print $cgi->start_html("Browser Information");
open (TXT, ">tmp.txt") || die "Can't create $file";
|
($file is not defined)
open (TXT, ">/some/absolute/path/tmp.txt") || die "Can't create file: $!";
You should always use an absolute path from a CGI script. Don't make
assumptions about what the current directory is. Probably, it's the script
directory, but you can't depend on that.
QUOTE |
print TXT"This is test"; close TXT; |
Wiggins d'Anconia
Jan 31 2005, 03:15 PM
[Email Removed] wrote:
QUOTE |
How do you check Perl 5.8.1 is configured for Dynamic Loading?
Dan
|
Bob Showalter
Feb 1 2005, 02:00 PM
Mark Martin wrote:
QUOTE |
Hi, okay - straight out of the coobook :
my @different = () foreach (keys %hash1) { delete $hash1{$_} unless exists $hash2$_}; push(@this_not_that,$_) unless exists $registered{$_}; }
easy to remove the different key from hash one and record the removed item in @different.
My question is how would you record the removed item in another hash so that it has the original key and corresponding value?
I've tried :
%different = (); foreach $key (keys %hash1) { $value = $hash1{$key}; @different{$_} = $value unless exists $hash2{$_}; }
|
You're using $_, but $key has the key
QUOTE |
but I get all sorts of rubbish
|
delete() returns the thing(s) deleted, so
for my $key (keys %hash1) {
$different{$key} = delete $hash1{$key} unless exists $hash2{$key};
}
JupiterHost.Net
Feb 1 2005, 04:56 PM
Michael Seele wrote:
Hello,
QUOTE |
i want to open a perl process and enter the code/files to compile via STDIN. i know it is possible. but how? does somebody know a tutorial or something like this which explains how i can compile perl code via STDIN?
|
Do you mean:
perl -e 'print "Hin";'
perl script.pl
Dave Gray
Feb 1 2005, 06:06 PM
QUOTE |
i want to open a perl process and enter the code/files to compile via STDIN. i know it is possible. but how? does somebody know a tutorial or something like this which explains how i can compile perl code via STDIN?
|
$ perl
print "Hello from STDINn";
^D
Hello from STDIN
$
If the answers so far don't solve your problem, it might help to be
more specific about what exactly you're trying to do.
Dave
Morning
I'm trying to run Authen:Smb
After spending some time debugging, it returns an error can not locate
valid_user.al
valid_user() is a function call from within Authen::Smb.
Which package/module contain valid_user.al ?
Dan
Roman Vaek
Feb 2 2005, 12:40 PM
Probably wrong/broken installation of Authen::Smb. Valid_User is defined
in file smbval/valid.c which is part of Authen::Smb distribution. Try to
recompile/reinstall Authen::Smb again.
Roman
On Wed, Feb 02, 2005 at 12:36:06PM +0000, [Email Removed] wrote:
QUOTE |
Morning
I'm trying to run Authen:Smb
After spending some time debugging, it returns an error can not locate valid_user.al
valid_user() is a function call from within Authen::Smb.
Which package/module contain valid_user.al ?
Dan
|
--
best regards
Ing. Roman Vasicek
software developer
+----------------------------------------------------------------------------+
PetaMem s.r.o., Ocelarska 1, 190 00 Praha 9 - Liben, Czech Republic
http://www.petamem.com/
Zentara
Feb 2 2005, 03:44 PM
On Wed, 02 Feb 2005 09:26:48 +0100, [Email Removed] (Michael
Seele) wrote:
QUOTE |
yes that is exactly what i mean. i want to do somthing like this:
$ perl - c print "Testn";
and now i want to get the infos of the compilation (sytax ok or errors...). after that i want to put the next perl code into the process, i don't want to close the process and reopen a new. i only want to work with one process because of the speed advantages i got when i must _not_ open a process for every file... is this possible? do you know a information source in the www? thx
|
It sounds like you want the IPC::Open3 module,
search the web(or this list archives) for examples, there
are plenty. You can open your program with IPC::Open3, then write
to it'd stdin, and listen for sdtout or stderr. You can continously
feed it data on it's stdin.
For simpler functionality, there is the "piped form of open", but
IPC::Open3 gives you more control.
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
I maybe missing something here.
I try to rpmbuild authen-smb.0.90.src.rpm
That fails to generate rpm file.
I did try to re-recompile authen-smb.0.91.tar.gz
It complies OK no errors.
It install Smb files. But, it do not generate valid_user.al file.
Do I need ti set a flag to create the valid_user.al file?
Dan
At 12:40 02/02/2005, Roman =?iso-8859-2?B?VmG57ehlaw==?= wrote:
QUOTE |
Probably wrong/broken installation of Authen::Smb. Valid_User is defined in file smbval/valid.c which is part of Authen::Smb distribution. Try to recompile/reinstall Authen::Smb again.
Roman
On Wed, Feb 02, 2005 at 12:36:06PM +0000, [Email Removed] wrote: Morning
I'm trying to run Authen:Smb
After spending some time debugging, it returns an error can not locate valid_user.al
valid_user() is a function call from within Authen::Smb.
Which package/module contain valid_user.al ?
Dan
-- best regards Ing. Roman Vasicek
software developer +----------------------------------------------------------------------------+ PetaMem s.r.o., Ocelarska 1, 190 00 Praha 9 - Liben, Czech Republic http://www.petamem.com/ |
Dave Gray
Feb 2 2005, 04:27 PM
QUOTE |
yes that is exactly what i mean. i want to do somthing like this:
$ perl - c print "Testn";
and now i want to get the infos of the compilation (sytax ok or errors...). after that i want to put the next perl code into the process, i don't want to close the process and reopen a new.
|
I don't think syntax checking mode has a batch option... AFAIK it's
just for one file at a time. What exactly are you doing that you're
concerned about interpreter startup time when checking syntax?
John Moon
Feb 2 2005, 10:49 PM
Subject: Checking last character of string for punctuation
Howdy all. I'm a newbie with a newbie question. I assume that I'm in the
right place.
I need to update some code a developer programmed for my organization.
The purpose of the code is really basic. It simply prints: $title.
$subtitle.
However, some of my titles and subtitles end with punctuation ("?", "!",
".", "..."). Thus, I end up with: Am I a title that ends with punctuation?.
Do-oh!.
So, I need to revise the code so that it first checks each string to see if
it ends with a punctuation character. If it does, don't add the period. If
not, do add the period. Note: the string should be kept as is.
I believe the key is a regular expression of some sort, but I can't seem to
figure it out.
Any help would be appreciated!
Thanks in advance,
Chris
$string .= '.' unless $string =~ /[$!.]+$/;
See if this helps...
jwm
Thomas Btzler
Feb 3 2005, 09:39 AM
Elliot Holden <[Email Removed]> asked:
QUOTE |
okay this may seem like a simple problem but here it goes: [...] open(OUTFILE, ">>survey.txt"); [...] |
Your web server may not have permission to create
the file in question. Always check return codes!
open(OUTFILE, ">>survey.txt") or die "Could not open file: $!";
HTH,
Thomas
Thomas Btzler
Feb 3 2005, 11:51 AM
Anish Kumar K. <[Email Removed]> asked:
QUOTE |
Is there any way in perl I can modify the permission of a directory using perl scripts
|
Of course. Check out the builtins chmod and chown.
HTH,
Thomas
Bob Showalter
Feb 4 2005, 07:22 PM
Kent, Mr. John (Contractor) wrote:
QUOTE |
Greetings,
Trying to be "cool" and apply map to the following lines of code (which work fine as shown)
@TIMES is an array of lines that look like Time: Thu Feb 3 15:10:39.290 GMT 2005 End: Default.2.def-edtp0.on Time: user 0.29 sec Delta: Default.2.def-edtp0.on Time: Thu Feb 3 15:10:43 GMT 2005 Start: Default.1.def-slpr-2m-t-10m-wind0.on Time: Thu Feb 3 15:10:43.580 GMT 2005 End: Default.1.def-slpr-2m-t-10m-wind0.on Time: user 0.58 sec Delta: Default.1.def-slpr-2m-t-10m-wind0.on
use Tie::IxHash; tie %BY_PROJ_HASH, "Tie::IxHash";
my @DELTAS = grep (/Delta/, @TIMES);
foreach (@DELTAS) { $_ = /(d+.d+)s+secs+Delta:s+[Default.]*s*(.+)[.on]*$/; my $proj = $2; my $time = $1;
|
If the regex doesn't match, $1 and $2 will be unchanged. You should not use
them without checking for the match.
I prefer a list assignment like:
my ($proj, $time) =
/(d+.d+)s+secs+Delta:s+[Default.]*s*(.+)[.on]*$/;
This will set the vars to undef if there is no match.
QUOTE |
print "<br>$proj => $timen" if ($DEBUG == 1); # Push the value into the hash making the value for the hash # an array (See Perl Cookbook Recipe 5.7) push( @{$BY_PROJ_HASH{$proj}},$time);
}
So far haven't had any luck using map to create the hash of arrays %BY_PROJ_HASH.
|
I don't think a map() construct would improve on what you're doing, since
$proj presumably can vary with each iteration. Leave it like it is.
Bob Showalter
Feb 7 2005, 04:58 PM
Elliot Holden wrote:
QUOTE |
This is my script
open(OUTFILE, ">>", "survey.txt") or die "$!";
I am runing this script through the webserver using a browswer, (action="http://localhost/pathtothecgiscript/cgiscript.cgi") and the file, survey.txt, does NOT get created. When running from the command line the file DOES get created. If I create the file in question, survey.txt, ahead of time and give it the permission 666, the webserver (Apache) can then write records to the file and the script runs with no problem. Alot of people have been telling me that the webserver, in my case Apache, usually runs as a different user with different permissions than the user I am logged in as. But NO ONE can yet tell me how to give Apache or any webserver the right to create a file.
|
In order to create a new file, the user must have write permission in the
*directory* the file is to be created in. chmod 777 on the directory should
do the trick. Or, change the ownership of the directory to the Apache user
and chmod 755. Or, add the Apache user to the group that owns the directory
and chmod 775.
(P.S. "Alot" is not a word)
John Moon
Feb 8 2005, 02:22 PM
Subject: Having trouble using "strict"
Hello there!
I got a trouble (I think). I'd like to store some variables in a
separated file ie vars.pl to have a possibility of using them in
different scripts by 'require "vars.pl"'. So if I use some script using
the variable $var1 = somevalue storing in 'vars.pl' and I call this
variable with using 'strict' pragma I get error message - $var1 is
unknown for this script of course because 'strict' is on; I must declare
this with "my". But in this case I hade the value of this variable. How
could I solve this trouble?
Thanks in advance.
See if this gives you some ideas...
#! /usr/local/bin/perl
use strict;
use lib "$ENV{HOME}/local/library";
require MyVars;
my $v1 = 'different Value';
print "$MyVars::v1n";
print "$v1n";
SUN1-BATCH>more ~/local/library/MyVars.pm
package MyVars;
$v1='AAAAAAAA';
$v2='BBBBBBBB';
SUN1-BATCH>
Zentara
Feb 8 2005, 01:19 PM
On Mon, 7 Feb 2005 11:58:57 -0500 , [Email Removed] (Bob
Showalter) wrote:
QUOTE |
(P.S. "Alot" is not a word)
|
Jees, you learn "much" more than just Perl here. :-)
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Nicolay Vasiliev
Feb 8 2005, 03:46 PM
Oh, I got the main direction, thank you very much!
John Moon wrote:
QUOTE |
Subject: Having trouble using "strict"
Hello there!
I got a trouble (I think). I'd like to store some variables in a separated file ie vars.pl to have a possibility of using them in different scripts by 'require "vars.pl"'. So if I use some script using the variable $var1 = somevalue storing in 'vars.pl' and I call this variable with using 'strict' pragma I get error message - $var1 is unknown for this script of course because 'strict' is on; I must declare this with "my". But in this case I hade the value of this variable. How could I solve this trouble?
Thanks in advance.
See if this gives you some ideas...
#! /usr/local/bin/perl use strict; use lib "$ENV{HOME}/local/library"; require MyVars; my $v1 = 'different Value'; print "$MyVars::v1n"; print "$v1n";
SUN1-BATCH>more ~/local/library/MyVars.pm package MyVars; $v1='AAAAAAAA'; $v2='BBBBBBBB'; SUN1-BATCH |
Bob Showalter
Feb 9 2005, 04:29 PM
Joe Mecklin wrote:
QUOTE |
i need the ability to accept keyboard input without echoing it back to the screen (passwords, etc.) and have not yet found anything that will accomplish that. is there something in Perl that will do that, do i need a module, ...?
thanks in advance for any/all help.
|
Take a look at the faq article:
perldoc -q 'How do I ask the user for a password?'
TapasranjanMohapatra
Feb 9 2005, 02:50 PM
-----Original Message-----
From: Chris Devers [mailto:[Email Removed]]
Sent: Wed 2/9/2005 8:01 PM
To: TapasranjanMohapatra
Cc: Perl Beginners List
Subject: Re: while(1){print "a"; sleep 1;}
On Wed, 9 Feb 2005, TapasranjanMohapatra wrote:
QUOTE |
Why I dont get a's printed with the code below?
|
Apparently it's an output buffering issue.
If you flush output, it works:
$ perl -e 'while(1){print"a";sleep 1}'
^C
$ perl -e '$|=1;while(1){print"a";sleep 1}'
aaaaaaa^C
$
So, setting $| to 1 seems to fix the problem...
--
Chris Devers
Thanks Chris,
Forcing the flush after print solves the problem.
Tapas
Joe Mecklin
Feb 9 2005, 04:35 PM
bless you, my child *g*
On Wed, 9 Feb 2005 11:29:00 -0500, Bob Showalter
<[Email Removed]> wrote:
QUOTE |
Joe Mecklin wrote: i need the ability to accept keyboard input without echoing it back to the screen (passwords, etc.) and have not yet found anything that will accomplish that. is there something in Perl that will do that, do i need a module, ...?
thanks in advance for any/all help.
Take a look at the faq article:
perldoc -q 'How do I ask the user for a password?'
|
Xiaofang Zhou
Feb 12 2005, 04:13 AM
Hi, Bob,
Sounds the expert.pm is cool. But when I try to install the pre-required
io::pty, I got a fatal error. Is it possible to install io::tty and expert
on xp?
---------------------------
cl -c -nologo -O1 -MD -Zi -DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT -DHAV
E_DES_FCRYPT -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DPERL_MSVCRT_READFIX -
O1 -MD -Zi -DNDEBUG -DVERSION="1.02" -DXS_VERSION="1.02" -IE:Perllib
CORE Tty.c
Tty.c
Tty.xs(94) : fatal error C1083: Cannot open include file: 'unistd.h': No such fi
le or directory
NMAKE : fatal error U1077: 'cl' : return code '0x2'
Stop.
---------------------------
2005-02-11 21:42:00 д
QUOTE |
Chris Devers wrote: Several programming languages have mechanisms for building wrappers around Expect, including Perl. In this case, you need a module like Expect.pm or Expect::Simple, as described here:
Pedantic point: Expect.pm is perhaps best thought of as an Expect "clone" or "workalike". It does not use or require a tcl installation.
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/> <http://learn.perl.org/first-response
|
Xiaofang Zhou
[Email Removed]
Perl has modules you can include that allow you to use the power of perl and the functionality of Expect.
----- Original Message -----
From: A B C <[Email Removed]>
Date: Friday, February 11, 2005 6:16 pm
Subject: Perl versus EXPECT(tcl)
QUOTE |
Greetings,
I've heard that Expect(tcl) is outstanding in what it specializes in. Can any experts in both Expect and Perl Networking comment?
I want to know if Perl's Networking arena is superior or equal to what Expect specializes in.
__________________________________ Do you Yahoo!? All your favorites on one personal page ? Try My Yahoo! http://my.yahoo.com
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/> <http://learn.perl.org/first-response
|
Xiaofang Zhou
Feb 12 2005, 05:57 AM
Hi,
I have trouble using B::Bytecode on xp/activeperl 5.6. With a very
simple script, the command:
perl -MO=Bytecode,-ofirst.byte first.pl
Can't work properly.
------first.pl
#!/usr/bin/perl -w
use strict;
print "Hello World!n";
1;
-------------
Please let me know if anyone have successfully do that on xp/active perl 5.6.
Thanks.
Xiaofang.
----output of the perl -MO command:
No package specified for compilation, assuming main::
Use of uninitialized value in hash element at E:/Perl/lib/B/Assembler.pm line 19
6.
Use of uninitialized value in concatenation (.) or string at E:/Perl/lib/B/Assem
bler.pm line 202.
1: no such instruction ""
Use of uninitialized value in hash element at E:/Perl/lib/B/Assembler.pm line 19
6.
Use of uninitialized value in concatenation (.) or string at E:/Perl/lib/B/Assem
bler.pm line 202.
2: no such instruction ""
Use of uninitialized value in hash element at E:/Perl/lib/B/Assembler.pm line 19
6.
Use of uninitialized value in concatenation (.) or string at E:/Perl/lib/B/Assem
bler.pm line 202.
3: no such instruction ""
Use of uninitialized value in hash element at E:/Perl/lib/B/Assembler.pm line 19
6.
Use of uninitialized value in concatenation (.) or string at E:/Perl/lib/B/Assem
bler.pm line 202.
4: no such instruction ""
..............................
....many many such warnings...
..............................
444: no such instruction ""
Use of uninitialized value in hash element at E:/Perl/lib/B/Assembler.pm line 19
6.
Use of uninitialized value in concatenation (.) or string at E:/Perl/lib/B/Assem
bler.pm line 202.
445: no such instruction ""
There were 445 assembly errors
CHECK failed--call queue aborted.
PLBCMSWin32-x86-multi-threada0.04a*aaa>aaa0x1234a
--------------------------------------------------------------
2005-02-12 12:13:00 д
QUOTE |
Hi, Bob,
Sounds the expert.pm is cool. But when I try to install the pre-required io::pty, I got a fatal error. Is it possible to install io::tty and expert on xp?
--------------------------- cl -c -nologo -O1 -MD -Zi -DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT -DHAV E_DES_FCRYPT -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DPERL_MSVCRT_READFIX - O1 -MD -Zi -DNDEBUG -DVERSION="1.02" -DXS_VERSION="1.02" -IE:Perllib CORE Tty.c Tty.c Tty.xs(94) : fatal error C1083: Cannot open include file: 'unistd.h': No such fi le or directory NMAKE : fatal error U1077: 'cl' : return code '0x2' Stop. ---------------------------
2005-02-11 21:42:00 д Chris Devers wrote: Several programming languages have mechanisms for building wrappers around Expect, including Perl. In this case, you need a module like Expect.pm or Expect::Simple, as described here:
Pedantic point: Expect.pm is perhaps best thought of as an Expect "clone" or "workalike". It does not use or require a tcl installation.
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/> <http://learn.perl.org/first-response
Xiaofang Zhou [Email Removed]
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/> <http://learn.perl.org/first-response
.
|
Xiaofang Zhou
[Email Removed]
Bob Showalter
Feb 12 2005, 02:24 PM
Xiaofang Zhou wrote:
QUOTE |
Hi, Bob,
Sounds the expert.pm is cool. But when I try to install the pre-required io::pty, I got a fatal error. Is it possible to install io::tty and expert on xp?
|
No. ptys are a Unix only thing.
Juan Gomez
Feb 12 2005, 03:59 PM
Hi Xiaofang
I dont think is posible you see pty's are only for unix
Thats what I remember and thats why it crash
Let me get some more info and I send it to you
Have a good day
-----Original Message-----
From: Xiaofang Zhou [mailto:[Email Removed]]
Sent: Friday, February 11, 2005 11:00 AM
To: Perl Beginners List
Subject: Re: Re: Perl versus EXPECT(tcl)
Hi, Bob,
Sounds the expert.pm is cool. But when I try to install the pre-required
io::pty, I got a fatal error. Is it possible to install io::tty and expert
on xp?
---------------------------
cl -c -nologo -O1 -MD -Zi -DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT
-DHAV E_DES_FCRYPT -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS
-DPERL_MSVCRT_READFIX -
O1 -MD -Zi -DNDEBUG -DVERSION="1.02" -DXS_VERSION="1.02"
-IE:Perllib
CORE Tty.c
Tty.c
Tty.xs(94) : fatal error C1083: Cannot open include file: 'unistd.h': No
such fi le or directory NMAKE : fatal error U1077: 'cl' : return code '0x2'
Stop.
---------------------------
2005-02-11 21:42:00 д
QUOTE |
Chris Devers wrote: Several programming languages have mechanisms for building wrappers around Expect, including Perl. In this case, you need a module like Expect.pm or Expect::Simple, as described here:
Pedantic point: Expect.pm is perhaps best thought of as an Expect "clone" or "workalike". It does not use or require a tcl installation.
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/ <http://learn.perl.org/first-response
|
Xiaofang Zhou
[Email Removed]
--
To unsubscribe, e-mail: [Email Removed] For additional
commands, e-mail: [Email Removed] <http://learn.perl.org/>
<http://learn.perl.org/first-response>
Hi Xiaofang:
Because expect waits response from unix (or linux) - it 'EXPECTS' a
response, it won't work on xp. Expect is more like a tcl script than perl
and I don't think there's a tcl script either for xp (or dos) - I may be
wrong.
Hope this helps a little.
Ron
----- Original Message -----
From: "Gomez, Juan" <[Email Removed]>
To: "Xiaofang Zhou" <[Email Removed]>; "Perl Beginners List"
<[Email Removed]>
Sent: Saturday, February 12, 2005 9:59 AM
Subject: RE: Re: Perl versus EXPECT(tcl)
Hi Xiaofang
I dont think is posible you see pty's are only for unix
Thats what I remember and thats why it crash
Let me get some more info and I send it to you
Have a good day
-----Original Message-----
From: Xiaofang Zhou [mailto:[Email Removed]]
Sent: Friday, February 11, 2005 11:00 AM
To: Perl Beginners List
Subject: Re: Re: Perl versus EXPECT(tcl)
Hi, Bob,
Sounds the expert.pm is cool. But when I try to install the pre-required
io::pty, I got a fatal error. Is it possible to install io::tty and expert
on xp?
---------------------------
cl -c -nologo -O1 -MD -Zi -DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT
-DHAV E_DES_FCRYPT -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS
-DPERL_MSVCRT_READFIX -
O1 -MD -Zi -DNDEBUG -DVERSION="1.02" -DXS_VERSION="1.02"
-IE:Perllib
CORE Tty.c
Tty.c
Tty.xs(94) : fatal error C1083: Cannot open include file: 'unistd.h': No
such fi le or directory NMAKE : fatal error U1077: 'cl' : return code '0x2'
Stop.
---------------------------
2005-02-11 21:42:00 д
QUOTE |
Chris Devers wrote: Several programming languages have mechanisms for building wrappers around Expect, including Perl. In this case, you need a module like Expect.pm or Expect::Simple, as described here:
Pedantic point: Expect.pm is perhaps best thought of as an Expect "clone" or "workalike". It does not use or require a tcl installation.
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/ <http://learn.perl.org/first-response
|
Xiaofang Zhou
[Email Removed]
--
To unsubscribe, e-mail: [Email Removed] For additional
commands, e-mail: [Email Removed] <http://learn.perl.org/>
<http://learn.perl.org/first-response>
--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Jan Eden
Feb 12 2005, 09:18 PM
Hi Ovid,
Ovid wrote on 11.02.2005:
QUOTE |
Hi Jan,
Apologies in advance if any of this seems too pedantic.
I was asking for pedantic remarks. ;-) |
QUOTE |
What you are essentially looking for is SQL capable of handling tree structures so you can pull this data in a single fetch. You can read about this in Joe Celko's SQL for Smarties or (more specifically) Joe Celko's Trees and Hierarchies in SQL for Smarties.
Thanks! You're right, that's what I was looking for. |
QUOTE |
First, you'll notice that I pass in $dbh and $page_hash. Good subroutines are frequently treated like black boxes. They accept information and they give a response. In your subroutine, it's almost there, but it relies on $dbh and $page_hash being declared globally. If those ever get munged, it can be quite difficult to track down the error. Plus, if you reuse this code elsewhere, it's harder to do if those are global.
Agreed. I actually know and follow that guideline. The code I posted was not yet fine tuned and complete. |
QUOTE |
The more serious problem, though, was how you were using selectrow_array. My guess, from how your form was set up, is that $mutterid was being passed in from the form. If you've carefully untainted it, it's probably OK to use it the way you were, but it's still a tripwire for further maintenance and possibly a serious security hole due to having it embedded directly in the SQL. This leaves the code vulnerable to an SQL injection attack (http://www.unixwiz.net/techtips/sql-injection.html).
|
Oh, that should not be an issue - $mutterid does not come from a user, but is pulled from the database itself.
Besides, I use the taint mode all the time and should be notified when I use untainted variables.
QUOTE |
I've converted the code to use bind values to prevent this security problem. See "perldoc DBI" and read the section entitled "Placeholders and Bind Values".
That's right, I should've done taken that measure anyway. |
Thanks again for your hints!
Have a great day,
Jan
--
There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence. - Jeremy S. Anderson
Jason Balicki
Feb 14 2005, 09:11 PM
Eduardo Vzquez Rodrguez <mailto:[Email Removed]> wrote:
QUOTE |
How can I start reading a file from n line? For example I open a File like this way open(INPUT, $file) while (<INPUT>) { do something with $_; }
|
I'm a newbie myself, but is there a reason you've
already discounted something like:
my linecount=1;
open (INPUT, $file);
while (<INPUT>){
if ($linecount >= 10) {
do something;
}
$linecount++;
}
--J(K)
John W. Krahn
Feb 14 2005, 09:13 PM
Eduardo Vzquez Rodrguez wrote:
QUOTE |
Hello Perl Programmers
|
Hello,
QUOTE |
I have a question regarding openning a file
How can I start reading a file from n line?
For example
I open a File like this way
open(INPUT, $file) while (<INPUT>) { do something with $_; }
But It starts to read from line #1, what I want is that start reading exactly from line 10.
|
Read the first nine lines before the while loop:
open INPUT, $file or die "Cannot open $file: $!";
# get rid of first nine lines
<INPUT> for 1 .. 9;
while ( <INPUT> ) {
do something with $_;
}
John
--
use Perl;
program
fulfillment
John W. Krahn
Feb 14 2005, 09:45 PM
Jason Balicki wrote:
QUOTE |
Eduardo Vzquez Rodrguez <mailto:[Email Removed]> wrote:
How can I start reading a file from n line? For example I open a File like this way open(INPUT, $file) while (<INPUT>) { do something with $_; }
I'm a newbie myself, but is there a reason you've already discounted something like:
my linecount=1; open (INPUT, $file); while (<INPUT>){ if ($linecount >= 10) { do something; } $linecount++; }
|
You could use the built-in $. variable for that:
while ( <INPUT> ) {
next if 1 .. 9;
do something;
}
John
--
use Perl;
program
fulfillment
if you want to use a built in function then use
$.
or its alternatives $NR, or $INPUT_LINE_NUMBER
By definition this says "the current input line number of the last file
handle that was read."
my $linect=$.
while ( <FILEHANDLE>) {
if ( $linect > 9 ) {
do whatever ...
}
}
derek
"John W. Krahn"
<[Email Removed]
QUOTE |
To Perl Beginners <[Email Removed] |
02/14/2005 04:13 cc
PM
Subject
Re: Start reading from a specific
line
Eduardo Vzquez Rodrguez wrote:
QUOTE |
Hello Perl Programmers
|
Hello,
QUOTE |
I have a question regarding openning a file
How can I start reading a file from n line?
For example
I open a File like this way
open(INPUT, $file) while (<INPUT>) { do something with $_; }
But It starts to read from line #1, what I want is that start reading exactly from line 10.
|
Read the first nine lines before the while loop:
open INPUT, $file or die "Cannot open $file: $!";
# get rid of first nine lines
<INPUT> for 1 .. 9;
while ( <INPUT> ) {
do something with $_;
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
John W. Krahn
Feb 14 2005, 11:38 PM
[Email Removed] wrote:
QUOTE |
if you want to use a built in function then use ^^^^^^^^ |
variable
QUOTE |
$.
or its alternatives $NR, or $INPUT_LINE_NUMBER
|
Which implys that you are using the 'English' module which is a bad idea if
speed is important.
QUOTE |
By definition this says "the current input line number of the last file handle that was read."
my $linect=$.
|
You haven't read from FILEHANDLE yet so $. has the value undef.
QUOTE |
while ( <FILEHANDLE>) { if ( $linect > 9 ) {
|
Unlike $. which changes for each line read, $linect does not change so will
never be greater than 9.
John
--
use Perl;
program
fulfillment
Xiaofang Zhou
Feb 15 2005, 07:15 AM
Hi, Ramprasad
use ( ) not { } in regexp.
$x=~s/^(.*?) (d+)/ $2 . ( defined $hash{$2} ? $hash{$2} : 'NOTFOUND' ) /e;
2005-02-15 12:30:00 д
QUOTE |
Hi
I want to so a substititue a string Say
my %hash = ( 1130, "a" , 2100, "b");
$x = 'SOMEJUNK 1130';
# I want $x to be '1130a'
# This regex does not work $x=~s/^(.*?) (d+)/ $2 . { defined $hash{$2} ? $hash{$2} : 'NOTFOUND' } /e;
Can someone fix the regex for me
Thanks Ram
|
Xiaofang Zhou
[Email Removed]
Jeff 'japhy' Pinyan
Feb 15 2005, 10:38 AM
On Feb 15, [Email Removed] said:
QUOTE |
my $linect="$."; while ( <FILEHANDLE>) { if ( $linect > 9 ) { do whatever ... } $linect++; }
need to use double-quotes around variable $.
|
No you don't.
my $linecount = $.;
works just fine.
And is there a reason you don't want to use $.? That is, why create
another variable which does the same thing?
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
Jeff 'japhy' Pinyan
Feb 15 2005, 11:51 AM
On Feb 15, [Email Removed] said:
QUOTE |
Could not find this variable in perlvar under 5.8.0 for sun solaris can you explain it?
|
perlmonk:~ 101:$ perldoc perlvar
....
$. Current line number for the last filehandle accessed.
Each filehandle in Perl counts the number of lines that have
been read from it. (Depending on the value of $/, Perl's idea
of what constitutes a line may not match yours.) When a line
is read from a filehandle (via readline() or "<>"), or when
tell() or seek() is called on it, $. becomes an alias to the
line counter for that filehandle.
You can adjust the counter by assigning to $., but this will
not actually move the seek pointer. Localizing $. will not
localize the filehandle's line count. Instead, it will local-
ize perl's notion of which filehandle $. is currently aliased
to.
$. is reset when the filehandle is closed, but not when an open
filehandle is reopened without an intervening close(). For
more details, see "I/O Operators" in perlop. Because "<>"
never does an explicit close, line numbers increase across ARGV
files (but see examples in "eof" in perlfunc).
You can also use "HANDLE->input_line_number(EXPR)" to access
the line counter for a given filehandle without having to worry
about which handle you last accessed.
(Mnemonic: many programs use "." to mean the current line num-
ber.)
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
oops I missed a line:
my $linect="$.";
while ( <FILEHANDLE>) {
if ( $linect > 9 ) {
do whatever ...
}
$linect++;
}
need to use double-quotes around variable $.
thanks for the correction... keeps me on my perl learning toes.
Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
Eduardo Vzquez
Rodrguez
<evazquez@insys-c To
orp.com.mx> "'[Email Removed] '"
<[Email Removed]>, 'Perl
02/14/2005 06:20 Beginners ' <[Email Removed]>
PM cc
Subject
RE: Start reading from a specific
line
Thanks I prove it and worked!!!!!
-----Original Message-----
From: [Email Removed]
To: Perl Beginners
Sent: 2/14/2005 5:13 PM
Subject: Re: Start reading from a specific line
if you want to use a built in function then use
$.
or its alternatives $NR, or $INPUT_LINE_NUMBER
By definition this says "the current input line number of the last file
handle that was read."
my $linect=$.
while ( <FILEHANDLE>) {
if ( $linect > 9 ) {
do whatever ...
}
}
derek
"John W. Krahn"
<[Email Removed]
Perl Beginners
<[Email Removed]>
02/14/2005 04:13
cc
PM
Subject
Re: Start reading from a specific
line
Eduardo Vzquez Rodrguez wrote:
QUOTE |
Hello Perl Programmers
|
Hello,
QUOTE |
I have a question regarding openning a file
How can I start reading a file from n line?
For example
I open a File like this way
open(INPUT, $file) while (<INPUT>) { do something with $_; }
But It starts to read from line #1, what I want is that start reading exactly from line 10.
|
Read the first nine lines before the while loop:
open INPUT, $file or die "Cannot open $file: $!";
# get rid of first nine lines
<INPUT> for 1 .. 9;
while ( <INPUT> ) {
do something with $_;
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>