[17828] in Perl-Users-Digest
Perl-Users Digest, Issue: 5248 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 4 22:59:08 2001
Date: Thu, 4 Jan 2001 19:58:14 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <978667094-v9-i5248@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 4 Jan 2001 Volume: 9 Number: 5248
Today's topics:
Serial Number script?! Help! <malpaine@earthlink.net>
Re: Serial Number script?! Help! (mountainarts)
Re: Serial Number script?! Help! <sunymoon@geocities.com>
Re: Serial Number script?! Help! (Martien Verbruggen)
Re: Serial Number script?! Help! dtbaker_dejanews@my-deja.com
Setting environment <Hajo.Wuellner@ascad.de>
setting variables in %ENV <Hajo.Wuellner@ascad.de>
setting variables in %ENV <Hajo.Wuellner@ascad.de>
sig JAPH query (was Re: PERLLIB,PERL5LIB - How to unset <nospam@nospam.com>
Re: sig JAPH query (was Re: PERLLIB,PERL5LIB - How to u (Martien Verbruggen)
Re: sig JAPH query (was Re: PERLLIB,PERL5LIB - How to u (Abigail)
Someone using MacPerl and WebSTAR ? <claudej@videotron.ca>
Re: Someone using MacPerl and WebSTAR ? <bart.lateur@skynet.be>
Sorting a Text file Database with perl jamesdandy@my-deja.com
Re: Sorting a Text file Database with perl <kstep@pepsdesign.com>
Re: Sorting a Text file Database with perl <W.Hielscher@mssys.com>
Specify source port with IO::Socket? kendelbanks@my-deja.com
Re: Specify source port with IO::Socket? (Colin Watson)
Re: Specify source port with IO::Socket? (Steven Smolinski)
Split String by Character, Except Between.... <no@spam.com>
Re: Split String by Character, Except Between.... <berndt.zeitler@tu-berlin.de>
Re: Split String by Character, Except Between.... <theaney@toadmail.toad.net>
Re: Split String by Character, Except Between.... <mjcarman@home.com>
Re: Stock Quote Script <nospam@nospam.com>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 29 Dec 2000 19:45:30 GMT
From: macman <malpaine@earthlink.net>
Subject: Serial Number script?! Help!
Message-Id: <B672376D.361%malpaine@earthlink.net>
Hi...I'm a long-time C/C++ programmer with next to zero perl experience. I'm
looking to write a fairly simple script that will help me with online order
processing. Basically, the user will select "Purchase" from one of my
program's menus, and their website will load a URL like this:
- the url would look like this:
http://www.mysite.com/cgi-bin/myscript.cgi?id=idstring&email=email@domain.co
m
idstring is a unique identifier for the user's computer. Keep in mind it's a
piece of Mac software and my identifier is nothing like Microsoft's UUIDs on
Windows. I custom generate them with my own algorithm.
email is their email address, obviously.
Next, I haven't even thought of a serial number algorithm, but for instance,
let's say the algorithm is very simple, as I don't want to spend any of my
time on copy protection. All I want to do is:
- Serial number begins with a constant string like "PROG4-". Next, I take
the first 12 characters of their email address. If it's less than 12, pad it
with characters from idstring until there are exactly 12 characters. Then, I
want to perform a character computation like this (how I'd do it in C):
for (int i=0;i<12;i++)
{
if (serialnumber[i] + idstring[i] > 255)
serialnumber[i] = serialnumber[i] - idstring[i];
else
serialnumber[i] = serialnumber[i] + idstring[i];
}
Once the script has calculated a serial number, write it to a file along
with the email address, IP, and then output some HTML.
I've looked through many Perl examples, and while I understand most of them,
I have never found one that would help me do this. I'm in a time crunch and
I know this script should be _really_ simple. I can't figure it out and my
ISP won't let me run C programs.
Any ideas on how I could write this script? Examples of similar scripts? Any
help at all?
TIA,
Benson
------------------------------
Date: Fri, 29 Dec 2000 20:40:49 GMT
From: mountainarts@altavista.com (mountainarts)
Subject: Re: Serial Number script?! Help!
Message-Id: <3a4df65e.22836987@news.dreamsoft.com>
macman <malpaine@earthlink.net> wrote:
>Hi...I'm a long-time C/C++ programmer with next to zero perl experience. I'm
>looking to write a fairly simple script that will help me with online order
>processing. Basically, the user will select "Purchase" from one of my
>program's menus, and their website will load a URL like this:
>
>- the url would look like this:
>http://www.mysite.com/cgi-bin/myscript.cgi?id=idstring&email=email@domain.co
>m
>
>idstring is a unique identifier for the user's computer. Keep in mind it's a
>piece of Mac software and my identifier is nothing like Microsoft's UUIDs on
>Windows. I custom generate them with my own algorithm.
>
>email is their email address, obviously.
>
>Next, I haven't even thought of a serial number algorithm, but for instance,
>let's say the algorithm is very simple, as I don't want to spend any of my
>time on copy protection. All I want to do is:
>
>- Serial number begins with a constant string like "PROG4-". Next, I take
>the first 12 characters of their email address. If it's less than 12, pad it
>with characters from idstring until there are exactly 12 characters. Then, I
>want to perform a character computation like this (how I'd do it in C):
>
>for (int i=0;i<12;i++)
>{
> if (serialnumber[i] + idstring[i] > 255)
> serialnumber[i] = serialnumber[i] - idstring[i];
> else
> serialnumber[i] = serialnumber[i] + idstring[i];
>}
>
>Once the script has calculated a serial number, write it to a file along
>with the email address, IP, and then output some HTML.
>
>I've looked through many Perl examples, and while I understand most of them,
>I have never found one that would help me do this. I'm in a time crunch and
>I know this script should be _really_ simple. I can't figure it out and my
>ISP won't let me run C programs.
>
>Any ideas on how I could write this script? Examples of similar scripts? Any
>help at all?
>
>TIA,
>Benson
try something like this (close to the spirit of c )
note substr, chr, ord
#!/usr/bin/perl -w
main ();
sub main {
$serialnumber = "ab3256789012";
$idstring = "123456789012";
$i = 0;
for ($i=0;$i<12;$i++) {
print "ascii value serialnumber[$i] =
",ord(substr($serialnumber,$i,1)),"\n";
print "char serialnumber[$i] =
",substr($serialnumber,$i,1),"\n";
if ( ord(substr($serialnumber,$i,1)) +
ord(substr($serialnumber,$i,1)) > 255) {
substr($serialnumber,$i,1) =
chr(ord(substr($serialnumber,$i,1)) -
ord(substr($idstring,$i,1)));
}
}
}
------------------------------
Date: Mon, 01 Jan 2001 19:30:34 GMT
From: "Sean" <sunymoon@geocities.com>
Subject: Re: Serial Number script?! Help!
Message-Id: <uV446.113781$7I.11517166@typhoon.nyroc.rr.com>
Think about this...
User enters their email address when they open the shop cart. The Perl
program assigns an ID number to that email. The cart then shoves the user
off to the shopping part. Whenever the user wants to return, he enters the
email and is off to the cart with his id #.
A simple ASCII flowchart *g*:
User Opens mysite.com -> User Clicks on Shop Cart -> User sees: "Enter
E-mail address" -> User Enters Email -> Shop Cart assigns ID -> Cart sends
user to shop -> User adds an item to his cart.
The links would be, maybe...
<form action="/cgi-bin/shop.cgi" action="post">
<input type="textbox" value="1" name="quantity">
<input type="hidden" value="idnumber" name="id">
<input type="hidden" value="productid" name="prodid">
<input type="submit" name="submit" value="Add item to card">Widget
</form>
That's the hard part -- getting the information to be passed from cart
addition to cart addition -- the rest is just hammering out code.
By the way, can't CGI scripts be writtin in C/C++? Might want to look into
that.
--
Sean S. -:- ICQ: 1826323
(Email: sunymoon <AT> GeoCities >DOT< com )
macman wrote in message ...
>Hi...I'm a long-time C/C++ programmer with next to zero perl experience.
I'm
>looking to write a fairly simple script that will help me with online order
>processing. Basically, the user will select "Purchase" from one of my
>program's menus, and their website will load a URL like this:
>
>- the url would look like this:
>http://www.mysite.com/cgi-bin/myscript.cgi?id=idstring&email=email@domain.c
o
>m
>
>idstring is a unique identifier for the user's computer. Keep in mind it's
a
>piece of Mac software and my identifier is nothing like Microsoft's UUIDs
on
>Windows. I custom generate them with my own algorithm.
>
>email is their email address, obviously.
>
>Next, I haven't even thought of a serial number algorithm, but for
instance,
>let's say the algorithm is very simple, as I don't want to spend any of my
>time on copy protection. All I want to do is:
>
>- Serial number begins with a constant string like "PROG4-". Next, I take
>the first 12 characters of their email address. If it's less than 12, pad
it
>with characters from idstring until there are exactly 12 characters. Then,
I
>want to perform a character computation like this (how I'd do it in C):
>
>for (int i=0;i<12;i++)
>{
> if (serialnumber[i] + idstring[i] > 255)
> serialnumber[i] = serialnumber[i] - idstring[i];
> else
> serialnumber[i] = serialnumber[i] + idstring[i];
>}
>
>Once the script has calculated a serial number, write it to a file along
>with the email address, IP, and then output some HTML.
>
>I've looked through many Perl examples, and while I understand most of
them,
>I have never found one that would help me do this. I'm in a time crunch and
>I know this script should be _really_ simple. I can't figure it out and my
>ISP won't let me run C programs.
>
>Any ideas on how I could write this script? Examples of similar scripts?
Any
>help at all?
>
>TIA,
>Benson
>
------------------------------
Date: Tue, 2 Jan 2001 09:52:23 +1100
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: Serial Number script?! Help!
Message-Id: <slrn9522h7.6lr.mgjv@martien.heliotrope.home>
[removed non-existing group comp.lang.perl, and refuse to post to
alt.perl if I can help it]
[original upside down quoting preserved]
[note f'ups]
On Mon, 01 Jan 2001 19:30:34 GMT,
Sean <sunymoon@geocities.com> wrote:
> Think about this...
And you, think about this:
> By the way, can't CGI scripts be writtin in C/C++? Might want to look into
> that.
Yes, they can. And in almost any other programming language. Since you
already seem to know this, you also know that it is OFFTOPIC for any
perl group. So, do NOT answer these questions here, except to tell
people where to go.
Furthermore: if you reply to a post, quote the
post BEFORE your reply, and curt it down to a relevant size.
Original bottom-quoted post follows, cut down to size by me.
> macman wrote in message ...
>>Hi...I'm a long-time C/C++ programmer with next to zero perl experience.
Then write your CGI programs in C or C++. There are libraries available
out there that can help you with that.
>>I'm looking to write a fairly simple script that will help me with
>>online order processing. Basically, the user will select "Purchase"
>>from one of my program's menus, and their website will load a URL like
>>this:
First, you need to find out what CGI is, and then, you need to post CGI
questions to a group that cares about it. Somewhere in
comp.infosystems.www.* would be a good place.
However, if you're only interested in _finding_ code, as opposed to
_writing_ it, you should just use the Web (www.cgi-resources.com might
be a good place to start).
Martien
--
Martien Verbruggen | The Second Law of Thermodenial: In
Interactive Media Division | any closed mind the quantity of
Commercial Dynamics Pty. Ltd. | ignorance remains constant or
NSW, Australia | increases.
------------------------------
Date: Thu, 04 Jan 2001 03:21:07 GMT
From: dtbaker_dejanews@my-deja.com
Subject: Re: Serial Number script?! Help!
Message-Id: <930q6t$bmg$1@nnrp1.deja.com>
In article <B672376D.361%malpaine@earthlink.net>,
macman <malpaine@earthlink.net> wrote:
> Next, I haven't even thought of a serial number algorithm,
---------------
so what are you actually asking for help on?
a random number generator or form data management?
or perhaps security holes and payment methods?
D
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 4 Jan 2001 15:11:21 +0100
From: "HajoW" <Hajo.Wuellner@ascad.de>
Subject: Setting environment
Message-Id: <3a548490$0$8782$4dbef881@businessnews.de.uu.net>
Hello,
I want to sent environment variables for a programm that I start from a perl
script.
Here a small example (env.pl).
%ENV{DUMMY}=1;
system("cmd");
When I start this on NT by double clicking on the script a command
interpreter is started.
When I then type in the command
echo %DUMMY%
the result is
1
on my system with perl 5.005_03 and
%DUMMY%
on other systems with perl 5.6.0.
Isn't it possible to set the environment with perl 5.6.0 or has this another
reason?
tia
--
Hajo
------------------------------
Date: Thu, 4 Jan 2001 15:36:30 +0100
From: "HajoW" <Hajo.Wuellner@ascad.de>
Subject: setting variables in %ENV
Message-Id: <3a548a75$0$8781$4dbef881@businessnews.de.uu.net>
Hello,
I want to sent environment variables for a programm that
I start from a perl script. Here a small example (env.pl).
$ENV{DUMMY}=1;
system("cmd");
When I start this on NT by double clicking on the script
a command interpreter is started. When I type in the command
echo %DUMMY%
the result is
1
on my system with perl 5.005_03 and
%DUMMY%
on other systems with perl 5.6.0.
Isn't it possible to set the environment with perl 5.6.0 or
has this another reason?
tia
--
Hajo
------------------------------
Date: Thu, 4 Jan 2001 15:37:50 +0100
From: "HajoW" <Hajo.Wuellner@ascad.de>
Subject: setting variables in %ENV
Message-Id: <3a548ac5$0$8783$4dbef881@businessnews.de.uu.net>
Hello,
I want to sent environment variables for a programm that
I start from a perl script. Here a small example (env.pl).
$ENV{DUMMY}=1;
system("cmd");
When I start this on NT by double clicking on the script
a command interpreter is started. When I type in the command
echo %DUMMY%
the result is
1
on my system with perl 5.005_03 and
%DUMMY%
on other systems with perl 5.6.0.
Isn't it possible to set the environment with perl 5.6.0 or
has this another reason?
tia
--
Hajo
------------------------------
Date: 4 Jan 2001 04:35:55 GMT
From: The WebDragon <nospam@nospam.com>
Subject: sig JAPH query (was Re: PERLLIB,PERL5LIB - How to unset inside perl script?)
Message-Id: <930ujb$giq$0@216.155.32.33>
In article <slrn956d97.f94.abigail@tsathoggua.rlyeh.net>,
abigail@foad.org wrote:
| Abigail
| --
| :$:=~s:$":Just$&another$&:;$:=~s:
| :Perl$"Hacker$&:;chop$:;print$:#:
just curious, but this wouldn't compile until I changed it to this:
;$:=~s:$":Just$&another$&:;$:=~s:
:Perl$"Hacker$&:;chop$:;print$:#:
(only one character changed)
using 5.004 here.. something changed later on to allow that to compile
as it was?
--
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: Thu, 4 Jan 2001 19:27:52 +1100
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: sig JAPH query (was Re: PERLLIB,PERL5LIB - How to unset inside perl script?)
Message-Id: <slrn958d08.edk.mgjv@martien.heliotrope.home>
On 4 Jan 2001 04:35:55 GMT,
The WebDragon <nospam@nospam.com> wrote:
> In article <slrn956d97.f94.abigail@tsathoggua.rlyeh.net>,
> abigail@foad.org wrote:
>
> | Abigail
> | --
> | :$:=~s:$":Just$&another$&:;$:=~s:
> | :Perl$"Hacker$&:;chop$:;print$:#:
>
> just curious, but this wouldn't compile until I changed it to this:
>
> ;$:=~s:$":Just$&another$&:;$:=~s:
>:Perl$"Hacker$&:;chop$:;print$:#:
>
> (only one character changed)
>
> using 5.004 here.. something changed later on to allow that to compile
> as it was?
The original works fine for me in perl 5.004_05
$ /opt/perl5.00405/bin/perl -v
This is perl, version 5.004_05 built for i686-linux
[snip]
$ /opt/perl5.00405/bin/perl
:$:=~s:$":Just$&another$&:;$:=~s:
:Perl$"Hacker$&:;chop$:;print$:#:
__END__
Just another Perl Hacker
$
It should be equivalent to something like:
$: =~ s/$"/Just$&another$&/; $: =~ s/\n/Perl$"Hacker$&/;
chop $:; print $:
Maybe empty labels weren't allowed some time before 5.004_05? Any error
messages?
Martien
--
Martien Verbruggen |
Interactive Media Division | Unix is user friendly. It's just
Commercial Dynamics Pty. Ltd. | selective about its friends.
NSW, Australia |
------------------------------
Date: 4 Jan 2001 08:47:06 GMT
From: abigail@foad.org (Abigail)
Subject: Re: sig JAPH query (was Re: PERLLIB,PERL5LIB - How to unset inside perl script?)
Message-Id: <slrn958e4a.hql.abigail@tsathoggua.rlyeh.net>
Martien Verbruggen (mgjv@tradingpost.com.au) wrote on MMDCLXXXIII
September MCMXCIII in <URL:news:slrn958d08.edk.mgjv@martien.heliotrope.home>:
'' On 4 Jan 2001 04:35:55 GMT,
'' The WebDragon <nospam@nospam.com> wrote:
'' > In article <slrn956d97.f94.abigail@tsathoggua.rlyeh.net>,
'' > abigail@foad.org wrote:
'' >
'' > | Abigail
'' > | --
'' > | :$:=~s:$":Just$&another$&:;$:=~s:
'' > | :Perl$"Hacker$&:;chop$:;print$:#:
'' >
'' > just curious, but this wouldn't compile until I changed it to this:
'' >
'' > ;$:=~s:$":Just$&another$&:;$:=~s:
'' >:Perl$"Hacker$&:;chop$:;print$:#:
'' >
'' > (only one character changed)
'' >
'' > using 5.004 here.. something changed later on to allow that to compile
'' > as it was?
''
'' The original works fine for me in perl 5.004_05
''
'' $ /opt/perl5.00405/bin/perl -v
''
'' This is perl, version 5.004_05 built for i686-linux
''
'' [snip]
'' $ /opt/perl5.00405/bin/perl
'' :$:=~s:$":Just$&another$&:;$:=~s:
'' :Perl$"Hacker$&:;chop$:;print$:#:
'' __END__
'' Just another Perl Hacker
'' $
''
'' It should be equivalent to something like:
''
'' $: =~ s/$"/Just$&another$&/; $: =~ s/\n/Perl$"Hacker$&/;
'' chop $:; print $:
''
'' Maybe empty labels weren't allowed some time before 5.004_05? Any error
'' messages?
Hint 1: It has nothing to do with labels.
Hint 2: Perl makes exceptions to characters in certain places of a file.
Hint 3: It's explained in the manual.
Abigail
--
sub f{sprintf'%c%s',$_[0],$_[1]}print f(74,f(117,f(115,f(116,f(32,f(97,
f(110,f(111,f(116,f(104,f(0x65,f(114,f(32,f(80,f(101,f(114,f(0x6c,f(32,
f(0x48,f(97,f(99,f(107,f(101,f(114,f(10,q ff)))))))))))))))))))))))))
------------------------------
Date: Sat, 30 Dec 2000 12:28:04 -0500
From: claudej <claudej@videotron.ca>
Subject: Someone using MacPerl and WebSTAR ?
Message-Id: <B6738554.1C3BB%claudej@videotron.ca>
No newsgroup about WebStar product ?
If anyone has a link , please let me know.
If I use a CGI <!--#include virtual="/cgi-bin/aa.cgi"-->
nothing is passed to MacPerl, not even an error message.
If instead I use a text file, the content is included.
<!--#include virtual="/cgi-bin/aa.txt"-->
So , #include,virtual, and the path are OK
In SSI setting (WebSTAR Admin) I have "Allow unrestricted use of commands
for #EXEC and #INCLUDE "
And if I click on "click me" the cgi works fine. (But it's not an include,
it makes an open() and print "Hello"). So I need the include. The real cgi
will be a lot more complex.
_______________________________________________
<HTML><HEAD><TITLE>My title</TITLE></HEAD><BODY>
<A HREF="/cgi-bin/aa.cgi">click me</A><BR>
<!--#include virtual="/cgi-bin/aa.cgi"-->
<!--#echo var="DATE_LOCAL"-->
</BODY></HTML>
____________________ The aa.cgi ______________________
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "Hello";
Thanks in advance,
Claude Johnson
http://pages.infinit.net/kurodo/
------------------------------
Date: Sat, 30 Dec 2000 18:08:57 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Someone using MacPerl and WebSTAR ?
Message-Id: <d09s4t0tcl64hnidp0346n5ra3hti7b0ue@4ax.com>
claudej wrote:
>Someone using MacPerl and WebSTAR ?
>No newsgroup about WebStar product ?
>If anyone has a link , please let me know.
There's the CGI specific MacPerl mailing list:
<macperl-webcgi@macperl.org>. See:
<http://www.macperl.com/depts/mlist.html>
No, I don't use the Mac for CGI.
--
Bart.
------------------------------
Date: Thu, 04 Jan 2001 15:07:45 GMT
From: jamesdandy@my-deja.com
Subject: Sorting a Text file Database with perl
Message-Id: <9323jq$bnl$1@nnrp1.deja.com>
I have a text file DB with five fields. I need to extract three of the
fields sort by two of them and output the results.
"10|10|madmax|mmax@tdome.com|7"
So far I have tried reading the data into the an array which I then
split into variables bubble sort these on the appropriate field and
write the results to a new text file.
This seems much too complicated. Any suggestions?
Also I am currently getting a "name without value" syntax error. The
syntax is correct, it is the same syntax that works thirty lines up in
the program. Any advice?
Thnx,
Jim Dandy
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 4 Jan 2001 12:16:09 -0500
From: "Kurt Stephens" <kstep@pepsdesign.com>
Subject: Re: Sorting a Text file Database with perl
Message-Id: <932b39$r11$1@slb7.atl.mindspring.net>
<jamesdandy@my-deja.com> wrote in message
news:9323jq$bnl$1@nnrp1.deja.com...
> I have a text file DB with five fields. I need to extract three of the
> fields sort by two of them and output the results.
>
> "10|10|madmax|mmax@tdome.com|7"
>
> So far I have tried reading the data into the an array which I then
> split into variables bubble sort these on the appropriate field and
> write the results to a new text file.
>
> This seems much too complicated. Any suggestions?
Read perlfaq4 "How do I sort an array by (anything)?"
Below is a script that:
1) Reads in a text file
2) Splits the data, discarding unwanted fields
3) Sorts the data using two fields
4) Prints the results
# -- Begin Code --
use strict;
use warnings;
my @data;
# Parse and save each record
# Use an array slice for fields 0, 2 and 3
while (<DATA>) {
chomp;
push @data, [(split /\|/)[0,2,3]];
}
# Sort the records
my @sorted = sort {
my $test0 = $a->[0] <=> $b->[0]; # numeric on field 0
my $test1 = $a->[1] cmp $b->[1]; # alphabetic on field 1
$test0 ? $test0 : $test1;
} @data;
# Print the results
print join ("|", @$_), "\n" foreach @sorted;
__DATA__
12|10|zack|zack@abc.com|7
12|11|yvonne|yvonne@bcd.com|8
11|12|xavier|xavier@cde.com|9
11|13|walt|walt@def.com|10
10|14|vanna|vanna@efg.com|11
10|15|ulysses|ulysses@fgh.com|12
# -- End Code --
Here's a brief explanation of the tricky parts:
push @data, [(split /\|/)[0,2,3]];
This line creates a reference to an array slice containing elements 0, 2 and
3 of the list created by the split command. The result is pushed onto the
@data array. This could also be expressed as follows:
my @tmp = split /\|/;
my @slice = @tmp[0,2,3];
push @data, \@slice;
------------------
my @sorted = sort {
my $test0 = $a->[0] <=> $b->[0]; # numeric on field 0
my $test1 = $a->[1] cmp $b->[1]; # alphabetic on field 1
$test0 ? $test0 : $test1;
} @data;
This code performs a sort using two comparisons; a numeric sort on field 0
and an alphabetic sort on field 1. In other words, if the values for field
zero are not equal, sort by field 0 - otherwise sort by field 1.
------------------
This whole mess can also be expressed using a Schwartzian Transform as
below:
# -- Begin Code --
# Read, parse and sort the records
my @sorted = sort {
my $t = $a->[0] <=> $b->[0];
$t ? $t : $a->[1] cmp $b->[1];
} map {
chomp;
[(split /\|/)[0,2,3]]
} <DATA>;
# -- End Code --
> Also I am currently getting a "name without value" syntax error. The
> syntax is correct, it is the same syntax that works thirty lines up in
> the program. Any advice?
We can't debug code that we can't see. Please post the offending snippet of
code.
------------------------------
Date: Thu, 04 Jan 2001 18:25:20 +0100
From: Wolfgang Hielscher <W.Hielscher@mssys.com>
Subject: Re: Sorting a Text file Database with perl
Message-Id: <3A54B200.4942BAF2@mssys.com>
jamesdandy@my-deja.com schrieb:
> I have a text file DB with five fields. I need to extract three of the
> fields sort by two of them and output the results.
>
> "10|10|madmax|mmax@tdome.com|7"
>
> So far I have tried reading the data into the an array which I then
> split into variables bubble sort these on the appropriate field and
> write the results to a new text file.
>
> This seems much too complicated. Any suggestions?
>
You really _bubble_ sorted them?! Why?
My suggestion:
perldoc -f sort
perldoc -q sort
> Also I am currently getting a "name without value" syntax error. The
> syntax is correct, it is the same syntax that works thirty lines up in
> the program. Any advice?
My advice:
You supplied nearly no relevant information, see for example _my_
assumptions below. If you got problems with your code, then show us the
_relevant parts_ of your code, better strip down your code to a
_minimal_ and _running_ program that still displays the misbehaviour.
Moreover it's hard to believe that you really worked on that problem
because it's trivial (at least according to the informations you gave).
Go ahead, do some reading!
And before you get mad for just being pointed to read the docs, this one
(command-)line:
(Assumtions:
- Your "text file DB" is named data.txt
- You're interested in field 2, 5 and 3
- You want the lines being sorted by field 2 and 5
- You don't try this line on Windows
)
perl -lne 'push@_,join("|",(split/\|/)[1,4,2])."\n"}{print sort@_'
data.txt
Golf anyone?!
Cheers
Wolfgang
------------------------------
Date: Sun, 31 Dec 2000 10:25:55 GMT
From: kendelbanks@my-deja.com
Subject: Specify source port with IO::Socket?
Message-Id: <92n1ji$tgv$1@nnrp1.deja.com>
I'm writing a yahoo chat client in perl. I've written much of the c/s
code but ran in to a problem. Y! requires the same source port as a type
of authentication. How do I accomplish this with Socket or another way
in perl? All I have to do is send from the same port every send().
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: 31 Dec 2000 17:25:19 GMT
From: cjw44@flatline.org.uk (Colin Watson)
Subject: Re: Specify source port with IO::Socket?
Message-Id: <92nq5v$p8a$1@riva.ucam.org>
kendelbanks@my-deja.com wrote:
>I'm writing a yahoo chat client in perl. I've written much of the c/s
>code but ran in to a problem. Y! requires the same source port as a type
>of authentication. How do I accomplish this with Socket or another way
>in perl? All I have to do is send from the same port every send().
Use bind(). See the "UDP: Message Passing" section in 'perldoc perlipc'
(that particular example lets the kernel pick a port by using "port" 0).
HTH,
--
Colin Watson [cjw44@flatline.org.uk]
"Microsoft deprived consumers of software innovation that they very
well may have found valuable." - USA vs. MS findings, para. 410
------------------------------
Date: Sun, 31 Dec 2000 17:38:22 GMT
From: sjs@linux.ca (Steven Smolinski)
Subject: Re: Specify source port with IO::Socket?
Message-Id: <slrn94uvq6.ik.sjs@ragnar.stevens.gulch>
kendelbanks@my-deja.com <kendelbanks@my-deja.com> wrote:
> I'm writing a yahoo chat client in perl. I've written much of the c/s
> code but ran in to a problem. Y! requires the same source port as a type
> of authentication. How do I accomplish this with Socket or another way
> in perl? All I have to do is send from the same port every send().
To send from the same port, bind your socket to a port before you send
anything.
The perlipc manpage has an example of a UDP client using bind(), but
it specifies a port number of 0 (in order to let the kernel pick a
port); just specify your port instead. I'm assuming (always
dangerous) that instant messaging is UDP, but you can bind a TCP
socket to a port on an interface as well. Here's the bit from
perlipc:
-----
$iaddr = gethostbyname(hostname());
$proto = getprotobyname('udp');
$port = getservbyname('time', 'udp');
$paddr = sockaddr_in(0, $iaddr); # 0 means let kernel pick
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) || die "socket: $!";
bind(SOCKET, $paddr) || die "bind: $!";
-----
Enjoy!
--
Steven Smolinski => http://www.steven.cx/
------------------------------
Date: Thu, 04 Jan 2001 08:42:04 GMT
From: Adrian Jackson <no@spam.com>
Subject: Split String by Character, Except Between....
Message-Id: <8hd85tkl4ahbjcgpauic9kdodsdcdakjmg@4ax.com>
I'm writing a script to aesthetically restructure some old BASIC
source code. A typical line (string) looks like this:
1062 S%=HP/3/(1+RND(1)):HP=HP-S%:VA=0:PRINT"<204>HIT POINTS: -"S%;
One thing I'm doing is breaking the lines up by colons, and treating
the line number as a label. The end result would for the above
example look like:
1062:
S%=HP/3/(1+RND(1))
HP=HP-S%
VA=0
PRINT"<204>HIT POINTS: -"S%;
Currently, I use an extremely simple split function to break up the
lines:
@line_expressions = split /:/, $line_code;
Unfortunately, this also breaks up the quoted line, because within the
quoted text is a colon. So the end result for that portion of the
string would look like:
PRINT"<204>HIT POINTS
-"S%;
I don't want this to happen.
I want to split a string up by colons, EXCEPT for those colons which
fall between quotes. There may be more than one quoted portion of
text per string, and indeed, more than one quoted portion of text with
a colon in it, per string. Is there an elegant way to do this?
------------------------------
Date: Thu, 4 Jan 2001 12:07:44 +0100
From: "berndt zeitler" <berndt.zeitler@tu-berlin.de>
Subject: Re: Split String by Character, Except Between....
Message-Id: <931l5h$1c9$1@mamenchi.zrz.TU-Berlin.DE>
hi adrian,
i'm quite new at perl, but i found a solution. i'm sure there is a better
why to do this, but here's my version:
$line_code = '1062 S%=HP/3/(1+RND(1)):HP=HP-S%:VA=0:PRINT"<204>HIT
OINTS: -"S%;';
$line_code =~ s/"(.*):(.*)"/\"$1~~~~~$2\"/g;
@line_expressions = split /:/, $line_code;
$length = @line_expressions;
for ($i = 0; $i < $length; $i++){
$line_expressions[$i] =~ s/~~~~~/:/g;
print "$line_expressions[$i]\n";
}
i just substituted the colon between the double quotes by five tildes and
after splitting subtituted them back again.
ciao for now,
berndt
Adrian Jackson <no@spam.com> wrote in message
news:8hd85tkl4ahbjcgpauic9kdodsdcdakjmg@4ax.com...
> I'm writing a script to aesthetically restructure some old BASIC
> source code. A typical line (string) looks like this:
>
> 1062 S%=HP/3/(1+RND(1)):HP=HP-S%:VA=0:PRINT"<204>HIT POINTS: -"S%;
>
> One thing I'm doing is breaking the lines up by colons, and treating
> the line number as a label. The end result would for the above
> example look like:
>
> 1062:
> S%=HP/3/(1+RND(1))
> HP=HP-S%
> VA=0
> PRINT"<204>HIT POINTS: -"S%;
>
> Currently, I use an extremely simple split function to break up the
> lines:
>
> @line_expressions = split /:/, $line_code;
>
> Unfortunately, this also breaks up the quoted line, because within the
> quoted text is a colon. So the end result for that portion of the
> string would look like:
>
> PRINT"<204>HIT POINTS
> -"S%;
>
> I don't want this to happen.
>
> I want to split a string up by colons, EXCEPT for those colons which
> fall between quotes. There may be more than one quoted portion of
> text per string, and indeed, more than one quoted portion of text with
> a colon in it, per string. Is there an elegant way to do this?
>
------------------------------
Date: 04 Jan 2001 09:47:21 -0500
From: Tim Heaney <theaney@toadmail.toad.net>
Subject: Re: Split String by Character, Except Between....
Message-Id: <87ofxnqsqu.fsf@susie.watterson>
Adrian Jackson <no@spam.com> writes:
>
> I want to split a string up by colons, EXCEPT for those colons which
> fall between quotes. There may be more than one quoted portion of
> text per string, and indeed, more than one quoted portion of text with
> a colon in it, per string. Is there an elegant way to do this?
Instead of splitting, try the Text::ParseWords module
use Text::ParseWords;
@line_expressions = parse_line ':', 1, $line_code;
Hope this helps,
Tim
------------------------------
Date: Thu, 04 Jan 2001 07:50:37 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: Split String by Character, Except Between....
Message-Id: <3A547FAD.9117474B@home.com>
Adrian Jackson wrote:
>
> I want to split a string up by colons, EXCEPT for those
> colons which fall between quotes.
Betcha didn't know that you already have the answer! It's in the FAQ,
which is on your computer as part of the standard distribution of Perl.
Running 'perldoc -q split' from a command line would have found the
appropriate entry:
How can I split a [character] delimited string except when inside
[character]? (Comma-seperated files)
For future reference please check the documentation and the FAQ *before*
posting here. It's quicker, the answers are better, and it will keep you
from ticking off the regulars.
-mjc
------------------------------
Date: 30 Dec 2000 03:41:47 GMT
From: The WebDragon <nospam@nospam.com>
Subject: Re: Stock Quote Script
Message-Id: <92jlhr$ft8$7@216.155.32.187>
In article <9LK26.26348$bw.1744557@news.flash.net>, egwong@netcom.com
wrote:
| Have you searched CPAN?
|
| http://search.cpan.org/search?mode=module&query=quote
Drat. Finance::Quote requires 5.005 ... got my hopes all up. :)
--
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: 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 5248
**************************************