[22778] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4999 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat May 17 18:05:50 2003

Date: Sat, 17 May 2003 15:05:11 -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           Sat, 17 May 2003     Volume: 10 Number: 4999

Today's topics:
    Re: [The/My] trouble with Perl <kevin@vaildc.net>
        cmp in perl (Xah Lee)
    Re: cmp in perl <palladium@spinn.net>
    Re: cmp in perl <palladium@spinn.net>
    Re: fchown and fchmod <grazz@pobox.com>
        how to unzip a file with perl? (niudou)
    Re: how to unzip a file with perl? <noreply@gunnar.cc>
    Re: how to unzip a file with perl? <jurgenex@hotmail.com>
    Re: oddity per sort internal (Xah Lee)
        Perl and MySQL <reply@newsgroup.invalid>
    Re: Perl and MySQL <mbudash@sonic.net>
    Re: Perl and MySQL <abuse@mweb.co.za>
    Re: Perl code segment <heaney@cablespeed.com>
    Re: Perl code segment (Veky)
        Perl debugger prompt question <<##>> <robert.j.sipe@boeing.com>
    Re: Perl debugger prompt question <<##>> (Peter Scott)
        perl to c <a@a.com>
    Re: possible bug in m// ? <REMOVEsdnCAPS@comcast.net>
    Re: possible bug in m// ? (Veky)
    Re: ReadLine with timeout <dt_spam@arcor.de>
    Re: search through @INC for file to open <jaspax@u.washington.edu>
    Re: Visual Basic Conversion to Perl.... Thunder9_NOSPAM@dsemail.net
    Re: Visual Basic Conversion to Perl.... (Veky)
        why key is added to hash after defined ? <perseus_medusa@hotmail.com>
    Re: why key is added to hash after defined ? <jurgenex@hotmail.com>
    Re: why key is added to hash after defined ? (Veky)
    Re: why key is added to hash after defined ? <mpapec@yahoo.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 17 May 2003 15:58:05 -0400
From: Kevin Michael Vail <kevin@vaildc.net>
Subject: Re: [The/My] trouble with Perl
Message-Id: <kevin-2FA120.15580517052003@vienna7.his.com>

In article <3ec3cc7c$0$49101$e4fe514c@news.xs4all.nl>,
 Henk van Bree <geenspam@geenspam.biz> wrote:

> Darren Dunham wrote:
> 
> > What's wrong with Tk?  
> 
> Don't get me started... ;-)
> 
> There's nothing inherently wrong with Tk, apart from the
> trouble that I'm having installing it (see other post).

ppm install Tk

Or are you not using ActiveState?

> Still, I think adding native GUI features to Perl would
> make the world a better place.

It would suddenly mean Perl didn't run on everything, though.
-- 
Kevin Michael Vail | Dogbert: That's circular reasoning.
kevin@vaildc.net   | Dilbert: I prefer to think of it as no loose ends.
http://www.vaildc.net/kevin/


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

Date: 17 May 2003 14:40:45 -0700
From: xah@xahlee.org (Xah Lee)
Subject: cmp in perl
Message-Id: <7fe97cc4.0305171340.56c63cd1@posting.google.com>

what's a good way to check if two files are identical?

I could do a system call

$truth = qx(cmp '$f1' '$f2' )

but this is undesirable.

thanks.

 Xah
 xah@xahlee.org
 http://xahlee.org/PageTwo_dir/more.html


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

Date: Sat, 17 May 2003 15:48:55 -0600
From: "Rodney" <palladium@spinn.net>
Subject: Re: cmp in perl
Message-Id: <vcdbjnr279vj5d@corp.supernews.com>

"Xah Lee" <xah@xahlee.org> wrote in message
news:7fe97cc4.0305171340.56c63cd1@posting.google.com...
> what's a good way to check if two files are identical?
>
> I could do a system call
>
> $truth = qx(cmp '$f1' '$f2' )
>
> but this is undesirable.
>
> thanks.
>
>  Xah
>  xah@xahlee.org
>  http://xahlee.org/PageTwo_dir/more.html

Have your tried the CPAN module File::Compare?

Good Luck,
Rodney




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

Date: Sat, 17 May 2003 16:00:09 -0600
From: "Rodney" <palladium@spinn.net>
Subject: Re: cmp in perl
Message-Id: <vcdc8npg4o0j91@corp.supernews.com>

"Rodney" <palladium@spinn.net> wrote in message
news:vcdbjnr279vj5d@corp.supernews.com...
> "Xah Lee" <xah@xahlee.org> wrote in message
> news:7fe97cc4.0305171340.56c63cd1@posting.google.com...
> > what's a good way to check if two files are identical?
> >
> > I could do a system call
> >
> > $truth = qx(cmp '$f1' '$f2' )
> >
> > but this is undesirable.
> >
> > thanks.
> >
> >  Xah
> >  xah@xahlee.org
> >  http://xahlee.org/PageTwo_dir/more.html
>
> Have your tried the CPAN module File::Compare?
>
> Good Luck,
> Rodney
>
>
There is also the CPAN module cmp which seems to mimic the unix cmp command.

HTH,
Rod




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

Date: Sat, 17 May 2003 18:50:22 GMT
From: Steve Grazzini <grazz@pobox.com>
Subject: Re: fchown and fchmod
Message-Id: <Ovvxa.32875$h42.28184@twister.nyc.rr.com>

Jason McManus <google@sleepdep.net> wrote:
> Rick Delaney <rick.delaney@rogers.com> wrote in message news:<m34r3umtuw.fsf@biff.bort.ca>...
>> google@sleepdep.net (Jason McManus) writes:
>> 
>>> I am really disturbed by the lack of existence of fchmod
>>> and fchown in perl.
>> 
>> perldoc -f syscall
> 
> That's great and all, and that's exactly how I implemented 
> it, but that does not address the lack of portability issue 
> that I was referring to.  The fact that one has to write 
> several dozen lines of code to make their program portable 
> (i.e. checking the syscall number, using file descriptor 
> pointers, etc.), whereas it could be easily implemented directly
> in the chown()/chmod() functions (mirroring the behaviour of 
> perl's stat() by accepting filenames OR filehandles) is the
> problem.

Yeah, that would have been nice.

But Perl's chown and chmod take a list of files, which means 
that this can't be parsed correctly:

    chmod 0700, FH;

You could make it take strings for filenames and globs for
filehandles.

    chmod 0700, *FH;

And maybe fchmod and fchown belong in the POSIX module, just
like fstat.

    use POSIX qw/fchmod/;
    fchmod($mode, fileno(FH));

That's at least nicer than using syscall()... but I don't 
think you can get what you really want without wrecking 
backward compatibility.

-- 
Steve


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

Date: 17 May 2003 09:11:11 -0700
From: niudou@hotmail.com (niudou)
Subject: how to unzip a file with perl?
Message-Id: <324590c7.0305170811.3019d3f3@posting.google.com>

Is there any module(pm files) to deal with the "unzip" process just as
WINZIP or WINRAR does?

Thanks in advance!


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

Date: Sat, 17 May 2003 18:21:35 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: how to unzip a file with perl?
Message-Id: <ba5nj9$pffj1$1@ID-184292.news.dfncis.de>

niudou wrote:
> Is there any module(pm files) to deal with the "unzip" process just as
> WINZIP or WINRAR does?

     Archive::Zip

/ Gunnar

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: Sat, 17 May 2003 16:23:36 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: how to unzip a file with perl?
Message-Id: <cmtxa.7063$%8.1872@nwrddc04.gnilink.net>

niudou wrote:
> Is there any module(pm files) to deal with the "unzip" process just as
> WINZIP or WINRAR does?

What did CPAN returned when you searched for a module with ZIP in its name?

jue




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

Date: 17 May 2003 14:54:41 -0700
From: xah@xahlee.org (Xah Lee)
Subject: Re: oddity per sort internal
Message-Id: <7fe97cc4.0305171354.4225b4bb@posting.google.com>

xah@xahlee.org (Xah Lee) wrote:
> > # problem: sort a given list @aa, by a given ordered superset @os.
> > what's a better way to write what i wanted

tiltonj@erols.com (Jay Tilton) wrote:
>     my %order;
>     @order{@os} = 0 .. $#os;
>     if( my @extras = grep !exists $order{$_}, @aa ) {
>         die "not in the superset: @extras";
>     }
>     my @result = sort { $order{$a} <=> $order{$b} } @aa;
>     print Dumper \@result;


i was hoping that a conspicuous check for existence can be avoided,
assuming the call to the sort predicate should by side-effect take
care of it.

thanks anyway.

 Xah
 xah@xahlee.org
 http://xahlee.org/PageTwo_dir/more.html


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

Date: Sat, 17 May 2003 18:47:52 +0200
From: Prodigy <reply@newsgroup.invalid>
Subject: Perl and MySQL
Message-Id: <ba5p13$1fkt$1@beast.euro.net>

Hi,

I want Perl to connect to a MySQL database, get some data from it, handle
the data (do some things with it), and after these actions, store the data
back in the database.

I have no idea where to start. Which Perl Module do I need? Any good
tutorials/HOWTOS? Any pointers or such?

Thanks in advance, Prodigy


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

Date: Sat, 17 May 2003 17:16:02 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: Perl and MySQL
Message-Id: <mbudash-EFD650.10160117052003@typhoon.sonic.net>

In article <ba5p13$1fkt$1@beast.euro.net>,
 Prodigy <reply@newsgroup.invalid> wrote:

> Hi,
> 
> I want Perl to connect to a MySQL database, get some data from it, handle
> the data (do some things with it), and after these actions, store the data
> back in the database.
> 
> I have no idea where to start. Which Perl Module do I need? Any good
> tutorials/HOWTOS? Any pointers or such?
> 
> Thanks in advance, Prodigy

all you need (assuming you have a network-accessible mysql db somewhere) 
is the DBI and DBD::mysql perl modules installed. then type:

perldoc DBI
 - and -
perldoc DBD::mysql

and read up.

this page can help too, though i'm sure folks will suggest others:

http://mysql.turbolift.com/DBD_3.21.X.php3

good luck!

-- 
Michael Budash


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

Date: Sat, 17 May 2003 21:34:14 +0200
From: "Nico Coetzee" <abuse@mweb.co.za>
Subject: Re: Perl and MySQL
Message-Id: <pan.2003.05.17.19.34.13.732850@mweb.co.za>

On Sat, 17 May 2003 18:47:52 +0200, Prodigy wrote:

> Hi,
> 
> I want Perl to connect to a MySQL database, get some data from it, handle
> the data (do some things with it), and after these actions, store the data
> back in the database.
> 
> I have no idea where to start. Which Perl Module do I need? Any good
> tutorials/HOWTOS? Any pointers or such?
> 
> Thanks in advance, Prodigy

Go to http://search.cpan.org/ and search for mysql - you will find a
number of modules related, but you are really Interested in DBI and
DBD::mysql

Full documentation is on the web site.

9/10 times the modules will be included with your Perl distro, especially
if you use Linux.

You can then try:

 * perldoc DBI

Cheers

-- 
Nico Coetzee

http://www.itfirms.co.za/
http://za.pm.org/
http://forums.databasejournal.com/

To the systems programmer, users and applications serve only to provide a
test load.



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

Date: 17 May 2003 11:20:40 -0400
From: "Tim Heaney" <heaney@cablespeed.com>
Subject: Re: Perl code segment
Message-Id: <87smrdvbuf.fsf@mrbun.watterson>

"Bob" <bigbadbob@yahoo.com> writes:

> Can anyone assist in helpiing decode this code fragment ?
> Or tell me what it might be doing ?
> 
> ($level, $group, $status, $failed) = ($line =~
> /^NetWorker\s+Savegroup:\s+\((\w+)\)\s(.*)\s(\w+),\s+\d+\sclient\(s\)\s\((.*
> )\)\n$/);

If $line matches that regular expression, then $level, $group,
$status, and $failed will contain the things enclosed in the unescaped
parentheses. If $line does not match, then these variables will be
undefined. Perhaps this little script will help.

Tim


#!/usr/bin/perl

use strict;
use warnings;

my @lines = ("NetWorker Savegroup: (foo) bar baz, 123 client(s) (bargle)\n",
	     "NetWorker Savegroup: (foo)  baz, 123 client(s) (bargle)\n",
	     " NetWorker Savegroup: (foo) bar baz, 123 client(s) (bargle)\n",
	     "NetWorker Savegroup: (foo) bar baz, 123 client(s) (bargle)",
	    );

foreach my $line (@lines) {
  print "\n";
  if ( my($level, $group, $status, $failed) = ($line =~ /^NetWorker\s+Savegroup:\s+\((\w+)\)\s(.*)\s(\w+),\s+\d+\sclient\(s\)\s\((.*)\)\n$/) ) {
    print "\$line=[$line]\n";
    print "Matched.\n";
    print "  \$level=[$level]\n";
    print "  \$group=[$group]\n";
    print "  \$status=[$status]\n"; 
    print "  \$failed=[$failed]\n";
  } else {
    print "\$line=[$line]\n";
    print "Did not match.\n";
  }
}
print "\n";


__END__


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

Date: Sat, 17 May 2003 15:21:35 +0000 (UTC)
From: veky@cromath.math.hr (Veky)
Subject: Re: Perl code segment
Message-Id: <ba5k1v$2e4$1@bagan.srce.hr>

Dok je Veky citao comp.lang.perl.misc, pod PIDom 5933 (290117 off, 2 to go...),
primijetio je kreaturu zvanu "Bob" <bigbadbob@yahoo.com>,
ispod cijih su prstiju izasle (izmedu ostalih) sljedece rijeci:

|Can anyone assist in helpiing decode this code fragment ?
|Or tell me what it might be doing ?
|
|($level, $group, $status, $failed) = ($line =~
|/^NetWorker\s+Savegroup:\s+\((\w+)\)\s(.*)\s(\w+),\s+\d+\sclient\(s\)\s\((.*
|)\)\n$/);

If $line equals:
"NetWorker", then some whitespace, then "Savegroup:", some whitespace
again, then some word in parens (call it $1), then something that ends
with a word (without that ending and whitepace immediately before it,
call it $2, and call the ending word $3), then ",", then some
whitespace, then an integer number, then a single whitespace, then
"sclient(s)", then a single whitespace again, and some text (call it $4)
followed by newline at the end;
Then:
set $level to $1, $group to $2, $status to $3 and $failed to $4 .

HTH,
-- 
\#{%	Sad gradi svoj grad iz snova... znaj da mozes i znaj da znas...


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

Date: Sat, 17 May 2003 15:01:34 GMT
From: "Robert" <robert.j.sipe@boeing.com>
Subject: Perl debugger prompt question <<##>>
Message-Id: <HF1DqK.2M@news.boeing.com>

I was in the debuger single stepping my program.  At one point I was
attempting to preview the value of some variables in a sub.  I observed the
debug prompt change from:

DB<17> yaddie yaddie
DB<18> yaddah yaddah

to

DB<<19>>  yaddie yaddah

and retain the <<##>> as the line numbers incremented.

What do the double chevrons indicate?




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

Date: Sat, 17 May 2003 17:24:39 GMT
From: peter@PSDT.com (Peter Scott)
Subject: Re: Perl debugger prompt question <<##>>
Message-Id: <rfuxa.8106$3C2.328978@news3.calgary.shaw.ca>

In article <HF1DqK.2M@news.boeing.com>,
 "Robert" <robert.j.sipe@boeing.com> writes:
>I was in the debuger single stepping my program.  At one point I was
>attempting to preview the value of some variables in a sub.  I observed the
>debug prompt change from:
>
>DB<17> yaddie yaddie
>DB<18> yaddah yaddah
>
>to
>
>DB<<19>>  yaddie yaddah
>
>and retain the <<##>> as the line numbers incremented.
>
>What do the double chevrons indicate?

That you started stepping through code that you entered from the debugger prompt:

$ perl -de 0
Default die handler restored.

Loading DB routines from perl5db.pl version 1.07
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1):   0
  DB<1> s print for 1..2
main::((eval 16)[/usr/lib/perl5/5.6.1/perl5db.pl:1521]:3):
3:      print for 1..2;
  DB<<2>> 

It's like recursive edit mode in Emacs.

-- 
Peter Scott
http://www.perldebugged.com


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

Date: Sat, 17 May 2003 23:16:44 +0800
From: "adams" <a@a.com>
Subject: perl to c
Message-Id: <ba5jlc$1vli$1@justice.itsc.cuhk.edu.hk>

I would like to write a perl program as a prototype for a program that i
would eventually write in c
in what way i need to  alter when  i write the perl prototype

and do perl have any feature has any feature i can use to ease the process

Thanks a lot^^




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

Date: Sat, 17 May 2003 11:27:43 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: possible bug in m// ?
Message-Id: <Xns937E7E89F1D37sdn.comcast@216.166.71.239>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

"Jürgen Exner" <jurgenex@hotmail.com> wrote in
news:4Vhxa.35183$Ur1.22282@nwrddc03.gnilink.net: 

> Well, the string "1234567890" contains surely 5 digits, doesn't it?
It

Reminds me of the joke, "How can you have two coins whose value add
up to 55 cents, if one of them is not a nickel?"  Answer: A
fifty-cent piece and a nickel.  *One* of them is not a nickel!

(Note to non-US people: a 5-cent piece in the USA is called a nickel.
 US coins come in denominations of 1, 5, 10, 25, 50, and 100 cents.)

- -- 
Eric
print scalar reverse sort qw p ekca lre reh 
ts uJ p, $/.r, map $_.$", qw e p h tona e;

-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

iD8DBQE+xmKmY96i4h5M0egRAnpnAKCQKnR2yEJ2kzEWhhl3esV/L3WZNgCdHB/4
HVrQQSBnMJ0YNpmeAJtJoJ8=
=XrJO
-----END PGP SIGNATURE-----


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

Date: Sat, 17 May 2003 16:35:30 +0000 (UTC)
From: veky@cromath.math.hr (Veky)
Subject: Re: possible bug in m// ?
Message-Id: <ba5oci$vu9$1@bagan.srce.hr>

Dok je Veky citao comp.lang.perl.misc, pod PIDom 6981 (290131 off, 0 to go...),
primijetio je kreaturu zvanu "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>,
ispod cijih su prstiju izasle (izmedu ostalih) sljedece rijeci:

|> Well, the string "1234567890" contains surely 5 digits, doesn't it?
|It
|
|Reminds me of the joke, "How can you have two coins whose value add
|up to 55 cents, if one of them is not a nickel?"  Answer: A
|fifty-cent piece and a nickel.  *One* of them is not a nickel!

 ... or this one: Which month of the year has 28 days:-?
Answer: Every. ;-)

-- 
\#{%	Sad gradi svoj grad iz snova... znaj da mozes i znaj da znas...


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

Date: 17 May 2003 16:35:37 GMT
From: Da.Ta. <dt_spam@arcor.de>
Subject: Re: ReadLine with timeout
Message-Id: <slrnbccke3.47.dt_spam@snickers.dahoam>

Sven Köhler wrote:
> hi,
> 
> i badly need a ReadLine-funtion that returns within a given timeout. 
> Does anybody know such a module? or any way to do this?

Try something like this:

#!/usr/bin/perl

use Term::ReadLine;

$t = new Term::ReadLine 'timeout test';

eval {
  local $SIG{ALRM} = sub { die("timeout\n") };
  alarm 10;
  $input = $t->readline("Enter something within 10 seconds: ");
  alarm 0;
};

print(($@=~/timeout/) ? "\nYou loose!\n"
                      : "You win: $input\n");
__END__

perldoc -f eval
perldoc -f alarm

bye,
Da.Ta


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

Date: Sat, 17 May 2003 10:53:47 -0700
From: JS Bangs <jaspax@u.washington.edu>
Subject: Re: search through @INC for file to open
Message-Id: <Pine.A41.4.55.0305171050240.78422@dante18.u.washington.edu>

Jay Tilton sikyal:

> JS Bangs <jaspax@u.washington.edu> wrote:
>
> : Tassilo v. Parseval sikyal:
> :
> : > Also sprach JS Bangs:
> : >
> : > > Is there an easy way to look through all of the directories in @INC trying
> : > > to open() a file there? I've looked through the man pages, and nothing
> : > > leaps out at me. For now I'm just using a small loop, like:
> : > >
> : > >     my $filename = "somefile";
> : > >     for (@INC) {
> : > > 	last if open FILE "$_/$filename";
> : > >     }
> : > >
> : > > But it seems like there should be an easier way to do this.
> : >
> : > It really depends on what you mean by "open".
> :
> Sounds like something I got into discussing recently.
>
> Check groups.google.com for the thread starting at
> Message-ID: <ntm76vskmtc4e89rph5vlk7uigklssmfqo@4ax.com>
>
> The information there should at least provide a starting point for
> you, if not an outright solution.

This is exactly the solution I was looking for. I made some minor tweaks
to fit my needs, and rolled it up into a module that my other modules can
use. Thanks!

Jesse S. Bangs jaspax@u.washington.edu
http://students.washington.edu/jaspax/
http://students.washington.edu/jaspax/blog




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

Date: Sat, 17 May 2003 15:10:38 GMT
From: Thunder9_NOSPAM@dsemail.net
Subject: Re: Visual Basic Conversion to Perl....
Message-Id: <urkccvcbf64gv88v6i0arfsihgl6g34io9@4ax.com>

Thanks!  This will help me focus on what I need to learn.


NOSPAM is antispam


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

Date: Sat, 17 May 2003 15:12:25 +0000 (UTC)
From: veky@cromath.math.hr (Veky)
Subject: Re: Visual Basic Conversion to Perl....
Message-Id: <ba5jgp$8ck$1@bagan.srce.hr>

Dok je Veky citao comp.lang.perl.misc, pod PIDom 5933 (290118 off, 0 to go...),
primijetio je kreaturu zvanu Thunder9_NOSPAM@dsemail.net,
ispod cijih su prstiju izasle (izmedu ostalih) sljedece rijeci:

|>    5.2 Emulate Instr, Left$, Right$, and Mid$ functions.
|
|        substr() does all three cases

index is for the fourth case... :-)

|>    5.4 Assignment to a variable based on a reg expression match within
|> another string
|
|        $string =~ /regular expression/$var = $&/e;

WT* is this:-?? Match, substitution or... :-?

-- 
\#{%	Sad gradi svoj grad iz snova... znaj da mozes i znaj da znas...


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

Date: Sat, 17 May 2003 23:33:04 +0800
From: "j" <perseus_medusa@hotmail.com>
Subject: why key is added to hash after defined ?
Message-Id: <3ec65563@newsgate.hknet.com>

Hi all ,
I have some little code like :

my %hash;
if (defined $hash{foo1}{foo2} ) {
    print "defined\n";
}
foreach $key (keys %hash) {
    print "key: $key\n";
}

It prints foo1 as key.
What I want to do is just check if $hash{foo1}{foo2}, which should be a
string has already been defined. But why it add a key in %hash ?
So what's the proper way of checking undef if I don't want to add any extra
key ?

Thanks.

perseus





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

Date: Sat, 17 May 2003 16:15:15 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: why key is added to hash after defined ?
Message-Id: <netxa.7053$%8.5004@nwrddc04.gnilink.net>

j wrote:
> Hi all ,
> I have some little code like :
>
> my %hash;
> if (defined $hash{foo1}{foo2} ) {
>     print "defined\n";
> }
> foreach $key (keys %hash) {
>     print "key: $key\n";
> }
>
> It prints foo1 as key.
> What I want to do is just check if $hash{foo1}{foo2}, which should be
> a string has already been defined. But why it add a key in %hash ?

Search google for auto-vivication, it has been discussed a few times before.
No need to rehash it again.

> So what's the proper way of checking undef if I don't want to add any
> extra key ?

Check if $hash{foo1} exists. If it doesn't then $hash{foo1}{foo2} can't
exist, either.
Only if $hash{foo1} exists already then test if $hash{foo1}{foo2} exists,
too.

BTW: you may want to double check if it's really "defined" what you are
looking for or if you want to check for "exists". See The Fine Manual about
the difference.

jue




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

Date: Sat, 17 May 2003 16:24:01 +0000 (UTC)
From: veky@cromath.math.hr (Veky)
Subject: Re: why key is added to hash after defined ?
Message-Id: <ba5nn1$o5d$1@bagan.srce.hr>

Dok je Veky citao comp.lang.perl.misc, pod PIDom 6727 (290125 off, 2 to go...),
primijetio je kreaturu zvanu "j" <perseus_medusa@hotmail.com>,
ispod cijih su prstiju izasle (izmedu ostalih) sljedece rijeci:

|Hi all ,
|I have some little code like :
|
|my %hash;
|if (defined $hash{foo1}{foo2} ) {
|    print "defined\n";
|}
|foreach $key (keys %hash) {
|    print "key: $key\n";
|}
|
|It prints foo1 as key.
|What I want to do is just check if $hash{foo1}{foo2}, which should be a
|string has already been defined. But why it add a key in %hash ?
|So what's the proper way of checking undef if I don't want to add any extra
|key ?

Test for  defined $hash{foo1} && defined $hash{foo1}{foo2} ...
this way, if there is no foo1 key in hash, short-circuiting of "&&" will
save you from autovivification...

-- 
\#{%	Sad gradi svoj grad iz snova... znaj da mozes i znaj da znas...


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

Date: Sat, 17 May 2003 21:55:01 +0200
From: Matija Papec <mpapec@yahoo.com>
Subject: Re: why key is added to hash after defined ?
Message-Id: <or4dcvgb2eqo1atq9povmoe05956fmeuv6@4ax.com>

X-Ftn-To: Veky 

veky@cromath.math.hr (Veky) wrote:
>|It prints foo1 as key.
>|What I want to do is just check if $hash{foo1}{foo2}, which should be a
>|string has already been defined. But why it add a key in %hash ?
>|So what's the proper way of checking undef if I don't want to add any extra
>|key ?
>
>Test for  defined $hash{foo1} && defined $hash{foo1}{foo2} ...
>this way, if there is no foo1 key in hash, short-circuiting of "&&" will
                       ^^^^^^^^^^^^^^^^^^^

Actually 'exists' does that, although defined is just fine most of the time.



-- 
Matija


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

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.  

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


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