[24049] in Perl-Users-Digest
Perl-Users Digest, Issue: 6246 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 11 14:05:54 2004
Date: Thu, 11 Mar 2004 11:05:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 11 Mar 2004 Volume: 10 Number: 6246
Today's topics:
Re: array performance- references problem ctcgag@hotmail.com
Re: Causing Perl to issue a single "write" system call? (Anno Siegel)
Re: Causing Perl to issue a single "write" system call? <usenet@morrow.me.uk>
Re: Deleting a folder recursively <usenet@morrow.me.uk>
Re: Deleting a folder recursively <dmcbride@naboo.to.org.no.spam.for.me>
Re: Extracting directories in a path - TMBABWTDI <noreply@gunnar.cc>
Re: Extracting directories in a path - TMBABWTDI <nospam_007@yahoo.com>
Re: Extracting directories in a path - TMBABWTDI <dwall@fastmail.fm>
Re: login to a website problem in Perl <tadmc@augustmail.com>
Re: Multiple compares -- TMTOWTDI (Steve The Geek)
Re: Ordering large files (in perl?) <tadmc@augustmail.com>
Re: Ordering large files (in perl?) (Daniel Cutter)
Re: Ordering large files (in perl?) <jwkenne@attglobal.net>
Parsing from file (Toni)
Re: Parsing from file <josef.moellers@fujitsu-siemens.com>
Re: Parsing from file (Anno Siegel)
Re: Parsing from file <tore@aursand.no>
Perl DB: how2 silence "redefined" warnings? <nospam@nospam.com>
Re: Perl DB: how2 silence "redefined" warnings? <usenet@morrow.me.uk>
Re: perl on windows <chatiman@free.fr>
Re: Simple Reference Question (Len)
Text editor implemented in Perl <spam-block-@-SEE-MY-SIG.com>
Re: Text editor implemented in Perl <tassilo.parseval@rwth-aachen.de>
Using Tie::Handle <vincent+news@vinc17.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 11 Mar 2004 18:32:23 GMT
From: ctcgag@hotmail.com
Subject: Re: array performance- references problem
Message-Id: <20040311133223.626$x2@newsreader.com>
"Andreas Goetz" <andreas.goetz.external@fujitsu-siemens.com> wrote:
> I'm creating a logfile analyzer for my company. I need to store blocks of
> log lines in a global array, to later retrieve them.
>
> Approach 1: push @statements, join("\n", @lines)
> Working fine, but seems ugly as I later need to access the individual
> lines from @statements:
> foreach $statement (@statements){
> @lines = split("\n", $statement)
> }
>
> Comes Approach 2: Just use arrays: push @statements, [@lines]
> later:
> foreach $statement (@statements){
> # use @{$statement}
> }
>
> This should take care of the unneeded join/splits. Only problem:
> performance is worse by a factor of 5, probably due to pushing an array
> onto an array?
>
> Approach 3: Use references: push @statements, \@lines
> laster:
> foreach $statement (@statements){
> # use @$line;
> }
>
> Only problem here: as I parse the log file into chunks of lines, I need
> to reuse the @lines array during parsing (after the push). Question is-
> how do I clean out (or recreate) @lines without destroying the reference
> stored in the array?
Under approach 3, if you use "my" to declare @lines within the loop, it
will automatically "clear" it out without destroying your saved
(referenced) array.
(If you don't use "my" to declare @lines in the smallest possible scope,
then I don't feel like helping you.)
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 11 Mar 2004 11:47:15 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Causing Perl to issue a single "write" system call?
Message-Id: <c2pjk3$h3s$1@mamenchi.zrz.TU-Berlin.DE>
Doc Miller <mankypro@yahoo.com> wrote in comp.lang.perl.misc:
> On Wed, 10 Mar 2004 20:21:24 +0000 (UTC), Ben Morrow <usenet@morrow.me.uk>
> appears to have written:
>
> [snip snip]
>
> >As a matter of fact, Linux appears to perform both write(2) and
> >writev(2) atomically for 'normal' filesystems, but I could be misreading
> >the code.
> >
> >> Is there any way to cause Perl to issue a single "write" system call?
> >
> >syswrite calls write(2) directly.
> >
> >Ben
>
> That is how I read it too. Since I am only writing a single line I suspect
> write(2) will work but I'm not going to bank on it. I'll run a couple of
> tests and if it doesn't work I'll go back to my flock() routiines.
In a POSIX system all writes are guaranteed to be atomic if the data
size is below a limit (some buffer size, I forget). The limit is, also
by POSIX, guaranteed to be at least 512 bytes. If you don't write
longer lines you should be fine.
Anno
------------------------------
Date: Thu, 11 Mar 2004 17:01:38 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Causing Perl to issue a single "write" system call?
Message-Id: <c2q61i$t2o$1@wisteria.csv.warwick.ac.uk>
Quoth anno4000@lublin.zrz.tu-berlin.de (Anno Siegel):
> In a POSIX system all writes are guaranteed to be atomic if the data
> size is below a limit (some buffer size, I forget). The limit is, also
> by POSIX, guaranteed to be at least 512 bytes. If you don't write
> longer lines you should be fine.
Is that so? The only reference I can find to atomicity in SUSv3 is when
writing to a pipe or fifo, when writes of less than PIPE_BUF are
guaranreed atomic (and PIPE_BUF is guaranteed >= 512). AFAICT, is says
nothing about writes to regular files... but again, I could be wrong.
Hmmm... reading the section on write() again, I suppose
| After a write() to a regular file has successfully returned:
| * Any successful read() from each byte position in the file that was
| modified by that write shall return the data specified by the
| write() for that position until such byte positions are again
| modified.
| * Any subsequent successful write() to the same byte position in the
| file shall overwrite that file data.
should probably be read as 'all writes to regular files are atomic': the
effect of the write call is only defined at the moment write() returns,
and we could take it as given that two write calls cannot return at the
same instant in different processes.... hmmm. Certainly, for O_APPEND to
be mech use you need some guarantee of atomicity of writes.
Ben
--
"The Earth is degenerating these days. Bribery and corruption abound.
Children no longer mind their parents, every man wants to write a book,
and it is evident that the end of the world is fast approaching."
-Assyrian stone tablet, c.2800 BC ben@morrow.me.uk
------------------------------
Date: Thu, 11 Mar 2004 16:35:56 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Deleting a folder recursively
Message-Id: <c2q4hc$rq5$1@wisteria.csv.warwick.ac.uk>
Quoth "Shabam" <info@pcconnect.net>:
> > Why are you ignoring files beginning with a dot? If you have a directory
> > with a dotfile in it, the dotfile won't be deleted and then you won't be
> > able to rmdir the directory.
> >
> > Either use File::Path or File::Find if you need to be more general.
>
> Could you provide sample code that would work better? I'm a newbie and I
> actually got that snippet off someone else's code on the newsgroups.
#!/usr/bin/perl
use strict;
use warnings;
use File::Path qw/rmtree/;
rmtree "/foo/bar";
__END__
Ben
--
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. ben@morrow.me.uk
------------------------------
Date: Thu, 11 Mar 2004 18:50:35 GMT
From: Darin McBride <dmcbride@naboo.to.org.no.spam.for.me>
Subject: Re: Deleting a folder recursively
Message-Id: <%x24c.776761$ts4.642592@pd7tw3no>
Ben Morrow wrote:
>
> Quoth "Shabam" <info@pcconnect.net>:
>> > Why are you ignoring files beginning with a dot? If you have a
>> > directory with a dotfile in it, the dotfile won't be deleted and then
>> > you won't be able to rmdir the directory.
>> >
>> > Either use File::Path or File::Find if you need to be more general.
>>
>> Could you provide sample code that would work better? I'm a newbie and I
>> actually got that snippet off someone else's code on the newsgroups.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use File::Path qw/rmtree/;
>
> rmtree "/foo/bar";
>
> __END__
Portable, but can be very slow. ;-) A bit larger and usually much
faster...
#!/usr/bin/perl -w
use strict;
$|=1;
foreach my $d (@ARGV)
{
remove_dir($d);
rmdir $d;
}
print "\nDone.\n";
sub remove_dir
{
my $d = shift;
if ( -f $d or -l $d )
{
unlink $d;
return;
}
# must be a directory?
my (@sfiles, @sdirs);
local *DIR;
opendir(DIR, $d) || do { print "Can't open $d: $!\n"; return };
foreach (readdir(DIR))
{
next if ($_ eq '.');
next if ($_ eq '..');
my $sd = "$d/$_";
if ( -l $sd ) { push(@sfiles, $sd);}
elsif ( -d $sd ) { push(@sdirs, $sd); }
else { push(@sfiles, $sd); }
}
closedir(DIR);
print ".";
# process subdirectories via fork
my $count;
foreach my $sd (@sdirs)
{
my $pid;
if ($pid = fork())
{
# parent
++$count;
}
elsif (defined $pid)
{
# child
remove_dir($sd);
exit;
}
else
{
# failure - try again in a bit
sleep 5;
redo;
}
while ($count > 2) {
wait();
$count--;
}
}
while (wait() != -1) {}
foreach (@sdirs) {
rmdir $_ || do {
warn "$0: Unable to remove directory $_: $!\n";
};
}
my @cannot = grep {!unlink($_)} @sfiles;
if (@cannot) {
warn "$0: cannot unlink @cannot\n";
}
}
__END__
------------------------------
Date: Thu, 11 Mar 2004 12:09:58 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Extracting directories in a path - TMBABWTDI
Message-Id: <c2phf2$1um07g$1@ID-184292.news.uni-berlin.de>
Henry Law wrote:
> (There must be a better way to do it)
>
> I have a path statement of the form "/dir1/dir2/dir3" and I want to
> separate the directories and subdirectories into an array.
Using the standard module File::Spec is the portable, and therefore
often the best, way.
use File::Spec;
my @nodes = File::Spec->splitdir( $path );
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Thu, 11 Mar 2004 12:52:30 GMT
From: Don Stefani <nospam_007@yahoo.com>
Subject: Re: Extracting directories in a path - TMBABWTDI
Message-Id: <iiZ3c.37004$Vb.33831@newssvr25.news.prodigy.com>
Gunnar Hjalmarsson wrote:
> Henry Law wrote:
>
>> (There must be a better way to do it)
> "Using the standard module..." <snip>
As one who comes here to learn, Annos' comments where very enjoyable.
But I also really appreciate the mention of a related module. I open perldiver, check for
it's presence,.. cool, read the docs. New tool to play with and use.
It's all good.
Thank you.
dstefani
------------------------------
Date: Thu, 11 Mar 2004 14:55:17 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: Extracting directories in a path - TMBABWTDI
Message-Id: <Xns94A964ECF3251dkwwashere@216.168.3.30>
Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
> Henry Law <news@lawshouse.org> wrote in comp.lang.perl.misc:
>> (There must be a better way to do it)
>>
>> I have a path statement of the form "/dir1/dir2/dir3" and I want to
>> separate the directories and subdirectories into an array. "split"
>> works well except that the leading delimiter causes a null value to be
>> entered into the first position of the array. The following code is
>> the best I have been able to do to remove the leading slash but the
>> separate statement strikes me as horribly inelegant; can someone
>> suggest a neater method?
>
> When split() doesn't work quite right, remember Randal's Rule: Use
> split when you know what to throw away, use pattern matching if you
> know what to keep. Saying what to keep:
>
> my @nodes = m!([^/]+)!g;
I like the above or Gunnar's suggestion of File::Spec better than what I'm
about to say, but in the spirit of TMTOWTDI: if you know the path is
*always* going to start at root, you could do this:
my $path = '/a/b/c';
my @nodes;
(undef, @nodes) = split /\//, $path;
--
David Wall
------------------------------
Date: Thu, 11 Mar 2004 07:27:12 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: login to a website problem in Perl
Message-Id: <slrnc50q9g.3fe.tadmc@magna.augustmail.com>
S Domadia <sangita_sjtr@yahoo.co.in> wrote:
> b'coz
The abbreviation is 5 characters long.
The word being abbreviated is 7 characters long when fully spelled-out.
You saved 2 characters while making it hard or impossible for
non-English speakers to understand you.
Please refrain from cutsie spellings when in an international forum.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 11 Mar 2004 08:34:45 -0800
From: slkleine@hotmail.com (Steve The Geek)
Subject: Re: Multiple compares -- TMTOWTDI
Message-Id: <863f122c.0403110834.291f1cb4@posting.google.com>
Tad McClellan <tadmc@augustmail.com> wrote in message news:<slrnc4uc2f.1pd.tadmc@magna.augustmail.com>...
> Steve The Geek <slkleine@hotmail.com> wrote:
> > Jim Gibson <jgibson@mail.arc.nasa.gov> wrote in message news:<090320041313445654%jgibson@mail.arc.nasa.gov>...
> >> In article <863f122c.0403090937.688d1c2d@posting.google.com>, Steve The
> >> Geek <slkleine@hotmail.com> wrote:
>
> >> > The job of this subroutine is to filter out TRAP codes from our daily
> >> > syslog files, specifically Generic TRAP 6 in combination with specific
> >> > trap 1, 2, 418 or 419.[1]
>
> > Since posting it, I've made a few (lot) of changes, but here's the
> > whole thing as it currently stands as working code
>
> > require 5.002;
> > use strict;
>
<<Much good advice taken and memorized >>
Much cleaner code is the result -- and it still works!
<begin paste>
######################################
## EnterasysEventLogParser.pl
## Creation date: 200400308
## Last edit: 200400311
## Author: Steve Kleine
############################################################################
## This script looks through a comma-delimited enterasys log file and
## filers out anything that's not a warning or critical. From there,
it
## excludes RMON, Microsoft, and port up/down events. What remains is
the
## things to look at.
############################################################################
## Log files are kept at \\HINMIS04\C$\Program Files\Enterasys
Networks\
## NetSight Element Manager\Log.
## The filename is formatted YYYYMMDD.CSV. By default, the script
will
## look for the log file produced the previous day.
############################################################################
## Thanks to: Matija, Paul Lalli, Jim Gibson, and Tad McClellan for
their
## help in making it easier to mainatin and work faster!
## Check them out in news://comp.lang.perl.misc
############################################################################
## Declare & initialize modules & variables
############################################################################
require 5.002;
require strict;
require warnings;
use Date::Simple ('.all');
my $Today = Date::Simple->new;
my $PreviousDay = ($Today->prev)->format('%Y%m%d');
my $LinesRead = $LinesWrite = $LineFromLog = 0;
my ($ReturnCode,$LOG,$DataLine);
my $FilePath = '\\\\10.146.0.4/C$/Program Files/Enterasys
Networks/NetSight Element Manager/Log/';
# Path has its own variable for modification easier when we switch to
Atlas.
my $FileName = "$FilePath$PreviousDay.CSV";
############################################################################
## MAIN ROUTINES
############################################################################
## OPEN FILES FOR READ & WRITE
############################################################################
open LOG , $FileName or die "I can\'t find yesterday's file: $!.";
open WriteLog , '>>c:\Filtered_log.csv' or die "Can't create write
file: $!";
print WriteLog 'Date,Type,IP address,Source,What happened,What it
means,Generic TRAP,specific TRAP';
############################################################################
## Parse logfile
############################################################################
while (defined($LineFromLog = <LOG>))
{
$LinesRead ++;
@DataLine = split /,/, $LineFromLog;
if ($DataLine[2] eq 'Warning') #Only deal with warnings
{
if ($DataLine[7] ne "Microsoft")
{
&RemoveIrrelevantCodes($DataLine);
if ($ReturnCode)
{
print WriteLog "\n$ReturnCode";
$LinesWrite ++;
}
}
}
}
############################################################################
## ENDING ROUTINES
############################################################################
print "\n$LinesRead lines were parsed from the logfile.\n$LinesWrite
were written to filtered file.\n";
print WriteLog "\n$LinesRead lines were parsed from the
logfile.\n$LinesWrite were written to filtered file.\n\n";
close WriteLog;
close LOG;
############################################################################
## SUBROUTINES
############################################################################
## Removes items from line that are RMON thresholds and port up/down
## messages. These are worthless in the current environment
## The code specified for these traps is "6" in field 14 AND 1, 2,
418, or
## 419 in field 15. Only if both of these are true will that line be
ignored.
############################################################################
sub RemoveIrrelevantCodes
{$ReturnCode = ''; #Initialize $ReturnCode
if ($DataLine[7] eq "RMON") #RMON exceeded
{return}
if (($DataLine[7] eq "Cabletron")) #General class of TRAP
{
if ($DataLine[15] eq "418") #Port up
{return}
if ($DataLine[15] eq "419") #Port down
{return}
}
$ReturnCode = join (",", @DataLine[1,2,5,7..9,14,15]);
}
<begin paste>
Again, many thanks!
Steve the (wannabe Perl coder) Geek
------------------------------
Date: Thu, 11 Mar 2004 07:22:15 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Ordering large files (in perl?)
Message-Id: <slrnc50q07.3fe.tadmc@magna.augustmail.com>
Oeln <ohmy9od@yahoo.com> wrote:
> Jim Gibson <jgibson@mail.arc.nasa.gov> wrote in message news:<090320041322458099%jgibson@mail.arc.nasa.gov>...
>> In article <ffde43bc.0403081722.6fdc2988@posting.google.com>, Oeln
>> <ohmy9od@yahoo.com> wrote:
>> > One idea I came upon implied that exec(sort) would be a good option.
>> Try 'system("sort infile -o outfile"). The exec command won't return
>> execution to your program, probably not what you want.
>
> I guess it isn't only that exec() only
^^^
> fails if the command itself is non-existent.
^^^
The double negative has left me not sure that I understand
what you mean there...
> I guess it won't wait for
^^
> the exec() to complete, either?
I guess the "it" refers to your Perl program?
"it" won't wait because "it" *no longer exists* after a successful exec().
After exec(), your Perl program has been replaced by the sort program.
> I'll continue looking for info on it
It isn't really a Perl issue, it is an issue with the Unix process model.
You may have better luck looking for more info in a Unix-place
rather than a Perl-place.
> (if nothing else I'm gonna look
> for an example of why one would ever go with exec instead)
When used in conjunction with fork().
(I don't see any way that exec() can help solve your sorting problem.)
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 11 Mar 2004 06:56:31 -0800
From: dcutter@gmx.de (Daniel Cutter)
Subject: Re: Ordering large files (in perl?)
Message-Id: <d536227a.0403110656.1c905160@posting.google.com>
ohmy9od@yahoo.com (Oeln) wrote in message news:<ffde43bc.0403102200.15ece154@posting.google.com>...
> Jim Gibson <jgibson@mail.arc.nasa.gov> wrote in message news:<090320041322458099%jgibson@mail.arc.nasa.gov>...
> > In article <ffde43bc.0403081722.6fdc2988@posting.google.com>, Oeln
> > <ohmy9od@yahoo.com> wrote:
> >
> > > I've got a large file in which there is one word on each line. I've
> > > got to output each word it includes to a new file, in order. I've
> > > currently got the following:
> > >
> > > @order = <INFILE>;
> > > @ordered = sort(@order);
> > >
> > > foreach $o (@ordered) {
> > > print OUTFILE "$o";
> > > }
> > >
> > > In general this is working fine; but if I've got to open a larger file
> > > (over 1gb, for example, or even 10 gb..whatever) it fails.
> > >
> > > I've found this issue being gone over to an extent; but the
> > > information is not clear to me. I've got a few ideas on how to get
> > > this to work (for instance, if I could get one word out of the infile
> > > at a time and insert it to the outfile, in order in the first place;
> > > but I'd imagine this would be incredibly inefficient).
> >
> > Yes. Do not reinvent the wheel. If you are on linux, use the sort
> > command. 'man sort'.
> >
> > >
> > > One idea I came upon implied that exec(sort) would be a good option.
> > > I'd imagine like exec(sort INFILE -o OUTFILE); but I'm not certain, or
> > > clear on the other options it offers. Is that good enough by itself
> > > for large files? I get the impression there is a way to output to a
> > > group of temp files, and then 'integrate' them into one file, etc.
> > > etc.
> >
> > Try 'system("sort infile -o outfile"). The exec command won't return
> > execution to your program, probably not what you want.
>
> On looking further into this, I guess it isn't only that exec() only
> fails if the command itself is non-existent. I guess it won't wait for
> the exec() to complete, either? I'm not overly clear on this, only
> cause with exec() I've not gotten the impression this is the case.
>
> I'll continue looking for info on it (if nothing else I'm gonna look
> for an example of why one would ever go with exec instead) - for now
> I've got:
>
> @order = ("sort", "-u", "$infile", "-o", "$oufile"); system(@order);
>
> > If you have one word per line, that should be all you need. Try it from
> > the command line and see if it does what you want, then run with it. I
> > am sure you have better things to worry about than implementing a merge
> > sort method. You might want '-b' option to sort to ignore leading
> > whitespace, -i to ignore non-ascii characters, and -f to use
> > case-insensitive sorting.
> >
>
> Thank you for the info.
>
> - Oeln
Far more appropriate is
use File::Sort;
It also swaps to temp if needed be. Just read the documentation on CPAN.
Daniel
------------------------------
Date: Thu, 11 Mar 2004 16:50:24 GMT
From: "John W. Kennedy" <jwkenne@attglobal.net>
Subject: Re: Ordering large files (in perl?)
Message-Id: <kN04c.5974$CW1.1661736@news4.srv.hcvlny.cv.net>
Jürgen Exner wrote:
> And if you are not on Linux then there is still no need to reinvent the
> wheel.
Even Windows XP's sort command is capable of going external at need.
--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
------------------------------
Date: 11 Mar 2004 05:33:42 -0800
From: ante.cepic@siemens.com (Toni)
Subject: Parsing from file
Message-Id: <8b22fe57.0403110533.6458140c@posting.google.com>
I have an input file with the folloving structure:
Option1 Par1 Val1 Par2 Val2 ...
Option2 Par3 Val3 Par4 Val4 Par5 Val5 ...
How to get the parameters and values in a hash?
I want to have
%hash1 = (
Par1 Val1
Par2 Val2
...
)
%hash2 = (
Par3 Val4
Par5 Val5
...
)
Thanks!
------------------------------
Date: Thu, 11 Mar 2004 14:56:23 +0100
From: =?ISO-8859-1?Q?Josef_M=F6llers?= <josef.moellers@fujitsu-siemens.com>
Subject: Re: Parsing from file
Message-Id: <c2pr26$2oc$1@nntp.fujitsu-siemens.com>
Toni wrote:
> I have an input file with the folloving structure:
> Option1 Par1 Val1 Par2 Val2 ...
> Option2 Par3 Val3 Par4 Val4 Par5 Val5 ...
>=20
> How to get the parameters and values in a hash?
> I want to have
>=20
> %hash1 =3D (
> Par1 Val1
> Par2 Val2
> ...
> )
>=20
> %hash2 =3D (
> Par3 Val4
> Par5 Val5
> ...
> )
>=20
> Thanks!
What have you tried so far, where does your code fail?
Look at split, shift and friends. Try to use them, show us your code,=20
then we can talk.
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
------------------------------
Date: 11 Mar 2004 13:54:13 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Parsing from file
Message-Id: <c2pr25$k1n$1@mamenchi.zrz.TU-Berlin.DE>
Toni <ante.cepic@siemens.com> wrote in comp.lang.perl.misc:
> I have an input file with the folloving structure:
> Option1 Par1 Val1 Par2 Val2 ...
> Option2 Par3 Val3 Par4 Val4 Par5 Val5 ...
>
> How to get the parameters and values in a hash?
> I want to have
>
> %hash1 = (
> Par1 Val1
> Par2 Val2
> ...
> )
>
> %hash2 = (
> Par3 Val4
> Par5 Val5
> ...
> )
Is that supposed to be Perl code? It isn't, so it isn't really clear
what it means. Use Perl, or use English to explain your problem. This
is neither here nor there.
So, I guess, you expect to draw one hash per line. You don't say
what you are going to do with "Option1" etc. that heads each line.
See how poorly specified your problem is?
You could use them as primary keys for a HoH. The following code
(untested) should do that:
my %opts;
while ( <DATA> ) {
my ( $option, %settings) = split;
$opts{ $option} = \ %settings;
}
Anno
------------------------------
Date: Thu, 11 Mar 2004 15:35:24 +0100
From: Tore Aursand <tore@aursand.no>
Subject: Re: Parsing from file
Message-Id: <pan.2004.03.11.14.29.30.964126@aursand.no>
On Thu, 11 Mar 2004 05:33:42 -0800, Toni wrote:
> I have an input file with the folloving structure: Option1 Par1 Val1
> Par2 Val2 ...
> Option2 Par3 Val3 Par4 Val4 Par5 Val5 ...
>
> How to get the parameters and values in a hash? I want to have
>
> %hash1 = (
> Par1 Val1
> Par2 Val2
> ...
> )
>
> %hash2 = (
> Par3 Val4
> Par5 Val5
> ...
> )
Why do you want to have something that Perl won't understand? Your "code"
above will generate syntax errors in Perl, but I guess you knew that.
Anyway: Your specification isn't clear. What do you want to do with the
leading 'Option' on each line?
And what code do you have already? What doesn't work?
--
Tore Aursand <tore@aursand.no>
"Fighting terrorism is like being a goalkeeper. You can make a hundred
brilliant saves but the only shot that people remember is the one that
gets past you." -- Paul Wilkinson
------------------------------
Date: Thu, 11 Mar 2004 15:43:06 +0000 (UTC)
From: kj <nospam@nospam.com>
Subject: Perl DB: how2 silence "redefined" warnings?
Message-Id: <c2q1ea$411$1@reader2.panix.com>
I'm doing some work from within the Perl debugger that requires me
to invoke "do 'some_perl_file.pl'" repeatedly, without invoking R
in between (there's a reason for this, too lengthy to go into here).
This results in a very long list of warnings of the form
Subroutine foo redefined at some_perl_file.pl line 101.
These redefinitions are indeed what I'm after whenever I re-invoke
"do ...", and I don't want to see all these warnings.
I've tried to silence them with statements such as
;{ local $SIG{__WARN__}; do 'some_perl_file.pl' }
but I still get the long list of warnings. Is there any way to
shut them up?
Thanks!
kj
--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.
------------------------------
Date: Thu, 11 Mar 2004 17:04:38 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Perl DB: how2 silence "redefined" warnings?
Message-Id: <c2q676$t2o$2@wisteria.csv.warwick.ac.uk>
Quoth kj <nospam@nospam.com>:
> This results in a very long list of warnings of the form
>
> Subroutine foo redefined at some_perl_file.pl line 101.
>
> These redefinitions are indeed what I'm after whenever I re-invoke
> "do ...", and I don't want to see all these warnings.
>
> I've tried to silence them with statements such as
>
> ;{ local $SIG{__WARN__}; do 'some_perl_file.pl' }
>
> but I still get the long list of warnings. Is there any way to
> shut them up?
Two ways:
{ local $SIG{__WARN__} = sub{}; do 'some.pl' }
{ no warnings 'redefine'; do 'some.pl' }
Ben
--
The cosmos, at best, is like a rubbish heap scattered at random.
- Heraclitus
ben@morrow.me.uk
------------------------------
Date: Thu, 11 Mar 2004 17:14:58 +0100
From: "chatiman" <chatiman@free.fr>
Subject: Re: perl on windows
Message-Id: <40509084$0$292$636a15ce@news.free.fr>
Thanks to all for your answers
------------------------------
Date: 11 Mar 2004 07:40:13 -0800
From: lpjacob@yahoo.com (Len)
Subject: Re: Simple Reference Question
Message-Id: <d7c81eae.0403110740.732cf692@posting.google.com>
Thank you very much for the supplied instruction and solution, I
really appreciate your help. That makes a lot of sense.
Thanks.
Len.
Ben Morrow <usenet@morrow.me.uk> wrote in message
> This uses symrefs: you are using a string holding a variable's name to
> get at the variable. This can lead to very hard-to-find bugs, which is
> why use strict disallows it (and why we insist you use strict :).
>
> You want to either use real references (see perldoc perlreftut, for
> starters):
>
> my @list2 = ("list2item1", "list2item2");
> my @list3 = ("list3item1", "list3item2");
> my @list1 = (\@list2, \@list3);
>
> # code as before
>
> or a hash-of-lists:
>
> my %lists = (
> list2 => [ qw(list2item1 list2item2) ],
> list3 => [ qw(list3item1 list3item1) ],
> );
> my @list1 = qw(list2 list3);
>
> $\ = "\n";
> for my $tmp (@list1) {
> print $tmpitem;
> next unless exists $lists{$tmp};
> for ( @{ $lists{$tmp} } ) {
> print "\t$_";
> }
> }
>
> See perlreftut again for both the [...] and the @{...} syntax.
> As side issues, note how my use of qw// and $\ makes the code clearer.
>
> Ben
------------------------------
Date: Thu, 11 Mar 2004 18:00:49 +0000
From: James Taylor <spam-block-@-SEE-MY-SIG.com>
Subject: Text editor implemented in Perl
Message-Id: <ant111849868fNdQ@nospam.demon.co.uk>
I just wondered whether anyone has thought to write a programmers'
text/data editor in Perl and allow the full power of Perl's s///eg
operator for manipulating the file being edited. The ability
to perform this kind of day to day manipulation visually using
my favourite language would be quite sincerely and deeply cool.
Ideally, I'd like the editor itself to be hackable in Perl too.
Is there such a thing? If not, what's the closest alternative?
--
James Taylor, Cheltenham, Gloucestershire, UK. PGP key: 3FBE1BF9
To protect against spam, the address in the "From:" header is not valid.
In any case, you should reply to the group so that everyone can benefit.
If you must send me a private email, use james at oakseed demon co uk.
------------------------------
Date: 11 Mar 2004 18:47:03 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Text editor implemented in Perl
Message-Id: <c2qc77$n69$1@nets3.rz.RWTH-Aachen.DE>
Also sprach James Taylor:
> I just wondered whether anyone has thought to write a programmers'
> text/data editor in Perl and allow the full power of Perl's s///eg
> operator for manipulating the file being edited. The ability
> to perform this kind of day to day manipulation visually using
> my favourite language would be quite sincerely and deeply cool.
>
> Ideally, I'd like the editor itself to be hackable in Perl too.
>
> Is there such a thing? If not, what's the closest alternative?
You can use vim with a Perl interpreter compiled in. This allows both
writing vim scripts in Perl as well as using Perl commands to edit the
buffer.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Thu, 11 Mar 2004 17:00:26 +0000 (UTC)
From: Vincent Lefevre <vincent+news@vinc17.org>
Subject: Using Tie::Handle
Message-Id: <20040311165309$70fb@vinc17.org>
To trap STDERR output, I've been told to use Tie::Handle. As I've
never used things like "tie", not even "bless", I need some help.
I've started to write the following test script:
------------------------------------------------------------
#!/usr/bin/env perl
use strict;
package NewHandle;
require Tie::Handle;
use FileHandle;
our @ISA = qw(Tie::Handle);
sub TIEHANDLE
{
my $fh;
bless \$fh, $_[0];
}
sub OPEN
{
print "OPEN FH: $_[0]\n";
}
sub PRINT
{
print "PRINT FH: $_[0]\n";
unless (defined $_[0])
{
print "PRINT: new FH...\n";
$_[0] = new FileHandle ">stderr.$$" or die;
}
print "PRINT: print ...\n";
print { $_[0] } @_[1,-1];
print "PRINT: end\n";
}
package main;
print "tie...\n";
tie *STDERR, 'NewHandle';
print "print STDERR ...\n";
print STDERR "Test print\n";
print "OK\n";
------------------------------------------------------------
I don't have many examples (mainly /usr/share/perl/5.8.3/Tie/Handle.pm)
and I don't understand everything. When I execute the script, I get:
greux:~> ./tietest
tie...
print STDERR ...
PRINT FH: NewHandle=SCALAR(0x81841b0)
PRINT: print ...
PRINT FH: NewHandle=SCALAR(0x81841b0)
PRINT: print ...
Not a GLOB reference at ./tietest line 31.
zsh: exit 255 ./tietest
Why isn't the filehandle initially undef?
Why does the "print" inside PRINT call PRINT recursively?
FYI, the final goal is to redirect stderr to a file, but create the
file only before the first output to stderr (so that if there is no
output to stderr, the file will never be created).
--
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/> - 100%
validated (X)HTML - Acorn Risc PC, Yellow Pig 17, Championnat International
des Jeux Mathématiques et Logiques, TETRHEX, etc.
Work: CR INRIA - computer arithmetic / SPACES project at LORIA
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 6246
***************************************