[24401] in Perl-Users-Digest
Perl-Users Digest, Issue: 6589 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu May 20 21:05:47 2004
Date: Thu, 20 May 2004 18:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 20 May 2004 Volume: 10 Number: 6589
Today's topics:
Re: Converting file-input argument as string <jurgenex@hotmail.com>
ISO two Perl idioms... <please_post@nomail.edu>
Re: ISO two Perl idioms... <tassilo.parseval@rwth-aachen.de>
Re: ISO two Perl idioms... <remorse@partners.org>
Re: ISO two Perl idioms... (Anno Siegel)
Re: ISO two Perl idioms... <usenet@morrow.me.uk>
Re: ISO two Perl idioms... <usenet@morrow.me.uk>
Re: ISO two Perl idioms... <dwall@fastmail.fm>
Re: ISO two Perl idioms... <please_post@nomail.edu>
Re: ISO two Perl idioms... <tassilo.parseval@rwth-aachen.de>
Re: iterating over arrays with map - problem <remorse@partners.org>
Re: iterating over arrays with map - problem (Anno Siegel)
Re: Meaning of Orthogonality (Anno Siegel)
Perl array manipulation questions <bryan@akanta.com>
Re: Perl array manipulation questions <usenet@morrow.me.uk>
Re: Perl array manipulation questions <usenet@morrow.me.uk>
Re: Perl array manipulation questions <jtc@shell.dimensional.com>
Re: Perl array manipulation questions <bryan@akanta.com>
Re: perl tcp server script works, but no data. (Kevin Collins)
Re: Perl vs PHP <cwilbur@mithril.chromatico.net>
Re: problem with references <bornslippy.SPAM.IS.NOT.OK@o2.pl>
Re: regexp to match time string <bmetcalf@nortelnetworks.com>
scoping problem <vze2mf4r@verizon.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 21 May 2004 00:58:18 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Converting file-input argument as string
Message-Id: <Kucrc.10872$r6.941@nwrddc03.gnilink.net>
Edward Wijaya wrote:
> $file = $ARGV[0];
> open(FILE,"<$file");
> @array = <FILE>;
> close(FILE);
> Is there anyway we can convert $file
> into strings (of file name)?
>> In so far you question doesn't make sense. What are you trying to
>> do?
>
> I would like to use that string [note: $file] as a name of
> output files (with some added string as index).
Ok, so what is the problem? For all practical purposes $file _is_ a string.
You can certainly append more text to it, you can split it, you can slice
and dice it any way you like, just like any other string.
Again, what is it you want to do and got stuck with?
jue
------------------------------
Date: Thu, 20 May 2004 18:11:15 +0000 (UTC)
From: bill <please_post@nomail.edu>
Subject: ISO two Perl idioms...
Message-Id: <c8isc3$k4d$1@reader2.panix.com>
There are a couple of situations that I encounter frequently, and
I wonder if there are established Perl idioms for dealing with them.
1. The problem is to read and discard lines from a file until a
condition is met (usually some regexp match), and then process
subsequent lines. E.g.
my $found;
while (<IN>) {
if (meets_condition($_)) {
$found = 1;
last;
}
}
die "Bad data\n" unless $found;
while (<IN>) {
# etc.
}
2. Finding the first position at which two strings differ.
sub first_different {
my $len = (sort map length, @_[0, 1])[-1];
my $n;
for (my $i = 0; $i < $len; ++$i) {
if (substr($_[0], $i, 1) ne substr($_[1], $i, 1)) {
$n = $i;
last;
}
}
return defined $n ? $n : -1;
}
Are there nice Perl idioms for either of these two tasks?
-bill
P.S. This is not golf, now! I'm *sure* that it is possible to use
some insanely arcane Perl constructs to make the above a *lot* more
succinct, but the results probably would not qualify as "idioms"
in my book. Idioms are pithy, but not terribly obscure. They
don't leave you scratching your head after several minutes of trying
to decrypt them. (I'd say that
select((select(FH), $|=1)[0]);
is the most obscure construct that I'd still be willing to call an
"idiom".)
--
NOTE: In my address everything before the period is backwards.
------------------------------
Date: Thu, 20 May 2004 20:39:49 +0200
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: ISO two Perl idioms...
Message-Id: <2h4cbqF983dkU1@uni-berlin.de>
Also sprach bill:
> There are a couple of situations that I encounter frequently, and
> I wonder if there are established Perl idioms for dealing with them.
> 2. Finding the first position at which two strings differ.
>
> sub first_different {
> my $len = (sort map length, @_[0, 1])[-1];
> my $n;
> for (my $i = 0; $i < $len; ++$i) {
> if (substr($_[0], $i, 1) ne substr($_[1], $i, 1)) {
> $n = $i;
> last;
> }
> }
> return defined $n ? $n : -1;
> }
>
> Are there nice Perl idioms for either of these two tasks?
There is the infamous xor trick for this one:
$a = "foobar";
$b = "foobAr";
($a ^ $b) =~ /^(\0*)/ and print length $1;
__END__
4
It makes use of the fact that xoring two identical values yields zero.
Otherwise something non-zero (1 when looking at each bit). So after
xoring you just have to find the first non-NULL character.
> P.S. This is not golf, now! I'm *sure* that it is possible to use
> some insanely arcane Perl constructs to make the above a *lot* more
> succinct, but the results probably would not qualify as "idioms"
> in my book. Idioms are pithy, but not terribly obscure.
I don't know whether the above qualifies as obscure or golf. It is
certainly used in golf once in a while. Nonetheless I think it can look
quite elegant to people having a certain affinity towards bitwise
operations.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Thu, 20 May 2004 14:47:30 -0400
From: Richard Morse <remorse@partners.org>
Subject: Re: ISO two Perl idioms...
Message-Id: <remorse-CAD3FA.14473020052004@plato.harvard.edu>
In article <c8isc3$k4d$1@reader2.panix.com>,
bill <please_post@nomail.edu> wrote:
> 1. The problem is to read and discard lines from a file until a
> condition is met (usually some regexp match), and then process
> subsequent lines. E.g.
>
> my $found;
> while (<IN>) {
> if (meets_condition($_)) {
> $found = 1;
> last;
> }
> }
>
> die "Bad data\n" unless $found;
>
> while (<IN>) {
> # etc.
> }
I think that you can do this with the '..' operator:
#!/usr/bin/perl
use strict;
use warnings;
while(<DATA>) {
if (m/now/ .. eof(DATA)) {
print $_;
}
}
__END__
this
is
a
test
now
i
will
print
This will still print the line that you are matching on, but you can
work with this...
HTH,
Ricky
--
Pukku
------------------------------
Date: 20 May 2004 19:44:11 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: ISO two Perl idioms...
Message-Id: <c8j1qb$4u4$1@mamenchi.zrz.TU-Berlin.DE>
Tassilo v. Parseval <tassilo.parseval@post.rwth-aachen.de> wrote in comp.lang.perl.misc:
> Also sprach bill:
>
> > There are a couple of situations that I encounter frequently, and
> > I wonder if there are established Perl idioms for dealing with them.
>
> > 2. Finding the first position at which two strings differ.
> >
> > sub first_different {
> > my $len = (sort map length, @_[0, 1])[-1];
> > my $n;
> > for (my $i = 0; $i < $len; ++$i) {
> > if (substr($_[0], $i, 1) ne substr($_[1], $i, 1)) {
> > $n = $i;
> > last;
> > }
> > }
> > return defined $n ? $n : -1;
> > }
> >
> > Are there nice Perl idioms for either of these two tasks?
>
> There is the infamous xor trick for this one:
>
> $a = "foobar";
> $b = "foobAr";
> ($a ^ $b) =~ /^(\0*)/ and print length $1;
> __END__
> 4
>
> It makes use of the fact that xoring two identical values yields zero.
> Otherwise something non-zero (1 when looking at each bit). So after
> xoring you just have to find the first non-NULL character.
>
> > P.S. This is not golf, now! I'm *sure* that it is possible to use
> > some insanely arcane Perl constructs to make the above a *lot* more
> > succinct, but the results probably would not qualify as "idioms"
> > in my book. Idioms are pithy, but not terribly obscure.
>
> I don't know whether the above qualifies as obscure or golf. It is
> certainly used in golf once in a while. Nonetheless I think it can look
> quite elegant to people having a certain affinity towards bitwise
> operations.
I was going to suggest the xor solution (note promotion from "infamous
trick" to "solution") if you hadn't. What I wonder is how it would
hold up with multibyte characters.
On a more general note, trie data structures have been invented to
tackle the prefix problem (or could have been invented for that
purpose, not knowing the actual history). There are at least two
trie modules on CPAN.
So, instead of an idiom, there's a trick and a couple of modules.
As to the other problem (skipping initial lines in a file), the
".." operator has also been suggested. I'd use it in a single
loop, somewhat like this:
while ( <DATA> ) {
next unless /ccc/ .. do { 0};
print;
}
The funny way of saying "0" (meaning forever as the second operand of
".."), "do { 0}" is necessary because ".." compares literal operands
to "$.". With this quirk, I wouldn't claim idiom status for this
either.
Anno
------------------------------
Date: Thu, 20 May 2004 19:45:07 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: ISO two Perl idioms...
Message-Id: <c8j1s2$qff$1@wisteria.csv.warwick.ac.uk>
Quoth Richard Morse <remorse@partners.org>:
> In article <c8isc3$k4d$1@reader2.panix.com>,
> bill <please_post@nomail.edu> wrote:
>
> > 1. The problem is to read and discard lines from a file until a
> > condition is met (usually some regexp match), and then process
> > subsequent lines. E.g.
>
> I think that you can do this with the '..' operator:
>
> while(<DATA>) {
> if (m/now/ .. eof(DATA)) {
This could just as well be written
if (/now/ .. 0) {
as you will stop at EOF anyway; unless you are using <> and want to
process each file given after the line that matches.
> This will still print the line that you are matching on, but you can
> work with this...
if ( (/now/ .. 0) > 1 ) {
will exclude the first line (see perldoc perlop).
Ben
--
perl -e'print map {/.(.)/s} sort unpack "a2"x26, pack "N"x13,
qw/1632265075 1651865445 1685354798 1696626283 1752131169 1769237618
1801808488 1830841936 1886550130 1914728293 1936225377 1969451372
2047502190/' # ben@morrow.me.uk
------------------------------
Date: Thu, 20 May 2004 19:49:39 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: ISO two Perl idioms...
Message-Id: <c8j24j$qff$2@wisteria.csv.warwick.ac.uk>
Quoth Ben Morrow <usenet@morrow.me.uk>:
>
> Quoth Richard Morse <remorse@partners.org>:
> > In article <c8isc3$k4d$1@reader2.panix.com>,
> > bill <please_post@nomail.edu> wrote:
> >
> > > 1. The problem is to read and discard lines from a file until a
> > > condition is met (usually some regexp match), and then process
> > > subsequent lines. E.g.
> >
> > I think that you can do this with the '..' operator:
> >
> > while(<DATA>) {
> > if (m/now/ .. eof(DATA)) {
>
> This could just as well be written
>
> if (/now/ .. 0) {
...except that, as Anno pointed out, this won't work. D'oh.
if (/now/ .. do{0}) {
or
if (/now/ .. !1) {
which are slightly nasty...
Ben
--
All persons, living or dead, are entirely coincidental.
ben@morrow.me.uk Kurt Vonnegut
Razors pain you / Rivers are damp
------------------------------
Date: Thu, 20 May 2004 20:19:27 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: ISO two Perl idioms...
Message-Id: <Xns94EFA60EA23FEdkwwashere@216.168.3.30>
bill <please_post@nomail.edu> wrote:
> 1. The problem is to read and discard lines from a file until a
> condition is met (usually some regexp match), and then process
> subsequent lines. E.g.
>
> my $found;
> while (<IN>) {
> if (meets_condition($_)) {
> $found = 1;
> last;
> }
> }
>
> die "Bad data\n" unless $found;
This could be a bit shorter and still not be obfuscated:
while (<DATA>) {
last if meets_condition($_);
}
die "Bad data" unless $_;
------------------------------
Date: Thu, 20 May 2004 20:35:39 +0000 (UTC)
From: bill <please_post@nomail.edu>
Subject: Re: ISO two Perl idioms...
Message-Id: <c8j4qr$n53$1@reader2.panix.com>
I must say that reading the replies to my original query has be
great fun. Thar's gold in them thar posts!
Thanks!!!
-bill
------------------------------
Date: Thu, 20 May 2004 23:55:03 +0200
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: ISO two Perl idioms...
Message-Id: <2h4npqF94frnU1@uni-berlin.de>
Also sprach Anno Siegel:
> Tassilo v. Parseval <tassilo.parseval@post.rwth-aachen.de> wrote in comp.lang.perl.misc:
>> Also sprach bill:
>>
>> > There are a couple of situations that I encounter frequently, and
>> > I wonder if there are established Perl idioms for dealing with them.
>>
>> > 2. Finding the first position at which two strings differ.
>> There is the infamous xor trick for this one:
>>
>> $a = "foobar";
>> $b = "foobAr";
>> ($a ^ $b) =~ /^(\0*)/ and print length $1;
>> __END__
>> 4
>>
>> It makes use of the fact that xoring two identical values yields zero.
>> Otherwise something non-zero (1 when looking at each bit). So after
>> xoring you just have to find the first non-NULL character.
>>
>> > P.S. This is not golf, now! I'm *sure* that it is possible to use
>> > some insanely arcane Perl constructs to make the above a *lot* more
>> > succinct, but the results probably would not qualify as "idioms"
>> > in my book. Idioms are pithy, but not terribly obscure.
>>
>> I don't know whether the above qualifies as obscure or golf. It is
>> certainly used in golf once in a while. Nonetheless I think it can look
>> quite elegant to people having a certain affinity towards bitwise
>> operations.
>
> I was going to suggest the xor solution (note promotion from "infamous
> trick" to "solution") if you hadn't. What I wonder is how it would
> hold up with multibyte characters.
Not very well, I guess. :-) Depends a bit on the circumstances. When
both strings are in the same encoding (preferably not a variable-width
encoding such as UTF-8 is one), it could even be used there. And
naturally, the xor-approach can only report the byte- and not the
character-number at which two strings begin to differ.
> On a more general note, trie data structures have been invented to
> tackle the prefix problem (or could have been invented for that
> purpose, not knowing the actual history). There are at least two
> trie modules on CPAN.
The OP's problem looks a bit too basic to benefit from a trie. It's just
a matter of walking through two strings in parallel and compare each
character until they are no longer equal which would be a two-liner in C
(something like 'while (*a && *a++ == *b++) i++;').
When generalizing the problem a bit (namely finding the longest common
substring within potentially large strings), a trie comes in handy. I
think it solve this problem even in linear time.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Thu, 20 May 2004 14:40:57 -0400
From: Richard Morse <remorse@partners.org>
Subject: Re: iterating over arrays with map - problem
Message-Id: <remorse-5B1E01.14405720052004@plato.harvard.edu>
In article
<Pine.SGI.3.96.1040520115318.47977A-100000@vcmr-64.server.rpi.edu>,
Jeff 'japhy' Pinyan <pinyaj@rpi.edu> wrote:
> [posted & mailed]
>
> On Wed, 19 May 2004, Mothra wrote:
>
> > push @dict, map { s/[aA]/4/g;$_ } @dict;
..
> > push @dict, map { s/[zZ]/2/g;$_ } @dict;
>
> Sounds like a job for tr///
>
> $str =~ tr/AaBbEeGgIiLlOoSsTtZz/44883366111100557722/;
Except that he doesn't want to do all of the substitutions at once --
he's looking to try and capture all possible variations, which would
require doing things iteratively.
Ricky
--
Pukku
------------------------------
Date: 20 May 2004 22:46:39 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: iterating over arrays with map - problem
Message-Id: <c8jcgf$arc$1@mamenchi.zrz.TU-Berlin.DE>
Richard Morse <remorse@partners.org> wrote in comp.lang.perl.misc:
> In article <rdJqc.15775781$Of.2631913@news.easynews.com>,
> Mothra <mothra@mothra.com> wrote:
[snip problem]
> I think the following may do what you want, although I'm not positive it
> catches every possible variation.
No, it seems to miss a few.
> I've left in the print statements
> that show everything it tries...
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $init_word = 'password';
>
> my %sub_matrix;
>
> @sub_matrix{qw/A a B b E e G g I i L l O o S s T t Z z/} =
> qw/4 4 8 8 3 3 6 6 1 1 1 1 0 0 5 5 7 7 2 2/;
>
> my %words;
>
> $words{$init_word}++;
> my $was_new = 1;
>
> while($was_new) {
> $was_new = 0;
> foreach my $word (keys %words) {
> foreach my $sub_let (keys %sub_matrix) {
> my $temp = $word;
> while ($temp =~ s/$sub_let/$sub_matrix{$sub_let}/) {
> print "# tried $temp";
> if (!defined($words{$temp})) {
> $was_new++;
> $words{$temp}++;
> print " - was new";
> }
> print "\n";
> }
> }
> }
> }
> print "\n";
>
> print map { $_ . "\n" } sort keys %words;
>
> __END__
>
> Output:
[snip tracing output]
> p455w0rd
> p455word
> p45sw0rd
> p45sword
> p4ssw0rd
> p4ssword
> pa55w0rd
> pa55word
> pa5sw0rd
> pa5sword
> passw0rd
> password
I get these:
password
p4ssword
pa5sword
p45sword
pas5word
p4s5word
pa55word
p455word
passw0rd
p4ssw0rd
pa5sw0rd
p45sw0rd
pas5w0rd
p4s5w0rd
pa55w0rd
p455w0rd
That are sixteen, as would be expected with four replaceable characters
for four binary decisions.
The problem can be seen as one of generating all combinations of character
groups, where a replaceable character forms a group of two, and a character
without replacement is a group of one. For the given "password", the
structure begins:
( [ 'p'], [ 'a', '4'], [ 's', '5'], [ 's', '5'], [ 'w'], ... )
I believe there are combinatorics modules on CPAN that operate on such
structures. Otherwise, this will do:
sub combinations { # starter-offer for the recursion
add_combinations( [ ''], @_);
}
sub add_combinations {
my $so_far = shift;
my @coll;
if ( @_ ) { # another character group to process?
# for each character in the group
for my $char ( @{ shift()} ) {
# make a copy of the collected strings, extended by one char
push @coll, map $_ . $char, @$so_far;
}
# repeat with remaining groups (we shifted the current one off)
@coll = add_combinations( \ @coll, @_);
} else { # no more groups
@coll = @$so_far; # done!
}
@coll;
}
To arrive at the list of character groups, given a string, I gladly
make use of your
my %sub_matrix;
@sub_matrix{qw/A a B b E e G g I i L l O o S s T t Z z/} =
qw/4 4 8 8 3 3 6 6 1 1 1 1 0 0 5 5 7 7 2 2/;
...and extend it like this to a table that covers the alphabet:
my %groups;
for ( 'A' .. 'Z', 'a' .. 'z' ) {
push @{ $groups{ $_}}, $_;
push @{ $groups{ $_}}, $sub_matrix{ $_} if exists $sub_matrix{ $_};
}
The list of sixteen combinations is now produced by:
print "$_\n" for combinations( @groups{ split //, $word });
This method doesn't generate repeated combinations that have to be
weeded out. It would also work with more than one (alternative)
replacement characters.
Anno
------------------------------
Date: 20 May 2004 21:31:31 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Meaning of Orthogonality
Message-Id: <c8j83j$83o$1@mamenchi.zrz.TU-Berlin.DE>
Greg Miller <gmiller@NOTforSPAM.gregmiller.net> wrote in comp.lang.perl.misc:
> On Wed, 19 May 2004 08:43:13 -0000, Edward Wijaya
> <ewijaya@singnet.com.sg> wrote:
>
> >Perl is said to be an "orthogonal" programming language.
> >What does it mean?
[...]
> I'm not sure how this word came to be associated with
> programming languages. The mathematical definition says two things
> are orthogonal if they are at 90 degrees to each other. If you try to
> adapt that to programming language structures, you'd think an
> orthogonal language is one that treats everything differently than
> everything else. But in the programming languages context, it means
> to treat everything the same.
The mathematical sense of "orthogonal" from which the usage in computer
languages derives isn't so much "90 degrees", but the concept of
orthogonal coordinates (or matrices), where the angles between any two
axes are 90 degrees. In such a coordinate system you can choose x, y,
and z independently. In an oblique system, the coordinates depend on
one another.
That idea carries over nicely to the usage in computer languages, where
you get to choose (say) an operator and the type of operands independently,
whereas in less orthogonal languages the choice of one may determine the
choices for the others. In mathematics, at least the dependency is linear,
in computer languages it can be arbitrary.
> Now that I've said that, I've noticed the mathematical
> definition of orthogonal, and the programming language definition are
> orthogonal to each other! Maybe it was a joke someone made that got
> out of hand.
I think it's a good metaphor, taken as above.
Anno
------------------------------
Date: Thu, 20 May 2004 21:42:11 GMT
From: Bryan <bryan@akanta.com>
Subject: Perl array manipulation questions
Message-Id: <TC9rc.69981$sL3.30678@newssvr25.news.prodigy.com>
Hi,
Two questions, which may be related...
1. If I have an array:
@a = (1, 2, 3, 4);
And I want to process each element in the array with some algorithm,
lets say multiply each element by 2, is there a way to do this without
creating a temp array, and -without- looping?
Something along the lines of @a * 2, and @a would then be (2, 4, 6, 8)?
2. If I have two arrays:
@a = (1, 2, 3, 4);
@b = (2, 4, 6, 8);
Is there a way to create a 3rd array of the same size such that @c = @a
+ @b, and after the operation @c = (3, 6, 9, 12). Again, without using
a for or a foreach loop??
Thanks,
Bryan
------------------------------
Date: Thu, 20 May 2004 21:46:56 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Perl array manipulation questions
Message-Id: <c8j90g$22s$1@wisteria.csv.warwick.ac.uk>
Quoth Bryan <bryan@akanta.com>:
> 1. If I have an array:
> @a = (1, 2, 3, 4);
>
> And I want to process each element in the array with some algorithm,
> lets say multiply each element by 2, is there a way to do this without
> creating a temp array, and -without- looping?
Well... you can't do it without a loop, but map provides one implicitly:
@a = map { $_ * 2 } @a;
> 2. If I have two arrays:
> @a = (1, 2, 3, 4);
> @b = (2, 4, 6, 8);
>
> Is there a way to create a 3rd array of the same size such that @c = @a
> + @b, and after the operation @c = (3, 6, 9, 12). Again, without using
> a for or a foreach loop??
@c = map { $a[$_] + $b[$_] } 0..$#a;
You may also want to look at List::Util for more map-likes.
Ben
--
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. ben@morrow.me.uk
------------------------------
Date: Thu, 20 May 2004 21:51:41 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Perl array manipulation questions
Message-Id: <c8j99d$2e0$1@wisteria.csv.warwick.ac.uk>
Quoth Ben Morrow <usenet@morrow.me.uk>:
>
> Quoth Bryan <bryan@akanta.com>:
> > 1. If I have an array:
> > @a = (1, 2, 3, 4);
> >
> > And I want to process each element in the array with some algorithm,
> > lets say multiply each element by 2, is there a way to do this without
> > creating a temp array, and -without- looping?
>
> Well... you can't do it without a loop, but map provides one implicitly:
>
> @a = map { $_ * 2 } @a;
In this case, of course, using a loop is actually easier:
$_ *= 2 for @a;
Ben
--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
/Alcestis/) [ flame, and falls out of sight. ] ben@morrow.me.uk
------------------------------
Date: 20 May 2004 17:58:45 -0600
From: Jim Cochrane <jtc@shell.dimensional.com>
Subject: Re: Perl array manipulation questions
Message-Id: <slrncaqhhq.4cu.jtc@shell.dimensional.com>
In article <c8j99d$2e0$1@wisteria.csv.warwick.ac.uk>, Ben Morrow wrote:
>
> Quoth Ben Morrow <usenet@morrow.me.uk>:
>>
>> Quoth Bryan <bryan@akanta.com>:
>> > 1. If I have an array:
>> > @a = (1, 2, 3, 4);
>> >
>> > And I want to process each element in the array with some algorithm,
>> > lets say multiply each element by 2, is there a way to do this without
>> > creating a temp array, and -without- looping?
>>
>> Well... you can't do it without a loop, but map provides one implicitly:
>>
>> @a = map { $_ * 2 } @a;
>
> In this case, of course, using a loop is actually easier:
>
> $_ *= 2 for @a;
>
> Ben
And if you want to get really fancy, you can define a class/module and
redefine the '*' operator. (Whether this is wise design is another issue.)
--
Jim Cochrane; jtc@dimensional.com
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]
------------------------------
Date: Fri, 21 May 2004 00:07:14 GMT
From: "BCC" <bryan@akanta.com>
Subject: Re: Perl array manipulation questions
Message-Id: <SKbrc.70019$jy4.17058@newssvr25.news.prodigy.com>
> > Well... you can't do it without a loop, but map provides one implicitly:
> >
> > @a = map { $_ * 2 } @a;
>
> In this case, of course, using a loop is actually easier:
>
> $_ *= 2 for @a;
>
> Ben
>
Very nice!
Thanks Ben!
------------------------------
Date: Thu, 20 May 2004 23:37:09 GMT
From: spamtotrash@toomuchfiction.com (Kevin Collins)
Subject: Re: perl tcp server script works, but no data.
Message-Id: <slrncaqg95.hc2.spamtotrash@doom.unix-guy.com>
In article <ef9a95b0.0405200724.2b8f1e58@posting.google.com>, Eli Sidwell
wrote:
Please, no top posting...
> Still nothing, the $nread even comes up 0.
>
> As far as the client, I tried to telnet a small file to that port and
> then I wrote an app using the VB winsock to move text to that port.
> Both times it opens the port and accepts the data (I think), I can get
> the port and IP from perl but, I don't see any data.
>
> Are there any good reference books on the perl socket with examples.
> I have the orilly cookbook, but its a lot of theory.
>
-snip-
perldoc 'perlipc' has a basic TCP client/server that I have used as the basis
for a couple of different things. Its not too complex.
Kevin
------------------------------
Date: Thu, 20 May 2004 22:44:12 GMT
From: Charlton Wilbur <cwilbur@mithril.chromatico.net>
Subject: Re: Perl vs PHP
Message-Id: <8765aq3eqn.fsf@mithril.chromatico.net>
>>>>> "W" == Wappler <wappler@gmx.net> writes:
W> Perhaps it is not possible to get a decent answer in a perl
W> newsgroup. Maybe you should try a google search with
W> "advantages of PHP versus Perl". There are people - not only
W> PHP freaks - who see advantages here.
Most of the advantages have to do with being quick and dirty, or small
and cheap. But as soon as you get to the point of needing to do large
projects in it, the advantages of PHP vanish.
For instance, mod_perl can't really be run safely in a shared
environment the way mod_php can, because mod_perl gives the user
access to server internals, and an error can hose all the server
children -- even those serving other virtual hosts. So for speed in a
shared hosting environment, you're pretty much stuck with PHP, because
mod_perl is too unsafe to run.
PHP doesn't have any features in the language that support large
applications gracefully; and the few that do are comparatively recent
additions. There's one enormous namespace with dozens of
inconsistently-named functions in it; from what I understand they've
recently added support for namespaces, but it's probably too little
too late.
So if you're working on a web application that's not too big and needs
to run in a shared hosting environment, well, PHP may be the right
tool for the job. For every other kind of project, Perl is a better
choice.
Charlton
--
cwilbur at chromatico dot net
cwilbur at mac dot com
------------------------------
Date: Thu, 20 May 2004 23:00:30 +0200
From: "jacek" <bornslippy.SPAM.IS.NOT.OK@o2.pl>
Subject: Re: problem with references
Message-Id: <c8j69g$6q7$1@sunflower.man.poznan.pl>
thanks a lot for help!!
jacek
------------------------------
Date: Thu, 20 May 2004 19:14:48 +0000 (UTC)
From: Brandon Metcalf <bmetcalf@nortelnetworks.com>
Subject: Re: regexp to match time string
Message-Id: <slrncapuot.cum.bmetcalf@cash.rhiamet.com>
On 2004-05-20, Ben Morrow <usenet@morrow.me.uk> wrote:
>
> Quoth Brandon Metcalf <bmetcalf@nortelnetworks.com>:
>> I'm trying to come up with a regexp to match a string containing the
>> date and time in the format:
>>
>> yyyy-mm-dd hh:mm:ss
>>
>> with either the date or time part being optional but not both and the
>> ":ss" part always being optional. I have the following which falls short
>> in a number of ways:
>>
>> /^(\d{4}(-\d{1,2}){1,2}\s+)?(\d{1,2}(:\d{2}){1,2})?$/
>>
>> Is there a way to do this with one regexp?
>
> Ummm...
>
> my $date = qr/\d{4}-\d{2}-\d{2}/;
> my $time = qr/\d{2}:\d{2} (?: :\d{2} )?/x;
> my $re = qr/^ (?: $date | $time | $date \s+ $time ) $/x;
I think that'll do the trick. Thanks for the help.
--
Brandon
------------------------------
Date: Thu, 20 May 2004 23:49:19 GMT
From: "ZMAN" <vze2mf4r@verizon.net>
Subject: scoping problem
Message-Id: <3ubrc.10396$r6.2250@nwrddc03.gnilink.net>
Hello all-
I have a routine to generate an image map that I've written for our HTML
wrapper that we use here at work.
I for the life of me have not been able to clear out the array @t_imaprows.
Keeps appending with each pass.
Any help would be greatle appreciated thanks in advance!
########################################################
my (@t_imaprows)=();
%env_imap = (
'begin_fn' => ['*SKIP*', 'BeginImap'],
'end_fn' => ['*SKIP*', 'EndImap'],
'name' => '',
);
sub mcr_imaprow
{
push(@t_imaprows, {@_});
return(undef);
}
sub BeginImap
{
my($cmp) = &df::CurrentComponent();
my($tag, $pass, %env) = @_;
return(undef) if ($factory::pass_level < 1);
my($r) = '';
my $name = $env{'name'};
$r .= "<map name=\"$name\">\n";
my($i);
for($i = 0; $i <= $#t_imaprows; $i++ )
{
my(%t_imaprow) = %{$t_imaprows[$i]};
my $coords = $t_imaprow{'coords'};
my $shape = $t_imaprow{'shape'};
my $target = $t_imaprow{'target'};
my($link) = $t_imaprow{'link'};
my($l) = &make_link($link);
my($target_str);
if ($t_imaprow{'target'} eq '')
{
$target_str = '';
} else {
$target_str = "target=\"_$setimaprow{'target'}\"";
}
$r .= "<AREA coords=\"$coords\" href=$l shape=\"$shape\"
$target_str>\n";
}
$r .= "</map>\n";
return($r);
}
sub EndImap
{
return(undef);
}
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 6589
***************************************