[26358] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8531 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 17 06:05:25 2005

Date: Mon, 17 Oct 2005 03:05:04 -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: 8531

Today's topics:
        finding if a hash entry exists <zebee@zip.com.au>
    Re: finding if a hash entry exists <1usa@llenroc.ude.invalid>
    Re: finding if a hash entry exists <jurgenex@hotmail.com>
    Re: finding if a hash entry exists <zebee@zip.com.au>
    Re: finding if a hash entry exists <peter@ginini.com>
        making same change to *lots* of files, *without* date c (David Combs)
    Re: making same change to *lots* of files, *without* da <1usa@llenroc.ude.invalid>
    Re: making same change to *lots* of files, *without* da <tester@uussssh.com>
    Re: making same change to *lots* of files, *without* da <someone@example.com>
    Re: making same change to *lots* of files, *without* da <tintin@invalid.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 17 Oct 2005 04:45:46 GMT
From: Zebee Johnstone <zebee@zip.com.au>
Subject: finding if a hash entry exists
Message-Id: <slrndl6b17.g9j.zebee@zeus.zipworld.com.au>

THis has got to be documented somewhere, but where?  Couldn't find it
in learning perl - where they document what happens, but not how to get
around it, nor in perlreftut or perldsc.  It's an obvious problem so
please point me where to look.

I have a hash, $hash.

If may or may not have an entry
	$hash->{'fred"}

that entry if it exists, may or may not have the value 0.

How do I determine if it exists without warnings if it doesn't?  

#!/usr/bin/perl -w
use strict;
use Data::Dumper;

my $hash;
$hash->{'one'} = 1;
$hash->{'zero'} = 0;

for my $item (qw(one zero two)) {
        if ($hash->{$item}) {
                print "$item $hash->{$item}\n";
        }
        else {
                print "$item doesn't exist\n";
        }
}
v

gives

one 1
zero doesn't exist
two doesn't exist

$hash->{'zero'} does exist, but how to determine it?

If the if statement is 
	if (($hash->{$item}) or ($hash->{$item} == 0))
then I get warnings

one 1
zero 0
Use of uninitialized value in numeric eq (==) at z.pl line 10.
Use of uninitialized value in concatenation (.) or string at z.pl line
11.
two

is stopping warnings for just that section the only solution?

Zebee

-- 
Zebee Johnstone (zebee@zip.com.au), proud holder of
aus.motorcycles Poser Permit #1.
"Motorcycles are like peanuts... who can stop at just one?"


------------------------------

Date: Mon, 17 Oct 2005 04:53:12 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: finding if a hash entry exists
Message-Id: <Y6G4f.446452$5N3.178826@bgtnsc05-news.ops.worldnet.att.net>

On 2005-10-17, Zebee Johnstone <zebee@zip.com.au> wrote:
> THis has got to be documented somewhere, but where?  Couldn't find it
> in learning perl - where they document what happens, but not how to get
> around it, nor in perlreftut or perldsc.  It's an obvious problem so
> please point me where to look.
>
> I have a hash, $hash.
>
> If may or may not have an entry
> 	$hash->{'fred"}
>
> that entry if it exists, may or may not have the value 0.
>
> How do I determine if it exists without warnings if it doesn't?  
>

You have asked a SAQ (self answering question). See 
http://www.ginini.com/perlsaq.html for more examples of SAQs.

perldoc -f exists

Read also:

perldoc perltoc
perldoc perlfunc
perldoc perlfaq

Yes, all of them, in their entirety.

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 04:55:08 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: finding if a hash entry exists
Message-Id: <M8G4f.4250$nk2.567@trnddc07>

Zebee Johnstone wrote:
> I have a hash, $hash.
>
> If may or may not have an entry
> $hash->{'fred"}
>
> that entry if it exists, may or may not have the value 0.
> How do I determine if it exists without warnings if it doesn't?

This is a SAQ (Self Answering Question): perldoc -f exists

jue 




------------------------------

Date: 17 Oct 2005 06:13:26 GMT
From: Zebee Johnstone <zebee@zip.com.au>
Subject: Re: finding if a hash entry exists
Message-Id: <slrndl6g5m.g9j.zebee@zeus.zipworld.com.au>

In comp.lang.perl.misc on Mon, 17 Oct 2005 04:55:08 GMT
Jürgen Exner <jurgenex@hotmail.com> wrote:
> Zebee Johnstone wrote:
>> I have a hash, $hash.
>>
>> If may or may not have an entry
>> $hash->{'fred"}
>>
>> that entry if it exists, may or may not have the value 0.
>> How do I determine if it exists without warnings if it doesn't?
> 
> This is a SAQ (Self Answering Question): perldoc -f exists

Thank you,.

Once I realised 'exists' was a function and that wasn't an odd way of
saying "try perldoc -f" :)

Zebee


------------------------------

Date: Mon, 17 Oct 2005 22:15:56 +1300
From: "Peter Sundstrom" <peter@ginini.com>
Subject: Re: finding if a hash entry exists
Message-Id: <mZJ4f.968$S24.57584@news.xtra.co.nz>


"Zebee Johnstone" <zebee@zip.com.au> wrote in message 
news:slrndl6b17.g9j.zebee@zeus.zipworld.com.au...
> THis has got to be documented somewhere, but where?  Couldn't find it
> in learning perl - where they document what happens, but not how to get
> around it, nor in perlreftut or perldsc.  It's an obvious problem so
> please point me where to look.
>
> I have a hash, $hash.
>
> If may or may not have an entry
> $hash->{'fred"}
>
> that entry if it exists, may or may not have the value 0.
>
> How do I determine if it exists without warnings if it doesn't?

Congratulations!  You've earned yourself an entry in the Perl SAQ.

http://www.ginini.com/perlsaq.html

There must be something in the water at the moment.  The Perl SAQ has had a 
recent flurry of activity.





------------------------------

Date: 17 Oct 2005 01:27:28 -0400
From: dkcombs@panix.com (David Combs)
Subject: making same change to *lots* of files, *without* date changed
Message-Id: <divco0$t3q$1@panix1.panix.com>


Well, it's time to bite the bullet, and quit fighting
the idea (requirement?) that "/home" isn't for my own
use, but for the automounter's.

Way back with sunos (before solaris), use rooted a lot
of our stuff at /home, and refer to it throughout our
programs.

But solaris says no can do, unless you hack the automounter
files.

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, on which I rely to remind me what I've been
working on recently.

Since I want to make this change to just about EVERYTHING,
I think it best if the dates do NOT change.

So, my question: how would *you* accomplish this task?

(maybe via a oneliner, the argument being a list of
the files that need this minor change?)

Yes, I've seen the faq, but before I try that,
I'd ask here if someone had already doine something
like this.

(Plus knew the dangers involved of dateless-hacking.)

Thanks!

David




------------------------------

Date: Mon, 17 Oct 2005 05:43:00 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: making same change to *lots* of files, *without* date changed
Message-Id: <slrndl6ef6.9k4.1usa@asu1cornelledu.unur.com>

On 2005-10-17, David Combs <dkcombs@panix.com> wrote:
 ...

> 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, on which I rely to remind me what I've been
> working on recently.
>
> Since I want to make this change to just about EVERYTHING,
> I think it best if the dates do NOT change.
>
> So, my question: how would *you* accomplish this task?

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
-- 
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 11:39:29 +0530
From: tester <tester@uussssh.com>
Subject: Re: making same change to *lots* of files, *without* date changed
Message-Id: <XtH4f.8$Oy5.135@news.oracle.com>

David Combs wrote:
> Well, it's time to bite the bullet, and quit fighting
> the idea (requirement?) that "/home" isn't for my own
> use, but for the automounter's.
> 
> Way back with sunos (before solaris), use rooted a lot
> of our stuff at /home, and refer to it throughout our
> programs.
> 
> But solaris says no can do, unless you hack the automounter
> files.
> 
> 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, on which I rely to remind me what I've been
> working on recently.
> 
> Since I want to make this change to just about EVERYTHING,
> I think it best if the dates do NOT change.
> 
> So, my question: how would *you* accomplish this task?
> 
> (maybe via a oneliner, the argument being a list of
> the files that need this minor change?)
> 
> Yes, I've seen the faq, but before I try that,
> I'd ask here if someone had already doine something
> like this.
> 
> (Plus knew the dangers involved of dateless-hacking.)
> 
> Thanks!
> 
> David
> 
> 

I think you can copy files from /home to /myhome with -p option! explore 
man cp

~ tester


------------------------------

Date: Mon, 17 Oct 2005 06:28:25 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: making same change to *lots* of files, *without* date changed
Message-Id: <dwH4f.29049$Io.3499@clgrps13>

David Combs wrote:
> Well, it's time to bite the bullet, and quit fighting
> the idea (requirement?) that "/home" isn't for my own
> use, but for the automounter's.
> 
> Way back with sunos (before solaris), use rooted a lot
> of our stuff at /home, and refer to it throughout our
> programs.
> 
> But solaris says no can do, unless you hack the automounter
> files.
> 
> 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, on which I rely to remind me what I've been
> working on recently.

AFAIK Unix doesn't record the creation dates of files anywhere.


> Since I want to make this change to just about EVERYTHING,
> I think it best if the dates do NOT change.
> 
> So, my question: how would *you* accomplish this task?

man tar
man cpio



John
-- 
use Perl;
program
fulfillment


------------------------------

Date: Mon, 17 Oct 2005 22:24:12 +1300
From: "Tintin" <tintin@invalid.invalid>
Subject: Re: making same change to *lots* of files, *without* date changed
Message-Id: <15K4f.974$S24.57600@news.xtra.co.nz>


"David Combs" <dkcombs@panix.com> wrote in message 
news:divco0$t3q$1@panix1.panix.com...
>
> Well, it's time to bite the bullet, and quit fighting
> the idea (requirement?) that "/home" isn't for my own
> use, but for the automounter's.

Only if you have a need for automounter.

>
> Way back with sunos (before solaris), use rooted a lot
> of our stuff at /home, and refer to it throughout our
> programs.
>
> But solaris says no can do, unless you hack the automounter
> files.

Or simply disable automounter.

>
> 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, on which I rely to remind me what I've been
> working on recently.

No such thing as a creation date in Unix.  You probably meant last 
modification date.

>
> Since I want to make this change to just about EVERYTHING,
> I think it best if the dates do NOT change.
>
> So, my question: how would *you* accomplish this task?

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 




------------------------------

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 8531
***************************************


home help back first fref pref prev next nref lref last post