[28214] in Perl-Users-Digest
Perl-Users Digest, Issue: 9578 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 8 18:05:44 2006
Date: Tue, 8 Aug 2006 15:05: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 Tue, 8 Aug 2006 Volume: 10 Number: 9578
Today's topics:
Re: Control characters - regex to match/lose these? <benmorrow@tiscali.co.uk>
Re: Control characters - regex to match/lose these? <benmorrow@tiscali.co.uk>
Re: Control characters - regex to match/lose these? <rvtol+news@isolution.nl>
Re: Control characters - regex to match/lose these? <rvtol+news@isolution.nl>
Re: HOA redundancy in array. <hpbenton@scripps.edu>
Re: HOA redundancy in array. <benmorrow@tiscali.co.uk>
Re: how to match or detect alphanumeric including - or <benmorrow@tiscali.co.uk>
Re: how to match or detect alphanumeric including - or <xicheng@gmail.com>
Re: how to match or detect alphanumeric including - or <benmorrow@tiscali.co.uk>
Re: how to match or detect alphanumeric including - or <someone@example.com>
Re: How to use OO and package? <john@castleamber.com>
Re: How to use OO and package? <benmorrow@tiscali.co.uk>
Solved:: Printing Hash Marks During File Download <hlarons@yahoo.com>
Re: Solved:: Printing Hash Marks During File Download usenet@DavidFilmer.com
Sort keys in a hash numerically <micky@hotmail.com>
Re: Sort keys in a hash numerically usenet@DavidFilmer.com
Re: Sort keys in a hash numerically xhoster@gmail.com
Re: Sort keys in a hash numerically <xicheng@gmail.com>
Re: Sort keys in a hash numerically <micky@hotmail.com>
Re: which file, regestry, environment variables do perl <tintin@invalid.invalid>
Re: which file, regestry, environment variables do perl <news@lawshouse.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 8 Aug 2006 19:46:57 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: Control characters - regex to match/lose these?
Message-Id: <1q2nq3-v7i.ln1@osiris.mauzo.dyndns.org>
Quoth justin.news@purestblue.com:
> On 2006-08-08, Dr.Ruud <rvtol+news@isolution.nl> wrote:
> > Justin C schreef:
> >
> >> Viewed in vim they look like: ^[(s12H ^[&16D ^[(16H ^[&18D
> >
> > s/\e(?:[^@A-Z]*[@A-Z])|[=9]//g
Without really knowing what you're matching, isn't there a bug here? Do
you really want to strip all = and all 9 from the input? Also, @ needs
escaping in qq strings:
s/\e (?: [^\@A-Z]* [\@A-Z] | [=9] )//gx;
> Gonna have to learn more about REs to understand this one!
I'll walk you through it with the /x flag:
s/ # find
\e # an escape character (^[ in vim)
(?: # followed by either
[^\@A-Z] # anything *but* @ or A-Z
* # 0-or-more times
[\@A-Z] # terminated by @ or A-Z
| # or
[=9] # = or 9
)
//gx; # and replace with nothing
The /x flag is your friend. I would have said that even my first version
above, without the comments, is *much* more readable that Dr.Ruud's
original.
> I'm reading through perlre, the ?: is doing my head in a bit, may be
> it's been a long day: ``it groups subexpressions like "()" but doesn't
> make backreferences'' ... ``This is for clustering, not capturing''
Basically, /(?: ... )/ is the same as /( ... )/ except it doesn't create
an $N variable. This makes it somewhat faster, and lets you capture what
you want to capture, rather than creating a whole lot of captures you
didn't really want; which helps when putting regexen together out of
separate pieces.
Ben
--
The Earth is degenerating these days. Bribery and corruption abound.
Children no longer mind their parents, every man wants to write a book,
and it is evident that the end of the world is fast approaching.
Assyrian stone tablet, c.2800 BC benmorrow@tiscali.co.uk
------------------------------
Date: Tue, 8 Aug 2006 19:52:46 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: Control characters - regex to match/lose these?
Message-Id: <u43nq3-v7i.ln1@osiris.mauzo.dyndns.org>
Quoth "Dr.Ruud" <rvtol+news@isolution.nl>:
> Justin C schreef:
>
> > Viewed in vim they look like: ^[(s12H ^[&16D ^[(16H ^[&18D
>
> s/\e(?:[^@A-Z]*[@A-Z])|[=9]//g
(you need to escape @)
> (no, [@-Z] isn't allways the same as [@A-Z])
>
> (no, [@[:upper:]] isn't a proper alternative either, in this context)
I seem to be missing something, would you mind explaining? Surely either
you're assuming ASCII, in which case /[\@-Z]/ *is* the same as
/[\@A-Z]/, or you're not, in which case you need either /[\@[:upper:]]/
or perhaps /[\x40-\x5a]/? If '@' ne "\x40" then [A-Z] probably isn't
just the upper-case letters either.
Ben
--
I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain.
benmorrow@tiscali.co.uk Frank Herbert, 'Dune'
------------------------------
Date: Tue, 8 Aug 2006 22:46:31 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Control characters - regex to match/lose these?
Message-Id: <ebb4gv.r0.1@news.isolution.nl>
Ben Morrow schreef:
> justin.news:
>> Dr.Ruud:
>>> Justin C:
>>>> Viewed in vim they look like: ^[(s12H ^[&16D ^[(16H ^[&18D
>>>
>>> s/\e(?:[^@A-Z]*[@A-Z])|[=9]//g
>
> Without really knowing what you're matching, isn't there a bug here?
Yes, there is. And I even ruined it further just before posting...
> I'll walk you through it with the /x flag:
>
> s/ # find
> \e # an escape character (^[ in vim)
> (?: # followed by either
> [^\@A-Z] # anything *but* @ or A-Z
> * # 0-or-more times
> [\@A-Z] # terminated by @ or A-Z
> | # or
> [=9] # = or 9
> )
> //gx; # and replace with nothing
>
> The /x flag is your friend. I would have said that even my first
> version above, without the comments, is *much* more readable that
> Dr.Ruud's original.
Thanks for the correction.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: Tue, 8 Aug 2006 23:24:56 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Control characters - regex to match/lose these?
Message-Id: <ebb6in.1f4.1@news.isolution.nl>
Ben Morrow schreef:
> Dr.Ruud:
>> (no, [@-Z] isn't allways the same as [@A-Z])
>> (no, [@[:upper:]] isn't a proper alternative either, in this context)
>
> I seem to be missing something, would you mind explaining? Surely
> either you're assuming ASCII, in which case /[\@-Z]/ *is* the same as
> /[\@A-Z]/, or you're not, in which case you need either
> /[\@[:upper:]]/ or perhaps /[\x40-\x5a]/? If '@' ne "\x40" then [A-Z]
> probably isn't just the upper-case letters either.
See perlebcdic:
REGULAR EXPRESSION DIFFERENCES
As of perl 5.005_03 the letter range regular expression such as
[A-Z]
and [a-z] have been especially coded to not pick up gap
characters.
In (for example) Latin1 and EBCDIC, [[:upper:]] contains more than
[A-Z], so [:upper:] is too much.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: 8 Aug 2006 13:10:13 -0700
From: "PB0711" <hpbenton@scripps.edu>
Subject: Re: HOA redundancy in array.
Message-Id: <1155067813.805077.217960@i3g2000cwc.googlegroups.com>
John Bokma wrote:
>
> > Thank you, sorry didn't look at manpages :|
>
> Most of the times it's the first place you should look :-D Also because
> that information is probably the most accurate you can find, and most
> often experienced people here are going to refer to it anyway (give
> someone a fish v.s. learn him/her how to fish :-D ).
>
> --
> John Bokma Freelance software developer
> &
> Experienced Perl programmer: http://castleamber.com/
Ok so I looked at the perldoc's and I see this (which looks the best
option for me, I think)
"undef %saw;
@saw{@in}=();
@out=sort keys %saw;"
If you don't mind could you explain to me how this works?
and if possible, and if you have time how I can use this with
a structure like this
"$HOA_protein[$pro_name]=[@pep];"
I'm sorry it's just HOA's and references are still a bit new to me :)
------------------------------
Date: Tue, 8 Aug 2006 22:10:17 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: HOA redundancy in array.
Message-Id: <p6bnq3-svj.ln1@osiris.mauzo.dyndns.org>
Quoth "PB0711" <hpbenton@scripps.edu>:
> John Bokma wrote:
> >
> > > Thank you, sorry didn't look at manpages :|
> >
> > Most of the times it's the first place you should look :-D Also because
> > that information is probably the most accurate you can find, and most
> > often experienced people here are going to refer to it anyway (give
> > someone a fish v.s. learn him/her how to fish :-D ).
[please don't quote .sigs]
> Ok so I looked at the perldoc's and I see this (which looks the best
> option for me, I think)
> "undef %saw;
This is not necessary. What is necessary, though, is to declare the
variable:
my %saw;
> @saw{@in}=();
> @out=sort keys %saw;"
>
> If you don't mind could you explain to me how this works?
my %saw;
creates a new (empty) hash called $saw.
@saw{@in} = ();
creates entries in %saw for everything in @in, with no values. How this
works is rather subtle, a combination of several effects. The first is
hash slices:
my %h = (a => 'b', c => 'd', e => 'f');
print @h{'a', 'c'};
This prints 'ad'. The @ sigil says that the result is a list. The {...}
says that the variable is a hash. The output is the list of values
corresponding to the given keys.
The second is autovivification[0]: this is a fancy way of saying 'if you
pretend a hash key/array index exists, Perl will create it for you'. So
you can say
my %h;
$h{a} = 'b';
and this creates the $h{a} key for you. Combining this with hash slices
gives
my %h;
@h{'a', 'b'} = ('c', 'd');
which is the same as
my %h = (a => 'c', b => 'd');
Finally, you are assigning the empty list, so the keys are just created
without undef for the values.
@out = sort keys %saw;
retrieves the list of keys, sorts it and puts in into @out (which also
should have been declared).
[0] OK, this isn't quite what is meant by autoviv.. that is more like
'if you pretend a hash key exists and is a reference, Perl will make it
so'. It's closely related, though.
> and if possible, and if you have time how I can use this with
> a structure like this
> "$HOA_protein[$pro_name]=[@pep];"
[FWIW, it's rather confusing to quote Perl code with "". I generally
indent it; another thing some people do is quote it with C<> like in
POD.]
[Given that @HOH_protein is an array, therefore $pro_name is a number,
it would be better called $pro_num or some such. Or did you mean
$HOA_protein{$pro_name} = [@pep];
? If so you'll need to adapt the below; replacing @HOA_protein with
values %HOA_protein throughout should do.]
[All code below is untested. Apologies for any silly errors :)]
You need to explain a bit more what you are trying to do here. Assuming
the above statement is in some kind of loop, you will end up with a
different arrayref every time ([] always creates a new arrayref) so the
method above will not find any duplicates. Guessing that you want to
find the set of unique lists of values, so that out of
(a, b, c)
(d, e, f)
(a, b, c)
(f, e, d)
you want to keep
(a, b, c)
(d, e, f)
(f, e, d)
there are three ways to proceed. The most simple-minded is to build a
list of unique values by hand:
my @uniqs;
# checks if two arrays are equal (to one level)
# call with to array refs
sub eq_array {
my ($l, $r) = @_;
return unless @$l == @$r;
for (0..$#$l) {
return unless $l->[$_] eq $r->[$_];
}
return 1;
}
PROTIEN: for my $new (@HOA_protein) {
for my $got (@uniqs) {
next PROTEIN if eq_array $new, $got;
}
push @uniqs, $new;
}
There are more compact ways of writing this, with grep or
List::Util::first, but this is probably the most comprehensible for a
Perl newbie. You could also use is_deeply from Test::More. Make sure you
really understand what's going on with references there: until you do,
you'll be pretty stuck with multi-level data structures in Perl.
This is clean and neat, but it gives up using a hash to find uniques,
which means it's probably slow. To use a hash, you need to find a way of
converting your arrays into strings, because hash keys are always
strings. One way is to find a character you *know* won't occur in any of
the values, and join the array with that character. Say you choose \034
(a pretty uncommon character); then you can do it like this
my %uniqs;
for my $prot (@HOA_protein) {
my $string = join "\034", @$prot;
$uniqs{$string} = 1;
}
my @uniqs = keys %uniqs;
or, more compactly and also probably faster
my %uniqs;
@uniqs{ map { join "\034", @$_ } @HOA_protein } = ();
my @uniqs = keys %uniqs;
. Notice how this looks quite like the extract you started with: the
difference is we need to convert (map) the values in the array to an
appropriate form.
Both these solutions suffer from depending on details of your data
structure. Both assume that what you have is an array of arrays of
strings; the second assumes that none of these strings contain "\034".
The Storable module gives you a way to convert a data structure of
arbitrary complexity into a string, fast, in such a way as to preserve
the whole structure. You can use it like this
use Storable qw/freeze/;
$Storable::canonical = 1; # needed when comparing frozen hashes
my %uniqs;
@uniqs{ map freeze($_), @HOA_protein } = ();
my @uniqs = keys %uniqs;
> I'm sorry it's just HOA's and references are still a bit new to me :)
That's fine. They are quite a complicated topic: once you understand all
the code in this article, you'll be well on yor way to being a decent
Perl programmer. Feel free to ask about anything you can't follow
(although be warned that some of it will take quite a lot of thinking
about, for which there is no substitute).
Ben
--
The cosmos, at best, is like a rubbish heap scattered at random.
Heraclitus
benmorrow@tiscali.co.uk
------------------------------
Date: Tue, 8 Aug 2006 19:58:49 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: how to match or detect alphanumeric including - or spaces through out
Message-Id: <9g3nq3-v7i.ln1@osiris.mauzo.dyndns.org>
Quoth "Paul Lalli" <mritty@gmail.com>:
> Jack wrote:
> > Does anyone have a solution that could handle all combinations of
> > alphanumeric and -, space
>
> I don't at all understand what you're *trying* to do, but the answer to
> this question is:
> m/^[\w\s-]+$/
That also matches _, which is not commonly considered alphanumeric.
/^[[:alnum:]\s-]+$/
or
/^[[:alnum:] -]+$/
depending on whether your definition of 'space' includes tab/CR/LF or
not.
> If that doesn't apply to your situation, you need to post a
> short-but-complete script, along with sample input, desired output, and
> actual output, that demonstrates the problems you're having. Be sure
> to include at least a couple strings that should match and a couple
> that should not.
Seconded :)
Ben
--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
benmorrow@tiscali.co.uk (Kate Rusby)
------------------------------
Date: 8 Aug 2006 13:16:01 -0700
From: "Xicheng Jia" <xicheng@gmail.com>
Subject: Re: how to match or detect alphanumeric including - or spaces through out
Message-Id: <1155068161.160204.251590@i3g2000cwc.googlegroups.com>
John W. Krahn wrote:
> Jack wrote:
> >
> > I have strings : 'SA-QA TAR TEL-LL' and 'SA QA-TA REG TEL-LL'
> > and I want to test that this is a valid alphanumeric ; I will have
> > strings which vary the space and - position. Looking to find a matcher
> > that allows for a 0 or 1 spaces and 0 or more dashes throughout without
> > overly complicating the regex.. I tried this but it started getting too
> > case specific and complex:
> >
> > $temp = 'SAQA TAR sd-f44TELLL';
> > if ($temp =~ m/^\w+\s?\w*\-*\w*\s?\w*\-*\w+\s?\w+$/) { print "
> > alphanum "; } #
> > # can try this also ! m/^\w+\s*w+$/
> >
> > Does anyone have a solution that could handle all combinations of
> > alphanumeric and -, space
>
> /\A[[:alnum:]][[:alnum:] -]*[[:alnum:]]\z/
>
Hi, John:
this won't match a stand-alone alnum, say $str = "A";
guess he may want to use lookaround for this purpose. :-)
Xicheng
------------------------------
Date: Tue, 8 Aug 2006 22:13:38 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: how to match or detect alphanumeric including - or spaces through out
Message-Id: <2dbnq3-svj.ln1@osiris.mauzo.dyndns.org>
Quoth "Xicheng Jia" <xicheng@gmail.com>:
> John W. Krahn wrote:
> > Jack wrote:
> > >
> > > I have strings : 'SA-QA TAR TEL-LL' and 'SA QA-TA REG TEL-LL'
> > > and I want to test that this is a valid alphanumeric ; I will have
> > > strings which vary the space and - position. Looking to find a matcher
> > > that allows for a 0 or 1 spaces and 0 or more dashes throughout without
> > > overly complicating the regex.. I tried this but it started getting too
> > > case specific and complex:
> > >
> > > $temp = 'SAQA TAR sd-f44TELLL';
> > > if ($temp =~ m/^\w+\s?\w*\-*\w*\s?\w*\-*\w+\s?\w+$/) { print "
> > > alphanum "; } #
> > > # can try this also ! m/^\w+\s*w+$/
> > >
> > > Does anyone have a solution that could handle all combinations of
> > > alphanumeric and -, space
> >
> > /\A[[:alnum:]][[:alnum:] -]*[[:alnum:]]\z/
>
> this won't match a stand-alone alnum, say $str = "A";
> guess he may want to use lookaround for this purpose. :-)
No need for that:
/\A [[:alnum:]] (?: [[:alnum:] -]* [[:alnum:]] )? \z/x
Ben
--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces mollit animos, tristesque mentes erigit.|benmorrow@tiscali.co.uk
Musica vel ipsas arbores et horridas movet feras. |
------------------------------
Date: Tue, 08 Aug 2006 20:11:46 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: how to match or detect alphanumeric including - or spaces through out
Message-Id: <6e6Cg.193559$S61.165417@edtnps90>
Jack wrote:
>
> I have strings : 'SA-QA TAR TEL-LL' and 'SA QA-TA REG TEL-LL'
> and I want to test that this is a valid alphanumeric ; I will have
> strings which vary the space and - position. Looking to find a matcher
> that allows for a 0 or 1 spaces and 0 or more dashes throughout without
> overly complicating the regex.. I tried this but it started getting too
> case specific and complex:
>
> $temp = 'SAQA TAR sd-f44TELLL';
> if ($temp =~ m/^\w+\s?\w*\-*\w*\s?\w*\-*\w+\s?\w+$/) { print "
> alphanum "; } #
> # can try this also ! m/^\w+\s*w+$/
>
> Does anyone have a solution that could handle all combinations of
> alphanumeric and -, space
/\A[[:alnum:]][[:alnum:] -]*[[:alnum:]]\z/
John
--
use Perl;
program
fulfillment
------------------------------
Date: 8 Aug 2006 19:10:46 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: How to use OO and package?
Message-Id: <Xns9819903D435B1castleamber@130.133.1.4>
Michele Dondi <bik.mido@tiscalinet.it> wrote:
> http://perlmonks.org/?node_id=559798
Just read it really fast, but does the author want something like:
my $factory = use Some::Very::Long::But::Unique::Module::Name;
my $obj = $factory->new;
?
Or something like:
use Some::Very::Long::But::Unique::Module::Name as Foo;
my $obj = Foo->new;
Which I personally would prefer :-D.
--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/
------------------------------
Date: Tue, 8 Aug 2006 20:41:27 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: How to use OO and package?
Message-Id: <706nq3-u2j.ln1@osiris.mauzo.dyndns.org>
[perl.beginners removed as my swerver doesn't accept it]
Quoth John Bokma <john@castleamber.com>:
> Michele Dondi <bik.mido@tiscalinet.it> wrote:
>
> > http://perlmonks.org/?node_id=559798
>
> Just read it really fast, but does the author want something like:
>
>
> my $factory = use Some::Very::Long::But::Unique::Module::Name;
> my $obj = $factory->new;
>
> ?
>
> Or something like:
>
> use Some::Very::Long::But::Unique::Module::Name as Foo;
>
> my $obj = Foo->new;
>
>
> Which I personally would prefer :-D.
The former. The latter can be got with aliased.pm.
--
I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain.
ben@morrow.me.uk Frank Herbert, 'Dune'
------------------------------
Date: Tue, 08 Aug 2006 19:21:32 -0000
From: HaroldWho <hlarons@yahoo.com>
Subject: Solved:: Printing Hash Marks During File Download
Message-Id: <slrnedhp1r.l6.hlarons@Beagle.mylocal.net>
On Mon, 07 Aug 2006 17:32:19 -0000, HaroldWho wrote in comp.lang.perl.misc:
> On 06 Aug 2006 02:01:28 GMT, xhoster@gmail.com wrote in comp.lang.perl.misc:
>> HaroldWho <hlarons@yahoo.com> wrote:
>>> xhoster@gmail.com wrote on Aug 1 14:36:20 2006:
>>> >>HaroldWho <hlarons@yahoo.com> wrote:
> ...
>>> I tried a version of this using Time::HiRes, floating times, and
>>> 'eval', thus:
> ...
>>> Alas, the 'print #' operation seems to get buffered or something, and
>>> does not appear until the 'long op' has finished.
>>
>> OK, so you need to unbuffer your output.
>> See $| in perlvar or autoflush in IO::Handle
...
> Once again, sincere thanks to you and DavidFilmer for sticking with me on
> this. I had read about the $| var, but wasn't sure how it applied to my
> case, before the bufered output thing arose.
A simple 'select STDOUT; $| =1;' and behold, problem solved.
BTW, I learned this from perlvar:
"Because loading in the IO::Handle class is an expensive operation, you
should learn how to use the regular built-in variables."
Thanks again folks; you made it happen.
HW
--
Powered by Slackware 10.2 Linux -- Kernel 2.6.13
News Reader slrn 0.9.8.1
------------------------------
Date: 8 Aug 2006 13:34:21 -0700
From: usenet@DavidFilmer.com
Subject: Re: Solved:: Printing Hash Marks During File Download
Message-Id: <1155069261.570757.56640@p79g2000cwp.googlegroups.com>
HaroldWho wrote:
> BTW, I learned this from perlvar:
> "Because loading in the IO::Handle class is an expensive operation, you
> should learn how to use the regular built-in variables."
FWIW, the least expensive option is not always best. See the discussion
on autoflushing pp224-226 in 'Perl Best Practices' by Damian Conway.
If you don't have this book, you should (and it's too bad Dr. Conway
didn't write it 8 years ago so I could have learned good Perl
programming practices from the beginning).
--
David Filmer (http://DavidFilmer.com)
------------------------------
Date: Tue, 08 Aug 2006 16:20:18 -0400
From: mickey <micky@hotmail.com>
Subject: Sort keys in a hash numerically
Message-Id: <ebarm1$imn$1@charm.magnus.acs.ohio-state.edu>
Hi,
I have a has with keys like
Char. 123
Char. 721
Char. 234
and so on, that is a char followed by an integer.
I want to iterate over the whole hash in numerical order. At present using
foreach my $key(sort (keys %char))
sorts them but in lexicographic order. Is there a way in Perl around that?
Thanks,
-M
------------------------------
Date: 8 Aug 2006 13:27:45 -0700
From: usenet@DavidFilmer.com
Subject: Re: Sort keys in a hash numerically
Message-Id: <1155068865.412501.285240@m73g2000cwd.googlegroups.com>
mickey wrote:
> sorts them but in lexicographic order. Is there a way in Perl around that?
perldoc -q sort
Your question is answered in the topic:
How do I sort an array by (anything)?
--
David Filmer (http://DavidFilmer.com)
------------------------------
Date: 08 Aug 2006 20:25:49 GMT
From: xhoster@gmail.com
Subject: Re: Sort keys in a hash numerically
Message-Id: <20060808163501.934$W1@newsreader.com>
mickey <micky@hotmail.com> wrote:
> Hi,
>
> I have a has with keys like
> Char. 123
> Char. 721
> Char. 234
>
> and so on, that is a char followed by an integer.
If all the keys start with "Char. ", that seems pretty uninteresting.
Why not just strip that off and store only the number?
>
> I want to iterate over the whole hash in numerical order. At present
> using
>
> foreach my $key(sort (keys %char))
foreach my $key (sort {sfrn($a) <=> sfrn($b)} keys %char)
where sfrn is Some Function that takes a key and Returns the Numberic
portion.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 8 Aug 2006 13:36:24 -0700
From: "Xicheng Jia" <xicheng@gmail.com>
Subject: Re: Sort keys in a hash numerically
Message-Id: <1155069384.884900.40740@i3g2000cwc.googlegroups.com>
mickey wrote:
> Hi,
>
> I have a has with keys like
> Char. 123
> Char. 721
> Char. 234
>
> and so on, that is a char followed by an integer.
>
> I want to iterate over the whole hash in numerical order. At present using
>
> foreach my $key(sort (keys %char))
>
> sorts them but in lexicographic order. Is there a way in Perl around that?
>
A solution with the Schwartzian Transform:
my %hash = (
"Char.123" => "part1",
"Char.721" => "part2",
"Char.234" => "part3",
);
print map { "$_->[0] => $hash{$_->[0]}\n" }
sort { $a->[1][1] <=> $b->[1][1] }
map { [ $_, [ split/\./ ] ] }
keys %hash;
Xicheng
------------------------------
Date: Tue, 08 Aug 2006 17:34:11 -0400
From: mickey <micky@hotmail.com>
Subject: Re: Sort keys in a hash numerically
Message-Id: <ebb00j$itb$1@charm.magnus.acs.ohio-state.edu>
xhoster@gmail.com wrote:
> mickey <micky@hotmail.com> wrote:
>
>>Hi,
>>
>>I have a has with keys like
>>Char. 123
>>Char. 721
>>Char. 234
>>
>>and so on, that is a char followed by an integer.
>
>
> If all the keys start with "Char. ", that seems pretty uninteresting.
> Why not just strip that off and store only the number?
>
Thanks everyone. I just stripped off the chars which made everything
simpler.
-M
------------------------------
Date: Wed, 9 Aug 2006 09:42:56 +1200
From: "Tintin" <tintin@invalid.invalid>
Subject: Re: which file, regestry, environment variables do perl install
Message-Id: <44d8f82c$0$1560$88260bb3@free.teranews.com>
"yav" <yan.vulich@gmail.com> wrote in message
news:1155056483.190863.99310@h48g2000cwc.googlegroups.com...
> Regarding Ben's question.
> I'm want to include perl as part of installation of my company product,
> install\uninstall our product should add\remove particular perl version
> and shouldn't touch other perl installed on the machine.
Sound like you could use PAR instead. http://par.perl.org/
--
Posted via a free Usenet account from http://www.teranews.com
------------------------------
Date: Tue, 08 Aug 2006 22:42:15 +0100
From: Henry Law <news@lawshouse.org>
Subject: Re: which file, regestry, environment variables do perl install
Message-Id: <1155073334.66379.0@demeter.uk.clara.net>
yav wrote:
> Regarding Ben's question.
> I'm want to include perl as part of installation of my company product,
If by "Perl" you mean ActiveState Perl then you should read the license
terms quite carefully, since they seem to me to forbid, or at least
restrict, this. (They are installed with the ActiveState distribution:
click on "License and Copyright" near the top of the "navigation" pane
when viewing the HTML help).
--
Henry Law <>< Manchester, England
------------------------------
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 9578
***************************************