[31694] in Perl-Users-Digest
Perl-Users Digest, Issue: 2957 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu May 20 21:09:28 2010
Date: Thu, 20 May 2010 18:09:13 -0700 (PDT)
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, 20 May 2010 Volume: 11 Number: 2957
Today's topics:
Re: bit-twiddling on 32-bit machines (Seymour J.)
Re: Efficiently searching multiple files <awkster@yahoo.com>
Re: Efficiently searching multiple files <jimsgibson@gmail.com>
Re: Efficiently searching multiple files <awkster@yahoo.com>
Re: getting results of multiple simultaneous system com <derykus@gmail.com>
Re: getting results of multiple simultaneous system com <derykus@gmail.com>
How can I do something based on the first word of a lin <steve@staticg.com>
Re: How can I do something based on the first word of a sln@netherlands.com
Re: How can I do something based on the first word of a <RedGrittyBrick@SpamWeary.invalid>
Re: How can I do something based on the first word of a <jurgenex@hotmail.com>
Re: How can I do something based on the first word of a <tadmc@seesig.invalid>
Re: How can I do something based on the first word of a <steve@staticg.com>
Re: How do I check if www site has Perl compatible? (Seymour J.)
Re: Validate $q->param() instead of copying to hash fir <tadmc@seesig.invalid>
Where to get the document for a library function in com <pengyu.ut@gmail.com>
Re: Where to get the document for a library function in <rvtol+usenet@xs4all.nl>
Why I can't use if(defined $var)? <pengyu.ut@gmail.com>
Re: Why I can't use if(defined $var)? <sisyphus359@gmail.com>
Re: Why I can't use if(defined $var)? <rvtol+usenet@xs4all.nl>
Re: Why I can't use if(defined $var)? <tadmc@seesig.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 20 May 2010 20:56:51 -0400
From: Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>
Subject: Re: bit-twiddling on 32-bit machines
Message-Id: <4bf5da53$5$fuzhry+tra$mr2ice@news.patriot.net>
In <286ec7-6f8.ln1@osiris.mauzo.dyndns.org>, on 05/19/2010
at 11:44 PM, Ben Morrow <ben@morrow.me.uk> said:
>I presume it's the final '0' you consider a bug, not the negative
>value? I'm not sure I agree that's a bug, since perldoc integer says
>| Finally, "use integer;" also has an additional affect on the bitwise
>| operators. Normally, the operands and results are treated as
>| unsigned integers, but with "use integer;" the operands and results
>| are signed. This means, among other things, that ~0 is -1, and -2 &
>| -5 is 6.
I take it that there is no Perl on 1s complement machines?
--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>
Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to spamtrap@library.lspace.org
------------------------------
Date: Thu, 20 May 2010 13:35:27 -0700 (PDT)
From: Jorge <awkster@yahoo.com>
Subject: Re: Efficiently searching multiple files
Message-Id: <50bac7f4-e723-4968-a574-3598d9e2ecad@l6g2000vbo.googlegroups.com>
I wasn't expecting a full critique but I'll take it -- learning is
good so thanks for your effort and time.
Most of your points are obvious (once read) and some I will have to
digest.
Thanks again
On May 20, 12:47=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> >>>>> "J" =3D=3D Jorge =A0<awks...@yahoo.com> writes:
>
> =A0 J> #!/usr/bin/perl -l
>
> =A0 J> use strict;
> =A0 J> use warnings;
>
> good
>
> =A0 J> &scripts_dir('/some/dir/that/holds/scripts');
>
> don't call subs with & as it isn't needed with () and it is perl4
> style. there are other subtle issues that can bite you.
>
> =A0 J> sub scripts_dir {
>
> =A0 J> =A0 =A0 # set some vars
> =A0 J> =A0 =A0 my $dir =3D shift;
> =A0 J> =A0 =A0 my(@paths, $paths, @scripts, $scripts, $string);
> =A0 J> =A0 =A0 my($lines, $leaf, $header, $header2);
>
> don't declare vars before you need them. in many cases you can declare
> then when first used.
>
> =A0 J> =A0 =A0 local($_);
>
> why? actually i recommend against using $_ as much as possible (it does
> have its places) so you can used named vars which are easier to read and
> make the code better
>
> =A0 J> =A0 =A0 # check dir can be opened for read
> =A0 J> =A0 =A0 unless (opendir(DIR, $dir)) {
>
> use a lexical handle
>
> unless (opendir(my $dirh, $dir)) {
>
> =A0 J> =A0 =A0 =A0 =A0 die "can't open $dir $!\n";
> =A0 J> =A0 =A0 =A0 =A0 closedir(DIR);
>
> why close the handle if it never opened?
>
> =A0 J> =A0 =A0 =A0 =A0 return;
>
> you can just exit here instead.
>
> =A0 J> =A0 =A0 }
>
> better yet, use File::Slurp's read_dir.
>
> =A0 J> =A0 =A0 #
> =A0 J> =A0 =A0 # read dir, skip over system files and bak files
> =A0 J> =A0 =A0 # build array of script names
> =A0 J> =A0 =A0 # build array of full-path script names
> =A0 J> =A0 =A0 #
> =A0 J> =A0 =A0 foreach (readdir(DIR)) {
> =A0 J> =A0 =A0 =A0 =A0 next if $_ eq '.' || $_ eq '..' || $_ =3D~ /\.bak$=
/;
>
> read_dir skips . and .. for you.
>
> =A0 J> =A0 =A0 =A0 =A0 push(@scripts, $_);
> =A0 J> =A0 =A0 =A0 =A0 $paths =3D $dir."/".$_;
> =A0 J> =A0 =A0 =A0 =A0 push(@paths, $paths);
> =A0 J> =A0 =A0 }
> =A0 J> =A0 =A0 closedir(DIR);
>
> that can all be done so much simpler like this (untested):
>
> my @paths =3D map "$dir/$_", grep !/\.bak$/, read_dir $dir ;
>
> =A0 J> =A0 =A0 # open ouput file, format and print headers
> =A0 J> =A0 =A0 open OUT, ">output" or die "cannot open output for writing=
$!
> =A0 J> =A0 =A0 ...
>
> ditto for lexical handles
>
> =A0 J> \n";
> =A0 J> =A0 =A0 $header =3D sprintf("%-25s%-25s%s", "SCRIPT NAME", "FOUND =
IN",
> =A0 J> "USAGE");
> =A0 J> =A0 =A0 $header2 =3D sprintf("%-25s%-25s%s", "=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D", "=3D=3D=3D=3D=3D=3D=3D=3D",
> =A0 J> "=3D=3D=3D=3D=3D");
>
> you use the same sprintf format three times. put that into a variable
> above so you can change it in one place.
>
> =A0 J> =A0 =A0 print OUT $header;
> =A0 J> =A0 =A0 print OUT $header2;
>
> no need for two print calls.
>
> print OUT $header, $header2 ;
>
> =A0 J> =A0 =A0 # loop through each script name
> =A0 J> =A0 =A0 foreach $scripts(@scripts){
>
> =A0 =A0 =A0 =A0 foreach my $scripts (@scripts) {
>
> that is how you can declare vars locally. and use more horizontal
> whitespace. others need to read your code so think about them when you
> write it. and YOU are an other too! :)
>
> i noticed this below but the point is true here. don't use plural names
> for singular things. $scripts has a single script name
>
> =A0 J> =A0 =A0 =A0 =A0 # loop through each script in directory
> =A0 J> =A0 =A0 =A0 =A0 foreach my $paths(@paths){
>
> you used my there. why not in the previous loop?
>
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 # get last leaf of script being searched -=
-
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 # if it matches itself; skip
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 $leaf =3D get_leaf($paths);
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 if($scripts eq $leaf) { next;}
>
> slightly faster as you don't need a block entry on next. also a better
> style for simple flow control like this.
>
> =A0 =A0 =A0 =A0 next if $scripts eq $leaf ;
>
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 # open each script for searching
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 open F, "$paths" or die "cannot open $path=
s for reading
>
> don't quote scalar vars as it can lead to subtle bugs. it is not needed h=
ere.
>
> =A0 J> $! ...\n";
>
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 while(my $lines =3D <F>) {
>
> $lines is a single line so make that singular. using a plural name
> implies an array or array ref
>
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # -l switch in place
>
> that only affects one liners using -p or -n. no effect on regular code
>
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 chomp($lines);
>
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # search for matches to the common=
ly-used command
> =A0 J> syntax
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if($lines =3D~ /\$\($scripts / || =
$lines =3D~ /\`
> =A0 J> $scripts /){
>
> this doesn't make sense. are you checking if the current script refers
> to itself??
>
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # format to line up with h=
eaders
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $string =3D sprintf("%-25s=
%-25s%s", $scripts, $leaf,
> =A0 J> $lines);
>
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # print to file
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 print OUT $string;
>
> since you just print the string, you can use printf directly.
>
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 J> =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 J> =A0 =A0 =A0 =A0 }
> =A0 J> =A0 =A0 }
> =A0 J> =A0 =A0 # close I/O streams
> =A0 J> =A0 =A0 close(F);
> =A0 J> =A0 =A0 close(OUT);
> =A0 J> }
>
> =A0 J> #=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D
> =A0 J> # subroutine get_leaf
> =A0 J> #=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D
> =A0 J> sub get_leaf
> =A0 J> {
> =A0 J> =A0 =A0 =A0 =A0 # get arg(s)
> =A0 J> =A0 =A0 =A0 =A0 my($pathname) =3D @_;
>
> =A0 J> =A0 =A0 =A0 =A0 # split on leafs
> =A0 J> =A0 =A0 =A0 =A0 my @split_pathname =3D split( /[\\\/]/, $pathname)=
;
>
> gack!!
>
> File::Basename does this for you and simpler.
>
> =A0 J> =A0 =A0 =A0 =A0 # grab last leaf
> =A0 J> =A0 =A0 =A0 =A0 my $leaf =3D pop( @split_pathname );
>
> you could just return the popped value directly. hell, you can do that
> in the split line too:
>
> =A0 =A0 =A0 =A0 return( (split( /[\\\/]/, $pathname)[-1] );
>
> as for speedups, i can't help much since i don't have your input
> data. nothing seems oddly slow looking but your core loops are deep and
> can be done better. slurping in the entire file (File::Slurp) and
> scanning for those lines in one regex would be noticeably faster. and
> your core logic is suspect as it doesn't seem to check for calling other
> scripts from a given one.
>
> uri
>
> --
> Uri Guttman =A0------ =A0u...@stemsystems.com =A0-------- =A0http://www.s=
ysarch.com--
> ----- =A0Perl Code Review , Architecture, Development, Training, Support =
------
> --------- =A0Gourmet Hot Cocoa Mix =A0---- =A0http://bestfriendscocoa.com=
---------
------------------------------
Date: Thu, 20 May 2010 14:30:55 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Efficiently searching multiple files
Message-Id: <200520101430559992%jimsgibson@gmail.com>
In article
<6bf31a76-1cd6-48be-8bea-a91b1c6b83b6@y21g2000vba.googlegroups.com>,
Jorge <awkster@yahoo.com> wrote:
> I have a Linux directory full of shell scripts (~100 scripts with
> average
> size of ~60 lines) with many of them calling other scripts as
> depencencies.
>
> Not being the author of the scripts and with the immediate need to
> become familiar with
> the overall scope of this script directory, I needed to create a cross-
> reference chart
> that shows what script depends on what other script.
>
> My Perl script (below) works in terms of doing what I need, however,
> IMO, it appears to run a bit slow. I timed it at ~3.5 sec. Possibly
> that is it's max performance but something tells me (from other Perl
> script experiences) this one just isn't up to speed.
>
> I would appreciate any ideas or comments.
>
> Thanks, Jorge
>
Uri and J. have given you thorough critiques of your posted program. I
can only add a few additional suggestions.
Concerning speedup, you have implemented an O(N**2) algorithm. You are
reading each of N files N-1 times, looking for a different script name
each time you read each file. You should read each file only once, and
look for all of your script names in each line. You shouldn't exclude
the script your are reading from this search, since scripts can call
themselves. I would also not look in comments, since it is very common
to put script names in comments, and you don't want those.
For how to search for many things at once, see the advice in 'perldoc
-q many' "How do I efficiently match many regular expressions at once?"
You are concatenating the directory name and file name into a path,
then taking many steps to strip the directory name from the path to
retrieve the file name. Don't bother, since you are only working in one
directory and the $dir variable only contains one value. Add $dir to
the open statement or a temp variable:
my $filename = "$dir/$scripts";
open( my $f, '<', $filename ) ...
Then you don't need the @paths array at all.
[program snipped]
--
Jim Gibson
------------------------------
Date: Thu, 20 May 2010 14:44:42 -0700 (PDT)
From: Jorge <awkster@yahoo.com>
Subject: Re: Efficiently searching multiple files
Message-Id: <12e58583-6e8c-4017-b8ef-124e3f10e77e@i31g2000vbt.googlegroups.com>
Yeah Uri and J gave the program a good going-over so I have plenty to
look at there and now I can add your pointers to the list.
You mention one very good point that should have been obvious yet it
completely got past me ... that beinging I am searching every file for
each single pattern where I should be looking at each file only once
for all patterns.
Ihanks again, Jorge
On May 20, 2:30=A0pm, Jim Gibson <jimsgib...@gmail.com> wrote:
> In article
> <6bf31a76-1cd6-48be-8bea-a91b1c6b8...@y21g2000vba.googlegroups.com>,
>
>
>
>
>
> Jorge <awks...@yahoo.com> wrote:
> > I have a Linux directory full of shell scripts (~100 scripts with
> > average
> > size of ~60 lines) with many of them calling other scripts as
> > depencencies.
>
> > Not being the author of the scripts and with the immediate need to
> > become familiar with
> > the overall scope of this script directory, I needed to create a cross-
> > reference chart
> > that shows what script depends on what other script.
>
> > My Perl script (below) works in terms of doing what I need, however,
> > IMO, it appears to run a bit slow. I timed it at ~3.5 sec. Possibly
> > that is it's max performance but something tells me (from other Perl
> > script experiences) this one just isn't up to speed.
>
> > I would appreciate any ideas or comments.
>
> > Thanks, Jorge
>
> Uri and J. have given you thorough critiques of your posted program. I
> can only add a few additional suggestions.
>
> Concerning speedup, you have implemented an O(N**2) algorithm. You are
> reading each of N files N-1 times, looking for a different script name
> each time you read each file. You should read each file only once, and
> look for all of your script names in each line. You shouldn't exclude
> the script your are reading from this search, since scripts can call
> themselves. I would also not look in comments, since it is very common
> to put script names in comments, and you don't want those.
>
> For how to search for many things at once, see the advice in 'perldoc
> -q many' "How do I efficiently match many regular expressions at once?"
>
> You are concatenating the directory name and file name into a path,
> then taking many steps to strip the directory name from the path to
> retrieve the file name. Don't bother, since you are only working in one
> directory and the $dir variable only contains one value. Add $dir to
> the open statement or a temp variable:
>
> =A0 my $filename =3D "$dir/$scripts";
> =A0 open( my $f, '<', $filename ) ...
>
> Then you don't need the @paths array at all.
>
> [program snipped]
>
> --
> Jim Gibson- Hide quoted text -
>
> - Show quoted text -
------------------------------
Date: Thu, 20 May 2010 14:34:28 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: getting results of multiple simultaneous system commands
Message-Id: <b90f691d-7e59-4cf6-8117-63e62b543d0c@t26g2000prt.googlegroups.com>
On May 20, 11:14=A0am, David Resnick <lndresn...@gmail.com> wrote:
> On May 20, 10:00=A0am, "Dr.Ruud" <rvtol+use...@xs4all.nl> wrote:> David R=
esnick wrote:
> > > I need to execute multiple simultaneous system() statements.
>
> > push @exit, system("$_ &") for @command;
>
> > > [...] =A0Unfortunately, I
> > > discovered that the perl I need to use on some of our systems was
> > > built without threads and replacing it isn't an option.
>
> > Consider it a blessing. Just fork and wait.
>
> Forking twice and waiting for both to exit (and checking $?, which was
> the thing I forgot about) seems to work nicely. =A0Thanks very much for
> your help.
IPC::Run can process in the background, eg,
(Also options for IO, timeout, etc)
use IPC::Run qw/start/;
$ref1 =3D sub{ system "/path/to/foo1";
print "foo1: $?"; };
$ref2 =3D sub { system "/path/to/foo2";
print "foo2: $?"; };
my $h =3D start $ref1, '&', $ref2
or die "finish: $?";
--
Charles DeRykus
------------------------------
Date: Thu, 20 May 2010 14:38:28 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: getting results of multiple simultaneous system commands
Message-Id: <9c51fd78-2d5d-472b-b160-5d88e361bad7@u3g2000prl.googlegroups.com>
On May 20, 2:34=A0pm, "C.DeRykus" <dery...@gmail.com> wrote:
> On May 20, 11:14=A0am, David Resnick <lndresn...@gmail.com> wrote:
>
> ...
> IPC::Run can process in the background, eg,
> (Also options for =A0IO, timeout, etc)
>
> use IPC::Run qw/start/;
>
> $ref1 =3D sub{ system "/path/to/foo1";
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 print "foo1: $?"; };
> $ref2 =3D sub { system "/path/to/foo2";
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 print "foo2: $?"; };
>
> my $h =3D start $ref1, '&', $ref2
> =A0 =A0 =A0or die "finish: $?";
>
last line should be replaced with
these below:
my $h =3D start $ref1, '&', $ref2;
finish $h or die "finish: $?";
--
Charles DeRykus
------------------------------
Date: Thu, 20 May 2010 15:32:55 -0700 (PDT)
From: Steve <steve@staticg.com>
Subject: How can I do something based on the first word of a line?
Message-Id: <48b451a2-de1f-4a65-8f28-dd8662a81949@q13g2000vbm.googlegroups.com>
I have this chunk of code which is broken because I can't figure this
out. Basically I have a file handle, and the file it reads in always
starts wit ha number... 1-99. I want to ONLY get that number, and not
the rest of the line because I want to be able to copy that line that
amount of times.
while(<FH3>)
{
my $x = 0;
#Make enough records based on quantity.. broken par
if(/^[0-9]|^[0-9][0-9]/)
{
print m/^[0-9]|^[0-9][0-9]/;
}
#Check each line for a type
if(/SWITCH|MONITOR|ROUTER|KVM/)
{
print HARDWARE $_;
$x=1;
}
if(/LICENCE|LICENCES/)
{
print LICENSES $_;
$x=1;
}
if(/COMPUTER|LAPTOP|THIN/)
{
print COMPUTER $_;
$x=1;
}
if(/SOFTWARE/)
{
print SOFTWARE $_;
$x=1;
}
if(/OS|'OPERATING SYSTEM'/)
{
print OS $_;
$x=1;
}
if ($x == '0')
{
print "Warning, following line didn't match anything and was
appended to the 'other' file!\n $_\n";
print OTHER $_;
}
}
------------------------------
Date: Thu, 20 May 2010 16:27:45 -0700
From: sln@netherlands.com
Subject: Re: How can I do something based on the first word of a line?
Message-Id: <dofbv5h5gnhr745m2mevesdjc0loqoc4bl@4ax.com>
On Thu, 20 May 2010 15:32:55 -0700 (PDT), Steve <steve@staticg.com> wrote:
>I have this chunk of code which is broken because I can't figure this
>out. Basically I have a file handle, and the file it reads in always
>starts wit ha number... 1-99. I want to ONLY get that number, and not
>the rest of the line because I want to be able to copy that line that
>amount of times.
Where is the code that "copy that line that amount of times"?
>
>while(<FH3>)
>{
> my $x = 0;
>#Make enough records based on quantity.. broken par
What does this mean?
> if(/^[0-9]|^[0-9][0-9]/)
This will only find the first digit.
> {
> print m/^[0-9]|^[0-9][0-9]/;
Don't need to do this again.
print "$1\n" if (/^(\d\d*)/)
> }
>
>#Check each line for a type
There must be a better way to do this.
Apparently each line could have multiple
items?
Untested:
my $x = 0;
while (/(SWITCH|MONITOR|ROUTER|KVM) |
(LICENCES?) |
(COMPUTER|LAPTOP|THIN) |
(SOFTWARE) /xg
) {
defined $1 && print HARDWARE $_;
defined $2 && print LICENSES $_;
defined $3 && print COMPUTER $_;
defined $4 && print SOFTWARE $_;
$x = 1;
)
> if(/SWITCH|MONITOR|ROUTER|KVM/)
> {
> print HARDWARE $_;
> $x=1;
> }
> if(/LICENCE|LICENCES/)
> {
> print LICENSES $_;
> $x=1;
> }
> if(/COMPUTER|LAPTOP|THIN/)
> {
> print COMPUTER $_;
> $x=1;
> }
> if(/SOFTWARE/)
> {
> print SOFTWARE $_;
> $x=1;
> }
> if(/OS|'OPERATING SYSTEM'/)
> {
> print OS $_;
> $x=1;
> }
> if ($x == '0')
> {
> print "Warning, following line didn't match anything and was
>appended to the 'other' file!\n $_\n";
> print OTHER $_;
> }
>}
-sln
------------------------------
Date: Fri, 21 May 2010 00:38:25 +0100
From: RedGrittyBrick <RedGrittyBrick@SpamWeary.invalid>
Subject: Re: How can I do something based on the first word of a line?
Message-Id: <gqKdnS6LL8JvWmjWnZ2dnUVZ7tadnZ2d@bt.com>
On 20/05/2010 23:32, Steve wrote:
> I have this chunk of code which is broken because I can't figure this
> out. Basically I have a file handle, and the file it reads in always
> starts wit ha number... 1-99. I want to ONLY get that number, and not
> the rest of the line because I want to be able to copy that line that
> amount of times.
>
> while(<FH3>)
Use lexical filehandles!
> {
> my $x = 0;
You probably don't need a flag variable. It's clutter. It's also poorly
named. For example you could have my $found = ""; and then set $found
instead of your print statements. If you only expect one condition to
match per line.
> #Make enough records based on quantity.. broken par
I don't understand this comment, nor your previous "copy that line that
many times". Examples of input and expected output help with understanding.
> if(/^[0-9]|^[0-9][0-9]/)
if (/^[0-9][0-9]?/) # ? means 0 or 1 of preceding
if (/^\d{1,2}/) # \d matches any digit, {min,max}
if (/^(\d{1,2})/) # capturing parentheses
> {
> print m/^[0-9]|^[0-9][0-9]/;
print "$1\n";
Though your intent is unclear to me.
> }
>
> #Check each line for a type
> if(/SWITCH|MONITOR|ROUTER|KVM/)
> {
> print HARDWARE $_;
print "HARDWARE $_";
It seems you are not beginning your script with
use strict;
use warnings;
If you do - you will get lots of helpful advice from perl.
Also your disregard for line endings is a bit of a concern.
> $x=1;
> }
> if(/LICENCE|LICENCES/)
> {
> print LICENSES $_;
> $x=1;
> }
> if(/COMPUTER|LAPTOP|THIN/)
> {
> print COMPUTER $_;
> $x=1;
> }
> if(/SOFTWARE/)
> {
> print SOFTWARE $_;
> $x=1;
> }
> if(/OS|'OPERATING SYSTEM'/)
Are those quote marks part of the text to be matched?
> {
> print OS $_;
> $x=1;
> }
> if ($x == '0')
Don't do numeric comparisons on text constants.
if ($x == 0)
if ($x eq '0')
Though if you followed my earlier suggestion, this would become
if ($found)
{
}
else
{
print
}
> {
> print "Warning, following line didn't match anything and was
> appended to the 'other' file!\n $_\n";
> print OTHER $_;
> }
> }
Repetitive code like that cries out for a better data structure or
expression.
use strict;
use warnings;
my %prefix = ( "SWITCH|MONITOR|ROUTER|KVM" => "HARDWARE",
"LICENCE|LICENCES" => "LICENCES",
...
)
while (<$fh>) {
my $verdict = "";
foreach $pattern (keys %prefix) {
if (/$pattern/i) {
$verdict = "$prefix{$pattern} $_\n";
last;
}
}
if ($verdict) { print $verdict; }
else { print "OTHER $_\n"; }
}
Untested - caveat emptor!
Initializing $verdict to "" is probably not needed but I'm too lazy to
check :-)
--
RGB
------------------------------
Date: Thu, 20 May 2010 17:23:51 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: How can I do something based on the first word of a line?
Message-Id: <6ikbv5taspcpop5iajqemh96urdbdtopjc@4ax.com>
Steve <steve@staticg.com> wrote:
>I have this chunk of code which is broken because I can't figure this
>out. Basically I have a file handle, and the file it reads in always
>starts wit ha number... 1-99. I want to ONLY get that number, and not
>the rest of the line
That is trivial, just use the text in numerical context. The numerical
value of a text is the leading number in that text, e.g.
print "123 only the number but not this text will be printed" + 0;
If you "use warnings" (as you should) you will have to temporarily
disable them for this conversion.
>because I want to be able to copy that line that
>amount of times.
my $line = "32 and whatever other text";
print $line x ($line+0);
jue
------------------------------
Date: Thu, 20 May 2010 19:56:46 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: How can I do something based on the first word of a line?
Message-Id: <slrnhvbm8b.s05.tadmc@tadbox.sbcglobal.net>
RedGrittyBrick <RedGrittyBrick@SpamWeary.invalid> wrote:
> On 20/05/2010 23:32, Steve wrote:
>> my $x = 0;
>
> It's also poorly
> named.
I pointed that out to this OP a few months ago...
>> if ($x == '0')
>
> Don't do numeric comparisons on text constants.
> if ($x == 0)
> if ($x eq '0')
I pointed that out to this OP a few months ago...
Steve, do you read the followups to your postings?
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Thu, 20 May 2010 17:59:06 -0700 (PDT)
From: Steve <steve@staticg.com>
Subject: Re: How can I do something based on the first word of a line?
Message-Id: <9daf10f7-4035-4a03-8d81-189ff4920c37@v29g2000prb.googlegroups.com>
On May 20, 4:27=A0pm, s...@netherlands.com wrote:
> On Thu, 20 May 2010 15:32:55 -0700 (PDT), Steve <st...@staticg.com> wrote=
:
> >I have this chunk of code which is broken because I can't figure this
> >out. =A0Basically I have a file handle, and the file it reads in always
> >starts wit ha number... 1-99. =A0I want to ONLY get that number, and not
> >the rest of the line because I want to be able to copy that line that
> >amount of times.
>
> Where is the code that "copy that line that amount of times"?
>
>
>
> >while(<FH3>)
> >{
> > =A0 =A0my $x =3D 0;
> >#Make enough records based on quantity.. broken par
>
> What does this mean?
>
> > =A0 =A0if(/^[0-9]|^[0-9][0-9]/)
>
> This will only find the first digit.
>
> > =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0print m/^[0-9]|^[0-9][0-9]/;
>
> Don't need to do this again.
> print "$1\n" if (/^(\d\d*)/)
>
> > =A0 =A0}
>
> >#Check each line for a type
>
> There must be a better way to do this.
> Apparently each line could have multiple
> items?
>
> Untested:
> =A0 =A0 =A0 =A0 my $x =3D 0;
> =A0 =A0 =A0 =A0 while (/(SWITCH|MONITOR|ROUTER|KVM) |
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (LICENCES?) |
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (COMPUTER|LAPTOP|THIN) |
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (SOFTWARE) /xg
> =A0 =A0 =A0 =A0 ) {
> =A0 =A0 =A0 =A0 =A0 =A0 defined $1 && print HARDWARE $_;
> =A0 =A0 =A0 =A0 =A0 =A0 defined $2 && print LICENSES $_;
> =A0 =A0 =A0 =A0 =A0 =A0 defined $3 && print COMPUTER $_;
> =A0 =A0 =A0 =A0 =A0 =A0 defined $4 && print SOFTWARE $_;
> =A0 =A0 =A0 =A0 =A0 =A0 $x =3D 1;
> =A0 =A0 =A0 =A0 )
>
>
>
> > =A0 =A0if(/SWITCH|MONITOR|ROUTER|KVM/)
> > =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0print HARDWARE $_;
> > =A0 =A0 =A0 =A0 =A0 =A0$x=3D1;
> > =A0 =A0}
> > =A0 =A0if(/LICENCE|LICENCES/)
> > =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0print LICENSES $_;
> > =A0 =A0 =A0 =A0 =A0 =A0$x=3D1;
> > =A0 =A0}
> > =A0 =A0if(/COMPUTER|LAPTOP|THIN/)
> > =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0print COMPUTER $_;
> > =A0 =A0 =A0 =A0 =A0 =A0$x=3D1;
> > =A0 =A0}
> > =A0 =A0if(/SOFTWARE/)
> > =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0print SOFTWARE $_;
> > =A0 =A0 =A0 =A0 =A0 =A0$x=3D1;
> > =A0 =A0}
> > =A0 =A0if(/OS|'OPERATING SYSTEM'/)
> > =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0print OS $_;
> > =A0 =A0 =A0 =A0 =A0 =A0$x=3D1;
> > =A0 =A0}
> > =A0 =A0if ($x =3D=3D '0')
> > =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0print "Warning, following line didn't match anyt=
hing and was
> >appended to the 'other' file!\n $_\n";
> > =A0 =A0 =A0 =A0 =A0 =A0print OTHER $_;
> > =A0 =A0}
> >}
>
> -sln
Sorry I'm no perl expert here, still learning. No idea what some of
the code you've changed even means. the \d\d* makes sense, except I
think it should be \d* right? the line will only ever start with 1 or
2 digits. Also, I just want to copy the entire line based on that
number. For example
3,ABC,SDFFDSF,344556
if that line is found, I want it to be duplicated 3 times, and chop
off the 15 after, so it will be:
ABC,SDFFDSF,344556
ABC,SDFFDSF,344556
ABC,SDFFDSF,344556
------------------------------
Date: Thu, 20 May 2010 20:58:31 -0400
From: Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>
Subject: Re: How do I check if www site has Perl compatible?
Message-Id: <4bf5dab7$8$fuzhry+tra$mr2ice@news.patriot.net>
In <ugs9v5da85geiklfuah7b1miepdq3u0b9n@4ax.com>, on 05/20/2010
at 11:56 AM, John <John.Smith@invalid.com> said:
>I sometimes get request to do some small perl problem on a www page
>but the owner does not know if their www has perl.
They don't own the WWW; all that they own is their web server.
You not only need to know whether they have Perl, but which and where.
Have the owner contact whoever installed his software and ask.
--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>
Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to spamtrap@library.lspace.org
------------------------------
Date: Thu, 20 May 2010 16:37:54 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Validate $q->param() instead of copying to hash first?
Message-Id: <slrnhvbajf.rlo.tadmc@tadbox.sbcglobal.net>
zaphod <abc@def.com> wrote:
> On 20/05/2010 17:34, J. Gleixner wrote:
>> zaphod wrote:
>>> $form_fields{$_} = $q->param($_) for $q->param();
>>
>> Why do that when there's Vars()?
> Vars() ?
See the "FETCHING THE PARAMETER LIST AS A HASH" section in
perldoc CGI
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Thu, 20 May 2010 14:38:08 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Where to get the document for a library function in command line (linux)?
Message-Id: <85500d45-7256-4ab1-a949-096d2729f31c@l6g2000vbo.googlegroups.com>
I'm not sure where to get the help page for a perl library function
(such as getops from Getopt::Std) from linux command line. Maybe this
is described in man perl. But I may overlook it. Would you please let
me know how to do it?
P.S. I could use webpage documentation. But I prefer command line
help.
Regards,
Peng
------------------------------
Date: Thu, 20 May 2010 23:51:45 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Where to get the document for a library function in command line (linux)?
Message-Id: <4bf5aef2$0$22917$e4fe514c@news.xs4all.nl>
Peng Yu wrote:
> where to get the help page for a perl library function
> (such as getops from Getopt::Std) from linux command line.
perldoc Getopt::Std
--
Ruud
------------------------------
Date: Thu, 20 May 2010 15:16:10 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Why I can't use if(defined $var)?
Message-Id: <5e1e6b84-7fc0-4fd2-8022-333668e8dd74@m21g2000vbr.googlegroups.com>
I'm puzzled why I can not use the first line below. Would you please
let me know how write it such that the if clause is at the beginning
rather than at the end.
if(defined $opt_o) print "$opt_o\n";
print "$opt_o\n" if defined $opt_o;
Regards,
Peng
------------------------------
Date: Thu, 20 May 2010 15:47:34 -0700 (PDT)
From: sisyphus <sisyphus359@gmail.com>
Subject: Re: Why I can't use if(defined $var)?
Message-Id: <d4a9635b-30fc-4ee0-88c7-8cb545f94112@v29g2000prb.googlegroups.com>
On May 21, 8:16=A0am, Peng Yu <pengyu...@gmail.com> wrote:
> I'm puzzled why I can not use the first line below. Would you please
> let me know how write it such that the if clause is at the beginning
> rather than at the end.
>
> if(defined $opt_o) print "$opt_o\n";
>
if(defined $opt_o) { print "$opt_o\n" }
Cheers,
Rob
------------------------------
Date: Fri, 21 May 2010 01:05:36 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Why I can't use if(defined $var)?
Message-Id: <4bf5c041$0$22943$e4fe514c@news.xs4all.nl>
Peng Yu wrote:
> Would you please
> let me know how write it such that the if clause is at the beginning
> rather than at the end.
>
> if(defined $opt_o) print "$opt_o\n";
defined $opt_o and print $opt_o, $/;
--
Ruud
------------------------------
Date: Thu, 20 May 2010 20:05:34 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Why I can't use if(defined $var)?
Message-Id: <slrnhvbmoq.s05.tadmc@tadbox.sbcglobal.net>
Peng Yu <pengyu.ut@gmail.com> wrote:
> I'm puzzled why I can not use the first line below.
Did you look up the "if" statement in Perl's docs?
perldoc perlsyn
Compound Statements
...
a block is delimited by curly brackets, also known as braces.
We will call this syntactic construct a BLOCK
...
if (EXPR) BLOCK
> Would you please
> let me know how write it such that the if clause is at the beginning
> rather than at the end.
>
> if(defined $opt_o) print "$opt_o\n";
that is
if (EXPR) EXPR
but you need
if (EXPR) BLOCK
if (defined $opt_o) {print "$opt_o\n"}
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 V11 Issue 2957
***************************************