[29177] in Perl-Users-Digest
Perl-Users Digest, Issue: 421 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 9 16:10:01 2007
Date: Wed, 9 May 2007 13:09:07 -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 Wed, 9 May 2007 Volume: 11 Number: 421
Today's topics:
Re: A question about lists <spamtrap@dot-app.org>
Can't spawn "cmd.exe" error while using system k.sanjith@gmail.com
Re: Click link to go to routine <uri@stemsystems.com>
Re: Click link to go to routine <news@lawshouse.org>
Re: Click link to go to routine <joe@inwap.com>
Re: Converting 4 bytes to a float <Larry.Martell@gmail.com>
Re: Converting 4 bytes to a float <Larry.Martell@gmail.com>
Re: Converting 4 bytes to a float <Larry.Martell@gmail.com>
Re: Converting 4 bytes to a float <spamtrap@dot-app.org>
DBD::Oracle, tracing, and stored procedures... <ru@vakuum.de>
Re: document/literal style SOAP:Lite service <efimfurman@gmail.com>
Re: document/literal style SOAP:Lite service <scobloke2@infotop.co.uk>
I'd like to create an array of unique values <MisterPerl@gmail.com>
Re: I'd like to create an array of unique values (Greg Bacon)
Re: I'd like to create an array of unique values <mritty@gmail.com>
Re: I'd like to create an array of unique values <cwilbur@chromatico.net>
Re: I'd like to create an array of unique values <someone@example.com>
Re: I'd like to create an array of unique values <jurgenex@hotmail.com>
Re: I'd like to create an array of unique values <awkster@yahoo.com>
Re: I'd like to create an array of unique values <purlgurl@purlgurl.net>
Re: Please give me a good "rule-of-thumb" for back-slas <mritty@gmail.com>
Re: Please give me a good "rule-of-thumb" for back-slas <MisterPerl@gmail.com>
Re: Please give me a good "rule-of-thumb" for back-slas <mritty@gmail.com>
Re: Please give me a good "rule-of-thumb" for back-slas <joe@inwap.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 09 May 2007 16:06:07 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: A question about lists
Message-Id: <m2hcqlrccw.fsf@local.wv-www.com>
hymie_@_lactose.homelinux.net (hymie!) writes:
> In our last episode, the evil Dr. Lacto had captured our hero,
> Sherm Pendley <spamtrap@dot-app.org>, who said:
>
>>Web Hosting by West Virginians, for West Virginians: http://wv-www.net
>
> Now *there's* a phone call I want to hear...
>
> "Just check our web site. it's doubleyou doubleyou doubleyou dot doubleyou
> vee dash doubleyou doubleyou doubleyou ... no no, doubleyou vee dash
> doubleyou doubleyou doubleyou ... "
LOL! My personal email, sherm (at) dot-app.org, is even more fun. :-)
Did I mention I try to stick to email whenever possible? :-)
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: 9 May 2007 09:13:20 -0700
From: k.sanjith@gmail.com
Subject: Can't spawn "cmd.exe" error while using system
Message-Id: <1178727200.536536.173550@u30g2000hsc.googlegroups.com>
I am getting "Can't spawn "cmd.exe"" Error while trying to run a sytem
call for starting my weblogic server.
I can call other System calls like Ssystem "cd" etc from the same
file.
What can be done to fix this error?
------------------------------
Date: Wed, 09 May 2007 14:29:15 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Click link to go to routine
Message-Id: <x7k5vhrguc.fsf@mail.sysarch.com>
>>>>> "SAT" == Sykigh A Trist <sykigh@trist.com> writes:
SAT> Thanks to all that replied. Some helpful tips from you. I will try to
SAT> put to use what you have given me. And about my tirade (see below from
SAT> my first post).
>>> Can this be done within this code and does anyone mind showing me how
>>> to do it? Thanks
SAT> The reason I put the words above in my first post is because it is hard
SAT> to get any answers from a few in this newsgroup. I've been here
SAT> before. Same thing, last time I was here. Some people, instead of trying
SAT> to help, like to point out how wrong you are in every aspect of your
SAT> question. It was a very simple question - I want to click on a link and
SAT> go to a subroutine in the code and not to a webpage. Again, thanks to
SAT> all that helped.
and you still don't get it. your assumptions and questions are poorly
thought out. html doesn't have anything to do with perl and vice
versa. you need to learn the whole cgi flow to understand this but you
seem to not care. clicking on something is not a perl thing but a web
browser/html thing. you need to get that as well. so the issue is more
with your lack of understanding of cgi and the way you express your
needs. the regulars here understand all that and more but need proper
and logical questions which you didn't provide. by asking for that they
are HELPING you try to express and solve your problem. if you don't
understand or acknowledge that as help, it is your problem again. so
don't bitch about how people here treat you. it is on your head to read
and follow the guidelines. blathering about a click inside a perl print
statement won't get you anywhere. maybe your boss will fall for such
nonsense but not here.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 09 May 2007 20:23:47 +0100
From: Henry Law <news@lawshouse.org>
Subject: Re: Click link to go to routine
Message-Id: <1178738628.16175.0@damia.uk.clara.net>
Sykigh A Trist wrote:
> The reason I put the words above in my first post is because it is hard
> to get any answers from a few in this newsgroup.
You're obviously one of the people who's never at fault. You've tried
more than once to get answers in this newsgroup and not managed it.
Spot the common factor? This newsgroup is wonderful for getting answers
to good questions.
> question. It was a very simple question - I want to click on a link and
> go to a subroutine in the code and not to a webpage. Again, thanks to
> all that helped.
It was _not_ a simple question; it was a confused question, which was
both difficult to understand in itself and also showed that you didn't
actually understand the background to it. Various people pointed that
out and you could have gained much insight from their posts, but no ...
you knew you were right and everyone else was wrong so you ploughed on.
But in the interests of being helpful, let me spell it out to you. When
you click on a link - ANY link - the browser sends a message to the web
server containing the URL you clicked on. If you want to do something
special in a Perl program as a result of that URL then you have to
(a) ensure that it's your Perl program, and not some other web page,
that gets control as a result of the URL being interpreted; and
(b) interpret the parameters that the CGI interface passes to your
program in the appropriate way.
You do (a) in the HTML that the user is clicking on. You do (b) in
standard Perl, using the CGI interface to retrieve the parameters and
then code branches appropriately.
If the HTML is generated by a Perl program in the first place (which
yours is, I think) then you can usually use the same program as the
target of the link, using hidden fields in the HTML to distinguish
between different invocations of the program.
--
Henry Law Manchester, England
------------------------------
Date: Wed, 09 May 2007 12:29:25 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: Click link to go to routine
Message-Id: <VtSdnYNXMYOLvN_bnZ2dnUVZ_ualnZ2d@comcast.com>
Sykigh A Trist wrote:
> Thanks to all that replied. Some helpful tips from you. I will try to
> put to use what you have given me. And about my tirade (see below from
> my first post).
>
>>> Can this be done within this code and does anyone mind showing me how
>>> to do it? Thanks
>
> The reason I put the words above in my first post is because it is hard
> to get any answers from a few in this newsgroup. I've been here before.
> Same thing, last time I was here. Some people, instead of trying to
> help, like to point out how wrong you are in every aspect of your
> question. It was a very simple question - I want to click on a link and
> go to a subroutine in the code and not to a webpage. Again, thanks to
> all that helped.
We're trying to help, but you've added added restrictions that make it
impossible to provide an answer to the problem _as_it_is_stated_.
Here is an outline for a solution that works, as long as you drop the
"not go to a webpage" restriction.
1) One way or another, the user gets to a URL that goes to server ALPHA
That web server executes perl program A.pl which includes
a print statement. That print statement outputs a clickable URL, like
'<a href="http://BETA.example.com/cgi-bin/B.pl?part=2">continue to step 2</a>'.
The process running A.pl terminates after outputting the HTML.
2) The user clicks on the link. The user's browser sends a GET request
to server BETA. This is a process that goes to a web page.
3) Server BETA executes perl program B.pl which looks at the information
the browser sent, and executes a specific function inside the B.pl program.
Program B.pl outputs a response, since the browser is expecting one.
if ($part == 2) {
print return_this();
}
In many cases, servers ALPHA and BETA are this same.
With a little program logic, program A.pl and program B.pl can be the same.
However, you cannot get around step 2. When a user clicks on a link,
(and JavaScript is not in the picture), then the user's browser _MUST_ go
to a web page. This means a new request to the web server, a new process
running on the web server to execute B.pl (A.pl with additional arguments).
a) "use CGI;" or other standard module for parsing POST or GET requests.
b) When your program detects that it has been invoked without arguments,
output a FORM or a link that can be clicked on.
print qq{<a href="http://www.scripts.net/cgi-bin/A.pl?back=true" target="_top">
Go Back</a>\n};
c) When your program detects that it has been invoked with the right arguments,
go to a specific routine in the code.
if ($back eq 'true') {
return_this();
} else {
print "Content-type: text/plain\n\n Oops! Wrong link was selected\n";
}
> It was a very simple question - I want to click on a link and
> go to a subroutine in the code and not to a webpage.
You want to click on a link that goes to a webpage that executes a
particular subroutine in the code. Same program as before, but a fresh
new instance with all new unset variables.
> Re: Click link to go to routine
Answer: Click link to invoke CGI program on a server with the idea that
the CGI program will go to a specific routine.
-Joe
P.S.: Don't forget that the second execution of your code will not have access
to any of the variable values that the first execution set, unless you store
temporary results in a file, a database, cookies, or hidden FORM parameters.
------------------------------
Date: 9 May 2007 12:02:44 -0700
From: "Larry.Martell@gmail.com" <Larry.Martell@gmail.com>
Subject: Re: Converting 4 bytes to a float
Message-Id: <1178737363.999793.192760@p77g2000hsh.googlegroups.com>
On May 8, 7:25 pm, Sherm Pendley <spamt...@dot-app.org> wrote:
> "Larry.Mart...@gmail.com" <Larry.Mart...@gmail.com> writes:
> > I have a situation where I have to read in 4 values from a text file,
> > then convert that into a float that I can use for arithmetic.
>
> > For example, the file contains "85,170,174,214" - I read this in, and
> > split it into an array, @d
>
> That's 14 bytes, not counting the quotes. Are you reading a text string
> of four numbers separated by commas, or are you reading four bytes?
I am reading a text string of four numbers separated by commas.
> > In C, I would do something akin to this:
>
> > union convert_float32 {
> > float f; // float view
> > unsigned int i; // uint32 view
> > } c;
> > unsigned char i[4];
> > char *q;
>
> > for (int j=0; j<4; j++) i[j] = (unsigned char)strtoul(d[j], &q, 10);
> > c.i = ((unsigned int)i[0] << 24) | ((unsigned int)i[1] << 16) |
> > ((unsigned int)i[2] << 8) | (unsigned int)i[3];
>
> > and c.f would have the value I want (in this example
> > 23458486419456.000000)
>
> > Is there some way I can achieve the result with perl?
>
> Well, assuming you want the *correct* result, not the one you get from
> the incorrect use of a union above,
The union does give me the correct result.
> and assuming you have the string
> from above (not four bytes):
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $numbers_line = <>;
> print "Numbers = $numbers_line";
>
> chomp $numbers_line;
> my @numbers = split /,/, $numbers_line;
>
> my $total = ($numbers[0] << 24) + ($numbers[1] << 16) +
> ($numbers[2] << 8) + $numbers[3];
>
> print "Total = $total\n";
>
> This prints:
>
> Numbers = 85,170,174,214
> Total = 1437249238
This is not correct. How would your method ever result in a floating
point
result? Your method will always produce an integer result.
> Why does print a different number? Because accessing c.f in the above C
> does not convert c.i to a float. A union uses the same memory for all of
> its members unchanged, treating that memory as if it were *already* of
> the appropriate type being accessed.
It is already of the appropriate type - that's why it works.
FYI I did get this to work using unpack as suggested by Brian McCauley
and Dr.Rudd.
Thanks!
-larry
------------------------------
Date: 9 May 2007 12:03:45 -0700
From: "Larry.Martell@gmail.com" <Larry.Martell@gmail.com>
Subject: Re: Converting 4 bytes to a float
Message-Id: <1178737425.789302.59500@y80g2000hsf.googlegroups.com>
On May 9, 12:37 am, Brian McCauley <nobul...@gmail.com> wrote:
> On May 9, 1:35 am, "Larry.Mart...@gmail.com" <Larry.Mart...@gmail.com>
> wrote:
>
>
>
> > I have a situation where I have to read in 4 values from a text file,
> > then convert that into a float that I can use for arithmetic.
>
> > For example, the file contains "85,170,174,214" - I read this in, and
> > split it into an array, @d
>
> > In C, I would do something akin to this:
>
> > union convert_float32 {
> > float f; // float view
> > unsigned int i; // uint32 view
> >} c;
> > unsigned char i[4];
> > char *q;
>
> > for (int j=0; j<4; j++) i[j] = (unsigned char)strtoul(d[j], &q, 10);
> > c.i = ((unsigned int)i[0] << 24) | ((unsigned int)i[1] << 16) |
> > ((unsigned int)i[2] << 8) | (unsigned int)i[3];
>
> > and c.f would have the value I want (in this example
> > 23458486419456.000000)
>
> Actually it would make more sense to get rid of the intermediate step.
>
> union convert_float32 {
> float f; // float view
> unsigned char i[4]; // 4 byte view
>
> } c;
> > Is there some way I can achieve the result with perl?
>
> You are looking for the pack() and unpack() functions.
Thanks - I got this to work using unpack().
-larry
------------------------------
Date: 9 May 2007 12:09:44 -0700
From: "Larry.Martell@gmail.com" <Larry.Martell@gmail.com>
Subject: Re: Converting 4 bytes to a float
Message-Id: <1178737784.142269.85270@q75g2000hsh.googlegroups.com>
On May 9, 12:51 am, "Dr.Ruud" <rvtol+n...@isolution.nl> wrote:
> Larry.Mart...@gmail.com schreef:
>
> > I have a situation where I have to read in 4 values from a text file,
> > then convert that into a float that I can use for arithmetic.
>
> First convert the values into a buffer of 4 bytes. Then use unpack().
> See perldoc -f unpack.
>
> $ perl -wle 'print length pack "f", 1.23'
> 4
>
> $ perl -wle 'print ord for split "", pack "f", 1.23'
> 164
> 112
> 157
> 63
>
> $ perl -wle 'print unpack "f", chr(164).chr(112).chr(157).chr(63)'
> 1.23000001907349
Thanks much - this is exactly what I was looking for. I had read about
pack and unpack,
but I had their functionality reversed. Your post clarified things for
me. I had to put the
bytes in in reverse order (to deal with the little-endiness of my
machine), but once I did
that I got the correct result:
$ perl -wle 'print unpack "f",chr(214).chr(174).chr(170).chr(85)'
23458486419456
Thanks again!
-larry
------------------------------
Date: Wed, 09 May 2007 16:00:13 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Converting 4 bytes to a float
Message-Id: <m2lkfxrcmq.fsf@local.wv-www.com>
"Larry.Martell@gmail.com" <Larry.Martell@gmail.com> writes:
> The union does give me the correct result.
...
> It is already of the appropriate type - that's why it works.
Oh sorry, I get it now! The int member of the union was so that you could
address the four bytes of the float individually. I was thinking the four
bytes were a 32-bit int, and you were trying to convert that to a float
with the union.
Sorry for the noise!
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: Wed, 9 May 2007 20:30:47 +0200
From: RU <ru@vakuum.de>
Subject: DBD::Oracle, tracing, and stored procedures...
Message-Id: <slrnf444qn.mki.urban@noisy.spielwiese.de>
Keywords: dbi dbd oracle tracing
Hi Perlers,
I'm trying to turn on tracing for a DB session using the oracle event 10046
by calling the stored procedure "sys.dbms_system.set_ev" (SCHEMA "sys",
PACKAGE "dbms_system", PROCEDURE "set_ev") using the DBI so far without
success.
Using sqlplus it's trivial:
> % sqlplus
> SQL> execute sys.dbms_system.set_ev(46, 53017, 10046, 4, ' ');
>
> PL/SQL procedure successfully completed.
now tracing is enabled.
I have tried all possible permutations that I can think of:
assuming:
I have a db-handle in $DBH,
$sess_id = 32
$serial = 53017;
this session exists, and my db-user had the necessary privs.
#======================================
# permutation 1:
#======================================
CODE>
my $sql = qq{
BEGIN
sys.dbms_system.set_ev(46, 53017, 10046, 4, '');
END;
};
$sql = $DBH->quote($sql);
my $rv = $DBH->do($sql);
RESULT>
DBD::Oracle::db do failed: ORA-00900: invalid SQL statement (DBD ERROR: OCIStmtExecute) [for Statement "'
BEGIN
sys.dbms_system.set_ev(46, 53017, 10046, 4, '''');
END;
'"] at ./trace.pl line 72.
#======================================
# permutation 2:
#======================================
CODE>
my $sql = qq{
BEGIN
sys.dbms_system.set_ev(:sid, :ser, :event, :level, :blank);
END;
};
my $sth = $DBH->prepare($sql);
$sth->bind_param(':sid', $sess_id);
$sth->bind_param(':ser', $serial);
$sth->bind_param(':event', $event);
$sth->bind_param(':level', $level);
$sth->bind_param(':blank', $blank);
my $rv = $sth->execute;
RESULT>
no error, but tracing is not enabled for the session corresponding to
SID/SERIAL#.
#======================================
# permutation 3:
#======================================
CODE>
my $sql = qq{
BEGIN
sys.dbms_system.set_ev(?, ?, ?, ?, ?);
END;
};
my $sth = $DBH->prepare($sql);
my $event = 10046;
my $blank = '';
$sth->bind_param(1, $sess_id);
$sth->bind_param(2, $serial);
$sth->bind_param(3, $event);
$sth->bind_param(4, $level);
$sth->bind_param(5, $blank);
my $rv = $sth->execute;
RESULT>
no error, but tracing is not enabled for the session corresponding to
SID/SERIAL#.
=========================================================================
anyone have an idea how to get this working???
thanks,
RU
------------------------------
Date: 9 May 2007 08:21:47 -0700
From: Fima Furman <efimfurman@gmail.com>
Subject: Re: document/literal style SOAP:Lite service
Message-Id: <1178724107.249439.241770@h2g2000hsg.googlegroups.com>
Ian,
Thank you so much for such a detailed reply! It's amazing this
information isn't really out there. Can you recommend a Soap::Lite
version I should use? The latest non-experimental version is 0.60a,
however there are versions up to .69 available.
Thanks, again
Fima.
On May 9, 10:28 am, Ian Wilson <scoblo...@infotop.co.uk> wrote:
> SOAP::Lite really only supports RPC/Encoded. The SOAP::Lite
> documentation mentions partial support for Doc/Literal but I believe
> this never advanced beyond some rudimentary and partial support.
------------------------------
Date: Wed, 09 May 2007 17:19:04 +0100
From: Ian Wilson <scobloke2@infotop.co.uk>
Subject: Re: document/literal style SOAP:Lite service
Message-Id: <4641f47c$0$6954$fa0fcedb@news.zen.co.uk>
Fima Furman wrote:
>
> On May 9, 10:28 am, Ian Wilson <scoblo...@infotop.co.uk> wrote:
>
>> SOAP::Lite really only supports RPC/Encoded. The SOAP::Lite
>> documentation mentions partial support for Doc/Literal but I
>> believe this never advanced beyond some rudimentary and partial
>> support.
>
> Thank you so much for such a detailed reply! It's amazing this
> information isn't really out there.
Yes, I spent *many* days struggling with this issue and explored many
dead ends. Useful tools included SOAP::Lite's +trace option, Wireshark
and an XML prettyprinter/indenter.
> Can you recommend a Soap::Lite version I should use? The latest
> non-experimental version is 0.60a, however there are versions up to
> .69 available.
Here's what I am using ...
Unix Server:
$ perl -MSOAP::Lite -e 'print "$SOAP::Lite::VERSION\n"'
0.55
Development PC (WinXP):
C:\> perl -MSOAP::Lite -e "print qq($SOAP::Lite::VERSION\n)"
0.55
Looks like I'm using rather old versions :-o
I recommend you use whatever comes as standard on your platform. If it
doesn't work, I'd upgrade to the latest stable version available.
------------------------------
Date: 9 May 2007 08:29:47 -0700
From: Mr P <MisterPerl@gmail.com>
Subject: I'd like to create an array of unique values
Message-Id: <1178724587.515336.158820@y80g2000hsf.googlegroups.com>
Sounds just like keys %h don't it.
Starting with
$_ =
"cat
man
dog
mouse
man
man
";
I want to end up with
[cat man dog mouse]
My approach is:
my @s = split /\n/;
# unique-afy it!
my %s;
@s{@s} = @s;
@s = keys %s;
My PREFERENCE is to not have to involve both a hash and an array, and
also to not have to include a non-native lib module.
Is there a syntax I can use to just work with the hash and populate
the keys-only directly from the split? I don't really care what the
values are. I don't even care if they are undef.
Thank-You, Peristas,
MP
------------------------------
Date: Wed, 09 May 2007 15:38:14 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: I'd like to create an array of unique values
Message-Id: <1343qn6b341rabe@corp.supernews.com>
In article <1178724587.515336.158820@y80g2000hsf.googlegroups.com>,
Mr P <MisterPerl@gmail.com> wrote:
: Is there a syntax I can use to just work with the hash and populate
: the keys-only directly from the split? I don't really care what the
: values are. I don't even care if they are undef.
You can condense your code a little:
my %seen;
my @s = grep !$seen{$_}++, split /\n/;
Hope this helps,
Greg
--
When government forces people to help their neighbors, conscience
atrophies. When people are free to choose whether to help their
neighbors or not, conscience is strengthened.
-- Jacob G. Hornberger
------------------------------
Date: 9 May 2007 08:45:09 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: I'd like to create an array of unique values
Message-Id: <1178725509.247794.220170@n59g2000hsh.googlegroups.com>
On May 9, 11:29 am, Mr P <MisterP...@gmail.com> wrote:
> Sounds just like keys %h don't it.
>
> Starting with
>
> $_ =
> "cat
> man
> dog
> mouse
> man
> man
> ";
>
> I want to end up with
>
> [cat man dog mouse]
>
> My approach is:
>
> my @s = split /\n/;
> # unique-afy it!
> my %s;
> @s{@s} = @s;
> @s = keys %s;
>
> My PREFERENCE is to not have to involve both a hash and an array, and
> also to not have to include a non-native lib module.
>
> Is there a syntax I can use to just work with the hash and populate
> the keys-only directly from the split? I don't really care what the
> values are. I don't even care if they are undef.
I don't think you're going to get around using both a hash and an
array, without some serious mumbo-jumbo involving repeatedly looping
through the array as you're building it. That will be unreadable and
inefficient.
The "right" answer is:
my %h = map { $_ => 1 } split /\n/, $_;
my @s = keys %h;
If you're worried about having too many named variables, however, you
could sacrifice some readability by making the hash anonymous:
my @s = keys %{ {map { $_ => 1 } split /\n/, $_} };
Perhaps you should be asking yourself why you're making this
constraint, however.
Paul Lalli
------------------------------
Date: 09 May 2007 11:46:16 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: I'd like to create an array of unique values
Message-Id: <878xbyngon.fsf@mithril.chromatico.net>
>>>>> "P" == P <MisterPerl@gmail.com> writes:
P> my @s = split /\n/;
P> my %s;
P> @s{@s} = @s;
P> @s = keys %s;
P> My PREFERENCE is to not have to involve both a hash and an
P> array, and also to not have to include a non-native lib module.
Well, those are your two options.
You can use a hash and an array, or you can include a non-core module.
P> Is there a syntax I can use to just work with the hash and
P> populate the keys-only directly from the split? I don't really
P> care what the values are. I don't even care if they are undef.
my %s;
$s{$_}++ foreach (split /\n/);
keys %s;
Note that you're not really eliminating the array, though -- you're
just using it implicitly in the foreach loop.
Charlton
--
Charlton Wilbur
cwilbur@chromatico.net
------------------------------
Date: Wed, 09 May 2007 16:20:08 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: I'd like to create an array of unique values
Message-Id: <Ywm0i.18688$Vi6.8399@edtnps82>
Mr P wrote:
> Sounds just like keys %h don't it.
>
> Starting with
>
> $_ =
> "cat
> man
> dog
> mouse
> man
> man
> ";
>
> I want to end up with
>
> [cat man dog mouse]
>
> My approach is:
>
> my @s = split /\n/;
> # unique-afy it!
> my %s;
> @s{@s} = @s;
> @s = keys %s;
>
> My PREFERENCE is to not have to involve both a hash and an array, and
> also to not have to include a non-native lib module.
>
> Is there a syntax I can use to just work with the hash and populate
> the keys-only directly from the split? I don't really care what the
> values are. I don't even care if they are undef.
$ perl -le'
$_ =
"cat
man
dog
mouse
man
man
";
my @s = do {
my %unique;
grep !$unique{$_}++, split /\n/;
};
print for @s;
'
cat
man
dog
mouse
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
------------------------------
Date: Wed, 09 May 2007 17:15:12 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: I'd like to create an array of unique values
Message-Id: <Akn0i.14644$rm.11025@trndny03>
Mr P wrote:
> Starting with
> $_ =
> "cat
> man
> dog
> mouse
> man
> man
> ";
>
> I want to end up with
>
> [cat man dog mouse]
>
> My approach is:
>
> my @s = split /\n/;
> # unique-afy it!
> my %s;
> @s{@s} = @s;
You can split() directly into a hash slice without reading the words into an
array first:
@s{split/\n/} = undef;
> @s = keys %s;
But that doesn't save you from still using keys() to get the list of unique
words.
jue
------------------------------
Date: 9 May 2007 10:17:13 -0700
From: Jorge <awkster@yahoo.com>
Subject: Re: I'd like to create an array of unique values
Message-Id: <1178731033.227664.190610@w5g2000hsg.googlegroups.com>
On May 9, 8:29 am, Mr P <MisterP...@gmail.com> wrote:
> Sounds just like keys %h don't it.
>
> Starting with
>
> $_ =
> "cat
> man
> dog
> mouse
> man
> man
> ";
>
> I want to end up with
>
> [cat man dog mouse]
>
> My approach is:
>
> my @s = split /\n/;
> # unique-afy it!
> my %s;
> @s{@s} = @s;
> @s = keys %s;
>
> My PREFERENCE is to not have to involve both a hash and an array, and
> also to not have to include a non-native lib module.
>
> Is there a syntax I can use to just work with the hash and populate
> the keys-only directly from the split? I don't really care what the
> values are. I don't even care if they are undef.
>
> Thank-You, Peristas,
>
> MP
using arrays only --
my @arr = qw(cat man dog mouse man man);
my $prev = 'none';
my @sorted_arr = sort(@arr);
my @uniq_arr = grep($_ ne $prev && ($prev = $_), @sorted_arr);
foreach my $uniq(@uniq_arr){
print $uniq, "\n";
}
cat
dog
man
mouse
------------------------------
Date: Wed, 09 May 2007 11:51:00 -0700
From: Purl Gurl <purlgurl@purlgurl.net>
Subject: Re: I'd like to create an array of unique values
Message-Id: <4uWdnaDVU7-Whd_bnZ2dnUVZ_q6vnZ2d@giganews.com>
Mr P wrote:
(snipped)
> $_ =
> "cat
> man
> dog
> mouse
> man
> man
> ";
> I want to end up with
> [cat man dog mouse]
> My PREFERENCE is to not have to involve both a hash and an array....
> Is there a syntax I can use to just work with the hash and populate....
This example is written precisely per parameters.
Purl Gurl
#!perl
$string =
"cat
man
dog
mouse
man
man
";
for (1 .. ($string =~ tr/\n//))
{
$variable = substr ($string, 0, index($string, "\n") + 1, "");
chomp ($variable);
$Hash{$variable}++;
}
foreach $key (keys %Hash)
{ print "$key\n"; }
PRINTED RESULTS:
mouse
man
cat
dog
------------------------------
Date: 9 May 2007 08:14:12 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Please give me a good "rule-of-thumb" for back-slashing in character classes
Message-Id: <1178723651.938407.63140@o5g2000hsb.googlegroups.com>
On May 9, 11:06 am, Mr P <MisterP...@gmail.com> wrote:
> It *seems like* any character other than "]" or "\", or in some cases
> "-", in a character class, would be interpreted "in-situ", as that
> exact character. But I get confusing error messages so often, that I
> usual just revert to a policy of "backslash everything" when in-doubt.
>
> I would appreciate a (hopefully one-sentance so I can remember it)
> guide/rule on what/when to backslash within character classes?
>
> [\$\#\@\*\=\-\)]
>
> or
>
> [$#Q*=-)]
>
> ?
First thing you need to understand regular expressions undergo
interpolation just like double-quoted strings, before the RegExp
parser ever gets a hold of the pattern. That means that Perl is
searching for $ @ and \, even though those aren't special in a
character class. So if your pattern contains those, they need to be
backslashed.
Second, the characters ], -, and ^ are special in a character class.
(Technically, ^ is only special if its the first character, however).
Those three therefore need to be backslashed as well.
Third, whatever your regexp delimiter is needs to be backslashed.
"Normally", that's the forward-slash, but you can choose any non-
alphanumeric as your delimiter.
I think that's about it. So your analogy is:
$ @ \ ] - ^ and sometimes /
Paul Lalli
------------------------------
Date: 9 May 2007 08:33:46 -0700
From: Mr P <MisterPerl@gmail.com>
Subject: Re: Please give me a good "rule-of-thumb" for back-slashing in character classes
Message-Id: <1178724826.168794.221190@p77g2000hsh.googlegroups.com>
On May 9, 11:14 am, Paul Lalli <mri...@gmail.com> wrote:
> On May 9, 11:06 am, Mr P <MisterP...@gmail.com> wrote:
>
> > It *seems like* any character other than "]" or "\", or in some cases
> > "-", in a character class, would be interpreted "in-situ", as that
> > exact character. But I get confusing error messages so often, that I
> > usual just revert to a policy of "backslash everything" when in-doubt.
>
> > I would appreciate a (hopefully one-sentance so I can remember it)
> > guide/rule on what/when to backslash within character classes?
>
> > [\$\#\@\*\=\-\)]
>
> > or
>
> > [$#Q*=-)]
>
> > ?
>
> First thing you need to understand regular expressions undergo
> interpolation just like double-quoted strings, before the RegExp
> parser ever gets a hold of the pattern. That means that Perl is
> searching for $ @ and \, even though those aren't special in a
> character class. So if your pattern contains those, they need to be
> backslashed.
>
> Second, the characters ], -, and ^ are special in a character class.
> (Technically, ^ is only special if its the first character, however).
> Those three therefore need to be backslashed as well.
>
> Third, whatever your regexp delimiter is needs to be backslashed.
> "Normally", that's the forward-slash, but you can choose any non-
> alphanumeric as your delimiter.
>
> I think that's about it. So your analogy is:
> $ @ \ ] - ^ and sometimes /
>
> Paul Lalli
Thank-You Paul. I realize that sometimes ^ is special (as you point
out, in the beginning). But I have used $ with no \ sucessfully. So at
least those two, I think, go on the "sometimes" side:
... and sometimes ^,$ and /
perhaps?
Good start! My only concern is that I'm afeared that the "sometimes"
part will be de-facto backslashed by me to avoiud potential errors.
But maybe the sometimes parts can be easily categorized?
------------------------------
Date: 9 May 2007 08:38:49 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Please give me a good "rule-of-thumb" for back-slashing in character classes
Message-Id: <1178725129.863816.246350@w5g2000hsg.googlegroups.com>
On May 9, 11:33 am, Mr P <MisterP...@gmail.com> wrote:
> On May 9, 11:14 am, Paul Lalli <mri...@gmail.com> wrote:
> > On May 9, 11:06 am, Mr P <MisterP...@gmail.com> wrote:
>
> > > It *seems like* any character other than "]" or "\", or in some cases
> > > "-", in a character class, would be interpreted "in-situ", as that
> > > exact character. But I get confusing error messages so often, that I
> > > usual just revert to a policy of "backslash everything" when in-doubt.
>
> > > I would appreciate a (hopefully one-sentance so I can remember it)
> > > guide/rule on what/when to backslash within character classes?
>
> > > [\$\#\@\*\=\-\)]
>
> > > or
>
> > > [$#Q*=-)]
>
> > > ?
>
> > First thing you need to understand regular expressions undergo
> > interpolation just like double-quoted strings, before the RegExp
> > parser ever gets a hold of the pattern. That means that Perl is
> > searching for $ @ and \, even though those aren't special in a
> > character class. So if your pattern contains those, they need to be
> > backslashed.
>
> > Second, the characters ], -, and ^ are special in a character class.
> > (Technically, ^ is only special if its the first character, however).
> > Those three therefore need to be backslashed as well.
>
> > Third, whatever your regexp delimiter is needs to be backslashed.
> > "Normally", that's the forward-slash, but you can choose any non-
> > alphanumeric as your delimiter.
>
> > I think that's about it. So your analogy is:
> > $ @ \ ] - ^ and sometimes /
>
> Thank-You Paul. I realize that sometimes ^ is special (as you point
> out, in the beginning). But I have used $ with no \ sucessfully. So at
> least those two, I think, go on the "sometimes" side:
>
> ... and sometimes ^,$ and /
>
> perhaps?
>
> Good start! My only concern is that I'm afeared that the "sometimes"
> part will be de-facto backslashed by me to avoiud potential errors.
> But maybe the sometimes parts can be easily categorized?
Sure.
$ and @ need to be backlashed if they're followed by anything that
Perl could consider to be a variable - whether it's a user-defined
variable or a built-in variable. That means that [fo$,] will need the
$ escaped, because $, is a valid variable.
/ needs to be backslashed if and only if it is used as the delimiter
to the regexp. If it is not, it does not need to be backslashed, but
whatever *is* the delimiter does.
^ needs to be backslashed if it is the first character of the
character class.
Paul Lalli
------------------------------
Date: Wed, 09 May 2007 11:05:10 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: Please give me a good "rule-of-thumb" for back-slashing in character classes
Message-Id: <2eWdnbW-edTKkN_bnZ2dnUVZ_qyjnZ2d@comcast.com>
Paul Lalli wrote:
> Second, the characters ], -, and ^ are special in a character class.
> (Technically, ^ is only special if its the first character, however).
Don't forget the other two exceptions:
] is not special if it's the first character.
- is not special if it's the first character.
-Joe
------------------------------
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 V11 Issue 421
**************************************