[19264] in Perl-Users-Digest
Perl-Users Digest, Issue: 1459 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 8 00:05:32 2001
Date: Tue, 7 Aug 2001 21:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <997243507-v10-i1459@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Tue, 7 Aug 2001 Volume: 10 Number: 1459
Today's topics:
Re: Child env. variables <bkennedy99@Home.com>
Embedded perl question (Robert J. Lee)
Re: Extract the relative sorting of items from multiple (John Lin)
Re: get rid of these leading zeros in Perl <monty@primenet.com>
Re: gmtime() and $isdst <bwalton@rochester.rr.com>
Re: join lines ? <goldbb2@earthlink.net>
Re: Matching Strings in an array <ahamm@sanderson.net.au>
Re: multiple line comments? <jurgenex@hotmail.com>
Re: multiple line comments? <mda@idatar.com>
Re: Pattern Matching: Which subpattern (rather than sub <barsticus@earthling.net>
Re: Pattern Matching: Which subpattern (rather than sub <barsticus@earthling.net>
Re: Pattern Matching: Which subpattern (rather than sub <barsticus@earthling.net>
Re: Perl Logical Knot Problem (And web pages that dont <stephen.p.harris@worldnet.att.net>
Re: perl, pgp & file system issue <goldbb2@earthlink.net>
Re: Q: csh together with perl <goldbb2@earthlink.net>
Re: Q:Of negative numbers and logical operations <rs55862@nospamyahoo.com>
Re: Reporting Questionable Programming Activity (Greg Andrews)
Re: sorting by a hash of arrays - how to elegantly exte <goldbb2@earthlink.net>
Re: string inside string <mda@idatar.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 08 Aug 2001 02:38:14 GMT
From: "Ben Kennedy" <bkennedy99@Home.com>
Subject: Re: Child env. variables
Message-Id: <qC1c7.80596$EP6.19708208@news1.rdc2.pa.home.com>
"Shah Kashani" <skashani@uk.ibm.com> wrote in message
news:9km2j3$q7u$1@sp15at20.hursley.ibm.com...
> Hello,
> I have a perl program running an external shell-script using system(); The
> external program sets environmental variables, but I cannot access them
> within the Perl program using $ENV. (Because the variables set in the
child
> process are not set in the parent's)
>
> Does anyone know how I can import these local variables set in the
external
> script?
If the external script is written in Perl, you may be able to use Perl's
normal mechanisms (use, require) to execute the script in the current Perl's
process space. If you can't do that, you could either capture the output of
the second script with backticks, or you could open up a pipe to explictly
pass data back to the parent.
--Ben Kennedy
------------------------------
Date: 7 Aug 2001 20:58:29 -0700
From: Zarlock@pretty.co.uk (Robert J. Lee)
Subject: Embedded perl question
Message-Id: <262e5e2a.0108071958.5a3fa92a@posting.google.com>
Is there any easy way of saving the state of the perl interpretor to a
file (i.e. the values of all variables AND any subroutines which have
been processed)?
Basically, I've decided to use embedded perl to add scripting
capabilities to a MUD I'm writing in C++, which has to be stopped and
restarted periodically as code changes (needing recompiling). So,
obviously, I need to save any changes to the perl bits as well.
I've got as far as getting a hashtable of all data out of the stash,
which should contain globs of all the data AFAICT. But the docs don't
seem to give much explanation on what I should be doing with globs
("GV*"s) to turn them into data to save. (Also, I seem to be getting
"SV*"s from the hash and I don't know how to turn them into "GV*"s).
Any help appreciated.
- Rob.
------------------------------
Date: 7 Aug 2001 19:43:26 -0700
From: johnlin@chttl.com.tw (John Lin)
Subject: Re: Extract the relative sorting of items from multiple lists
Message-Id: <a73bcad1.0108071843.36dc52c@posting.google.com>
Sorry if reposted. No, our news server was broken and didn't post it at all.
"Bart Lateur" wrote
> I have to extract the ordering of each item in some arrays, and not
> every item is present in every array. For example:
>
> @list = ([qw(A C F)], [qw(A B C)], [qw(A C D E)], [qw(E F)]);
>
> From this, I'd need to deduce that 'B' comes before 'C' and after 'A',
> and so on, so I'd eventually have to come up with the list qw(A B C D E
> F). How would you do that?
I happen to have ever solved a "dependency problem".
If I correctly understand your question, it can be "reduced" to mine.
use strict;
my $verbose = 0;
# @list = ([qw(A C F)], [qw(A B C)], [qw(A C D E)], [qw(E F)]);
my %dependency = (
'A'=>['C','F','B','C','C','D','E'],
'B'=>['C'],
'C'=>['F','D','E'],
'D'=>['E'],
'E'=>['F']
);
# You know how to convert from @list to %dependency, right?
my %weight;
sub adjust { # adjust($key,$base_weight,$dependlist) : recursive
my($key,$base_weight,$dependlist) = @_;
if(not defined $weight{$key} or $weight{$key} < $base_weight) {
$weight{$key} = $base_weight;
print "$key => $base_weight" if $verbose;
print $dependlist? ": check [@$dependlist]\n": "\n" if $verbose;
adjust($_, $base_weight+1, $dependency{$_}) for @$dependlist;
}
}
while(my($key,$dependlist) = each %dependency) { adjust $key,0,$dependlist }
# print sort by weight
print map {$$_[0]} sort {$$a[1]<=>$$b[1]} map {[$_,$weight{$_}]} keys %weight;
__END__
ABCDEF
> As a thought for a starting point: for qw(A C F), I'd assign a number to
> each entry, for example (A => 1, C => 2, F => 3), and try to assign
> intermediate values for values that have to come in between existing
> items.
My principle is:
When you assign $key => #n, all its dependant should be at least #n+1
When you assign A => 0, all ['C','F','B','C','C','D','E'] should be at least 1
First, assign A => 0, call adjust (recursively) to make sure all the elements
in its dependence list ['C','F','B','C','C','D','E'] are all at least 1, thus
C is assigned to 1, then check C's list: ['F','D','E'] are all at least 2, thus
F is assigned to 2, (F has no dependence list) and
D is assigned to 2, then check D's list: ['E'] are all at least 3, thus
E is assigned to 3, then check E's list: ['F'] are all at least 4, thus
F is REASSIGNED to 4...
The processed sequence is (set $verbose = 1 to see it)
A => 0: check [C F B C C D E]
C => 1: check [F D E]
F => 2
D => 2: check [E]
E => 3: check [F]
F => 4
B => 1: check [C]
C => 2: check [F D E]
D => 3: check [E]
E => 4: check [F]
F => 5
> If there's more than one solution, any solution will do
Yes, it only produce one result, depend on the order of processing.
> and if there's a conflict, I'd like to be warned about it.
If there is "looped dependency" my program will hang on deep recursion.
It needs some adjustment.
Best regards.
John Lin
------------------------------
Date: 8 Aug 2001 01:10:46 GMT
From: Jim Monty <monty@primenet.com>
Subject: Re: get rid of these leading zeros in Perl
Message-Id: <9kq3im$sv8$1@nnrp2.phx.gblx.net>
Abigail <abigail@foad.org> wrote:
> Jim Monty <monty@primenet.com> wrote:
> >
> > my $id1 = ...;
> > $id1 =~ s/^0+//; # Safest general solution
>
> my $id1 = "0000000";
Ok.
$id1 =~ s/^0+(?=\d)//; # Safest general solution?
--
Jim Monty
monty@primenet.com
Tempe, Arizona USA
------------------------------
Date: Wed, 08 Aug 2001 01:15:27 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: gmtime() and $isdst
Message-Id: <3B7092F4.5F130A35@rochester.rr.com>
"Eric D." wrote:
>
> I would like to adjust the eastern time field in my text file according to
> GMT and Daylight Savings Time. i.e. If there is daylight savings, adjust
> clock one hour (ahead or back). What is the best way to do this? Should I
> use gmtime? If so $isdst gives you 0 or 1, how does that help?
...
> Eric
Daylight savings time for what? The current date/time setting on the
computer on which you are running? A date/time stamp in the data file?
gmtime() does not return an isdst field. Perhaps you are confusing
gmtime() with localtime()? You should be able to use localtime()'s
isdst field to determine if a given timestamp of the format used by
time() is daylight time in the timezone you have told your computer's OS
it is in. But that might vary a bit by OS, and definitely by whether
your OS was installed and maintained correctly time-wise. GMT doesn't
do daylight savings.
--
Bob Walton
------------------------------
Date: Tue, 07 Aug 2001 23:22:55 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: join lines ?
Message-Id: <3B70B08F.E6AAB213@earthlink.net>
Tim wrote:
>
> Hi ,
>
> Could someone help me on this ?
>
> I am reading lines from input file.
>
> The lines has the following format:
>
> line "line1" = "001\
> 010\
> 11";
> line "line2" = "111\
> 011\
> 1";
>
> I would like to have the output as follow:
>
> line "line1" = "0,0,1,0,1,0,1,1";
> line "line2" = "1,1,1,0,1,1,1";
#!/usr/local/bin/perl -w
use strict;
local $_ = do { local $/; <DATA> };
while( m[
(\w+) \s+ "(\w+)" \s+ = \s+
"([^"\\]|\\.)";\n
]xgs ) {
my ($n1, $n2, $data) = ($1, $2, $3);
$data =~ s/\\\n\s*//;
$data =~ s/(?<=.)(?=.)/,/gs;
print qq{$n1 "$n2" = "$data";\n};
}
__DATA__
line "line1" = "001\
010\
11";
line "line2" = "111\
011\
1";
__END__
NB: The above code is untested.
--
I need more taglines. This one is getting old.
------------------------------
Date: Wed, 8 Aug 2001 11:22:19 +1000
From: "Andrew Hamm" <ahamm@sanderson.net.au>
Subject: Re: Matching Strings in an array
Message-Id: <3b7094b4@news.iprimus.com.au>
Yves Orton wrote in message
<74f348f7.0108070017.2998174@posting.google.com>...
>>
>> I often (well, sometimes) string together expressions a la
>>
>> X and Y and Z
>>
>> so there could easily be precedence problems especially with assignments
or
>> function calls.
>
>I usually use the perenthetical form of function calls, partially
>because of this reason partially because I find it easier to read and
>paritially cause it reminds me of VB (shudder).
>
I like to reduce the parenthesis for clarity. I haven't got my 1st edition
camel book any more (gave it to a needy programmer) but I'm sure it taught
non-paren use of functions like push, pop, shift etc. It was nice to see the
need reduced in Perl 5 (is that correct memory?).
But having said that, I only reduce parens on what I call command-style
lines: lines which do open, push, pop or simple shifts into vars etc. When a
line contains a full-on piece of maths-style code, precedence gets nasty and
it's time to USE parens for clarity. You'd never find me calling tan or cos
without parens (actually, in this job, you'd never find me calling tan or
cos ;-)
>Mostly I end up doing things like (credit Tom Christiansen)
>
>return (@_) ? ($self->{param}=shift,$self)
> : $self->{param};
>
With all due respect to TC (and there's bucket-loads of that) I'm not
impressed with that style of coding. I stopped enjoying reading my genius
code fragments several years ago. Or more correctly, several years ago I
realised that even my genius code was unreadable 6 months after I wrote it,
no matter how compact and clever it seemed at the time. And considering I
cut my programming teeth on a language called APL, if you know that language
you will indeed know that my early code was from outer space.
I think that line is heaps clearer expressed as:
$self->{param} = shift if @_;
return $self->{param};
or
@_ and $self->{param} = shift;
return $self->{param};
What's the difference? In opcodes, probably nothing or damn little. There's
still just one conditional. Two references to $self->{param}. Which form is
easier for a beginner to read? Or even an advanced programmer? I had to
study your sample for 30 seconds to confirm my gut-feeling about it. 30
seconds doesn't sound like much, but it sure adds up and each moment of
thought fries another brain cell for the day.
I don't think you could have the same problem with my suggestion. The sample
you give would be something you'd learn to recognise in a blink if you wrote
it 1000 times, but what about someone who's never seen it before? A whole
script full of such cleverness just fills me with the heebee-geebees
(cousins of the Bee Gees, only they sound even worse)
Now that I think about it, I must confess to learning one technique from
FORTRAN. My C code used to go like this:
if(a > b)
x = a;
else
x = b;
When I was forced to make some mods to an engineering program written in
FORTRAN (dragged kicking and screaming I was), you had to:
x=b
if(a.gt.b) x=a
So you picked the most likely case as the one this is always executed.
Then it dawned on me one day - well, that's saving 2 physical lines in the
source code, and it sure is simple. Even in C
x = b;
if(a > b) x = a;
pick the most likely path and be done with it. For me, clean and simple is
King, and I'll be stuffed if I'm going to fuss about 20 nanoseconds of
timing difference. I just don't work in that world. If I did, Perl may not
be the smartest choice anyway. It's a high-level language. Not C.
A good algorithm will sh!t on a bad one, no matter how many clever
instant-optimisations have been put into the individual lines. Take a bad
algorithm containing cleverness, replace it with a good algorithm containing
simplicity and clarity, and you'll achieve an optimisation that's in the
order of big numbers, not just a few percent. That's MY kind of
optimisation - Yeah Baby!
>Actually on this point (dense code) is there a site or book dedicated
>to JAPH signatures? Sometime I try to disect the odd one here or
>there, usually with only mediocre success.
>
I used to run them, after a brief squiz to be sure they looked harmless.
Then one day, a genius on the perl/Tk newsgroup had something that popped up
a window that kept ducking the cursor. The trick was to hit shift or
something while moving the mouse. Some other genius had one to spit out all
sorts of permutations of some rubbish. Lesson learned.
I'm not sure that understanding obsfucated code would really help your
programming. Compact and elegant is far different from compact and obscure.
Which are both different from plain old dense and obscure.
--
Space Corps Directive #001
It is a prime, overriding duty to contact other lifeforms, exchange
information, and, whenever possible, bring them home.
-- Red Dwarf
------------------------------
Date: Tue, 7 Aug 2001 19:15:11 -0700
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: multiple line comments?
Message-Id: <3b70a0b2@news.microsoft.com>
"Kevin Bartz" <l_pantin@hotmail.com> wrote in message
news:nH%b7.7007$1p1.549421@bgtnsc04-news.ops.worldnet.att.net...
> If you're using Emacs, just define a Lisp routine to take in a region
> of text and put a hash before every line.
Why re-invent the wheel?
In Perl mode just use "comment-region".
jue
------------------------------
Date: Wed, 08 Aug 2001 04:01:37 GMT
From: "David Allen" <mda@idatar.com>
Subject: Re: multiple line comments?
Message-Id: <BQ2c7.1937$Pz1.310771@typhoon1.gnilink.net>
In article <nH%b7.7007$1p1.549421@bgtnsc04-news.ops.worldnet.att.net>, "Kevin
Bartz" <l_pantin@hotmail.com> wrote:
> If you're using Emacs, just define a Lisp routine to take in a region of
> text and put a hash before every line.
It's already been done for you. While you're in perl-mode, just select
a region of code using the normal method and then do:
M-x comment-region.
This isn't always great, because actually I prefer commenting by inserting
"# " and not just "#" but it works great.
--
David Allen
http://opop.nols.com/
------------------------------
Date: Wed, 08 Aug 2001 03:01:18 +0100
From: Simon Best <barsticus@earthling.net>
Subject: Re: Pattern Matching: Which subpattern (rather than substring) was matched?
Message-Id: <3B709D6E.2CEB543C@earthling.net>
"John W. Krahn" wrote:
>
> "John W. Krahn" wrote:
> >
> > Simon Best wrote:
> > >
> > > Here's what I want to do. I want to find out which subpattern in a
> > > regular expression was the one that matched the matched string, if there
> > > was a match.
[...]
> > > Thanks to anyone who might have an answer :-)
> >
> > defined $-[$_] and print "Matched pattern number $_\n" for 1 .. $#-;
>
> Sorry, should be simpler:
>
> print "Matched pattern number $#-\n"
> # OR
> print "Matched pattern number $#+\n"
>
> John
> --
> use Perl;
> program
> fulfillment
Thanks, but @- and @+ don't seem to exist! Perhaps it's 'cause I'm
using Perl 5.005_03?
Simon
--
_______________________________________________________________________________
Email: barsticus@earthling.net
Website: http://www.cydonia.f2s.com
------------------------------
Date: Wed, 08 Aug 2001 03:38:43 +0100
From: Simon Best <barsticus@earthling.net>
Subject: Re: Pattern Matching: Which subpattern (rather than substring) was matched?
Message-Id: <3B70A632.C0A6D02C@earthling.net>
gimi wrote:
>
> hi
>
> Simon Best wrote:
> > I'm new here. But anyway...
>
> i'm not, but who cares..
>
> > Here's what I want to do. I want to find out which subpattern in a
> > regular expression was the one that matched the matched string, if there
> > was a match.
[...]
>
> not cool, but it would work this way:
>
> $string =~ /$subpattern1/ and print "matched $subpattern1\n"
> or $string =~ /$subpattern2/ and print "matched $subpattern2\n"
> # and so on
> ;
>
> so you would know which.
Thanks, but I'd already discarded that sort of approach. Each
successive match searches through the string until there's either a
match, or the end of the string is passed. With a lot of such pattern
matching, it would end up taking many times as long as putting all the
subpatterns in one pattern.
> > Anyway, the reason I wish to do this is because I wish to find the
> > earliest match (the one furthest to the left in the string) out of a
> > number of alternatives, and know which alternative it was that matched.
>
> as for the 'earliest'.. ask pos() every time and compare the
> numbers you get. see
> % perldoc -f pos
Yeah, I could do. Or I could do something like this:
for( my $offset = 0; $offset <= length $string; ++$offset )
{
foreach $subpattern ( @subpatterns )
{
if( substr( $string, $offset ) =~ m/^$subpattern/ )
{
print "Matched $subpattern.\n";
last;
}
}
}
which would reduce redundant searching, but takes over from the pattern
matching engine when it comes to finding the leftmost match. I'd rather
leave it up to the engine, but without causing unnecessarily long seach
times for each pattern.
[...]
> > I could write a function to do it, but it just seems nicer, somehow, to
> > have Perl's pattern matching engine doing the business, as it already
> > does it the way I need it to. It just doesn't seem to say (as far as I
> > can tell) which subpattern was actually matched.
>
> i don't think so, yes. - but it can be worked around,
> so to speak.
>
> > Anyway, if there is a quick'n'easy way of finding which subpattern
> > matched, I'd like to know!
>
> i'm easy. :D
>
> gimi
>
> --
> http://www.psico.ch/
Thanks for your suggestions! It does indeed seem that some sort of
workaround is necessary.
Simon
--
_______________________________________________________________________________
Email: barsticus@earthling.net
Website: http://www.cydonia.f2s.com
------------------------------
Date: Wed, 08 Aug 2001 03:47:24 +0100
From: Simon Best <barsticus@earthling.net>
Subject: Re: Pattern Matching: Which subpattern (rather than substring) was matched?
Message-Id: <3B70A83B.59E698C4@earthling.net>
Bart Lateur wrote:
>
> Simon Best wrote:
>
> >For example, I could have:
> >
> > $STRING =~ m/($SUBPATTERN1)|($SUBPATTERN2)|($SUBPATTERN3)/;
> >
> >and, if $STRING eq "Blah blah blah $SOMETHING_MATCHING_SUBPATTERN2 blah
> >blah.", I'd want to know that it was $SUBPATTERN2 that matched. I'd
> >like something like backreferences, but where it's $SUBPATTERN2 that's
> >given, not $SOMETHING_MATCHING_SUBPATTERN2.
>
> If there are no capturing parens inside the subpatterns, you can check
> the definedness of the submatches. In your example, $1 would be undef,
> but $2 would be defined.
>
> In order to be able to loop through the submatches, check the special
> array @+ and/or @-, which indicate where the submatches start and end.
> If the second subpattern did match, $+[2] and $-[2] will be defined, but
> $+[1] and $-[1] will not.
>
> --
> Bart.
Thanks! Alas, I don't seem to have @+ and @- (I'm using Perl
5.005_03). But I can search through the backreference variables using
symbolic links instead. @+ and @- would be very handy, though. I take
it that $#- would be a quick and direct way of finding the relevant
backreference number? That's exactly the sort of thing I need! :-)
Thanks again!
Simon
--
_______________________________________________________________________________
Email: barsticus@earthling.net
Website: http://www.cydonia.f2s.com
------------------------------
Date: Wed, 08 Aug 2001 01:17:59 GMT
From: "Stephen Harris" <stephen.p.harris@worldnet.att.net>
Subject: Re: Perl Logical Knot Problem (And web pages that dont work outside of NA)
Message-Id: <br0c7.7069$1p1.568874@bgtnsc04-news.ops.worldnet.att.net>
"Yves Orton" <demerphq@hotmail.com> wrote in message
news:74f348f7.0108070205.25f906d9@posting.google.com...
> Andras Malatinszky <andras@mortgagestats.com> wrote in message
news:<3B6F70C3.46EA410C@mortgagestats.com>...
>
> Exactly. If I try to buy a product or use a site that has this type
> of N.A. arrogance then they dont get my business.
>
> Its amazing the number of sites that dont understand
> internationalization issues.
>
> Yves
This is not a commercial site but a piddly guestbook. Those fields
are optional for everyone. I have an indirect reason for those
fields which has to do with a US encrytpion software policy. I'm
sorry, I could tell you what that policy is, but then I would have
to kill you(which is inefficient)! People in the US have started
poking fun at this line which has become a spy movie cliche.
I thought I would explain that line just in case it hasn't become
popular in Europe, though that tends to spoil the humor.
--
"O wonder! How many goodly creatures are
there here! O brave new world." Stephen
admiring the functional language, Miranda
------------------------------
Date: Tue, 07 Aug 2001 21:33:08 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: perl, pgp & file system issue
Message-Id: <3B7096D4.B60A1877@earthlink.net>
Blixa Bargeld wrote:
>
> Hi. Here's my dilemma - I have a perl script which accepts some form
> data which I'd like to encrypt & transmit in an email message. The
> thing is, I never want this data written to the file system - I'd like
> the encryption & data manipulation to be done solely in memory. I get
> the impression that the pgp modules for perl use temp files to assist
> in encryption. Does anyone know how this could be done? Thanks.
There was a recently released pure-perl implementation of OpenPGP,
available on CPAN as Crypt::OpenPGP. This will do what you want without
writing anything to disk (or even calling any external programs, so you
can even install it on machines which don't have pgp).
One missing feature it [preventing me from giving an example analagous
to Michael Budash's], is that for public key encrypting, you have to
specify a KeyID (a 16 hex digit string specifying the public key), and
cannot choose to give instead an Identity (a name and email address
specifying the public key).
--
I need more taglines. This one is getting old.
------------------------------
Date: Tue, 07 Aug 2001 23:41:06 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Q: csh together with perl
Message-Id: <3B70B4D2.65882D87@earthlink.net>
Rainer Theuer wrote:
>
> Hi all,
>
> i try to write a script which should replace a string in a given file
> .
> The perl call has a problem with ENV variables , because the perl
> call don't know the content.
>
> Does anyone know whats wrong or how to solve my problem ?
>
> Thanks
>
> /Rainer
>
> #!/bin/csh -f
>
> setenv new $2
> setenv old $1
> setenv files $3
>
> echo "replacing : $old --> $new in file $files "
>
> perl -p -i.bak -e 's/$ENV{"old"}/$ENV{"new"}/g' $files
You can use commandline switches on the #! line:
#! perl -p -i.bak
BEGIN { $old = shift @ARGV; $new = shift @ARGV }
s/$old/$new/g
__END__
To use it, just put the above four lines (you can leave off the __END__
line if you want) into a file (which *doesn't* have to end in .pl), use
chmod to give yourself execute access to it, and call it as a program.
E.g., if you put it in the file "mysed", you can do:
chmod a+x mysed
mysed file1 file2 file3 etc
--
I need more taglines. This one is getting old.
------------------------------
Date: Mon, 06 Aug 2001 13:27:07 +0300
From: "Rami Saarinen" <rs55862@nospamyahoo.com>
Subject: Re: Q:Of negative numbers and logical operations
Message-Id: <20010806.132706.1295166342.1009@nospamyahoo.com>
In article <u94rrpxgre.fsf@wcl-l.bham.ac.uk>, "nobull" <nobull@mail.com>
wrote:
>> problem: In one point I need to run three variables throught exclusive
>> or (^) and in the process I end up having a wrong result if a negative
>> (original) value was involved. I think this has something to do with
>> the (bit) sizes of variables (i.e. in C the variable is long integer
>> (32 bit) and something else in perl) thus instead of marking the
>> negative number the first bit is translated as a number. Or
>> something...
> use integer; # All arithmetic operations to use architecture's signed
> int
Worked like a charm. Thanks!
Also the stuff about limiting the scope of the "use" statement was very
inetresting.. I had never though of it that way.. Dummy, eh?
Oh well, live and learn...
--
Rami Saarinen
If you want to reply by e-mail, please remove "NOSPAM"
from my e-mail address.
------------------------------
Date: 8 Aug 2001 03:51:26 GMT
From: gerg@panix.com (Greg Andrews)
Subject: Re: Reporting Questionable Programming Activity
Message-Id: <9kqcvu$cu2$1@news.panix.com>
gurm@intrasof.com (Smiley) writes:
>
>The company I'm working for purchased a Perl CGI Script that turns out
>to be seriously faulty
>
Somebody charged you money for a script from Matt's Script Archive, eh?
-Greg
--
+++++ Greg Andrews +++ gerg@panix.com +++++
I have a map of the United States that's actual size
-- Steven Wright
------------------------------
Date: Tue, 07 Aug 2001 22:22:50 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: sorting by a hash of arrays - how to elegantly extend this code
Message-Id: <3B70A27A.CD9A871D@earthlink.net>
chris wallace wrote:
>
> In short: my question is about how to (elegantly!) extend the sort
> @new = sort { ${$pos{$a}}[0] <=> ${$pos{$b}}[0] ||
> ${$pos{$a}}[1] <=> ${$pos{$b}}[1]
> } @old;
> to
> @new = sort { ${$pos{$a}}[0] <=> ${$pos{$b}}[0] ||
> ${$pos{$a}}[1] <=> ${$pos{$b}}[1] ||
> ${$pos{$a}}[2] <=> ${$pos{$b}}[2] ||
>
> ....
>
> ${$pos{$a}}[n] <=> ${$pos{$b}}[n] ||
> } @old;
> where n = $#{$pos{$a}} = $#{$pos{$b}}.
@new =
map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [ $_, pack("N*", @$pos{$_}) ] },
@old;
or:
my %newpos;
@newpos{keys %pos} = map { pack "N*", @$_ };
@new = sort { $newpos{$a} cmp $newpos{$b} } @old;
> Background and longer version:
>
> I have some data where each person has been assigned a pair of
> (non-identical, unordered) values.
>
> I want to order these values by decreasing frequency, dealing with
> ties by ordering by which person first had the value. If there are
> ties still, order by the second person, etc. So if the values 3 and 4
> appear twice each, but the person 1 had value 3 and person 4 had value
> 4, then I want 3 to come before 4 in the final output.
To be able to make sense of this, I'm going to pretend that this is a
"most common answers" contest, and the question was "what are your two
most favorite numbers". You want to list the most common answers first,
subsorted by which user had a particular answer first.
> Initially, data is an array, listing person 1's values, then person
> 2's, etc.
[original code snipped]
#!/usr/local/bin/perl -w
use strict;
my @user2answers = ([1,2,1,2],
[1,2,2,1],
[1,2,1,3],
[1,2,3,1],
[1,2,3,2],
[1,2,2,3],
[1,2,1,3,2,4],
[2,1,1,3,2,4],
[1,2,2,3,1,4]
);
my %answer2userorder;
my $answer2frequency;
for my $user ( 0 .. $#user2answers ) {
foreach my $answer (@{$user2answers[$user]}) {
push @{$answer2userorder{$answer}}, $user
unless $answer2userorder{$answer}[-1] == $user;
++ $answer2frequency{$answer};
}
}
# results in:
# %answer2frequency = ( 1 => 17, 2 => 15, 3 => 7, 4 => 3 )
# %answer2userorder = ( 1=>[0..8], 2=>[0..8], 3=>[2..8], 4=>[6..8] )
my @sorted =
map { $_->{answer} }
sort { $b->{weight} cmp $a->{weight} }
map { +{
answer => $_,
weight => pack( "N*",
$answer2frequency{$_},
@{$answer2userorder{$_}} )
} } keys %answer2frequency;
# @sorted = ( 1, 2, 3, 4 )
foreach ( @sorted ) {
print "Answer $_ occurred $answer2frequency{$_} times.\n";
print "Each of the following users gave it at least once :\n";
print join( ", ", @{$answer2userorder{$_}} ), "\n";
}
--
I need more taglines. This one is getting old.
------------------------------
Date: Wed, 08 Aug 2001 03:58:53 GMT
From: "David Allen" <mda@idatar.com>
Subject: Re: string inside string
Message-Id: <1O2c7.370$9K5.324620@typhoon2.gnilink.net>
In article <w0vb7.15735$lB.3025510@afrodite.telenet-ops.be>, "Bart Van der
Donck" <bart@nijlen.com> wrote:
> $F{'k1'}="hello";
> $i=1;
> print $F{'k$i'};
>
> Is there a way so that $F{'k$i'} will display "hello" ?
Absolutely. Just do $F{"k$i"} instead of $F{'k$i'}.
That "string within a string" stuff you're talking about is actually called
variable interpolation. The double quotes will always interpolate values,
and the single quotes never will.
So if you do this:
$x = 1;
$y = "1 + $x = 2";
Then $y holds the value 1 + 1 = 2. But if you do this:
$x = 1
$y = '1 + $x = 2';
Then $y holds the value 1 + $x = 2.
--
David Allen
http://opop.nols.com/
------------------------------
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.
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 1459
***************************************