[16221] in Perl-Users-Digest
Perl-Users Digest, Issue: 3633 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jul 12 06:10:30 2000
Date: Wed, 12 Jul 2000 03:10:19 -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: <963396619-v9-i3633@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 12 Jul 2000 Volume: 9 Number: 3633
Today's topics:
Getting number of running processes in a cron job ?? <e.bras@hccnet.nl>
Re: Getting number of running processes in a cron job ? <adetalabi@clara.co.uk>
Re: how to make my program with csh (doco)
Re: how to make my program with csh (doco)
Installing NDBM_file module <fabascal@gredos.cnb.uam.es>
Re: Is there a way to make a perl script into an execut <nnickee@nnickee.com>
Re: Is this an application for PERL?? (Abigail)
Re: Is this an application for PERL?? <bill.kemp@wire2.com>
Re: Jeopardy Quoting [Was: <newbie>How to determine cur (Csaba Raduly)
Re: make test problems compiling 5.6 for Solaris 2.7 <adetalabi@clara.co.uk>
Re: Method to obfuscate or disguise Perl source code? <phill@modulus.com.au>
Re: Method to obfuscate or disguise Perl source code? (brian d foy)
Re: multidimensional associative arrays <nospam@nospam.com>
Re: multidimensional associative arrays <debjit@oyeindia.com>
Never satisfied <john@nomailplease>
Re: New to perl, need help <merlik@my-deja.com>
Raise Exception when failure occurs ?? <e.bras@hccnet.nl>
Re: Raise Exception when failure occurs ?? (Iain Chalmers)
Re: Raise Exception when failure occurs ?? <e.bras@hccnet.nl>
Reading lines of a file within a loop? <John_member@newsguy.com>
Reading perl packets ? <e.bras@hccnet.nl>
Sorting a tab separated table ? dpi27@my-deja.com
Re: Unique Items philhibbs@my-deja.com
Re: Unique Items (Sam Holden)
Re: Unique Items philhibbs@my-deja.com
Re: Unique Items philhibbs@my-deja.com
Re: Weird behavior when using foreach loop and referenc (Tad McClellan)
Re: Which one is better? $_[0] or shift? <iltzu@sci.invalid>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 12 Jul 2000 09:59:30 +0200
From: "Ed Bras" <e.bras@hccnet.nl>
Subject: Getting number of running processes in a cron job ??
Message-Id: <8kh8fj$th6$1@enterprise.cistron.net>
Could you please give me a little bit of advise on the following:
In a function a give back the number ofrunning processes of the pocess that
is given as input argument.
I do that by:
$_ = `ps -A | grep <process>`
$lv_Num = s/<process>/<process>/g
which works, but when I run the application as a cronjob and call the
function I don't receive the output of the `...` command as all the output
is redirected when starting the cron job: <app nam> > /dev/null 2>&1
What is a good way to get the number of processes ?
I was thinking about:
1: a file locking mechanisme to overcome that a nother application starts,
but this only works for one application, not if I want to allow more
applications running at the same time
2: not redirecting the output when starting the cronjob, but doning this in
the application after calling the funtion that gives back the number of
processed running (which is at the beginning of the application). But I
don't no if I then already receive email from the cronjob which I certainly
don't want.
Please, some advice on how to do this,
Regards,
Ed Bras
------------------------------
Date: Wed, 12 Jul 2000 10:51:17 +0100
From: Ade Talabi <adetalabi@clara.co.uk>
Subject: Re: Getting number of running processes in a cron job ??
Message-Id: <396C3F95.1AEDD10B@clara.co.uk>
Ed Bras wrote:
>
> Could you please give me a little bit of advise on the following:
>
> In a function a give back the number ofrunning processes of the pocess that
> is given as input argument.
> I do that by:
> $_ = `ps -A | grep <process>`
> $lv_Num = s/<process>/<process>/g
>
> which works, but when I run the application as a cronjob and call the
> function I don't receive the output of the `...` command as all the output
> is redirected when starting the cron job: <app nam> > /dev/null 2>&1
>
> What is a good way to get the number of processes ?
> I was thinking about:
> 1: a file locking mechanisme to overcome that a nother application starts,
> but this only works for one application, not if I want to allow more
> applications running at the same time
> 2: not redirecting the output when starting the cronjob, but doning this in
> the application after calling the funtion that gives back the number of
> processed running (which is at the beginning of the application). But I
> don't no if I then already receive email from the cronjob which I certainly
> don't want.
>
> Please, some advice on how to do this,
>
> Regards,
> Ed Bras
When you run jobs in cron you hae to give the absolute path. Try that.
ie rather than say myfile.sh, you would have to supply
/home/edbaras/scripts/myfile.sh
--
/--------------------------------------------------------------\
| Ade Talabi | Internet : adetalabi@clara.co.uk |
| ::M1ETW:: | Web Page : http://www.net-africa.com |
\--------------------------------------------------------------/
------------------------------
Date: 12 Jul 2000 06:06:50 GMT
From: is83024@cis.nctu.edu.tw (doco)
Subject: Re: how to make my program with csh
Message-Id: <8kh1tq$459$1@news.cis.nctu.edu.tw>
Drew Simonis (care227@attglobal.net) ´£¨ì:
: I notice English isn't your native language, so lets make sure
: I understand your question: You have the example below written
: in c shell syntax, and you would like to convert it to Perl, right?
yes ^^;
: > i want to transfer the follow csh promgram into perl
: > #!/usr/bin/csh
: > tar cvf kkk.tar . >&! error
: If all you want to do is execute that one command, you can use
: several means. Using the system() function, the exec() function
: or the backwards quotes (the ` charachter, or the qx{} quoting
: method) They each do different things, but they all would suffice
: for the execution of an external program.
: At your Unix command prompt, type the following for online documentation
: of these functions:
: perldoc -f system # for system()
: perldoc -f exec # for exec
: perldoc perlop # look for information on back quotes (`) under the
: # heading "Quote and Quote Like Operators"
: You might also like to take a look at the CPAN archive, which has some
: nice Perl modules to do archiving and backup.
: HTH
my english is poor .
I need to know "Redirect the standard output to a file" under th PERL
using "csh" commands,not "sh".
Please tell me in details,thx.
--
------------------------------
Date: 12 Jul 2000 06:10:57 GMT
From: is83024@cis.nctu.edu.tw (doco)
Subject: Re: how to make my program with csh
Message-Id: <8kh25h$459$2@news.cis.nctu.edu.tw>
Drew Simonis (care227@attglobal.net) ´£¨ì:
: doco wrote:
: I notice English isn't your native language, so lets make sure
: I understand your question: You have the example below written
: in c shell syntax, and you would like to convert it to Perl, right?
I need to know "Redirect the standard output to a file" under PERL
using "csh" commands,not "sh".
Tell me in details,please. Thx.
------------------------------
Date: Wed, 12 Jul 2000 11:07:07 +0200
From: Federico Abascal <fabascal@gredos.cnb.uam.es>
Subject: Installing NDBM_file module
Message-Id: <396C353B.16DEE1C6@gredos.cnb.uam.es>
Hello,
I have a program that uses the module NDBM_File; but I need to run the
same program in a different machine (differente o.s., architecture...).
The problem is that the second one doesn't have that module. I went to
CPAN site (/CPAN/modules/by-module/NDBM_File/) but I get lost there.
Help is very wellcome: How to install in a non-superuser way?
Alternatives?
Thanks in advance,
Federico
------------------------------
Date: Wed, 12 Jul 2000 00:02:14 -0500
From: Nnickee <nnickee@nnickee.com>
Subject: Re: Is there a way to make a perl script into an executable?
Message-Id: <FE4313E01B8C1248.4F6007A9C244F90E.0FAEED0AF65F2359@lp.airnews.net>
On Tue, 11 Jul 2000 21:44:23 -0400, someone claiming to be Drew
Simonis <care227@attglobal.net> said:
>> He probably just knew that you have way more time on your hands than he
>> does :)
>I get paid to read Usenet. I can't help it. =)
Ok Drew, you suck. <g>
I only get paid to irc.
Nnickee
------------------------------
Date: 12 Jul 2000 01:29:12 EDT
From: abigail@delanet.com (Abigail)
Subject: Re: Is this an application for PERL??
Message-Id: <slrn8mo1il.am3.abigail@alexandra.delanet.com>
Peter Brooker (ra5589@email.sps.mot.com) wrote on MMDVII September
MCMXCIII in <URL:news:396BBDB9.1DC59A08@email.sps.mot.com>:
`'
`' I am guessing that a PERL script could be created that could
`' "automatically" create the html file. Am I correct?
Yes.
`' Is using a PERL script a good way to go?
Insufficient data to answer this question. But, judging that you have
to ask, probably not. What you are asking is fairly trivial, and that
indicates you hardly have any Perl knowledge (confirmed by the fact you
don't know how to spell Perl). It's much better to stick to a language
you know.
Abigail
--
sub _'_{$_'_=~s/$a/$_/}map{$$_=$Z++}Y,a..z,A..X;*{($_::_=sprintf+q=%X==>"$A$Y".
"$b$r$T$u")=~s~0~O~g;map+_::_,U=>T=>L=>$Z;$_::_}=*_;sub _{print+/.*::(.*)/s};;;
*_'_=*{chr($b*$e)};*__=*{chr(1<<$e)}; # Perl 5.6.0 broke this...
_::_(r(e(k(c(a(H(__(l(r(e(P(__(r(e(h(t(o(n(a(__(t(us(J())))))))))))))))))))))))
------------------------------
Date: Wed, 12 Jul 2000 08:54:04 +0100
From: "W Kemp" <bill.kemp@wire2.com>
Subject: Re: Is this an application for PERL??
Message-Id: <963388584.20162.0.nnrp-01.c3ad6973@news.demon.co.uk>
Peter Sundstrom wrote in message <8kgioh$p3v$1@hermes.nz.eds.com>...
>
>Peter Brooker wrote in message <396BBDB9.1DC59A08@email.sps.mot.com>...
>>I have an application that produces a numerical sequence of *.gif files.
>>
>>The sequence looks like
>>
>>n1_dio_001.gif
>>n1_dio_002.gif
>>.
>>.
>>n1_dio_xxx.gif, 50 < xxx < 200
>>
>>I do this frequently and consequently I want to write a script that will
>>
>>create an html file to display these gifs in sequence. This file will
>>work as follows. When the user clicks on the html file, he/she will see
>>the first gif file with a forward and backward arrow at the bottom of
>>the screen. Clicking on the forward arrow will allow the user to display
>>
>>the next gif image. The backward arrow will display the previous image
>>in the sequence.
>>
>>I am guessing that a PERL script could be created that could
>>"automatically" create the html file. Am I correct?
>>
>>Is using a PERL script a good way to go?
>
>
>No, but you could use a Perl script.
PEdanTIC!!
>
>What you are describing is a slideshow. Have a look at
>http://cgi.resourceindex.com/Programs_and_Scripts/Perl/Image_Display/Slide_
S
>hows/ for some ideas.
>
A rather unixy/linuxy way of doing something similar:-
#!/usr/bin/perl
my @flist=`ls -1`;
print "<html><head><title>showing all images</title></head><body>";
foreach (@flist){
chomp;
if (/\.(JPG|jpeg|gif|bmp)$/i){
print "\n <img src=\"$_\"><br><br>";
}
}
print "</body></html>";
Note- it isn't CGI its a way of making a quick page (that needs to be in the
same directory as the picture.
It doesn't even save it so you need to "perl xxxx.pl >quick_piclist.html".
it would be better to use 'glob' instead of `ls`
------------------------------
Date: 12 Jul 2000 09:49:49 GMT
From: csaba_r@my-deja.com (Csaba Raduly)
Subject: Re: Jeopardy Quoting [Was: <newbie>How to determine current ...]
Message-Id: <8F6F601FCquuxi@194.203.134.200>
12 Jul 2000: A formal bug report was sent to Seti@Home, because the
following message originated from DNess@Home.Com (David Ness) was
reported as containing signs of intelligence:
>Jeff Susanj wrote:
>>
>> This "Jeopardy Quoting" must be a perl thing because I have never
>> heard anyone object to it in any other newsgroup.
Other newsgroups call it other names, but I bat Jeopardy will make the Jargon
file some day (maybe it did already)
>> I really hate
>> to wade through a big long quote when I just read it in the last
>> 10 replies.
That's what the PageDown and/or Del keys are for
[snip]
>
>The show `Jeopardy' prides itself on being backwards (answer comes
>before question). That's why putting your comment _before_ the thing
>being commented on is called `Jeopardy Quoting'.
>
>You are welcome to prefer presenting information any way you like.
>Your arguments have been made countless times before and are _very_
>familiar to everyone. Some newsgroups don't care about the `style'
>of communications.
>
>However, some newsgroups do have a pronounced style and this is one
>of them. The people who do the heavy lifting here (I am not one of
>them, BTW) are used to reading things their way, so if you want
>answers you'd be well advised to follow _their_ rules. They really
>read a lot of mail, and switching between some people Jeopardy
>Quoting and others not doing so is a real PITA to them
[snip]
It also makes following the chronological order in the thread pretty near
impossible.
<OFFTOPIC>
Jeff, I see you're using OE. For your newsreader needs, check out XNews
(http://xnews.3dnews.net)
</OFFTOPIC>
Csaba
--
Rats! Where's my sig ?
------------------------------
Date: Wed, 12 Jul 2000 10:48:37 +0100
From: Ade Talabi <adetalabi@clara.co.uk>
Subject: Re: make test problems compiling 5.6 for Solaris 2.7
Message-Id: <396C3EF5.7E963448@clara.co.uk>
Dave Encisco wrote:
>
> Hi,
>
> I'm having one of those frustrating days getting perl to compile with
> minimal "dubious(es)" when I run make test. All the lib/db stuff,
> lib/gdbm and lib/ndbm gives me the message: test program seems to have
> generated a core.
>
> I had previously installed db-3.1.14 and gdbm-1.8.0 and have since
> reinstalled older versions, but to no avail. I've configured perl to
> look in the /usr/local/include and /usr/local/lib...nota.
>
> What obvious thing am I missing?
>
> Thanks!
> Dave
>
> dencisco@eos.arc.nasa.gov
> --
> ===========================================
> Dave Encisco
> dencisco@eos.arc.nasa.gov
> ===========================================
It would be easier to debug the core file, to find out where you a going
wrong, but I suspect incompatible library versions.
--
/--------------------------------------------------------------\
| Ade Talabi | Internet : adetalabi@clara.co.uk |
| ::M1ETW:: | Web Page : http://www.net-africa.com |
\--------------------------------------------------------------/
------------------------------
Date: Wed, 12 Jul 2000 15:36:02 +1000
From: Peter Hill <phill@modulus.com.au>
Subject: Re: Method to obfuscate or disguise Perl source code?
Message-Id: <396C03C2.823@modulus.com.au>
Silicon Cowboy wrote:
>
> I'm looking for a program that would obfuscate, disguise, or camouflage
> Perl source code.
The best method is to write your Perl source code in another language,
such as e.g. C++ or Java. This will confuse your enemies. Of course, the
Perl compiler will have some difficulty with your code, so submit it to
an e.g. C++ or Java compiler.
Ship the final product as Perl and you will have portable but
obfuscated, disguised and camouflaged Perl code.
hth. hand. ymmv.
--
Peter Hill,
Modulus Pty. Ltd.,
http://www.modulus.com.au/
------------------------------
Date: Wed, 12 Jul 2000 03:12:01 -0400
From: brian@smithrenaud.com (brian d foy)
Subject: Re: Method to obfuscate or disguise Perl source code?
Message-Id: <brian-ya02408000R1207000312010001@news.panix.com>
In article <396C03C2.823@modulus.com.au>, phill@modulus.com.au posted:
> Silicon Cowboy wrote:
> > I'm looking for a program that would obfuscate, disguise, or camouflage
> > Perl source code.
> The best method is to write your Perl source code in another language,
> such as e.g. C++ or Java.
not Java - Mocha works too well ;)
--
brian d foy
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Mongers <URL:http://www.perl.org/>
------------------------------
Date: 12 Jul 2000 05:12:36 GMT
From: The WebDragon <nospam@nospam.com>
Subject: Re: multidimensional associative arrays
Message-Id: <8kguo4$1r1$2@216.155.32.201>
In article <slrn8mku33.7v7.abigail@alexandra.delanet.com>,
abigail@delanet.com wrote:
| Abigail
| -
| sub J::FETCH{Just }$_.='print+"@{[map';sub J::TIESCALAR{bless\my$J,J}
| sub A::FETCH{Another}$_.='{tie my($x),$';sub A::TIESCALAR{bless\my$A,A}
| sub P::FETCH{Perl }$_.='_;$x}qw/J A P';sub P::TIESCALAR{bless\my$P,P}
| sub H::FETCH{Hacker }$_.=' H/]}\n"';eval;sub H::TIESCALAR{bless\my$H,H}
evil, evil, evil..
<grin>
--
send mail to mactech (at) webdragon (dot) net instead of the above address.
this is to prevent spamming. e-mail reply-to's have been altered
to prevent scan software from extracting my address for the purpose
of spamming me, which I hate with a passion bordering on obsession.
------------------------------
Date: Wed, 12 Jul 2000 14:58:28 +0530
From: "Debjit" <debjit@oyeindia.com>
Subject: Re: multidimensional associative arrays
Message-Id: <8kiik6$3c3$1@news.vsnl.net.in>
for $i(1..10) {
print $a{$i}->{6},"\n"
}
Philip Rennert wrote in message <396B65ED.C1F2E55E@ioip.com>...
>After
>
>for $i (1..10)
> {
> for $j (1..10)
> {
> $a{$i}{$j}=2*$i+$j;
> }
> }
>
>while ( ($key,$value) = each %{$a{6}}) {print $value."\n"}
>
>as you said will print the numbers 13 .. 22 in some order, since the first
index $i
>is fixed at the value 6 and $j varies between 1 and 10.
>
>How do you print the even numbers from 8 to 26, which you get if $j is
fixed at 6
>and $i varies between 1 and 10?
------------------------------
Date: Wed, 12 Jul 2000 10:40:39 +0100
From: "John" <john@nomailplease>
Subject: Never satisfied
Message-Id: <396c3d98@news.telinco.net>
At last I feel the arcane mystery of regex starting to reveal itself to me !
However chuffed I am that my code below works I would appreciate it if
someone would show me how it could be done a bit better.
I'm sure it could be done in two lines by not destroying the RHS but am
stumped.
Thanks, JohnShep
$alpha = $numeric = $string_in;
$alpha =~ s/[\W]|[\d]//g; # remove any non letters
$numeric =~ s/[\D]//g; # remove any non digits
------------------------------
Date: Wed, 12 Jul 2000 04:44:15 GMT
From: Merlik <merlik@my-deja.com>
Subject: Re: New to perl, need help
Message-Id: <8kgt2t$qj3$1@nnrp1.deja.com>
> #!/usr/local/bin/perl -w
> use strict;
>
> print "Input a number: ";
> chomp (my $number = <STDIN>);
> print "Result: ", fibonacci($number + 1), "\n";
>
> sub fibonacci {
> my $number = shift;
> return 1 if $number == 1;
> return 0 if $number == 0;
> return fibonacci($number - 1) + fibonacci($number - 2);
> }
Thank you. I did that and it works perfectly.
-- Merlik
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Wed, 12 Jul 2000 10:17:10 +0200
From: "Ed Bras" <e.bras@hccnet.nl>
Subject: Raise Exception when failure occurs ??
Message-Id: <8kh9gs$8uk$1@enterprise.cistron.net>
What I really "miss" (or at least don't know how to do it) in Perl, is that
when an runtime exception occurs the script just dies, without giving you
the oppurtonity to handle the exception, just like delphi, java or visual
basic.
What can I do to overcome this problem in perl ?
For example, when I have a database transaction and something goes wrong, I
want to make a explicit rollback in some kind of an exception handler!
Regards,
Ed Bras
------------------------------
Date: Wed, 12 Jul 2000 18:35:47 +1000
From: bigiain@mightymedia.com.au (Iain Chalmers)
Subject: Re: Raise Exception when failure occurs ??
Message-Id: <bigiain-1207001835470001@bigman.mighty.com.au>
In article <8kh9gs$8uk$1@enterprise.cistron.net>, "Ed Bras"
<e.bras@hccnet.nl> wrote:
>What I really "miss" (or at least don't know how to do it) in Perl, is that
>when an runtime exception occurs the script just dies, without giving you
>the oppurtonity to handle the exception, just like delphi, java or visual
>basic.
>
>What can I do to overcome this problem in perl ?
perldoc -f eval
cheers
Iain
------------------------------
Date: Wed, 12 Jul 2000 11:53:14 +0200
From: "Ed Bras" <e.bras@hccnet.nl>
Subject: Re: Raise Exception when failure occurs ??
Message-Id: <8khf5d$esm$1@enterprise.cistron.net>
Thanks for the answer.
Buuuut, I know eval, but is that THE proper way to do it ??
I mean does it work ok, to put eval around about hundred statement for
example ??? Isn't there a more elegant way to do it ??
Regards,
Ed
Iain Chalmers <bigiain@mightymedia.com.au> wrote in message
news:bigiain-1207001835470001@bigman.mighty.com.au...
> In article <8kh9gs$8uk$1@enterprise.cistron.net>, "Ed Bras"
> <e.bras@hccnet.nl> wrote:
>
> >What I really "miss" (or at least don't know how to do it) in Perl, is
that
> >when an runtime exception occurs the script just dies, without giving you
> >the oppurtonity to handle the exception, just like delphi, java or visual
> >basic.
> >
> >What can I do to overcome this problem in perl ?
>
> perldoc -f eval
>
> cheers
>
> Iain
------------------------------
Date: 11 Jul 2000 21:35:51 -0700
From: John Casey <John_member@newsguy.com>
Subject: Reading lines of a file within a loop?
Message-Id: <8kgsj7$1is0@drn.newsguy.com>
Hi,
I need to write a script to scan a XML file for the lines containing the search
pattern "GEEQualify" . The line would be typically like this, (with the values
of the variables being different for different lines):
<GEEQualify Length="9.979" DCode="A6" Speed="970"/>
Then, when this line is found, it has the to check the value of
the "Speed" variable (eg: Speed="970" here) in the line. If it is above 500,
the script has to proceed to do the following (otherwise it has to continue
the search for the remaining of the file ); Scan the next two
lines which would be as follows:
******
<NUM>86756</NUM>
<Info Available="Y" Name="Jackie" GEEAvailable="Fri Jun 21, 2000 00:00:00 EDT"
LateC="128"/>
******
It has to take the numeric value (86756 here) between the <NUM> tags
and the alphabetic value of the 'Available' variable (Y here)
and print them out in a new file with the format
86756#Y#
---
The outline of the script i am trying to work on is
While (<FILEID>)
{
chomp;
if /GEEQualify/
{
$line = $_;
????? (How do i pick the "speed=xyz" variable from the $line? ) ??
if ($speed >= 500 )
{
???(how do i) read the next two lines within the loop ???
?? PatternMatch the two lines to get the required NUM and Available
values??
print OUTPUTFILE, " $NUM#$AVAILABLE# \n"
}
}
Please suggest on how to read two lines within a loop and
the regular expression for pattern matching the two lines to get the values.
thanks
John
------------------------------
Date: Wed, 12 Jul 2000 11:37:54 +0200
From: "Ed Bras" <e.bras@hccnet.nl>
Subject: Reading perl packets ?
Message-Id: <8khe8f$3l2$1@enterprise.cistron.net>
Which rights must a packets have to be able to be read/executed by another
perl program ?
Do a perl program executes a packets or reads it when you include a packets
with "use <packetname>
The problem I am having that at my local server, the rights must at least
include "read" for other, as the apache server is executed as nobody.
Hoever as the isp where I put everyting online the rights must be at least
execute for the group, as the apache server is in the same group. The
strange thing that I don't understand is that at the isp the packets files
don't need read rights for the group, such that the webserver is able to use
it, instead as my local server needs this ??
Regards,
Ed Bras
------------------------------
Date: Wed, 12 Jul 2000 08:13:40 GMT
From: dpi27@my-deja.com
Subject: Sorting a tab separated table ?
Message-Id: <8kh9bb$3is$1@nnrp1.deja.com>
Hi,
Some of you may think it's a newbie question, but I'm stuck on this.
I want to sort a table using an element of this table as the sorting
key.
I thought of coding it like this :
foreach $element (sort { (split(/\t/,$a))[1] cmp (split(/\t/,$b))[1]) }
@table)
so for a table like this one :
@table=("val1.1\tval2.1\tval3.1\tval4.1","val2.1\tval2.2\tval3.2\val4.2"....)
So my though was that with my sort function, table would be sorted using
the val2.x columns since split return a table and that I take the 2nd
element of it. But I have an error during compilation.
Anybody got any idea how to make it ?
Thanks
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Wed, 12 Jul 2000 08:03:33 GMT
From: philhibbs@my-deja.com
Subject: Re: Unique Items
Message-Id: <8kh8oe$368$1@nnrp1.deja.com>
In article <slrn8mlsh5.r6h.sholden@pgrad.cs.usyd.edu.au>,
sholden@cs.usyd.edu.au wrote:
> From the perlop description of s//
> ... returns the number of substitutions made
> So each element of @in will have 'xxx' prepended (since s// will
> modify the original values). Each substition will return 1 and so
> $saw{1} will be incremented.
So this is more like what I want:
@out = grep(!$saw{"xxx".$_}++, @in);
Or, if I need pattern replacement:
@out = grep(!$saw{($tmp=$_)=~s/.*/xxx$1/}++, @in);
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 12 Jul 2000 08:55:45 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: Unique Items
Message-Id: <slrn8mockh.1o9.sholden@pgrad.cs.usyd.edu.au>
On Wed, 12 Jul 2000 08:03:33 GMT, philhibbs@my-deja.com wrote:
>In article <slrn8mlsh5.r6h.sholden@pgrad.cs.usyd.edu.au>,
> sholden@cs.usyd.edu.au wrote:
>> From the perlop description of s//
>> ... returns the number of substitutions made
>> So each element of @in will have 'xxx' prepended (since s// will
>> modify the original values). Each substition will return 1 and so
>> $saw{1} will be incremented.
>
>So this is more like what I want:
>
>@out = grep(!$saw{"xxx".$_}++, @in);
That is equivalent (aside from %saw at the end) to @out=grep(!$saw{$_}++,@in);
>
>Or, if I need pattern replacement:
>
>@out = grep(!$saw{($tmp=$_)=~s/.*/xxx$1/}++, @in);
You could try something like:
@out = grep !$saw{$_}++, @in;
$_="xxx$_" for (@out);
or
@out = grep !$saw{$_}++, map {"xxx$_"} @in;
or
@out = map {$saw{$_}++?():"xxx$_"} @in;
--
Sam
You can blame it all on the internet. I do...
--Larry Wall
------------------------------
Date: Wed, 12 Jul 2000 09:19:42 GMT
From: philhibbs@my-deja.com
Subject: Re: Unique Items
Message-Id: <8khd74$670$1@nnrp1.deja.com>
In article <8kh8oe$368$1@nnrp1.deja.com>,
philhibbs@my-deja.com wrote:
> Or, if I need pattern replacement:
> @out = grep(!$saw{($tmp=$_)=~s/.*/xxx$1/}++, @in);
D'oh - that should be s/(.*)/ in order to set $1, shouldn't it? I know
there's a $ that contains the entire match, but I can't remember what
it is.
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Wed, 12 Jul 2000 09:28:40 GMT
From: philhibbs@my-deja.com
Subject: Re: Unique Items
Message-Id: <8khdnt$6c5$1@nnrp1.deja.com>
In article <slrn8mockh.1o9.sholden@pgrad.cs.usyd.edu.au>,
sholden@cs.usyd.edu.au wrote:
> That is equivalent (aside from %saw at the end) to @out=grep(!$saw{$_}
++,@in);
> You could try something like:
>
> @out = grep !$saw{$_}++, @in;
> $_="xxx$_" for (@out);
_
|~| ! I understand!
=
%saw is only being used to check for duplicates, the array isn't being
created from it! I was adding altered values into the hash, and
checking for duplicated altered values, then adding the unaltered value
into @out!
Muchos gracias!
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Wed, 12 Jul 2000 00:21:26 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Weird behavior when using foreach loop and references
Message-Id: <slrn8mnsi6.5ct.tadmc@magna.metronet.com>
On Tue, 11 Jul 2000 20:11:48 -0700, Hervé Masson <herve-news@mindstep.com> wrote:
>
>
>(Perl version: 5.005_03, reproduced as well on v5.6.0)
>
>I experienced really strange behavior when using a foreach loop,
^^^^^^^^^^^^^^^^
s/strange/as documented/; # :-)
>going through a list, known by its reference.
You cannot have a reference to a list.
You have a reference to an (anonymous) array there (in $list).
>When I change
>the loop-variable value inside the loop, it also change the item
>inside the list ! That's not what I expected at all, because I
^^^^^^^^^^^^^^^^^^^
You should not sign contracts without reading them first.
Read about foreach() if you want to know what to expect from foreach().
>thought I was the only "owner" of my local variable '$item',
^^ ^^^^^ ^^^^^
You misunderstand multiple things, I think.
You are starting to confuse me too.
Let's start from the beginning:
Perl has 2 kinds of variables:
Perl FAQ, part 7:
"What's the difference between dynamic and lexical (static) scoping?
Between local() and my()?"
local() -> dynamic variable
my() -> lexical variable
So, your $item below is NOT a "local variable".
It is a "lexical variable".
$item is "in scope" within the foreach() body, so it can
be changed in the body.
>and I can safety put any value I want without affecting external
>data, even if my variable used to refer them via a foreach loop.
^^^^^^^^^^^^
So naturally you have already read the "Foreach Loops" section
in perlsyn.pod then, right?
>Is there any reason for that ?
I guess you missed where it talks about "alias" then.
>Misterious interpreter optimization ?
>Did I completely miss to understand perl ?
>
>I would be really interested to know more about that...
errr, OK.
Let's just quote perlsyn here then:
--------------------------------
The C<foreach> loop iterates over a normal list value and sets the
variable VAR to be each element of the list in turn. If the variable
is preceded with the keyword C<my>, then it is lexically scoped, and
is therefore visible only within the loop. Otherwise, the variable is
implicitly local to the loop and regains its former value upon exiting
the loop. If the variable was previously declared with C<my>, it uses
that variable instead of the global one, but it's still localized to
the loop.
The C<foreach> keyword is actually a synonym for the C<for> keyword, so
you can use C<foreach> for readability or C<for> for brevity. (Or because
the Bourne shell is more familiar to you than I<csh>, so writing C<for>
comes more naturally.) If VAR is omitted, C<$_> is set to each value.
If any element of LIST is an lvalue, you can modify it by modifying VAR
inside the loop. That's because the C<foreach> loop index variable is
an implicit alias for each item in the list that you're looping over.
--------------------------------
Note the last sentence there.
>-[The sample code]---------------------------------------------------
>
>use strict;
>use Data::Dumper;
>
>my($item,$list);
>$list=
> [
> [ 'A', 1 ],
> [ 'B', 2 ],
> [ 'C', 3 ],
> ];
>
>printf("Before loop : %s",Dumper($list));
>foreach $item (@$list)
>{
> $item=0;
>}
>printf("After loop : %s",Dumper($list));
>I did not expect that code to change the original array '$list'...
Bad things can happen when you sign without reading.
Don't do that :-)
Use the docs, Luke.
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: 12 Jul 2000 09:20:36 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: Which one is better? $_[0] or shift?
Message-Id: <963392840.1844@itz.pp.sci.fi>
In article <MPG.13d5375eb2b87da598abb7@nntp.hpl.hp.com>, Larry Rosler wrote:
>In article <smn2c9lrnd6175@corp.supernews.com> on Tue, 11 Jul 2000
>20:54:33 GMT, Craig Berry <cberry@cinenet.net> says...
>> Prakash Kailasa (prakash@gate.net) wrote:
>> : Ilmari Karonen <iltzu@sci.invalid> wrote:
>> :
>> : : my $optional = @_ ? shift : 'default';
>> :
>> : my $optional = shift || 'default';
>>
>> What if the caller wants $optional to be 0 or '' or undef?
>
>I'm sure the point is to replace undef by 'default'. Otherwise why
>bother with providing the default at all?
Because you want foo() to do the same as foo('default'), which might
not be the same as foo(undef)? Particularly useful where the optional
value is used as a boolean.
I recently submitted a patch for CGI::Carp which contains a perfect
example of this idiom:
sub warningsToBrowser {
$EMIT_WARNINGS = @_ ? shift : 1;
_warn(shift @WARNINGS) while $EMIT_WARNINGS and @WARNINGS;
}
(before you ask: http://www.sci.fi/~iltzu/CGI_Carp.pm.diff)
--
Ilmari Karonen - http://www.sci.fi/~iltzu/
"The screwdriver *is* the portable method." -- Abigail
Please ignore Godzilla and its pseudonyms - do not feed the troll.
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 3633
**************************************