[26090] in Perl-Users-Digest
Perl-Users Digest, Issue: 8290 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jul 29 14:05:14 2005
Date: Fri, 29 Jul 2005 11:05:06 -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 Fri, 29 Jul 2005 Volume: 10 Number: 8290
Today's topics:
Converting a 4 byte field to integer. <shashank@mia.ece.uic.edu>
Extract lines by name <dreamer@cox.net>
Re: Extract lines by name <jgibson@mail.arc.nasa.gov>
Passing vars to a "require"d script <someone@somewhere.com>
Re: Passing vars to a "require"d script <klaus_gb@yahoo.com>
Re: Passing vars to a "require"d script <tadmc@augustmail.com>
Perl experts wanted <Wim.Hoogenraad@webmasterslookup.com>
Re: slow substr? xhoster@gmail.com
Re: true false ? : expression thingy xhoster@gmail.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 29 Jul 2005 12:46:52 -0500
From: Shashank Khanvilkar <shashank@mia.ece.uic.edu>
Subject: Converting a 4 byte field to integer.
Message-Id: <dcdpuu$1q9$1@newsx.cc.uic.edu>
Hi,
All help is appreciated.
I have to read a binary file having a given structure (not important here).
One field that i need to read is a 4 byte field specifying some page
numeber.
If i want to display this page number, how can u do this?
The below fragment does not seem to work,
--SNIP--
read(IN, $buffer, 4); #IN is the fh for a file open in binary mode..
$page_number = ord($buffer);
print "[XXX] $page_number\n";
--SNIP--
For example, if $buffer contains the following four values,
0x74 0x29 0x00 0x00
then
print will print
[XXX] 116
which is dec(0x74)while I want dec(0x74|0x29|0x00|0x00);
(do not worry about the endianess at this point.)
Thanks
Shank
------------------------------
Date: Fri, 29 Jul 2005 19:25:57 +0200
From: Fred Hare <dreamer@cox.net>
Subject: Extract lines by name
Message-Id: <t_udnfoxIc47-3ffRVnyjw@giganews.com>
I have textfiles with 6000 lines (about 674k) and I want to print lines
in groups containing the same name. The following code (with 3 names)
works OK. But I actually use a script with 24 names and I would like to
find a way where I have to open and close the infile only once.
#!/usr/bin/perl
use strict ;
use warnings ;
my $infile = 'D:\aa\headers.txt';
my $outfile = 'D:\aa\outfile.txt' ;
open(OUT, "> $outfile")
or die "Could not open $outfile for writing: $!\n";
open(IN, "< $infile")
or die "Could not open $infile for reading: $!\n";
while(<IN>){
if (m/adam/ig ) {
print OUT $_;
}
}
print OUT "\n" ;
close(IN) or die "could not close IN: $!";
open(IN, "< $infile")
or die "Could not open $infile for reading: $!\n";
while(<IN>){
if (m/baker/ig ) {
print OUT $_;
}
}
print OUT "\n" ;
close(IN) or die "could not close IN: $!";
open(IN, "< $infile")
or die "Could not open $infile for reading: $!\n";
while(<IN>){
if (m/Charly/ig ) {
print OUT $_;
}
}
print OUT "\n" ;
close(IN) or die "could not close IN: $!";
close(OUT) or die "could not close OUT: $!";
------------------------------
Date: Fri, 29 Jul 2005 11:03:59 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Extract lines by name
Message-Id: <290720051103593179%jgibson@mail.arc.nasa.gov>
In article <t_udnfoxIc47-3ffRVnyjw@giganews.com>, Fred Hare
<dreamer@cox.net> wrote:
> I have textfiles with 6000 lines (about 674k) and I want to print lines
> in groups containing the same name. The following code (with 3 names)
> works OK. But I actually use a script with 24 names and I would like to
> find a way where I have to open and close the infile only once.
>
>
[program snipped]
Store your lines arrays which are referred to by elements of a hash.
This will work as long as your text lines fit into memory:
#!/usr/local/bin/perl
#
use warnings;
use strict;
my %text;
while (<DATA>) {
if( /(adam|baker|Charly)/ ) {
push( @{$text{$1}}, $_ );
}
}
for my $name ( keys %text ) {
print "\n$name:\n\n";
print @{$text{$name}};
}
__DATA__
line 1
line 2 adam
line 3
line 4 baker
line 5
line 6 Charly
line 7
line 8 baker
line 9
__OUTPUT__
adam:
line 2 adam
Charly:
line 6 Charly
baker:
line 4 baker
line 8 baker
Note: there is a way to generate the regex (adam|baker|Charly|...) from
a list of names, but I have forgotten what it is and cannot reconstruct
it.
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
------------------------------
Date: Fri, 29 Jul 2005 16:33:11 +0100
From: "Bigus" <someone@somewhere.com>
Subject: Passing vars to a "require"d script
Message-Id: <dcdi7n$i4b$1@blackmamba.itd.rl.ac.uk>
If I call a separate script from within a CGI script using "require", is
there any (simple) way of passing a variable to it?
I've tried:
require "/admin/scripts/live/otherscript.pl?123"
and trying to pick it up via @ARGV but it comes up with an error saying it
can't find the script. In other words it seems to treat the "?123" as part
of the file name.
Bigus
------------------------------
Date: Fri, 29 Jul 2005 18:26:34 +0100
From: "Klaus Eichner" <klaus_gb@yahoo.com>
Subject: Re: Passing vars to a "require"d script
Message-Id: <42ea5707$0$5043$636a15ce@news.free.fr>
"Bigus" <someone@somewhere.com> wrote in message
news:dcdi7n$i4b$1@blackmamba.itd.rl.ac.uk...
> If I call a separate script from within a CGI script using "require", is
> there any (simple) way of passing a variable to it?
>
> I've tried:
>
> require "/admin/scripts/live/otherscript.pl?123"
>
> and trying to pick it up via @ARGV but it comes up with an error saying it
> can't find the script. In other words it seems to treat the "?123" as part
> of the file name.
You can always define your own variables with "our" to pass values, like:
our @param = ('?', '123');
require "/admin/scripts/live/otherscript.pl";
Then, inside "/admin/scripts/live/otherscript.pl", you repeat the "our
@param", but without the "= ('?', '123')". This allows you to access the
variables inside the "otherscript.pl":
our @param;
print "\@param = (@param)\n";
--
Klaus
------------------------------
Date: Fri, 29 Jul 2005 11:50:35 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Passing vars to a "require"d script
Message-Id: <slrndeknir.6b6.tadmc@magna.augustmail.com>
Bigus <someone@somewhere.com> wrote:
> If I call a separate script from within a CGI script using "require",
Then we have entered the Twilight Zone, as what you describe
is impossible. :-)
require does not "call" a "separate" program, it "includes" some
code in the _current_ program.
system(), exec(), backticks or a pipe-open is how truly "separate"
programs are called from within Perl.
> is
> there any (simple) way of passing a variable to it?
Yes, but we should first address why you think that you need to do that.
Why do you feel that you need to pass an argument to a require'd file?
Why not simply rewrite the .pl function to take a proper argument,
and then supply the argument in the main program's function call?
> I've tried:
>
> require "/admin/scripts/live/otherscript.pl?123"
That indicates that you have an improper mental model of how things
work. Correcting that will go a long way toward avoiding future confusions.
URL-like methods have no connection whatsoever to Perl, so your thinking
that that question-mark thing would work indicates your model problem.
> and trying to pick it up via @ARGV but it comes up with an error saying it
> can't find the script. In other words it seems to treat the "?123" as part
> of the file name.
That is exactly what it is supposed to do.
If you really really need to "pass" variables (I doubt that you do),
you can do it thus:
--------------------------
use warnings; # foo.pl
use strict;
our $foo;
sub func1 { print $foo }
1;
--------------------------
#!/usr/bin/perl
use warnings; # main program
use strict;
our $foo = 'foobar';
require 'foo.pl';
func1();
--------------------------
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 29 Jul 2005 19:51:14 +0200
From: "Wim Hoogenraad" <Wim.Hoogenraad@webmasterslookup.com>
Subject: Perl experts wanted
Message-Id: <42ea6c4b$0$66418$58c7af7e@news.kabelfoon.nl>
Dear developer,
Do you often wonder who built a certain website and how it is done? And who
manages that website right now?
Strange, but developers usually stay anonymous while they should present
themselfs to get more orders.
We have been working on a website to change this situation:
WebmastersLookup.com. It's a special Searchengine that lookes for
Webpersons, not for Websites.
We want as many webpersons as possible to join us. So that visitors can
search for somebody who will do the job for them. If they find that person
they can view his/her profile and portfolio. They can search on keywords,
skills or location.
For you, the developer, it can be useful when you are looking for someone to
complete your team or project. You can create your own network of
sitebuilders.
We ask people with various skills to join us! For example webmasters,
webdesigners, graphical designers, DBA's, site-owners, marketingmanagers,
frontpage users, contentmanagers, etc. etc.
I know you are active on the internet yourself, thats why I'm asking you to
join. Your membership is free offcourse and you can leave at anytime.
So join us now ! And help us and yourself to fill this network of
sitebuilders. Send this e-mail to other webpeople you know. They can join
your network and benifit from WebmastersLookup to.
The url is http://www.webmasterslookup.com , click on 'Join us'.
Thank you for reading this e-mail.
Kind regards,
Wim Hoogenraad
Wim.Hoogenraad@webmasterslookup.com
------------------------------
Date: 29 Jul 2005 16:14:43 GMT
From: xhoster@gmail.com
Subject: Re: slow substr?
Message-Id: <20050729121443.913$xd@newsreader.com>
bugbear <bugbear@trim_papermule.co.uk_trim> wrote:
>
> However, 50% of the time is not spent
> in a class utility; when a method
> has decided what to take from the class
> buffer, it calls this:
>
> sub use {
> my ($self, $l) = @_;
> $self->{buf} = substr($self->{buf}, $l);
> }
>
> Which simply notes the fact that $l bytes have been consumed.
You are forcing Perl to copy the string each time. Don't do that.
> As I understand it, substr is a very simple internal
> "book keeping" method in perl, that shouldn't need to allocate
> memory, or copy memory.
It is not the substr that needs to allocate and/or copy, it is the
assignment that you are doing with the results of the substr. Use the 4
argument form of substr, so that it modifies the buffer in place.
substr($self->{buf}, 0, $l, "");
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 29 Jul 2005 16:36:29 GMT
From: xhoster@gmail.com
Subject: Re: true false ? : expression thingy
Message-Id: <20050729123629.317$85@newsreader.com>
Ian Wilson <scobloke2@infotop.co.uk> wrote:
> Bigus wrote:
> > when you want a
> > 1-line alternative to an if...else.. construct,
>
> Whilst I quite like
> $this = condition ? "foo" : "bar";
>
> What's so horrible about
> if (condition) {$this="foo";} else {$this="bar"}
> which I find quite readable?
Where do you put the "my"?
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
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 8290
***************************************