* Steve Fink sph...@gmail.com [2012-05-18 10:25]:
On Thu, May 17, 2012 at 3:14 AM, Aristotle Pagaltzis pagalt...@gmx.de wrote:
* Mike Erickson m...@quidquam.com [2012-05-16 15:45]:
If you don't care about order, but just want those elements, you
can also do:
keys
use `values` these problems go away… except that to get the odd-
index elements from it you have to `reverse` the array, at which point
a not-especially-golfed grep is shorter.
--
*AUTOLOAD=*_;sub _{s/$/$/;s/(.*):://;wantarray//substr$_,-1,1,,$/;print;$1}
Just-another-Perl-hack;
#Aristotle Pagaltzis
for doing
half as much work. (For even elements, the map and grep solutions would
yield exactly equally long code, with half-as-much-work still applying.)
--
*AUTOLOAD=*_;sub _{s/$/$/;s/(.*):://;wantarray//substr$_,-1,1,,$/;print;$1}
Just-another-Perl-hack;
#Aristotle Pagaltzis // http
* Ronald J Kimball rjk-perl-...@tamias.net [2011-11-16 21:50]:
It is greedy, but the important thing to remember is that the regular
expression engine will find the longest *leftmost* match.
To put that a third way: the engine will match at the first possible
location, and will make the match
] with
an ISO-8859 charset) in the same document.
[1]: Well, Modern Greek anyway.
Regards,
--
Aristotle Pagaltzis // http://plasmasturm.org/
in either case, and which
one is affected by the `shift`.
--
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(,$\/, )[defined wantarray]/e;$1}
Just-another-Perl-hack;
#Aristotle Pagaltzis // http://plasmasturm.org/
to write delegative
code:
# ...
$self-get_veeblefitzer()-frobnitz( @_ );
# ...
--
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(,$\/, )[defined wantarray]/e;$1}
Just-another-Perl-hack;
#Aristotle Pagaltzis // http://plasmasturm.org/
* David Landgren [EMAIL PROTECTED] [2007-11-24 10:45]:
Uri Guttman writes:
AP == A Pagaltzis [EMAIL PROTECTED] writes:
AP * Jerrad Pierce [EMAIL PROTECTED] [2007-11-23 22:50]:
exists( $dispatch{$sub} ) ? $dispatch{$sub}-() :
warn Key $sub does not exist in the dispatch table;
AP
the check for whether the default value is
a CODE ref?
Regards,
--
Aristotle Pagaltzis // http://plasmasturm.org/
wantarray]/e;$1}
Just-another-Perl-hack;
#Aristotle Pagaltzis // http://plasmasturm.org/
. You probably want to take this to Perlmonks.
Regards,
--
Aristotle Pagaltzis // http://plasmasturm.org/
to unwarranted grumpiness.
Regards,
--
Aristotle Pagaltzis // http://plasmasturm.org/
. Matter of taste/style.
Regards,
--
Aristotle Pagaltzis // http://plasmasturm.org/
Pagaltzis // http://plasmasturm.org/
* Alexandre Jousset [EMAIL PROTECTED] [2006-08-28 18:15]:
For the moment I have a (too) complicated sub spacify and I would
like to simplify it to a regexp.
It doesn’t have to be complicated, even without a regexp.
scalar reverse join ' ', unpack '(A3)*', reverse $num;
Regards,
--
* Philippe BooK Bruhat [EMAIL PROTECTED] [2006-07-20 08:40]:
Le jeudi 20 juillet 2006 à 02:41, A. Pagaltzis écrivait:
* Philippe BooK Bruhat [EMAIL PROTECTED] [2006-07-19 22:10]:
'=cut';
=pod
[...snip...]
=cut
Damn, what a cool hack!
That's exactly
: 4
square: 9
In other words it’s also useful when you need to break aliasing
to make sure you’re operating on a modifiable copied scalar.
Regards,
--
Aristotle Pagaltzis // http://plasmasturm.org/
* Smylers [EMAIL PROTECTED] [2006-07-11 23:05]:
babycart is 3 syllables; shopping trolley
Plus I could see myself adopting the latter (reluctantly) if it
became part of the vernacular, but I’d feel too silly using the
former to pick it up.
I still like “fat brackets” better though, in vague
* [EMAIL PROTECTED] [EMAIL PROTECTED] [2006-02-22 20:15]:
map {$s+=(/\d/?$_:(ord()-55))*(36**$i++)} reverse(split(//,uc shift));
@v{ 0..9, A..Z } = 0..35; $s = $s * 36 + $v{ uc $_ } for /./g;
Even readably spaced, the less clever solution is shorter.
$s =~ s/(...)/$1-/g; print $s\n;
print
* Yanick Champoux [EMAIL PROTECTED] [2005-10-18 03:05]:
Well, I'll be darned. Some people actually volunteered. :-)
You might garner some more interest by posting this to
rec.games.corewar – they’re always interested in hearing about
variations on the game.
Regards,
--
#Aristotle
* A. Pagaltzis [EMAIL PROTECTED] [2005-05-09 12:10]:
The problem with this and all the grep solutions is that you
^^
dont know whether you matched the same alternate multiple
times,
Err, that bit was non-sense, of course. I need some coffee
* Jos Castro [EMAIL PROTECTED] [2005-05-05 16:30]:
So suppose you want a regular expression to match at least one
of three words:
/word1|word2|word3/
What solution would you use if you wanted at least _two_ of
those three words?
$alt = join '|', qw( word1 word2 word3 );
/ ($alt)
* Smylers [EMAIL PROTECTED] [2004-08-04 00:34]:
[*0] It was cluttering up the error log with benign 'uninitialized'
messages, which were obscuring the output (from some other script)
that I wanted to see.
Maybe you can try just disabling those?
no warnings 'uninitialized';
* Babichev Dmitry [EMAIL PROTECTED] [2004-06-25 09:17]:
Could anybody provide the best practices of implementation the
error handling in perl modules?
This is the fun with Perl list. Your inquiry is No Fun.
You want to go to www.perlmonks.org or some such.
Regards,
--
Aristotle
If you can't
* Tobias Gödderz [EMAIL PROTECTED] [2004-06-24 23:42]:
Okay, I'm down to 148 now, and I think, I'm on my limit. I'll
publish mine as soon as no one wants to compete anymore.
I am working on a solution, but I don't think I'll manage to
finish tonight, and tomorrow I'll be gone from home until
* Brad Greenlee [EMAIL PROTECTED] [2004-06-21 00:37]:
Golf anyone? My opening shot is 288.
* Tobias Gödderz [EMAIL PROTECTED] [2004-06-23 19:16]:
We (a friend of mine and me) made it with 246 chars.
Uh, you're supposed to publish your solutions when golfing.
Regards,
--
Aristotle
If you
* Jose Alves de Castro [EMAIL PROTECTED] [2004-06-01 11:59]:
BTW: This was made so it would work with $year consisting of
four digits... any care to make it generic? :-)
sub rx_gt {
local $_ = shift;
return if /\D/;
my @alt = '';
for(split //) {
* Tobias Wolter [EMAIL PROTECTED] [2004-06-01 17:33]:
On 2004-06-01T10:52:55+0100 (Tuesday), Jose Alves de Castro wrote:
How can I say that I want every single year after the one I
have? It's impossible...
The operator works quite well for a four-digit year.
Yeah, but that requires
* Rick Delaney [EMAIL PROTECTED] [2004-05-03 15:23]:
while (my @a = map { eof() ? () : scalar } 1 .. $n) {
print @a;
print SEPARATOR\n;
}
while (my @a = grep defined, map scalar , 1 .. $n) {
print @a;
print SEPARATOR\n;
}
--
Regards,
* Jose Alves De Castro [EMAIL PROTECTED] [2004-04-26 18:36]:
#!/usr/bin/perl -l0n
/:/;$_{$'.$}.= $`,}{map{s/,$/
/;print}%_
Trivial improvement, for a gain of 5 chars:
#!/usr/bin/perl -l0p
/:/;$_{$'.$}.= $`,}for(%_){s/,$/
/;
I've tried other clever things, but they all come out
* Keith C. Ivey [EMAIL PROTECTED] [2004-04-22 14:48]:
Math won't work, because floats aren't exact.
Actually, that is a point I meant to make somewhere in this
thread.
When you're dealing with money, it's better to use cents (or
tenths or hundredths of cents) as a unit, rather than dollars.
You
* John Douglas Porter [EMAIL PROTECTED] [2004-04-20 08:39]:
It seems to me that the precision desired should depend on
context, and nothing else. And that being the case...
printf $fractional_cents ? '%7.3f' : '%7.sf', $amt;
irrespective of the value of $amt. Why is this not right?
* Bernie Cosell [EMAIL PROTECTED] [2004-04-22 00:02]:
On 21 Apr 2004 at 22:55, A. Pagaltzis wrote:
If you do this by looking at $amt, then your method must be
mathematical, because chopping characters in the string
representation of the unrounded $amt might occasionally lead to
results
* Bernie Cosell [EMAIL PROTECTED] [2004-04-19 17:37]:
So: what I want is something to format money correctly, whther
it is fractional or not. there's the fairly awful:
sprintf ($amt =~ /\.\d\d\d/? %7.3f: %7.2f), $amt
Right off the bat I thought of %g, but that doesn't really help:
* Quantum Mechanic [EMAIL PROTECTED] [2004-04-19 23:09]:
--- A. Pagaltzis [EMAIL PROTECTED] wrote:
my $amt_str = sprintf %7.3f, $amt;
for($amt_str) { chop if /\.\d\d0\z/ }
Why would you do for..chop when you could do s///?
You're using an re either way
* Gaal Yahas [EMAIL PROTECTED] [2003-10-03 11:24]:
Two points about this. First, the lvalue ?: is kinda, uh,
Perlish to the extreme.
Yeah. It tends to be a powerful obfuscant too. I had to look
thrice at your code (then went oh, duh!). For more than two or
maybe three lvalues to switch between,
* Gaal Yahas [EMAIL PROTECTED] [2003-09-30 20:14]:
Sadly, this doesn't work as an lvalue:
@hash{ qw/aaa bbb ccc/ } = ($one, $two, $three);
I use that *all* the time. The problem is not with Perl here..
--
Regards,
Aristotle
If you can't laugh at yourself, you don't take life seriously
* Etienne Grossmann [EMAIL PROTECTED] [2003-09-18 09:29]:
I hared a sutdy has been dnoe on taht scjebut in an Eigslnh
urtiivesny, but I coannt get hold of it. Any ionirmtfoan on
that sceujbt is welomce.
http://www.bisso.com/ujg_archives/000224.html
perl -pe 'sub r{join,map chop,sort
* Kripa Sundar [EMAIL PROTECTED] [2003-09-18 15:41]:
The following does guarantee re-ordering.
How does it reorder loop? :-)
--
Regards,
Aristotle
If you can't laugh at yourself, you don't take life seriously enough.
* Andrew Savige [EMAIL PROTECTED] [2003-08-24 04:27]:
the Klement Transform? Or should that read Klementian or even
Klementine Transform? :-)
Well, it's not called the Guttman-Roslerian Transform (or
Guttmanian-Roslerian?) either.
Schwartzian is really a red herring.
--
Regards,
Aristotle
* Adam Rice [EMAIL PROTECTED] [2003-07-22 16:02]:
I wrote a program to generate these, but has an unfun tendency
to take forever when the solution is non-trivial so I won't
post it. If there was a better technique than brute force to
find the answers that might be fun, but I just can't get my
* [EMAIL PROTECTED] [EMAIL PROTECTED] [2003-07-17 21:15]:
What is the most interesting program that is restricted to
use each ASCII character at most once?
The definition I chose was to check if a program (from @ARGV)
contains duplicate chars and printing a duplicate char iff it
has one.
* Jon Bjornstad [EMAIL PROTECTED] [2003-07-15 00:00]:
I don't often get to use the '.' operator or the 'x' operator
and I thought this was pretty cool.
Pretty standard fare for some people, though I guess the exact
idea of fun is different for everyone. :)
There are a few flaws with the above
* Keith C. Ivey [EMAIL PROTECTED] [2003-07-15 14:42]:
which will be handled by the regex but may cause a parser to
blow up (though some are more tolerant than others)
Did you read what I said? You need a tolerant parser indeed. Did
you take any look HTML::Parser at all?
| That leaves input
* Pense, Joachim [EMAIL PROTECTED] [2003-07-02 14:15]:
You can write
if ($some_condition) {
do_this;
do_that;
do_something_else;
}
and you can write
do_this if $some_condition;
You need not write
if ($some_condition) {do_this}
You can also write
do {
do_this;
* Bernie Cosell [EMAIL PROTECTED] [2003-07-01 18:39]:
Perl is filled with multiple ways to do things and the simple
argument that you can do something similar using some other
mechanism is rarely determinative.
...but I hesitate to make ten ways to do it. -- Larry Wall
--
Regards,
Aristotle
Still untested:
* A. Pagaltzis [EMAIL PROTECTED] [2003-02-23 17:50]:
@found3 = map pop @{$hash{$_}}, amatch('3456', @info);
That should rather read
@found3 = map shift @{$hash{$_}}, amatch('3456', @info);
The point is that the order of identical items in the
initial list is preserved
Try this. Untested.
$t-start('transforming2');
my $i;
for(list) {
$i = $_-{'info'};
push info, $i;
push {$hash{$i}}, $_;
}
found3 = map pop {$hash{$_}}, amatch('3456', info);
$t-stop;
--
Regards,
Aristotle
Alternatively it can be done with negative lookaheads.
print ok if /^(?![a-z0-9]+$)(?![A-Z0-9]+$)(?![a-zA-Z]+$)[\w-]{6,14}$/;
--
Regards,
Aristotle
Sorting 1000 items involves a _lot_ more than 1000
comparisons - which is really all you need. If you insist
on your approach you should memoize vercmp() or use the
orcish maneuver in your sort() callback. (Memoizing will
accelerate it across all of your script, but the orcish
maneuvre will
* A. Pagaltzis [EMAIL PROTECTED] [2003-02-12 07:35]:
and -1 == vercmp($2,$3, @{$installed{$_}});
That is, of course,
and -1 == vercmp($2,$3, @{$installed{$1}});
--
Regards,
Aristotle
* Andrew Savige [EMAIL PROTECTED] [2003-02-10 17:54]:
*whistle* *whistle* *red card* *disqualified*
multiplying by length (x+length) will not give the desired
result here; it must be boolean (0 or 1 only).
Doh! Of course. I'm an idiot. *grmbl*
--
Regards,
Aristotle
* Eugene van der Pijll [EMAIL PROTECTED] [2003-02-10 18:35]:
Unfortunately, you had use strict in your first post,
and neither of these are use-strict safe.
Oh? What makes you say so?
--
Regards,
Aristotle
* John W. Krahn [EMAIL PROTECTED] [2003-02-09 12:27]:
You can also speed up j1 by using map without the braces.
sub j1 { my @lines = map chomp $_, split /^/, $x, -1 }
Which will promptly fail on a last line without a trailing
record separator because chomp would return 0 and cause the
to
* Andrew Savige [EMAIL PROTECTED] [2003-02-10 07:28]:
@lines = ($x=~/^.*/mg) x !!length($x);
$_=$x;@lines=(/^.*/mg)x+length;
--
Regards,
Aristotle
* Andrew Savige [EMAIL PROTECTED] [2003-02-07 13:47]:
my @lines = split(/^/, $x, -1); chomp(@lines); # fastest?
my @lines = split m[\Q$/], $x, -1;
--
Regards,
Aristotle
* John Douglas Porter [EMAIL PROTECTED] [2003-02-07 14:15]:
@lines = split /\n/, $x, -1; pop @lines;
$/ can be different from \n though.
And popping the last field is dangerous - you don't know if
the file ends with a newline. Also, you now have no chance
to reconstruct the exact equivalent
* John Douglas Porter [EMAIL PROTECTED] [2003-02-07 14:50]:
Yes, but his example data was text in a here document.
Then add a note about the caveat.
split m,$/, $x, -1;
In bizarre cases, $/ might contain regex metacharacters.
Don't forget the \Q.
And popping the last field is dangerous -
* Yitzchak Scott-Thoennes [EMAIL PROTECTED] [2003-02-07 20:51]:
chomp(my $tmp=$x); my @lines=split /\n/,$tmp,-1
Very nice.
--
Regards,
Aristotle
* Scott Wiersdorf [EMAIL PROTECTED] [2002-12-14 13:01]:
perl -n0777
exit !(/pattern/)
Necessary Improvements:
- concisely get the pattern from the command-line (e.g., pattern as
$ARGV[0] and the file as $ARGV[1]) such that the syntax is more like
grep (e.g., grep 'pattern' filename =
* Paul Makepeace [EMAIL PROTECTED] [2002-11-27 08:53]:
perl apparently doesn't consider $d exists by the second $d and issues
an error. Can someone explain this? (Esp. in light of all this sequence
point talk.)
The entire statement ist compiled at a time; at this time,
$d is undeclared. The
* Andrew Molyneux [EMAIL PROTECTED] [2002-11-20 12:47]:
You're not the only one. I'd probably do:
my ($max, $sep, $end) = @_;
aolme too/aol
but I'd love to know if Steven had a specific reason
for doing it the other way.
There doesn't seem to be any here.
--
Regards,
Aristotle
* Steven Lembark [EMAIL PROTECTED] [2002-11-20 17:35]:
Yes, becuase if you did it this way you'd get $end equal
to the integer coult of the number of list arguments passed
plus one for the end value. Notice the usage:
my $string = commify 90, ', ', 'etc...', @names;
The other
* Steven Lembark [EMAIL PROTECTED] [2002-11-20 17:35]:
Wasted work. Two comparisons per element and you don't bail
once you've filled your available space.
Two comparisons on a short list, at which point the remainder
of them are a simple comparison to get a () out, which ends
up as a
* Josh Goldberg [EMAIL PROTECTED] [2002-11-20 19:20]:
map{$i++$lim?push @n,$_:$i!=$lim?{push @n,'etc.' and goto FOO}:''}@names;
Sigh. map in void context and goto where a for would have
done. And it doesn't even conform to the specs, you were
asked to provide a string with less than 90
* Steven Lembark [EMAIL PROTECTED] [2002-11-20 20:51]:
Look up what happens to arrays in a scalar context.
my ( $a, $b, $c ) = qw( foo bar bletch blort bim bam blort );
what do yo get for $c?
'bletch' - and that's not an array there.
--
Regards,
Aristotle
* Jonathan E. Paton [EMAIL PROTECTED] [2002-11-20 20:51]:
if (@array $bound) {
$array[$bound-1] = , etc; # Set element at boundary
$#array = $bound-1; # Shorten array to boundary
}
print join , , @array;
Again, that's the same as:
join ', ', @array[0 .. $bound], @array
* Andrew Molyneux [EMAIL PROTECTED] [2002-11-20 18:25]:
A. Pagaltzis wrote:
Enter splice.
my ($max, $sep, $end) = splice @_, 0, 3;
That has brevity, certainly, but for legibility, I think I
prefer Steven's original (shift,shift,shift)
Really? I find the splice version a lot easier
* Dave Arnold [EMAIL PROTECTED] [2002-11-21 02:45]:
$str = join ', ', @names;
if ( length($str) 90 )
{
substr($str,rindex($str,,,90)) = , etc.;
}
Nice, and so obvious too - in retrospect, of course.
The only thing I don't like about it is it still joins
everything, even if it only needs
* Selector, Lev Y [EMAIL PROTECTED] [2002-11-20 00:55]:
an array of names is converted into a comma-separated list.
if the list gets to long - limit it and add , etc. at the end.
$str = join ', ', @names;
if (length($str)90) {
($str = substr($str,0,90)) =~ s/,[^,]*$/, etc./;
* A. Pagaltzis [EMAIL PROTECTED] [2002-11-20 02:00]:
my ($i, $total);
($total += $_) 90 or last, $i++ for map length, @names;
$str = join ',', @names[0 .. $i];
$str .= ', etc' if $i $#names;
Sorry, that would of course be
($total += length) = 90 or last, $i++ for @names;
The map was left
Ok, another attempt, this time with brain engaged and coffee
consumed.
$str = join ', ', @names;
if (length($str)90) {
($str = substr($str,0,90)) =~ s/,[^,]*$/, etc./;
}
my ($i, $total);
($total += length) 90 ? $i++ : last for @ARGV;
$str = join ', ', @ARGV[0 .. $i];
$str .= ', etc' if
* Mark-Jason Dominus [EMAIL PROTECTED] [2002-10-13 17:48]:
I am now back. If you want to get the quiz-of-the-week, send a note
to
[EMAIL PROTECTED]
Please feel free to circulate this announcement to appropriate venues.
I posted this (among others) to PerlMonks at
* Adam Rice [EMAIL PROTECTED] [2002-10-05 23:26]:
If I was writing this for work I would include error
checking and read and write the file in 64k blocks, but
there's no fun way to do that in Perl so I'll leave it out
here.
Yes there is.
$/ = \(65536);
See perldoc perlvar on $/
--
* Vladi Belperchinov-Shabanski [EMAIL PROTECTED] [2002-09-30 19:24]:
$_='' if !/\S/ $e; # i.e. don't print if empty and last one was empty
$e = !/\S/; # remember if last one is empty...
Since you have already done $_='' in the first line, you
don't need a regex in the
75 matches
Mail list logo