[22943] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5163 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jul 1 14:06:50 2003

Date: Tue, 1 Jul 2003 11:05:08 -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, 1 Jul 2003     Volume: 10 Number: 5163

Today's topics:
        Delete array element from inside function <jaspax@u.washington.edu>
    Re: Delete array element from inside function (Andrew Perrin (CLists))
    Re: Delete array element from inside function <nobull@mail.com>
    Re: letters in a scalar to array (Carlton Brown)
    Re: looking for Win32::OLE examples (automating Lotus 1 (Tom Niesytto)
        OLE Variant <ken.brown@dialwrap.co.uk>
        perl for Tru64 <bland@umich.edu>
        Problem with tk and displaying on the screen <abuse@sgrail.org>
    Re: Problem with tk and displaying on the screen <ehm.weihs@utanet.at>
    Re: Problem with tk and displaying on the screen <abuse@sgrail.org>
    Re: Problem with tk and displaying on the screen <ehm.weihs@utanet.at>
    Re: Problem with tk and displaying on the screen <abuse@sgrail.org>
    Re: Speed of file vrs dbase access <Juha.Laiho@iki.fi>
    Re: transform 3*0.0 into 0.0, 0.0, 0.0 <nobull@mail.com>
        Using undef as an array subscript (Yehuda Berlinger)
    Re: Using undef as an array subscript (Greg Bacon)
    Re: Using undef as an array subscript <nobull@mail.com>
    Re: Using undef as an array subscript <jaspax@u.washington.edu>
    Re: Using undef as an array subscript (Greg Bacon)
    Re: Using undef as an array subscript <mpapec@yahoo.com>
    Re: Using undef as an array subscript <jaspax@u.washington.edu>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 1 Jul 2003 08:45:28 -0700
From: JS Bangs <jaspax@u.washington.edu>
Subject: Delete array element from inside function
Message-Id: <Pine.A41.4.55.0307010837290.65722@dante18.u.washington.edu>

Hey, all:

I've got a function that would like to be able to delete an array element
from an array in the caller's scope, given only the array element (not the
array itself, or a reference to it). The idea is to have something that
duplicates the functionality of delete(), but which tacks a bit of
other processing onto it. The existing code is quite simple, and looks
like this:

##########

sub delete_seg {
	return undef unless is_segment($_[0]);
	$_[0]->clear;
	delete($_[0]);
}

#########

In the calling script (which has imported the function delete_seg()):

#########

delete_seg($word[3]);

#########

Unfortunately, this does not work. $word[3] remains firmly in place with a
defined value. What can I do to make this work?


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


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

Date: 01 Jul 2003 13:02:29 -0400
From: clists@perrin.socsci.unc.edu (Andrew Perrin (CLists))
Subject: Re: Delete array element from inside function
Message-Id: <84d6guw57e.fsf@perrin.socsci.unc.edu>

JS Bangs <jaspax@u.washington.edu> writes:

> Hey, all:
> 
> I've got a function that would like to be able to delete an array element
> from an array in the caller's scope, given only the array element (not the
> array itself, or a reference to it). 

I don't think this is possible, since $word[3] is a scalar and
contains no reference to @word as a whole.  If you don't need it to be
a general solution, and for some reason you're opposed to passing an
array ref, I suppose you could do something like:

$word[3] = {val => $word[3],
            parent => \@word};
delete_seg($word[3]);

and then:

sub delete_seg {
        my @parent = @{$_[0]->{parent}};
        ...
}

but I don't see why that would be useful.

Again, I don't think there's a general solution (but would be happy to
be proved wrong). You could also probably use tie() for a specific
solution, but again I think it's unlikely to be a better solution than
just passing the array ref.

ap

-- 
----------------------------------------------------------------------
Andrew J Perrin - http://www.unc.edu/~aperrin
Assistant Professor of Sociology, U of North Carolina, Chapel Hill
clists@perrin.socsci.unc.edu * andrew_perrin (at) unc.edu


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

Date: 01 Jul 2003 18:03:38 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Delete array element from inside function
Message-Id: <u9adby89hx.fsf@wcl-l.bham.ac.uk>

JS Bangs <jaspax@u.washington.edu> writes:

> I've got a function that would like to be able to delete an array element
> from an array in the caller's scope, given only the array element (not the
> array itself, or a reference to it). The idea is to have something that
> duplicates the functionality of delete(), but which tacks a bit of
> other processing onto it.

Short answer is you can't.

Slightly longer answer is defined(prototype("CORE::delete")) is false
meaning that the inbuilt delete() cannot be simulated by a subroutine.

Slightly longer answer still is that the scalars that are elements of
an array have an existance independant of the array.  When I delete()
the second element of an array it has no actual effect on the the
scalar that holds the second element, instead if just disconnects if
from the array.

my @array = (1,2,3);
my $r = \$array[1];
delete $array[2];
print $$r;

Similarly if I use the for() or subroutine call mechansims to make $_
or and element of @_ into an alias for an array element it becomes an
alias to the scalar that contains the value of the array element - the
alias is in no way associated with the array.

> The existing code is quite simple, and looks
> like this:
> 
> ##########
> 
> sub delete_seg {
> 	return undef unless is_segment($_[0]);
> 	$_[0]->clear;
> 	delete($_[0]);
> }
> 
> #########
> 
> In the calling script (which has imported the function delete_seg()):
> 
> #########
> 
> delete_seg($word[3]);
> 
> #########
> 
> Unfortunately, this does not work. $word[3] remains firmly in place with a
> defined value. What can I do to make this work?

You cannot, you need to avoid wanting to do it.

Without more background I can't be sure, but at a guess I'd say you
appear to be trying to work arround the misconception that Perl
doesn't have destructors.

If this is the case forget about delete_seg() function totally and
rename the 'clear' method to 'DESTROY'.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 1 Jul 2003 08:35:14 -0700
From: carltonbrown@hotmail.com (Carlton Brown)
Subject: Re: letters in a scalar to array
Message-Id: <aa611a32.0307010735.51ece7d4@posting.google.com>

John E <sheltonNOSPAM@onr.com> wrote in message news:<JH3Ma.34637$hV.2124146@twister.austin.rr.com>...
> hello.
> i'd like to take the letters in a scalar and make each of 
> them an element in an array.
> here's what i've got so far....
> 
> #!/usr/bin/perl -w
> $filename='test.pl';
> $filename =~ s/.pl//;
# Note - in the above line, if you want to exclude filenames that look
like PERl scripts,  you probably really want at least /\.pl/, perhaps
even /\.pl$/, or maybe even /\.p(er)?l$/, for reasons described
elsewhere in the thread.  Otherwise, disregard.  Totally up to you.

# To harvest your "letters" you want something like this:
@blah = $filename =~ /([A-Za-z])/g;

That'll put all the "letters" in an array, as you asked.  
(Note that for purposes of this demonstration, "letters" is taken in
the traditional sense and therefore does not include digits,
whitespace, metacharacters, kana, etc.  If you believe otherwise, then
you'll want to change the character class accordingly).


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

Date: 1 Jul 2003 10:47:31 -0700
From: woland99@yahoo.com (Tom Niesytto)
Subject: Re: looking for Win32::OLE examples (automating Lotus 123 and Approach)
Message-Id: <694461f6.0307010947.436eb9f5@posting.google.com>

rook_5150@yahoo.com (Bryan Castillo) wrote in message  
> If you have ActiveState installed in C:\perl look at
> 
> C:\Perl\html\OLE-Browser\Browser.html
> 
> It will allow you to browse through OLE objects available to your system.
> It will show methods, attributes etc.....

Thanks Bryan - that is really extremely helpful - now at least I can see 
some syntax.

JT


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

Date: Tue, 1 Jul 2003 18:48:39 -0700
From: "Ken Brown" <ken.brown@dialwrap.co.uk>
Subject: OLE Variant
Message-Id: <bdshcf$t60$1@news6.svr.pol.co.uk>

Trying to get excel formatted as dd/mm/yyyy into perl variable

seems to be ruturning ole::variant but i cant get any sense out of return
date

    $$item = Variant(VT_DATE, "$sheet->Cells(6,$CurCol)->{Value}");
    $$item = $$item->Date("dd/MM/yyyy");

it keeps returing 30/12/1899 - not overly helpful

any ideas?




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

Date: Tue, 01 Jul 2003 12:38:46 -0400
From: Peyton Bland <bland@umich.edu>
Subject: perl for Tru64
Message-Id: <010720031238465875%bland@umich.edu>

Hi,

Sorry if this is a lame question, but I'm having trouble finding a
compiled perl for a Tru64 V5.x system. ...

http://www.cpan.org/ports/ states:
"Starting from Tru64 V5.0 Perl 5 ships standard with Tru64 as
/usr/bin/perl, but the runtime support (modules and documentation) are
in a separate optional subset. (As of Tru64 V5.0 5.004_04, but 5.005_03
is on the supplementary freeware CD-ROM.) "

I have the perl executable as stated above.  But what is the best way
to get the runtime support files?

Thanks,
Peyton Bland


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

Date: Tue, 01 Jul 2003 16:15:39 GMT
From: derek / nul <abuse@sgrail.org>
Subject: Problem with tk and displaying on the screen
Message-Id: <6gc3gv0l5ujfdkmqhgl5v1rb2v78d1e314@4ax.com>

I have just copied this program from a book (Active perl Developers Guide)

I get an error:-

Undefined subroutine &main::MainLoop called at tk2.pl line 28. (MainLoop();)

What have I missed?

Derek
 


#win32 Activestate 5.8.0

use strict;
use warnings;
use tk;

my $main;
my $label;
my $button;
my $icon;

$main = MainWindow->new();
$main->title("Hello World!");

$label = $main->Label(text => 'Hello from tk!');
$button = $main->Button();

$icon = $button->Photo(-file => 'icon.gif');
$button->configure(image => $icon,
	command => sub { exit; }
	);

$label->pack(side => 'left');
$button->pack(side => 'left',
	padx => 5
	);

MainLoop();



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

Date: Tue, 1 Jul 2003 18:44:50 +0200
From: "Markus W." <ehm.weihs@utanet.at>
Subject: Re: Problem with tk and displaying on the screen
Message-Id: <bdsdln$i1b$1@newsreader1.netway.at>

Hi  Derek!

Try Tk::MainLoop();

Markus Weihs

"derek / nul" <abuse@sgrail.org> schrieb im Newsbeitrag
news:6gc3gv0l5ujfdkmqhgl5v1rb2v78d1e314@4ax.com...
> I have just copied this program from a book (Active perl Developers Guide)
>
> I get an error:-
>
> Undefined subroutine &main::MainLoop called at tk2.pl line 28.
(MainLoop();)
>
> What have I missed?
>
> Derek
>
>
>
> #win32 Activestate 5.8.0
>
> use strict;
> use warnings;
> use tk;
>
> my $main;
> my $label;
> my $button;
> my $icon;
>
> $main = MainWindow->new();
> $main->title("Hello World!");
>
> $label = $main->Label(text => 'Hello from tk!');
> $button = $main->Button();
>
> $icon = $button->Photo(-file => 'icon.gif');
> $button->configure(image => $icon,
> command => sub { exit; }
> );
>
> $label->pack(side => 'left');
> $button->pack(side => 'left',
> padx => 5
> );
>
> MainLoop();
>




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

Date: Tue, 01 Jul 2003 16:46:50 GMT
From: derek / nul <abuse@sgrail.org>
Subject: Re: Problem with tk and displaying on the screen
Message-Id: <4ne3gvg5it1trevt3kh8q3thlmeo3kcif7@4ax.com>

On Tue, 1 Jul 2003 18:44:50 +0200, "Markus W." <ehm.weihs@utanet.at> wrote:

>Hi  Derek!
>
>Try Tk::MainLoop();

Undefined subroutine &tk::MainLoop called at tk2.pl line 28.

??

>"derek / nul" <abuse@sgrail.org> schrieb im Newsbeitrag
>news:6gc3gv0l5ujfdkmqhgl5v1rb2v78d1e314@4ax.com...
>> I have just copied this program from a book (Active perl Developers Guide)
>>
>> I get an error:-
>>
>> Undefined subroutine &main::MainLoop called at tk2.pl line 28.
>(MainLoop();)
>>
>> What have I missed?
>>
>> Derek
>>
>>
>>
>> #win32 Activestate 5.8.0
>>
>> use strict;
>> use warnings;
>> use tk;
>>
>> my $main;
>> my $label;
>> my $button;
>> my $icon;
>>
>> $main = MainWindow->new();
>> $main->title("Hello World!");
>>
>> $label = $main->Label(text => 'Hello from tk!');
>> $button = $main->Button();
>>
>> $icon = $button->Photo(-file => 'icon.gif');
>> $button->configure(image => $icon,
>> command => sub { exit; }
>> );
>>
>> $label->pack(side => 'left');
>> $button->pack(side => 'left',
>> padx => 5
>> );
>>
>> MainLoop();
>>
>



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

Date: Tue, 1 Jul 2003 19:00:45 +0200
From: "Markus W." <ehm.weihs@utanet.at>
Subject: Re: Problem with tk and displaying on the screen
Message-Id: <bdsejh$iif$1@newsreader1.netway.at>

Did you write
    tk::MainLoop()
or
    Tk::MainLoop()
?


"derek / nul" <abuse@sgrail.org> schrieb im Newsbeitrag
news:4ne3gvg5it1trevt3kh8q3thlmeo3kcif7@4ax.com...
> On Tue, 1 Jul 2003 18:44:50 +0200, "Markus W." <ehm.weihs@utanet.at>
wrote:
>
> >Hi  Derek!
> >
> >Try Tk::MainLoop();
>
> Undefined subroutine &tk::MainLoop called at tk2.pl line 28.
>
> ??
>
> >"derek / nul" <abuse@sgrail.org> schrieb im Newsbeitrag
> >news:6gc3gv0l5ujfdkmqhgl5v1rb2v78d1e314@4ax.com...
> >> I have just copied this program from a book (Active perl Developers
Guide)
> >>
> >> I get an error:-
> >>
> >> Undefined subroutine &main::MainLoop called at tk2.pl line 28.
> >(MainLoop();)
> >>
> >> What have I missed?
> >>
> >> Derek
> >>
> >>
> >>
> >> #win32 Activestate 5.8.0
> >>
> >> use strict;
> >> use warnings;
> >> use tk;
> >>
> >> my $main;
> >> my $label;
> >> my $button;
> >> my $icon;
> >>
> >> $main = MainWindow->new();
> >> $main->title("Hello World!");
> >>
> >> $label = $main->Label(text => 'Hello from tk!');
> >> $button = $main->Button();
> >>
> >> $icon = $button->Photo(-file => 'icon.gif');
> >> $button->configure(image => $icon,
> >> command => sub { exit; }
> >> );
> >>
> >> $label->pack(side => 'left');
> >> $button->pack(side => 'left',
> >> padx => 5
> >> );
> >>
> >> MainLoop();
> >>
> >
>




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

Date: Tue, 01 Jul 2003 17:11:23 GMT
From: derek / nul <abuse@sgrail.org>
Subject: Re: Problem with tk and displaying on the screen
Message-Id: <q2g3gvka9ougjqmk7ejjhvga0n8b7hhbmj@4ax.com>

good call and thank you.

The use tk; was wrong

put in use Tk;  and all is well

many thanks

On Tue, 1 Jul 2003 19:00:45 +0200, "Markus W." <ehm.weihs@utanet.at> wrote:

,snip>
>Did you write
>    tk::MainLoop()
>or
>    Tk::MainLoop()
>?
>
>
>> >> #win32 Activestate 5.8.0
>> >>
>> >> use strict;
>> >> use warnings;
>> >> use tk;
^^^^^^^^^^^^^^^^^^^^
>> >> my $main;
>> >> my $label;
>> >> my $button;
>> >> my $icon;
>> >>
>> >> $main = MainWindow->new();
>> >> $main->title("Hello World!");
>> >>
>> >> $label = $main->Label(text => 'Hello from tk!');
>> >> $button = $main->Button();
>> >>
>> >> $icon = $button->Photo(-file => 'icon.gif');
>> >> $button->configure(image => $icon,
>> >> command => sub { exit; }
>> >> );
>> >>
>> >> $label->pack(side => 'left');
>> >> $button->pack(side => 'left',
>> >> padx => 5
>> >> );
>> >>
>> >> MainLoop();
>> >>
>> >
>>
>



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

Date: Tue, 01 Jul 2003 16:42:00 GMT
From: Juha Laiho <Juha.Laiho@iki.fi>
Subject: Re: Speed of file vrs dbase access
Message-Id: <bdsdjd$p4v$1@ichaos.ichaos-int>

First my apologies for _notoriously_ bad reading to Fatted.
Then a revised response.

"Fatted" <fatted@yahoo.com> said:
>Data currently is stored in a plain text file on linux system, less than
>600 ascii chars per file, perhaps 100 files. Accessed using perls open
>command and <>.
>I'm thinking of storing contents of each file in a table, 1 file content
>per row.

Yes, this makes sense -- you would have as the key field on the row the
data that you currently store as file name. For some reason I was
previously reading this as storing one piece of data in one table.
Thus my previous confusing (and confused) response.

And still, as you only have one key-value pair, DB_File still would
be a very real alternative (and even comes with the Perl distribution,
I think). Speed-wise, DB_File should be roughly as good as MySQL, if
not better.

And as for an _easy_ way to access a file in the DB_File format, see
documentation for "tie" function: so, you can access this kind of file
just as you would access a hash. This might not be overall as efficient
as the direct DB_File interface, but not too bad either.

>I was just wondering whether there would be appreciable differences,
>between the two, perhaps answers of the "Are you crazy doing it like
>that, stick to ..." variety (and maybe a because :).

Well, having it split to the separate files is a strange solution,
given your data size - but that might be a choice dictated by those
parts of your environment you chose not to disclose. As it's currently
a set of files, I guess you don't have concurrency issues (multiple
programs modifying the set of files while other copies of the same
program are trying to read the data). So, still guessing there will
be just one copy of the program running as any given time.

This sounds like you could even use a single text file read into a hash
when the program starts, and possibly write modifications back to the
single file upon exiting the program - and still have better performance
than with your current solution. But as the "hash files" (DB_File,
NDBM_File, ...) are created to handle these kinds of data, most possibly
I'd use them - even with this small amount of data.
-- 
Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
         PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)


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

Date: 01 Jul 2003 17:48:03 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: transform 3*0.0 into 0.0, 0.0, 0.0
Message-Id: <u9el1a8a7w.fsf@wcl-l.bham.ac.uk>

CM <starobs99@yahoo.com> writes:

> Thanks to all, I finaly adopted the following program (just for people
> who could have a similar question) :
> 
> --------------------------------------------------------------------
> #!/usr/bin/perl

Always:
  use strict;
  use warnings;

Do not wait for your failure to do so to cause you pain and
embarassement, do it now.

> # Transform e.g. 3*4.5 into 4.5 4.5 4.5
> # Transform , into _space_
> # Usage: removestars.pl input.dat > output.dat
> #
> open(INPUT, "<$ARGV[0]") || die "Cannot open $ARGV[0]";

Don't waste your time.  Just use the magic ARGV filehandle.

> while (<INPUT>)
>    {
>      $a =$_;

Either use a named variable or use the default variable $_.

If you do use a named variable make it lexically scoped.

Don't use $a

>      $a =~ s/(\d+)\*(\S+)(,)/print "$2 " for 1 .. $1/e;
>      $a =~ s/(\d+)\*(\S+)($)/print "$2 " for 1 .. $1/e;

Eeek!  

You really should not be printing in the RHS of the s///

You should not use a for loop in a non-void context.

Do you really want to rely on their being whitespace after the comma?

Do you really want only to consider a maximum of 2 * in a line?

>      $a =~ s/,/ /g;
>      print $a;
>    }
> close(INPUT);
> --------------------------------------------------------------------

So did you really want...

 '1, 4*2,3, 4*5, 3*6'

 ...to be reansformed into...

 '2,3 2,3 2,3 2,3 6 6 6 1 '

 ...?

I would have thought you'd have wanted:

 '1 2 2 2 2 3 5 5 5 5 6 6 6' 

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 1 Jul 2003 08:30:21 -0700
From: jon@actcom.co.il (Yehuda Berlinger)
Subject: Using undef as an array subscript
Message-Id: <75e50dfd.0307010730.63f6253@posting.google.com>

This one is a surprise:

    @a = (0,undef);
    @b = (1,2);

    @c[@a] = @b;

I was hoping to see $c[0] == '1', but I got '2'. Apparently, undef is
converted to '0' when used as an array subscript. Is this documented
anywhere, or is it something too obvious to document? I coudn't find
it in the usual places.

I was hoping to avoid doing a loop such as:

    @bb = @b;
    foreach $a (@a) {
        my $b = shift @bb;
        next unless defined $a;
        $c[$a] = $b;
    }

Any thoughts?

Yehuda


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

Date: Tue, 01 Jul 2003 16:21:07 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Using undef as an array subscript
Message-Id: <vg3d7jqrvc874c@corp.supernews.com>

In article <75e50dfd.0307010730.63f6253@posting.google.com>,
    Yehuda Berlinger <jon@actcom.co.il> wrote:

: This one is a surprise:
: 
:     @a = (0,undef);
:     @b = (1,2);
: 
:     @c[@a] = @b;
: 
: I was hoping to see $c[0] == '1', but I got '2'. Apparently, undef is
: converted to '0' when used as an array subscript. Is this documented
: anywhere, or is it something too obvious to document? I coudn't find
: it in the usual places.

Well, it's defined in the perldiag manpage:

    Use of uninitialized value%s
        (W uninitialized) An undefined value was used as if it
        were already defined.  It was interpreted as a "" or a
        0, but maybe it was a mistake.  To suppress this
        warning assign a defined value to your variables.

Certainly this is documented elsewhere, but a quick glance didn't bear
fruit.

: I was hoping to avoid doing a loop such as:
: 
:     @bb = @b;
:     foreach $a (@a) {
:         my $b = shift @bb;
:         next unless defined $a;
:         $c[$a] = $b;
:     }
: 
: Any thoughts?

Can we step up a level conceptually?  What are you trying to do?

Greg
-- 
 . . . aggressors cannot wage total war without introducing socialism.
    -- Ludwig von Mises, *Interventionism: an Economic Analysis*


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

Date: 01 Jul 2003 17:37:53 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Using undef as an array subscript
Message-Id: <u9llvi8b0o.fsf@wcl-l.bham.ac.uk>

jon@actcom.co.il (Yehuda Berlinger) writes:

> This one is a surprise:

Not, really.  I've long since ceased to be _supprised_ by questions in
clpm that result from people's bone-headded refusal to use strict and
warnings.

I am, of course, still _irritated_ by them.

>     @a = (0,undef);
>     @b = (1,2);
> 
>     @c[@a] = @b;
> 
> I was hoping to see $c[0] == '1', but I got '2'. Apparently, undef is
> converted to '0' when used as an array subscript.

Yes. Or, indeed, in any other numeric context.

> Is this documented anywhere, or is it something too obvious to
> document?

It think it is considered too obvious.  I'm not aware of it being
documentented anywhere except perldiag.

> I coudn't find it in the usual places.

use diagnostics;

(Or if you prefer, use warnings, and look up the explaination yourself).

> I was hoping to avoid doing a loop such as:
> 
>     @bb = @b;
>     foreach $a (@a) {
>         my $b = shift @bb;
>         next unless defined $a;
>         $c[$a] = $b;
>     }
> 
> Any thoughts?

Perhaps you could start counting at 1 so that it wouldn't matter that
undef is treated as zero and keeps overwriting $c[0].

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Tue, 1 Jul 2003 09:26:06 -0700
From: JS Bangs <jaspax@u.washington.edu>
Subject: Re: Using undef as an array subscript
Message-Id: <Pine.A41.4.55.0307010922210.83754@dante14.u.washington.edu>

Yehuda Berlinger sikyal:

> This one is a surprise:
>
>     @a = (0,undef);
>     @b = (1,2);
>
>     @c[@a] = @b;
>
> I was hoping to see $c[0] == '1', but I got '2'. Apparently, undef is
> converted to '0' when used as an array subscript. Is this documented
> anywhere, or is it something too obvious to document? I coudn't find
> it in the usual places.
>
> I was hoping to avoid doing a loop such as:
>
>     @bb = @b;
>     foreach $a (@a) {
>         my $b = shift @bb;
>         next unless defined $a;
>         $c[$a] = $b;
>     }
>
> Any thoughts?

Since your goal is apparently to use the contents of @a to define
subscripts of @c, you could just filter the undefs out of @a first:

@a = grep { defined($_) } @a;

No messy loop, no undefs to mess up your subscripting. You could even put
the whole thing inside the subscript definition for @c, if you don't mind
being slightly obtuse:

@c[grep { defined($_) } @a] = @b;


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

Jesus asked them, "Who do you say that I am?"

And they answered, "You are the eschatological manifestation of the ground
of our being, the kerygma in which we find the ultimate meaning of our
interpersonal relationship."

And Jesus said, "What?"


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

Date: Tue, 01 Jul 2003 16:54:54 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Using undef as an array subscript
Message-Id: <vg3f6u6slnr3c4@corp.supernews.com>

In article <Pine.A41.4.55.0307010922210.83754@dante14.u.washington.edu>,
    JS Bangs  <jaspax@u.washington.edu> wrote:

: @c[grep { defined($_) } @a] = @b;

That doesn't throw away the corresponding elements of @b.

Greg
-- 
I would rather be exposed to the inconveniences attending too much
liberty than those attending too small a degree of it. 
    -- Thomas Jefferson


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

Date: Tue, 01 Jul 2003 19:45:17 +0200
From: Matija Papec <mpapec@yahoo.com>
Subject: Re: Using undef as an array subscript
Message-Id: <urh3gvk3et8lk3h1nflpnh0mfnvq0mm912@4ax.com>

X-Ftn-To: Yehuda Berlinger 

jon@actcom.co.il (Yehuda Berlinger) wrote:
>    @a = (0,undef);
>    @b = (1,2);
>
>    @c[@a] = @b;
>
>I was hoping to see $c[0] == '1', but I got '2'. Apparently, undef is
>converted to '0' when used as an array subscript. Is this documented
>anywhere, or is it something too obvious to document? I coudn't find
>it in the usual places.
>
>I was hoping to avoid doing a loop such as:
>
>    @bb = @b;
>    foreach $a (@a) {
>        my $b = shift @bb;
>        next unless defined $a;
>        $c[$a] = $b;
>    }
>
>Any thoughts?

you could,
$c[0] = $b[ 
  (grep defined $a[$_] && !$a[$_], 0..$#a)[-1]
];
just after @c[@a] = @b;

but after this, foreach looks very tempting ;)

btw, why are you having undefs in @a in the first place?



-- 
Matija


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

Date: Tue, 1 Jul 2003 10:39:48 -0700
From: JS Bangs <jaspax@u.washington.edu>
Subject: Re: Using undef as an array subscript
Message-Id: <Pine.A41.4.55.0307011037430.83754@dante14.u.washington.edu>

Greg Bacon sikyal:

> In article <Pine.A41.4.55.0307010922210.83754@dante14.u.washington.edu>,
>     JS Bangs  <jaspax@u.washington.edu> wrote:
>
> : @c[grep { defined($_) } @a] = @b;
>
> That doesn't throw away the corresponding elements of @b.

???? I don't understand what you mean by "throw away the corresponding
elements of @b". Based on the original code we saw, the OP doesn't seem to
want to alter @b at all, but only to make @c into a subset of @b. But
perhaps I've missed something. Can the OP herself clarify?


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

Jesus asked them, "Who do you say that I am?"

And they answered, "You are the eschatological manifestation of the ground
of our being, the kerygma in which we find the ultimate meaning of our
interpersonal relationship."

And Jesus said, "What?"


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

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


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