[26360] in Perl-Users-Digest
Perl-Users Digest, Issue: 8533 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 17 18:05:33 2005
Date: Mon, 17 Oct 2005 15:05:06 -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 Mon, 17 Oct 2005 Volume: 10 Number: 8533
Today's topics:
Re: FAQ 4.59 How do I sort a hash (optionally by value <jgibson@mail.arc.nasa.gov>
Re: FAQ 4.69 How can I use a reference as a hash key? <jgibson@mail.arc.nasa.gov>
Re: making same change to *lots* of files, *without* da <1usa@llenroc.ude.invalid>
Re: making same change to *lots* of files, *without* da <tadmc@augustmail.com>
Re: perldoc -q 'date|time' FAQ -- and -- Re: making sam <1usa@llenroc.ude.invalid>
Request for help <harrison.leigh@gmail.com>
Re: Request for help <glex_no-spam@qwest-spam-no.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 17 Oct 2005 12:46:54 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: FAQ 4.59 How do I sort a hash (optionally by value instead of key)?
Message-Id: <171020051246542647%jgibson@mail.arc.nasa.gov>
In article <dig2kk$35v$1@reader1.panix.com>, PerlFAQ Server
<comdog@panix.com> wrote:
> 4.59: How do I sort a hash (optionally by value instead of key)?
>
> Internally, hashes are stored in a way that prevents you from imposing
> an order on key-value pairs. Instead, you have to sort a list of the
> keys or values:
>
> @keys = sort keys %hash; # sorted by key
> @keys = sort {
> $hash{$a} cmp $hash{$b}
> } keys %hash; # and by value
>
> Here we'll do a reverse numeric sort by value, and if two keys are
I think this is confusing sort "key" with hash "key" and it should read
"... and if two _values_ are identical ... ".
> identical, sort by length of key, or if that fails, by straight ASCII
> comparison of the keys (well, possibly modified by your locale--see
> perllocale).
>
> @keys = sort {
> $hash{$b} <=> $hash{$a}
> ||
> length($b) <=> length($a)
> ||
> $a cmp $b
> } keys %hash;
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
------------------------------
Date: Mon, 17 Oct 2005 12:06:51 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: FAQ 4.69 How can I use a reference as a hash key?
Message-Id: <171020051206518489%jgibson@mail.arc.nasa.gov>
In article <091020051535518794%comdog@panix.com>, brian d foy
<comdog@panix.com> wrote:
> In article <190920051034314514%jgibson@mail.arc.nasa.gov>, Jim Gibson
> <jgibson@mail.arc.nasa.gov> wrote:
>
> > In article <dgm2cl$cgt$1@reader1.panix.com>, PerlFAQ Server
> > <comdog@panix.com> wrote:
> >
> > > This message is one of several periodic postings to comp.lang.perl.misc
> > > intended to make it easier for perl programmers to find answers to
> > > common questions. The core of this message represents an excerpt
> > > from the documentation provided with Perl.
> > >
> > > --------------------------------------------------------------------
> > >
> > > 4.69: How can I use a reference as a hash key?
> > >
> > > You can't do this directly, but you could use the standard
> > > Tie::RefHash
> > > module distributed with Perl.
> >
> > This answer seems incomplete and therefore misleading. You can use hard
> > references as hash keys, because they become stringified and are
> > unique.
>
> That's not using references as keys though (or, at least not in the
> way people want and Perl 6 will provide). Stringified references
> aren't really unique strings either since two references can yeild
> the same string. Of course, they point to the same thing, but the
> user doesn't necessarily have to know that.
Stringified references are unique in the sense that two references to
the same object will be stringified the same, and two references to
different objects will be stringified to different strings.
Your objection that two separate references to the same object will be
stringified to the same string key is exactly the same as objecting to
the fact that two scalar variables containing the same string and used
as hash keys will access the same hash value. The hash is used to
associate a value with a given key "value", not a specific variable,
regardless of whether the variables in question contain a string or a
reference.
You claim to know what people want and that using references as hashes
is not not one such function. I do not claim any such knowledge. I do,
however, claim that the FAQ statement "You can't do this directly" is
incorrect because you can, in fact, use references as hash keys
directly.
I wanted to use references as hash keys once in a program, but wasn't
sure that it would work. I found a reference in a Perl book that
indicated that it should work, so I tried it. It did work and did
exactly what I wanted: associated a value with an object reference (I
can't find that reference now, I am afraid). Fortunately, I did not
encounter this FAQ entry first, or I might have not even tried.
I would think the FAQ would better serve its readers if it explained
how they might use references as hash keys and what the limitations
are, rather than simply stating an untruth.
Thanks for considering my suggestion.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
------------------------------
Date: Mon, 17 Oct 2005 18:14:27 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: making same change to *lots* of files, *without* date changed
Message-Id: <slrndl7qg5.568.1usa@asu1cornelledu.unur.com>
On 2005-10-17, David Combs <dkcombs@panix.com> wrote:
> In article <slrndl6ef6.9k4.1usa@asu1cornelledu.unur.com>,
> A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
>>
>>Process files one by one. First stat the file, and save the result of
>>the stat call, then touch the newly created file with the saved stat
>>information.
>>
>>You can use either the touch command line utility or File::Touch
>>
>><URL: http://search.cpan.org/~nwetters/File-Touch-0.01/>
>>
>>Sinan
>
> One by one, *by hand*?
I don't understand what you don't understand. I am assuming you have
some idea how to get (in Perl) a list of files which need to be changed.
Open each file in that list, save the stat information, open a file to
write to, write the changed contents to that file, and, after closing
that file, touch with the cached stat information.
It is possible to write that script in the time it took you to come up
with this genius response.
Alternatively, you can use the shell script provided by Tintin.
Personally, that's what I would have done, but you asked on a Perl
group.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: Mon, 17 Oct 2005 13:17:58 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: making same change to *lots* of files, *without* date changed
Message-Id: <slrndl7qmm.7mp.tadmc@magna.augustmail.com>
David Combs <dkcombs@panix.com> wrote:
> In article <15K4f.974$S24.57600@news.xtra.co.nz>,
> Tintin <tintin@invalid.invalid> wrote:
>>"David Combs" <dkcombs@panix.com> wrote in message
>>news:divco0$t3q$1@panix1.panix.com...
>>> Well, the time has come to modify several hundred files,
>>> changing every /home to /myhome.
>>>
>>> But doing that will screw up the creation dates of the
>>> files,
I'll assume you meant "modification timestamp" where you
said "creation date".
>>You certainly could do it in Perl, however it's just as easy with simple
>>Unix tools (my example references Perl to make it vaguely relevant to this
>>group), eg:
>>
>>#!/bin/sh
>>for file in *.sh
>>do
>> perl -pe 's:/home:/myhome:g' $file >$file.new && touch -r $file
>>$file.new && mv $file.new $file
>>done
>
>
> AH HA! COOL -- THANK YOU!
>
> Please, (other) perl/unix gurus, visually/mentally verify the
> above nifty-trick` (obvious only once you see it!) and
> RSVP here -- before I go try the thing!
>
> (Of course I'll try it on some test files first --
> but maybe some of you will see some inherent problems,
> eg if a file has certain attributes ...?)
What will it do to a directory named
/homebase
??
A: change it to
/myhomebase
So, we'd better make that:
s:/home/:/myhome/:g
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Mon, 17 Oct 2005 18:17:20 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: perldoc -q 'date|time' FAQ -- and -- Re: making same change to *lots* of files, *without* date changed
Message-Id: <slrndl7qli.568.1usa@asu1cornelledu.unur.com>
> FYI: Here's what "perldoc -q 'date|time' >! foo.out" gets me:
What is your point?
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: Mon, 17 Oct 2005 20:31:09 GMT
From: leigh <harrison.leigh@gmail.com>
Subject: Request for help
Message-Id: <hST4f.4882$cA4.2089@newsfe3-gui.ntli.net>
All,
I'd like to request some help.
I am a network engineer and am currently studying for my Cisco ccie.
I'm doing loads of practice labs trying to get up to speed for the exam.
I have used a bit of Perl in the past - configuring progams such as mrtg
on both linux and windows.
During my lab configurations, I write out false routes on my boxes so
that I have got lots of routes to tweak and play around with. However,
this is a very tedious task, especially when writing out many (sometimes
hundreds) of static routes.
I would like to devise a perl script that I could share with other ccie
candidates (mainly on www.groupstudy.com) to produce random routes that
we can paste into the routers for testing.
routes are entered in the form:-
ip route 1.2.3.0 255.255.255.0 null 0
breakdown:-
ip route - tells the router what this line is for
1.2.3.0 - is the address of a network
255.255.255.0 - is the network mask
null 0 - is the interface that the route points to (null 0 is a logical
interface)
I would need "ip route " to be static
1.2.3.0 to be a random number. Anything from 1 - 223 in the first octet
and anything from 0 - 255 in the latter 3.
The network mask portion would need to be either: 128 192 224 240 248
252 254 or 255 for the first octet
For the three latter octets, 0 128 192 224 240 248 252 254 or 255
Network mask is written from left to right. If one of the octest is
less than 255 then the rest have to be 0. ie:
255.128.0.0 is ok
255.128.192.0 would not work
255.255.255.0 is ok
128.0.0.0 is ok
128.0.224.0 would not work
"null 0" should also be static.
Would it also be possible to enter the amount of routes that I would
like to generate ? i.e - have a script so that I could type in "200"
and it would fire me back 200 random routes.
Any help on this, or any pointers would be greatly appreciated.
On the flip side - if anyone has any network questions - I would be more
than happy to help.
Regards,
Leigh H
------------------------------
Date: Mon, 17 Oct 2005 16:30:16 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Request for help
Message-Id: <IJU4f.25$%F1.714@news.uswest.net>
leigh wrote:
[...]
> Would it also be possible to enter the amount of routes that I would
> like to generate ? i.e - have a script so that I could type in "200"
> and it would fire me back 200 random routes.
Yes. What have you tried?
> Any help on this, or any pointers would be greatly appreciated.
perldoc perlintro
perldoc -f rand
A couple of arrays, maybe a hash to ensure unique network masks, a few
if's and some for loops would be all that's left.
> On the flip side - if anyone has any network questions - I would be more
> than happy to help.
That's for another newsgroup.
------------------------------
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 8533
***************************************