Re: Perl defined Object, Array, Hash classes

2005-06-13 Thread Larry Wall
You shouldn't be able to reopen/clobber an existing class/module unless
you specify

class Object is augmented {...}
class Object is replaced {...}

or some such (the trait names are still negotiable).  In general,
private classes should start with "my" or "our", though I don't know
if Pugs handles inner classes yet.  Also note that only the class
definition needs special scope annotation, since (unlike Perl 5), Perl
6 searches upward/outward for matching class names when you mention
something like "Object".  You can still get in trouble with an inner
Object hiding an outer Object, but at least you aren't clobbering
everyone's Object definition that way, and global Objects created
outside the scope of your private Object still know which kind of
Object they are.

Larry


Perl defined Object, Array, Hash classes

2005-06-13 Thread Eric
Hey,

Found out this morning that wizard.p6 suddenly stopped wondering and I was 
stumped as to why. The autrijus came along and pointed out that i was 
defineing an Object class of my own. This was obliterating the built in 
class causing all other classes to fail to work at all. It would seem from 
my point of view then that adding class definitions like that through the 
language should not be done in the root namespace. By putting them there we 
obliterate any user defined classes (or vice versa in this case). I think it 
would be much better to instead put those internal definitions in there own 
namespace or seperate them out in some similar manner. That way in the 
future the language can add its own classes without fear of conflicts with 
user defined classes. After all there is no reason to paint ourselves into 
that corner so early, no one wants to have to fight for new internal classes 
because every added class could possibly compromise existing code. 

Well that is my 2 cents and my first post here so please forgive anything 
that apears rude or stupid and guide me on how to improve future posts.

Thanks,

__
Eric Hodges


RE: Array/Hash Slices, multidimensional

2004-04-16 Thread Abhijit A. Mahabal

On Fri, 16 Apr 2004, Aaron Sherman wrote:

> > >   @matrix... = <<1 0 0 1>>;

>
> In the case of:
>
>   @matrix = <<1 2 3 4 5>>;
>
> You need only add the type:
>
>   int @matrix = <<1 2 3 4 5>>;

> There is no string phase, or at least should never be.
> The compiler can
> pre-compute the list:
>
>   int @matrix = ('1','2','3','4','5');
>
> And it then has another obvious pre-computation to perform:
>
>   int @matrix = (+'1', +'2', +'3', +'4', +'5');
>
> And since everything is a constant, you end up with:
>
>   int @matrix = (1, 2, 3, 4, 5);
>

This int business may make sense for a one dimensional array, but I meant
@matrix to be 2 dimensional, and hence you'd need something like

my @matrix is Array of Array of Int = <<1 2 3 4 5>>;
# that syntax won't work, obviously, but I do not know how to take slices,
and that was my original question...

It seems to me that trying to get the compiler to do all that for all
sorts of weird constructs is hard, more work than what we get out of it,
and all you'd probably get is something that works for a few special
cases.

In any case, I used <<1 2 3 4 5>> because it is less typing, and I use
stuff like qw{2 3 5 7 11} in P5 all the time.

--Abhijit


RE: Array/Hash Slices, multidimensional

2004-04-16 Thread Aaron Sherman
On Thu, 2004-04-15 at 18:23, Austin Hastings wrote:

> > @matrix... = <<1 0 0 1>>;

> Keep in mind that you're using a quoting operator. For numbers, you can just
> use (0, 1, 2, 3)
> and probably be better understood. (The <> approach will
> work, but it will take all the numbers through a string phase first,
> followed by needless conversion.)

I agree with most of what you say, but here, you need to be clearer.

In the case of:

@matrix = <<1 2 3 4 5>>;

You need only add the type:

int @matrix = <<1 2 3 4 5>>;

There is no string phase, or at least should never be. The compiler can
pre-compute the list:

int @matrix = ('1','2','3','4','5');

And it then has another obvious pre-computation to perform:

int @matrix = (+'1', +'2', +'3', +'4', +'5');

And since everything is a constant, you end up with:

int @matrix = (1, 2, 3, 4, 5);

It's magic ;-)

The last step above is what I would expect a B::Deparse-like thing for
Perl 6 to produce.

-- 
Aaron Sherman <[EMAIL PROTECTED]>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback




Re: Array/Hash Slices, multidimensional

2004-04-15 Thread Luke Palmer
Austin Hastings writes:
> 
> 
> > -Original Message-
> > From: Abhijit A. Mahabal [mailto:[EMAIL PROTECTED]
> > Sent: Thursday, 15 April, 2004 05:13 PM
> > To: [EMAIL PROTECTED]
> > Subject: Array/Hash Slices, multidimensional
> >
> >
> > As the hash syntax is being worked out, I thought it'd be a good time to
> > ask if the following will be supported in some form:
> >
> > If I have some structure like %foo{"monday"}, %foo{"tuesday"} etc,
> > I can set their values enmass using:
> > %foo<> = <>;
> >
> >
> > What if I had
> > %foo{"monday"}{"food_expenditure"}   = 10;
> > %foo{"tuesday"}{"fuel_expenditure"}  = 100;
> > %foo{"monday"}{"food_expenditure"}   = 15;
> > %foo{"tuesday"}{"fuel_expenditure"}  = 150;
> >
> > Can I say %foo... = <<10 100 15 150>>;
> > for some definition of ...?
> 
> No, but thanks to Luke Palmer's "outer" opiterator, you can get it in a
> loop.
> 
> See http://www.perl.com/lpt/a/2004/03/p6pdigest/20040328.html
> 
> Something like:
> 
>   my @workdays = ;
>   my @expense_categories =  misc>;
>   my @error_prone_list_of_unremarked_numbers = (...);
> 
>   for outer(@workdays, @expense_categories) -> $day, $cat
>   {
>   %foo{$day}{$cat} = shift @error_prone_list_of_unremarked_numbers;
>   }
> 
> Or whatever.

Hmm.. that's all well and good, but...

sub deref (%hash, @keylist) is rw {
if @keylist == 1 {
[EMAIL PROTECTED]
}
else {
deref [EMAIL PROTECTED], @keylist[1...]
}
}

(map { deref %foo, @^x } outer(@workdays, @expense_categories)) = 
@error_prone_list_of_unremarked_numbers;

That doesn't work in Perl 5, but I'm allowed to speculate. :-)

Seriously, we haven't heard the word on the new slicing semantics.  All
that may be as easy as:

[EMAIL PROTECTED]@expense_categories} =
@error_prone_list_of_unremarked_numbers;

Though I'd love to be reassured or contradicted... hint, hint.

Luke



RE: Array/Hash Slices, multidimensional

2004-04-15 Thread Austin Hastings


> -Original Message-
> From: Abhijit A. Mahabal [mailto:[EMAIL PROTECTED]
> Sent: Thursday, 15 April, 2004 05:13 PM
> To: [EMAIL PROTECTED]
> Subject: Array/Hash Slices, multidimensional
>
>
> As the hash syntax is being worked out, I thought it'd be a good time to
> ask if the following will be supported in some form:
>
> If I have some structure like %foo{"monday"}, %foo{"tuesday"} etc,
> I can set their values enmass using:
>   %foo<> = <>;
>
>
> What if I had
>   %foo{"monday"}{"food_expenditure"}   = 10;
>   %foo{"tuesday"}{"fuel_expenditure"}  = 100;
>   %foo{"monday"}{"food_expenditure"}   = 15;
>   %foo{"tuesday"}{"fuel_expenditure"}  = 150;
>
> Can I say %foo... = <<10 100 15 150>>;
>   for some definition of ...?

No, but thanks to Luke Palmer's "outer" opiterator, you can get it in a
loop.

See http://www.perl.com/lpt/a/2004/03/p6pdigest/20040328.html

Something like:

  my @workdays = ;
  my @expense_categories = ;
  my @error_prone_list_of_unremarked_numbers = (...);

  for outer(@workdays, @expense_categories) -> $day, $cat
  {
%foo{$day}{$cat} = shift @error_prone_list_of_unremarked_numbers;
  }

Or whatever.

>
> I don't claim that we'd need that frequently.
>
> We probably do need the array version of the same problem frequently,
> though:
>
>   @matrix... = <<1 0 0 1>>;
>

Keep in mind that you're using a quoting operator. For numbers, you can just
use (0, 1, 2, 3)
and probably be better understood. (The <> approach will
work, but it will take all the numbers through a string phase first,
followed by needless conversion.)

> At least we'd need it more frequently if we had it. A2 says that something
> like this will be supported, come A9.

According to popular wisdom, each Apocalypse appears after the previous one
in something like F(n) months, where F(n) is the nth Fibonacci number.
Currently we're waiting for A6, which is secretly the 7th one, since A7
slipped early. Guess how long it will be until A9?

=Austin



Array/Hash Slices, multidimensional

2004-04-15 Thread Abhijit A. Mahabal
As the hash syntax is being worked out, I thought it'd be a good time to
ask if the following will be supported in some form:

If I have some structure like %foo{"monday"}, %foo{"tuesday"} etc,
I can set their values enmass using:
%foo<> = <>;


What if I had
%foo{"monday"}{"food_expenditure"}   = 10;
%foo{"tuesday"}{"fuel_expenditure"}  = 100;
%foo{"monday"}{"food_expenditure"}   = 15;
%foo{"tuesday"}{"fuel_expenditure"}  = 150;

Can I say %foo... = <<10 100 15 150>>;
for some definition of ...?

I don't claim that we'd need that frequently.

We probably do need the array version of the same problem frequently,
though:

@matrix... = <<1 0 0 1>>;

At least we'd need it more frequently if we had it. A2 says that something
like this will be supported, come A9.

--Abhijit



Abhijit A. Mahabal  http://www.cs.indiana.edu/~amahabal/



Re: @array = %hash

2002-09-08 Thread Me

> [run time control of assignment behavior when array contains pairs]

How much have I misunderstood things from a mechanisms
available point of view (as against a practical / nice way to
do things) when I suggest something along the lines of:

my sub op:= (*@list : %adverbs) {
...
if %adverbs{keyed} = PAIR {
...
}
}

# create 2 element hash:
%hash = : { keyed=>PAIR } (1, 2, 3=>4, 5=6);

--
ralph




Re: @array = %hash

2002-09-03 Thread Dan Sugalski

At 8:48 AM -0600 9/3/02, Luke Palmer wrote:
>  > Hmm... I think I'd rather see
>>
>>my $foo is Bag = @array.as('Bag');
>>
>>  The idea being that one could treat hashes and arrays as syntactic
>>  vitamins meaning 'Dictionary' (to use the Smalltalk term) and
>>  'OrderedCollection', but all Collections would implement an C
>>  method allowing conversion between the different classes.
>>
>>  Hmm... you know, the more I think about that idea, the more I like
>>  it...
>
>Likewise.  I've always liked languages with complete standard libraries in
>terms of data structures.  It would be great to see Perl be one of them.

PMCs can be loaded on demand. Make them good and there's no reason 
they can't be in a standard SDK for parrot...
-- 
 Dan

--"it's like this"---
Dan Sugalski  even samurai
[EMAIL PROTECTED] have teddy bears and even
   teddy bears get drunk



Re: @array = %hash

2002-09-03 Thread Luke Palmer

> Hmm... I think I'd rather see 
> 
>   my $foo is Bag = @array.as('Bag');
> 
> The idea being that one could treat hashes and arrays as syntactic
> vitamins meaning 'Dictionary' (to use the Smalltalk term) and
> 'OrderedCollection', but all Collections would implement an C
> method allowing conversion between the different classes.
> 
> Hmm... you know, the more I think about that idea, the more I like
> it... 

Likewise.  I've always liked languages with complete standard libraries in 
terms of data structures.  It would be great to see Perl be one of them.

Luke




Re: @array = %hash

2002-09-03 Thread Piers Cawley

Damian Conway <[EMAIL PROTECTED]> writes:

> Uri Guttman wrote:
>
>> but what simon was saying (and i agree) is the the pair IS a single
>> item. it becomes the key and its value is 'scalars'.
>
> No. If it's a PAIR, then its key is the key and its value is the value.
>
>
>> hashes can now take objects as keys and won't just stringify them.
>
> Correct. But I believe that's only if the hash has a property that marks
> its keys as being objects, not strings:
>
>   my %hash is keyed(REF);
>
> And, even if that's the default, it still oughtn't apply to PAIRs.
> To get keys that are PAIRs, you should have to say:
>
>   my %hash is keyed(PAIR);

I really hope you're wrong about that last one. Or are you really
proposing that, given C, one can't do
C<%hash{$arbitrary_object} = $value> and get back the 'real'
C<$arbitrary_object> from C<%hash.values>, that seems to run counter
to what's been said about perl6 hashes. Requiring C
seems kludgy, but 'is keyed(PAIR)' for anything but optimization or
'contract' stuff seems Bad And Wrong.

>>  @array = ( key => 1, key2 => 3, 4, 5 ) ;
>>  %hash = @array ;
>> what does that do? 3 pairs in the hash or 2 (the first pair is the
>> key
>> for the second pair)?
>
> Three. As above. You'd get:
>
>   %hash{'key'}  = 1;
>   %hash{'key2'} = 2;
>   %hash{'4'}= 5;


I'm not sure I agree. If you're going to make it behave like that
then at least allow us to do something like

%hash = @array but no_special_pairs;

(Not sure that's a good property name, but it's the best I could come
up with at the moment.)

>  > there needs to be some semantic way to select the hash assignment style
>
> C

No; that puts the property in the wrong place. 

-- 
Piers

   "It is a truth universally acknowledged that a language in
possession of a rich syntax must be in need of a rewrite."
 -- Jane Austen?



Re: @array = %hash

2002-09-03 Thread Piers Cawley

David Whipp <[EMAIL PROTECTED]> writes:

> Piers Cawley wrote:
>> Maybe we should just say 'sod it' and implement the entire Smalltalk
>> Collection hierarchy and have done with it? Sets, bags, hashes
>> (dictionaries for the Smalltalker), whatever, all have their uses...
>
> I'm not sure if you were being facetious, 

I wasn't. I'd love to see something akin to the full Smalltalk
collection hierarchy available with Perl. And I'd love to see them
'blessed' into core Perl so people would *use* them. But I'm not sure
it'll actually happen.

> but I do think all the functionality of these should exist: how many
> times do we have to explain, to newbies, the perl idioms for using
> hashes as sets?  Collections boil down to two basic properties:
> ordered/unordered and duplicates/unique. We only have c<%> and c<@>
> available for 4 combinations; and perl uses these to indicate the
> underlying implementation (sans ties), and the type of key. Seems to
> me that we will either end up using c<$> (objects) for most
> collections; or we'll be creative in our use of properties for @ and
> %.

  %foo is Ordered

  @bar is Set

Hmm... I think I'd rather see 

  my $foo is Bag = @array.as('Bag');

The idea being that one could treat hashes and arrays as syntactic
vitamins meaning 'Dictionary' (to use the Smalltalk term) and
'OrderedCollection', but all Collections would implement an C
method allowing conversion between the different classes.

Hmm... you know, the more I think about that idea, the more I like
it... 

-- 
Piers

   "It is a truth universally acknowledged that a language in
possession of a rich syntax must be in need of a rewrite."
 -- Jane Austen?



Re: @array = %hash

2002-09-02 Thread Piers Cawley

Uri Guttman <[EMAIL PROTECTED]> writes:

>> "SC" == Simon Cozens <[EMAIL PROTECTED]> writes:
>
>   SC> [EMAIL PROTECTED] (Damian Conway) writes:
>   >> > hashes can now take objects as keys and won't just stringify them.
>   >> 
>   >> Correct. But I believe that's only if the hash has a property that marks
>   >> its keys as being objects, not strings:
>   >> 
>   >> my %hash is keyed(REF);
>   >> 
>   >> And, even if that's the default, it still oughtn't apply to PAIRs.
>
>   SC> So, uhm, what *does* happen if I do
>
>   SC> $hash{$pair} = "foo";
>
>   SC> Runtime error? And what if I do this:
>
>   SC>my %hash is keyed(REF);
>   SC>$hash{bless $pair, "NotAPairReally"} = "foo";
>   SC>...
>   SC>for %hash.kv -> ($k, $v) {
>   SC>bless $k, "PAIR";
>   SC>}
>
>   SC> Storing pairs as hash keys could lead to interestingly funky data
>   SC> structures.  I'm sad that this is being ruled out.
>
>
> i don't think he said that is ruled out. it is not the default
> behavior. i think if you declared it as keyed(PAIR) it would do what you
> want.

But that implies that *all* the keys will be pairs. Which is
(emphatically) not necessarily the case.

-- 
Piers

   "It is a truth universally acknowledged that a language in
possession of a rich syntax must be in need of a rewrite."
 -- Jane Austen?



Re: @array = %hash

2002-09-02 Thread Damian Conway

Uri Guttman wrote:

> but what about mixing pairs and scalars which was the core of this
> thread? 

Then you get whatever behaviour you defined the hash to give.


> by default it seems assigning such a list to a hash would use
> the pairs as 2 elements 

It's not the right way to think about what happens. Pairs are
always single elements. They don't "flatten" (unless you .kv them).
Instead, the hash, upon encountering a pair, assigns the pair's
value to the entry whose key is the pair's key.


> (and how does that handle even/odd slot issues?).

A pair in an even slot is probably an error and ought to be warned
about at least.


> declaring the hash as pairs makes it take them as pairs during
> assignment. but what if you had a mix of them in an array and needed to
> control the way they get assigned to a hash. 

Like I showed in the last post, if the default isn't doing what you want,
you unpack 'em and iteratively assign 'em the way you want 'em.
Not everything has to be easy. Only the easy things. ;-)


> in some cases you want the array fully listified (maybe a .list method 
 > on the array?) and in other cases you want any pairs to remain as single
 > elements be they key or pair (default behavior?).

Pairs are *always* single elements. By default when a hash is initialized with
one, it internally unpacks it. If you want anything else, you have to tell the
hash to act (i.e. by keyed) differently. Or act (i.e. code) differently yourself.

Damian




Re: @array = %hash

2002-09-02 Thread Uri Guttman

> "DC" == Damian Conway <[EMAIL PROTECTED]> writes:

  DC> Uri Guttman wrote:
  >> so what that attribute does is force the hash to keep all pairs as
  >> single objects. but what about run time control of it? sometimes you
  >> might want a list of pairs to be handled like pairs and other times you
  >> want pairs to be scalars in a hash assignment. is there any way to
  >> manage that?

  DC> Sure. Just use the pairs as explicit keys and values:

  DC>   # Pairs as key/values...
  DC>   %hash = (a=>1, b=>2);
  DC>   %hash = @pairs;

but what about mixing pairs and scalars which was the core of this
thread? by default it seems assigning such a list to a hash would use
the pairs as 2 elements (and how does that handle even/odd slot
issues?). declaring the hash as pairs makes it take them as pairs during
assignment. but what if you had a mix of them in an array and needed to
control the way they get assigned to a hash. in some cases you want the
array fully listified (maybe a .list method on the array?) and in other
cases you want any pairs to remain as single elements be they key or
pair (default behavior?).

uri

-- 
Uri Guttman  --  [EMAIL PROTECTED]   http://www.stemsystems.com
- Stem and Perl Development, Systems Architecture, Design and Coding 
Search or Offer Perl Jobs    http://jobs.perl.org



Re: @array = %hash

2002-09-02 Thread Damian Conway

Uri Guttman wrote:

> so what that attribute does is force the hash to keep all pairs as
> single objects. but what about run time control of it? sometimes you
> might want a list of pairs to be handled like pairs and other times you
> want pairs to be scalars in a hash assignment. is there any way to
> manage that?

Sure. Just use the pairs as explicit keys and values:

# Pairs as key/values...
%hash = (a=>1, b=>2);
%hash = @pairs;

# Pairs as keys and then values...
%hash{a=>1} = b=>2;
for @pairs -> $k, $v { %hash{$k} = $v }


Damian





Re: @array = %hash

2002-09-01 Thread Uri Guttman

> "SC" == Simon Cozens <[EMAIL PROTECTED]> writes:

  SC> [EMAIL PROTECTED] (Damian Conway) writes:
  >> > hashes can now take objects as keys and won't just stringify them.
  >> 
  >> Correct. But I believe that's only if the hash has a property that marks
  >> its keys as being objects, not strings:
  >> 
  >> my %hash is keyed(REF);
  >> 
  >> And, even if that's the default, it still oughtn't apply to PAIRs.

  SC> So, uhm, what *does* happen if I do

  SC> $hash{$pair} = "foo";

  SC> Runtime error? And what if I do this:

  SC>my %hash is keyed(REF);
  SC>$hash{bless $pair, "NotAPairReally"} = "foo";
  SC>...
  SC>for %hash.kv -> ($k, $v) {
  SC>bless $k, "PAIR";
  SC>}

  SC> Storing pairs as hash keys could lead to interestingly funky data
  SC> structures.  I'm sad that this is being ruled out.


i don't think he said that is ruled out. it is not the default
behavior. i think if you declared it as keyed(PAIR) it would do what you
want.

damian just wrote:

And, even if that's the default, it still oughtn't apply to
PAIRs.  To get keys that are PAIRs, you should have to say:

my %hash is keyed(PAIR);

the question then is will that allow you to do the above loop or is that
too late? the hash already stores refs as keys but will it allow the
pair ref as a key? i would think so in that case. but you would have to
declare it like the above to support:

%hash = ( key => val, 1 ) ;

as a single key/value with the key being the pair and its value = 1.

so what that attribute does is force the hash to keep all pairs as
single objects. but what about run time control of it? sometimes you
might want a list of pairs to be handled like pairs and other times you
want pairs to be scalars in a hash assignment. is there any way to
manage that?

uri




-- 
Uri Guttman  --  [EMAIL PROTECTED]   http://www.stemsystems.com
- Stem and Perl Development, Systems Architecture, Design and Coding 
Search or Offer Perl Jobs    http://jobs.perl.org



Re: @array = %hash

2002-09-01 Thread Simon Cozens

[EMAIL PROTECTED] (Damian Conway) writes:
> > hashes can now take objects as keys and won't just stringify them.
> 
> Correct. But I believe that's only if the hash has a property that marks
> its keys as being objects, not strings:
> 
>   my %hash is keyed(REF);
> 
> And, even if that's the default, it still oughtn't apply to PAIRs.

So, uhm, what *does* happen if I do

$hash{$pair} = "foo";

Runtime error? And what if I do this:

   my %hash is keyed(REF);
   $hash{bless $pair, "NotAPairReally"} = "foo";
   ...
   for %hash.kv -> ($k, $v) {
   bless $k, "PAIR";
   }

Storing pairs as hash keys could lead to interestingly funky data
structures.  I'm sad that this is being ruled out.

-- 
I knew that a goodly number of train operating companies had
introduced quiet coaches, but I was still a little concerned
to find that my tickets were prominently marked NOT READING.
- Geraint Jones



Re: @array = %hash

2002-09-01 Thread Damian Conway

Uri Guttman wrote:

> but what simon was saying (and i agree) is the the pair IS a single
> item. it becomes the key and its value is 'scalars'.

No. If it's a PAIR, then its key is the key and its value is the value.


> hashes can now take objects as keys and won't just stringify them.

Correct. But I believe that's only if the hash has a property that marks
its keys as being objects, not strings:

my %hash is keyed(REF);

And, even if that's the default, it still oughtn't apply to PAIRs.
To get keys that are PAIRs, you should have to say:

my %hash is keyed(PAIR);



>   @array = ( key => 1, key2 => 3, 4, 5 ) ;
>   %hash = @array ;
> 
> what does that do? 3 pairs in the hash or 2 (the first pair is the key
> for the second pair)?

Three. As above. You'd get:

%hash{'key'}  = 1;
%hash{'key2'} = 2;
%hash{'4'}= 5;



> it comes down to how much perl6 will look inside the list data assigned
> to a hash. will it scan items and handle pairs specially 

Yes.


> or just act like they are 1 item? 

No.


> and i like pairs a lot. they make the => into something very useful and
> not just sugar. but i think the handling of pairs in hash assignment is
> still not on solid ground.

It is. It's just that that grounding has evidently not been adequately
communicated yet.


 > there needs to be some semantic way to select the hash assignment style

C

 > and a proper definition of the default style.

See above.
And my other post on this topic.

Damian





Re: @array = %hash

2002-08-31 Thread Uri Guttman

> "KF" == Ken Fox <[EMAIL PROTECTED]> writes:

  KF> Simon Cozens wrote:
  >> [EMAIL PROTECTED] (Damian Conway) writes:
  >> 
   %hash4 = ("Something", "mixing", pairs => and, "scalars");
  >>> 
  >>> That's perfectly okay (except you forgot the quotes around the 
  >>> and you have an odd number of elements initializing the hash).
  >> Urgh, no. Either a pair is an atomic entity or it isn't. Which?

  KF> Odd meaning "not correct"... When initializing the hash, the pair
  KF> comes off as a single element. That leaves "scalars" as a key without
  KF> a value. So there's an even, but insufficient, number of elements
  KF> initializing the hash.

but what simon was saying (and i agree) is the the pair IS a single
item. it becomes the key and its value is 'scalars'. hashes can now take
objects as keys and won't just stringify them. i assume some string form
will be used for the internal hash key but the real object ref is saved
and returned by keys and friends.

so the question really is, what does assigning a list of mixed scalars
AND pairs to hash do? are the pairs listified and the whole thing
assigned 2 by 2? or are the pairs kept as single objects and they become
key or value according to their even/odd slot?

the earlier post said something about a list of pairs being assigned to
a hash works just fine. but if i do:

@array = ( key => 1, key2 => 3, 4, 5 ) ;
%hash = @array ;

what does that do? 3 pairs in the hash or 2 (the first pair is the key
for the second pair)?

it comes down to how much perl6 will look inside the list data assigned
to a hash. will it scan items and handle pairs specially or just act
like they are 1 item? it seems that a list of just pairs will be
assigned to a hash logically but what about a mixed list? both
assignment styles have their uses.

and i like pairs a lot. they make the => into something very useful and
not just sugar. but i think the handling of pairs in hash assignment is
still not on solid ground.  there needs to be some semantic way to
select the hash assignment style and a proper definition of the default
style.

uri

-- 
Uri Guttman  --  [EMAIL PROTECTED]   http://www.stemsystems.com
- Stem and Perl Development, Systems Architecture, Design and Coding 
Search or Offer Perl Jobs    http://jobs.perl.org



Re: @array = %hash

2002-08-31 Thread Ken Fox

Simon Cozens wrote:
> [EMAIL PROTECTED] (Damian Conway) writes:
> 
>>>  %hash4 = ("Something", "mixing", pairs => and, "scalars");
>>
>>That's perfectly okay (except you forgot the quotes around the 
>>and you have an odd number of elements initializing the hash).
> 
> Urgh, no. Either a pair is an atomic entity or it isn't. Which?

Odd meaning "not correct"... When initializing the hash, the pair
comes off as a single element. That leaves "scalars" as a key without
a value. So there's an even, but insufficient, number of elements
initializing the hash.

- Ken




Re: @array = %hash

2002-08-31 Thread Simon Cozens

[EMAIL PROTECTED] (Damian Conway) writes:
> >   %hash4 = ("Something", "mixing", pairs => and, "scalars");
> 
> That's perfectly okay (except you forgot the quotes around the 
> and you have an odd number of elements initializing the hash).

Urgh, no. Either a pair is an atomic entity or it isn't. Which?

-- 
teco < /dev/audio
- Ignatios Souvatzis



Re: @array = %hash

2002-08-31 Thread Damian Conway

Nicholas Clark asked:

>%hash3 = @kv_array
> 
> Is perl6 going to spot that @kv_array has an even number of entries, all
> are scalars (no pairs), and so do this
> 
>for @kv_array -> key, value {
>%hash3{$key} = $value;
>}

Yes. Just like in Perl 5.


> Or is it going to treat non-pairs like this:
> 
>for @kv_array -> key {
>%hash3{$key} = undef; # Or some other suitable default
>}

No.


> And what happens if I write
> 
>   %hash4 = ("Something", "mixing", pairs => and, "scalars");

That's perfectly okay (except you forgot the quotes around the 
and you have an odd number of elements initializing the hash).

Assuming you meant to write:

 %hash4 = ("Something", "mixing", pairs => "and", "scalars", "together");

then you get:

%hash4{"Something"} = "mixing";
%hash4{"pairs"} = "and";
%hash4{"scalars"}   = "together";

Damian





Re: @array = %hash

2002-08-31 Thread Miko O'Sullivan

From: "Nicholas Clark" <[EMAIL PROTECTED]>
> In Damian's excellent perl6 talk, I think he said that by default a hash
> in list context will return a list of pairs. Hence this
> 
>@array = %hash
> 
> for %hash with n keys would give an array of n elements, all pairs.

Will there actually be a "pair" object class?  Something like this...

  foreach my $pair in %myhash {
 print $pair.name, '=', $pair.value, "\n";
  }

-Miko




RE: @array = %hash

2002-08-30 Thread David Whipp

Piers Cawley wrote:
> Maybe we should just say 'sod it' and implement the entire Smalltalk
> Collection hierarchy and have done with it? Sets, bags, hashes
> (dictionaries for the Smalltalker), whatever, all have their uses...

I'm not sure if you were being facetious, but I do think all the
functionality of these should exist: how many times do we have to
explain, to newbies, the perl idioms for using hashes as sets?
Collections boil down to two basic properties: ordered/unordered
and duplicates/unique. We only have c<%> and c<@> available for
4 combinations; and perl uses these to indicate the underlying
implementation (sans ties), and the type of key. Seems to me that
we will either end up using c<$> (objects) for most collections;
or we'll be creative in our use of properties for @ and %.


Dave.



RE: @array = %hash

2002-08-30 Thread David Whipp

Steffen Mueller
> >   %hash4 = ("Something", "mixing", pairs => and, "scalars");
>1   23  4   5
> Perl5 says "Odd number of elements in hash assignment at -e line 1."
> And Perl6 should, too.

Hmm, I rather like the idea of thinking of a %foo variable as a set, not a
map: a map is a set of pairs; with an equivalence class based on the first
of the pair. But I'm not sure if that way of thinking can work in Perl6. if
%set_of_ints is assigned a set of ints, it should return a list of ints, not
a list of pairs. But if it's a map with some DWIM for assignment from lists
of ints, then it would return a list of pairs. Perhaps I need declarations
like:

  my %foo is set; # a set: $foo{bar} returns a boolean
  my %foo is set(int); # set of ints
  my %foo is hash; #default
  my %foo is hash(int, any); # hash whose keys must be ints
  my %foo is hash(string is equiv_class{lc $^a}, any); # case insensitive
string keys

I've just reviewed A2: it doesn't seem to say much about this stuff.

Dave.



Re: @array = %hash

2002-08-30 Thread Simon Cozens

[EMAIL PROTECTED] (Steffen Mueller) writes:
> >   %hash4 = ("Something", "mixing", pairs => and, "scalars");
>1   23  4   5
> Perl5 says "Odd number of elements in hash assignment at -e line 1."
> And Perl6 should, too.

Except that a pair is a single thing.

%hash4 = ("Something", "mixing", Pair->new("pairs", "and"), "scalars");

-- 
You are in a maze of little twisting passages, all different.



Re: @array = %hash

2002-08-30 Thread Steffen Mueller

Nicholas Clark wrote:
[...]
> And what happens if I write
>
>   %hash4 = ("Something", "mixing", pairs => and, "scalars");
   1   23  4   5
Perl5 says "Odd number of elements in hash assignment at -e line 1."
And Perl6 should, too.

IMHO, your example isn't too good anyway. Something involving a pair at the
place where a Perl5-like behaviour would expect to set a value would be even
a bit more evil.

Let's use this example:
%h = ( 'a', pk => 'pv', 'b', 'c');

Other than that, some possible behaviours I could think of would be:

- If the list contains scalars, consider pairs as two scalars (bad).
Resulting hash (key=>value)
a => pk
pv => b
c => undef

- If the list contains pairs, assign the pairs, then do the Perl5-thing of
alternatingly assigning keys and values from the scalars.
a => b
pk => pv
c => undef

- Fatal error.

- Wherever a pair starts but a value is expected, add an undef.
a => undef
pk => pv
b => c

I think the latter would be most intuitive. Maybe.

Steffen
--
@n=(544290696690,305106661574,116357),$b=16,@c=' ,JPacehklnorstu'=~
/./g;for$n(@n){map{$h=int$n/$b**$_;$n-=$b**$_*$h;$c[@c]=$h}c(0..9);
push@p,map{$c[$_]}@c[c($b..$#c)];$#c=$b-1}print@p;sub'c{reverse @_}




@array = %hash

2002-08-30 Thread Nicholas Clark

In Damian's excellent perl6 talk, I think he said that by default a hash
in list context will return a list of pairs. Hence this

   @array = %hash

for %hash with n keys would give an array of n elements, all pairs.
If you want the perl5 tradition of a list alternating key,value,key,value...
you'd say

   @kv_array = %hash.kv

which would give an array of 2n elements.

What I'm not clear about is what happens the other way.

If I then write

   %hash2 = @array

presumably %hash2 is a copy of %hash, with each pair in @array initialising
one key and value in %hash2

But what happens if I write

   %hash3 = @kv_array

Is perl6 going to spot that @kv_array has an even number of entries, all
are scalars (no pairs), and so do this

   for @kv_array -> key, value {
   %hash3{$key} = $value;
   }

Or is it going to treat non-pairs like this:

   for @kv_array -> key {
   %hash3{$key} = undef; # Or some other suitable default
   }

or what?

And what happens if I write

  %hash4 = ("Something", "mixing", pairs => and, "scalars");

Nicholas Clark
-- 
Even better than the real thing:http://nms-cgi.sourceforge.net/



RE: array/hash manipulation [was :what's with 'with'?]

2001-07-25 Thread Sterin, Ilya

Yes, a what you described would work just fine, but I am wondering if just a
loop control variable that can be possibly set at the begining of the loop
which would not necessarily exit the loop, but rather yield kind of a true
or false.

for ($a, $b, $c) (@A, @B, @C) : @B  ### This would assign a true|false 1|0
to control variable when it reaches this loop number.

But I guess incrementing/decrementing the control variable would work just
fine, since it can later be accessed in the loop and to the shortest array.

It can be easily done with a few lines of code without any special vars,
etc..., just by incrementing a counter, while comparing to the shortes
array, but I'm wondering if a control variable would yield other benefits
and if nothing else decrease the amount of written code.

Ilya


-Original Message-
From: David L. Nicol
To: Sterin, Ilya
Cc: Perl 6 Language
Sent: 07/24/2001 6:03 PM
Subject: Re: array/hash manipulation [was :what's with 'with'?]

"Sterin, Ilya" wrote:

> But now I am trying to figure out, if you are not comparing elements
of the
> array and for example if you need to loop through 3 arrays at the same
time,
> but you need to know, whithin the loop, when the shortest array's last
> element is reached, how would that be accomplished within the loop if
the
> array would just be reset and continue until the longest end is
reached?
> 
> Ilya

You have three generator functions that each produce one element at a
time,
and they, and a fourth, flag-checking function (possibly the loop
control variable itself) all have access to a flag that is decremented
by each generator when it starts looping.  For instance, rewrite

for ($c,$d,$e) (@A, @B, @C) { ... };

into something very similar if not exactly like this:



{   
my $arrays_index=0;
while ($arrays_index < @A
and $arrays_index < @B
and $arrays_index < @C){
local (*c, *d, *e) =
($A[$arrays_index % @A],
 $B[$arrays_index % @B],
 $C[$arrays_index % @C]);

...

$arrays_index+=1;
};
};


a workaround of some kind would be required for empty arrays, or
maybe having it die on an empty array is fine.


-- 
   David Nicol 816.235.1187
"Hush!" said the little boy's mother.  But it was too late.  The
emperor's agents had already heard his meek, innocent question and
were rushing through the crowd.  The emperor's agents took the boy
away and he was never, ever, seen or heard from again.



RE: array/hash manipulation [was :what's with 'with'?]

2001-07-21 Thread Sterin, Ilya

> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Saturday, July 21, 2001 5:50 AM
> To: Sterin, Ilya; 'raptor '; Perl 6 Language
> Subject: RE: array/hash manipulation [was :what's with 'with'?]
>
>
> "Sterin, Ilya" <[EMAIL PROTECTED]> wrote:
> > Just one question, how
> > would merge behave on two different sized arrays.
> >
> > @a = (1..5);
> > @b = (1..10);
> > merge(@a, @b);
> >
> > ##Would return (1,1,2,2,3,3,4,4,5,5,??
> >
> > Would it stop on the shortest array.  Couldn't quite find such
> explanation
> > in the RFC.
> >
> I don't think I specified this in the RFC, since I remember having some
> debates with Damian and others about it that weren't resolved. Now that
> I've had a chance to think about this some more, I think the correct
> behaviour is for the shorter list to reset to the start and continue.

That would be one possible way, but when comparing two array of different
lengths, I guess I would expect undef to compare to the elements of the
longest array that bypassed the length of the shortest array, but that
wouldn't be a problem, just extra code to undef all elements of the shortes
array to equal the length of the longest array.

$shor_arr[$_] = undef for (($#long_arr - $#shor_arr)..$#long_arr);

But now I am trying to figure out, if you are not comparing elements of the
array and for example if you need to loop through 3 arrays at the same time,
but you need to know, whithin the loop, when the shortest array's last
element is reached, how would that be accomplished within the loop if the
array would just be reset and continue until the longest end is reached?


Ilya


>It
> is this behaviour that is the source of J and APL's broadcasting
> flexibility. For details see:
>   http://www.jsoftware.com/primer/agreement.htm
>
> --
>   Jeremy Howard
>   [EMAIL PROTECTED]



RE: array/hash manipulation [was :what's with 'with'?]

2001-07-21 Thread Sterin, Ilya



> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Saturday, July 21, 2001 5:50 AM
> To: Sterin, Ilya; 'raptor '; Perl 6 Language
> Subject: RE: array/hash manipulation [was :what's with 'with'?]
>
>
> "Sterin, Ilya" <[EMAIL PROTECTED]> wrote:
> > Just one question, how
> > would merge behave on two different sized arrays.
> >
> > @a = (1..5);
> > @b = (1..10);
> > merge(@a, @b);
> >
> > ##Would return (1,1,2,2,3,3,4,4,5,5,??
> >
> > Would it stop on the shortest array.  Couldn't quite find such
> explanation
> > in the RFC.
> >
> I don't think I specified this in the RFC, since I remember having some
> debates with Damian and others about it that weren't resolved. Now that
> I've had a chance to think about this some more, I think the correct
> behaviour is for the shorter list to reset to the start and continue.

That would be one possible way, but when comparing two array of different
lengths, I guess I would expect undef to compare to the elements of the
longest array that bypassed the length of the shortest array, but that
wouldn't be a problem, just extra code to undef all elements of the shortes
array to equal the length of the longest array.

$shor_arr[$_] = undef for (($#long_arr - $#shor_arr)..$#long_arr);

But now I am trying to figure out, if you are not comparing elements of the
array and for example if you need to loop through 3 arrays at the same time,
but you need to know, whithin the loop, when the shortest array's last
element is reached, how would that be accomplished within the loop if the
array would just be reset and continue until the longest end is reached?


Ilya


>It
> is this behaviour that is the source of J and APL's broadcasting
> flexibility. For details see:
>   http://www.jsoftware.com/primer/agreement.htm
>
> --
>   Jeremy Howard
>   [EMAIL PROTECTED]



RE: array/hash manipulation [was :what's with 'with'?]

2001-07-21 Thread jh_lists

"Sterin, Ilya" <[EMAIL PROTECTED]> wrote:
> Just one question, how
> would merge behave on two different sized arrays.
> 
> @a = (1..5);
> @b = (1..10);
> merge(@a, @b);
> 
> ##Would return (1,1,2,2,3,3,4,4,5,5,??
> 
> Would it stop on the shortest array.  Couldn't quite find such explanation
> in the RFC.
> 
I don't think I specified this in the RFC, since I remember having some
debates with Damian and others about it that weren't resolved. Now that
I've had a chance to think about this some more, I think the correct
behaviour is for the shorter list to reset to the start and continue. It
is this behaviour that is the source of J and APL's broadcasting
flexibility. For details see:
  http://www.jsoftware.com/primer/agreement.htm

-- 
  Jeremy Howard
  [EMAIL PROTECTED]



RE: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Sterin, Ilya



> -Original Message-
> From: Jeremy Howard [mailto:[EMAIL PROTECTED]]
> Sent: Friday, July 20, 2001 8:40 PM
> To: Sterin, Ilya; 'raptor '; [EMAIL PROTECTED]
> Subject: Re: array/hash manipulation [was :what's with 'with'?]
>
>
> "Sterin, Ilya" <[EMAIL PROTECTED]> wrote:
> > Hmmm. Didn't think about that.  That would be a nice way, that
> way you can
> > manipulate it's behaviour depending with how many aliases you provide.
> >
> > for my $el1, $el2 ( (@foo, @bar) ) {
> > print "$el\n"
> >  }
> >
> > $el1 and $el2 would of course be aliases, right?
> >
> I don't think that this special purpose notation is necessary. With the
> improved 'want' proposed by Damian, the following should be easy
> to achieve:
>
>   @a = (1,2,3,4);
>   for ($b,$c) (@a) { print "$b $c"}
>   # prints:
>   # 1 2
>   # 3 4
>   %d = (a=>1, b=>2);
>   for ($b,$c) (@a) { print "$b $c"}
>   # prints:
>   # a 1
>   # b 2
>
> Which with the merge() RFC makes the desired behaviour for multiple lists
> easy:
>
>   @a = (1,2);
>   @b = (3,4);
>   for ($b,$c) merge(@a,@b) { print "$b $c"}
>   # prints:
>   # 1 3
>   # 2 4

Now this would be cool.  I guess this would be easier to interleave the
arrays, than to implement new ways of calling loops.  Just one question, how
would merge behave on two different sized arrays.

@a = (1..5);
@b = (1..10);
merge(@a, @b);

##Would return (1,1,2,2,3,3,4,4,5,5,??

Would it stop on the shortest array.  Couldn't quite find such explanation
in the RFC.

Ilya



>
> So, no really new syntax, no special purpose behaviour, just the obvious
> extension of for-iterators to list context, and the introduction
> of one new
> function (which happens to have many other applications).
>



Re: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Jeremy Howard

"Sterin, Ilya" <[EMAIL PROTECTED]> wrote:
> Hmmm. Didn't think about that.  That would be a nice way, that way you can
> manipulate it's behaviour depending with how many aliases you provide.
>
> for my $el1, $el2 ( (@foo, @bar) ) {
> print "$el\n"
>  }
>
> $el1 and $el2 would of course be aliases, right?
>
I don't think that this special purpose notation is necessary. With the
improved 'want' proposed by Damian, the following should be easy to achieve:

  @a = (1,2,3,4);
  for ($b,$c) (@a) { print "$b $c"}
  # prints:
  # 1 2
  # 3 4
  %d = (a=>1, b=>2);
  for ($b,$c) (@a) { print "$b $c"}
  # prints:
  # a 1
  # b 2

Which with the merge() RFC makes the desired behaviour for multiple lists
easy:

  @a = (1,2);
  @b = (3,4);
  for ($b,$c) merge(@a,@b) { print "$b $c"}
  # prints:
  # 1 3
  # 2 4

So, no really new syntax, no special purpose behaviour, just the obvious
extension of for-iterators to list context, and the introduction of one new
function (which happens to have many other applications).





Re: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Jeremy Howard

"John Porter" wrote:
> Sterin, Ilya wrote:
> > Don't really know which would be more helpful, since I first need to
find a
> > scenerio where I would use this facility, then what result would I
expect
> > once the shortest list runs out.
>
> Let us ask the PDL folks.
>
> In fact, I'm quite sure this has been done already.
>
Well, I'm not a PDL folk, but I'm a p6-data folk so perhaps I qualify.

The interest in non-matching indices comes in 'broadcasting', which,
assuming the element-wise operators mentioned by Larry, works like this:

  @b = (1,2,3);
  @c = (2,4,6);
  @d := @b :* @c;   # Returns (2,8,18)
  @e := 3 :* @c;# Returns (6,12,18)

Notice that the scalar '3' is 'broadcast' across the length of @c just as if
it was the list (3,3,3).

Or if you prefer text-crunching examples to number crunching, it works like
this:

  @people = ('adam', 'eve ', 'bob ');
  @scores = (7,9,5);  # Score for each person
  @histogram := '#' :x @scores; # Returns ('xxx','x','x')
  print join("\n", @people . ' ' . @histogram);

Notice that the scalar '#' has been broadcast across the length of @scores.

For more information, see:
  http://dev.perl.org/rfc/82.html#Broadcasting
which explains the more interesting case of multidimensional broadcasting.
Note that this RFC is a little dated now, in that Larry has proposed the
adverb ':' to mean "apply element-wise", so the examples in the RFC really
need a ':' added before all the operators. Other than that nothing should
need to change.

For Pyton's implementation of this concept, see:
  http://starship.python.net/~da/numtut/array.html#SEC19

See also implementations in J and APL (which is the best role model), PDL,
functional languages like Haskell, and mathematical languages like
Mathematica.





Re: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread John Porter

Sterin, Ilya wrote:
> Don't really know which would be more helpful, since I first need to find a
> scenerio where I would use this facility, then what result would I expect
> once the shortest list runs out.  

Let us ask the PDL folks.

In fact, I'm quite sure this has been done already.

-- 
John Porter




RE: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Sterin, Ilya

Right it can either stop as the shortest list iteration is done, or just set
the corresponding alias to undef.

Don't really know which would be more helpful, since I first need to find a
scenerio where I would use this facility, then what result would I expect
once the shortest list runs out.  Do I still need the values of the longer
list, for one reason or another, or do I want the loop aborted?

Ilya

-Original Message-
From: David L. Nicol
To: Sterin, Ilya
Cc: 'raptor '; '[EMAIL PROTECTED] '
Sent: 07/20/2001 1:44 PM
Subject: Re: array/hash manipulation [was :what's with 'with'?]

"Sterin, Ilya" wrote:
> 
> Hmmm. Didn't think about that.  That would be a nice way, that way you
can
> manipulate it's behaviour depending with how many aliases you provide.
> 
> for my $el1, $el2 ( (@foo, @bar) ) {
> print "$el\n"
>  }
> 
> $el1 and $el2 would of course be aliases, right?
> 
> But one though might be, what happens if this is written...
> 
> for my $el1, $el2 ( (@foo, @bar, @arr) ) {
> print "$el\n"
>  }
> 
> Does this bahave in the same way as the first one, but we just don't
set
> @arr elements, since the user forgot to provide a third alias, or
should
> this croak at compile time?
> 
> Ilya

Given your definition of 

for [list of variables] ([comma-listed arrays]) {...}

I'd consider anything less than a full crash and burn if the number of
variables and the number of arrays did not match to be dangerous.  If
you
want @bar and @arr combined, you could present them as 

for $fooelem, $bar_arr_elem (@foo, (@bar, @arr)) { ...

I would also expect this to stop as soon as the shortest list is out
of elements; also to take other kinds of generators besides
preconstucted
lists in there as the arrays, making the syntax

for [list of N variables] ([list of N generators]) {...}

 
-- 
   David Nicol 816.235.1187
   "Mary had a little chainsaw" -- Angus Oblong



RE: array/hash manipulation

2001-07-20 Thread Eric Roode

On Friday, July 20, Ilya Sterin wrote:
>No, I don't think you are understanding it correctly.  It's not about
>looping sequentially, but rather simultaneouly, for comparison purposes.
>
>@foo = (1,2,3);
>@bar = (1,2,3);
>for my ($foo, $bar) (@foo, @bar)  #As the index for @foo increases, so 
>  #does @bar index
>{
>print "OK\n" if $foo == $bar;
>}
>
>Will print...
>OK
>OK
>OK
>
>Ilya

Yes, right. I'm saying that's very easy to code in perl 5.

@foo = (1, 2, 3);
@bar = (1, 2, 3);
@baz = (1, 2, 3);
$ai = aiter (@foo, @bar, @baz);
while ( ($foo,$bar,$baz) = &$ai )
{
print "OK\n" if $foo == $bar  &&  $bar == $baz;
}

>From my library:(feel free to copy&paste into yours)

#---> $array_iterator = aiter(@a, @b, @c, ...);
# aiter : Creates an array iterator to return the elements of a set of
# arrays in turn.  That is, the first time it is called, it returns
# the first element of each array.  The next time, it returns the
# second elements. And so on, until all elements are exhausted.
#
# This is useful for looping over more than one array at once:
# $ai = aiter(@a, @b);
# while ( ($a,$b) = $ai->() )   {  }
sub aiter (\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@)
{
my @arr_list  = @_; # The list of (references to the) arrays
my $index = 0;  # Which one the caller will get next
my $max_num   = 0;  # Number of elements in longest array

# Get the length of the longest input array
foreach (@arr_list)
{
$max_num = @$_  if @$_ > $max_num;
}

# Return the iterator as a closure wrt the above variables.
return sub
{
my $i = $index++;
return ()  if $i >= $max_num; # No more elements to return
return map $_->[$i], @arr_list;   # Return ith elements
}
}

 --
 Eric J. Roode[EMAIL PROTECTED]
 Senior Software Engineer, Myxa Corporation




RE: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Sterin, Ilya

No, I don't think you are understanding it correctly.  It's not about
looping sequentially, but rather simultaneouly, for comparison purposes.

@foo = (1,2,3);
@bar = (1,2,3);
for my ($foo, $bar) (@foo, @bar)  #As the index for @foo increases, so 
  #does @bar index
{
print "OK\n" if $foo == $bar;
}

Will print...
OK
OK
OK

Ilya

-Original Message-
From: Eric Roode
To: [EMAIL PROTECTED]
Sent: 07/20/2001 11:30 AM
Subject: Re: array/hash manipulation [was :what's with 'with'?]

on Fri Jul 20, Mark REED wrote:
>I'm sorry, but I fail to see how this is a big improvement over the
>current version:
>
>while (my ($key, $val) = each %my_hash)
>{ ... }

And a workalike to

 while ( ($a,$b,$c) = (@a, @b, @c) )
or
 for my ($el1, $el2)  (@foo, @bar)
 
is very easy to code in perl 5. At the risk of sounding reactionary,
this doesn't seem like a Big Win for perl.

 --
 Eric J. Roode[EMAIL PROTECTED]
 Senior Software Engineer, Myxa Corporation



RE: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Sterin, Ilya

It's really not an improvement, but rather a comment, since if aliases and
iterations for numerous arrays were implemented, they would of course have
to somehow behave with hashes, so this would be a bahavior that could be
implemented.

Ilya

-Original Message-
From: Mark J. Reed
To: '[EMAIL PROTECTED] '
Sent: 07/20/2001 11:21 AM
Subject: Re: array/hash manipulation [was :what's with 'with'?]

On Fri, Jul 20, 2001 at 11:17:13AM -0600, Sterin, Ilya wrote:
> But this will be flattened, so I would think
> 
> for my($key, $val)(%my_hash)
> { ... }
> 
> Would be a great convenience.  $key and $val being aliased
accordingly.
I'm sorry, but I fail to see how this is a big improvement over the
current version:

while (my ($key, $val) = each %my_hash)
{ ... }

-- 
Mark J. REED<[EMAIL PROTECTED]>



Re: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Eric Roode

on Fri Jul 20, Mark REED wrote:
>I'm sorry, but I fail to see how this is a big improvement over the
>current version:
>
>while (my ($key, $val) = each %my_hash)
>{ ... }

And a workalike to

 while ( ($a,$b,$c) = (@a, @b, @c) )
or
 for my ($el1, $el2)  (@foo, @bar)
 
is very easy to code in perl 5. At the risk of sounding reactionary,
this doesn't seem like a Big Win for perl.

 --
 Eric J. Roode[EMAIL PROTECTED]
 Senior Software Engineer, Myxa Corporation




Re: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Mark J. Reed

Well, other than the fact that the while(each) doesn't do aliasing. 
Since that would be the whole point, ignore that last message.

On Fri, Jul 20, 2001 at 01:21:57PM -0400, Mark J. Reed wrote:
> On Fri, Jul 20, 2001 at 11:17:13AM -0600, Sterin, Ilya wrote:
> > But this will be flattened, so I would think
> > 
> > for my($key, $val)(%my_hash)
> > { ... }
> > 
> > Would be a great convenience.  $key and $val being aliased accordingly.
> I'm sorry, but I fail to see how this is a big improvement over the
> current version:
> 
> while (my ($key, $val) = each %my_hash)
> { ... }
> 
> -- 
> Mark J. REED  <[EMAIL PROTECTED]>

-- 
Mark J. REED<[EMAIL PROTECTED]>



Re: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread raptor

ooops I forgot if the vars in for are aliesed then it will be ok for using
it like 'with' :

for my $el ( $Request->{Param} ) {
  print $el{qsParam1}
  print $el{qsParam2}

}

but then what will be $_ ... alias OR copy !?! :") I mean mostly backward
compatibility...
One other way is 'local' to make copy & 'my' alias in this particular case
?!?!?! I can't remember the current-descision about 'local'
Say :
for my $el , local $el2 (@a1, @a2) {
   print $_; #alias
   print local $_;#copy
};


Dusk till down :")
=
iVAN
[EMAIL PROTECTED]
=




Re: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Mark J. Reed

On Fri, Jul 20, 2001 at 11:17:13AM -0600, Sterin, Ilya wrote:
> But this will be flattened, so I would think
> 
> for my($key, $val)(%my_hash)
> { ... }
> 
> Would be a great convenience.  $key and $val being aliased accordingly.
I'm sorry, but I fail to see how this is a big improvement over the
current version:

while (my ($key, $val) = each %my_hash)
{ ... }

-- 
Mark J. REED<[EMAIL PROTECTED]>



RE: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Sterin, Ilya

My only concern is with hashes, since they come in no particular order
unless sorted, there probably would not be any use to iterate over the
values of more than one hash?  At least I can't think of any use.  But it
would be nice to iterate over one hash, like so...

for (%my_hash)
{ ... }

But this will be flattened, so I would think

for my($key, $val)(%my_hash)
{ ... }

Would be a great convenience.  $key and $val being aliased accordingly.

Ilya


-Original Message-
From: raptor
To: Sterin, Ilya; [EMAIL PROTECTED]
Sent: 07/20/2001 9:10 AM
Subject: Re: array/hash manipulation [was :what's with 'with'?]

> Hmmm. Didn't think about that.  That would be a nice way, that way you
can
> manipulate it's behaviour depending with how many aliases you provide.
>
> for my $el1, $el2 ( (@foo, @bar) ) {
> print "$el\n"
>  }
>
> $el1 and $el2 would of course be aliases, right?

]- yes ALIASING will be better, instead of copyng values into $el,$el2
scalars just one point I placed around them "(" ")", so that the
arrays
would be flattened :") ... but now as u told it will be beter they to be
aliases.. so may be this is the right one :

for my ($e1,$2,e3...,$eX) ( @a1, @a2, @a3, .. @aX) {
 .blah...
}

and later on the first iteration $el's are aliased to the zero elements
of
arrays (if the $el's are more than @a's then the latest  $el's are not
aliased/probably undef'ed/, if @a's are more then all $el's are occuped
and
on the next iteration they doesn't start to be aliesed again from the
@a1
but from the next @a's !!).
If we have Scalars  in the list, then they behave as array with one
element
(just got aliesed every time).
If we have Hashes then VALUES get aliased... if someone wants keys it
should
write explictly keys %hash (temporarily has to be created scalars may
be),
the same with each.

just one possible solution.. may be there is many things i've not seen..

And then what about this :")

for my ( $e1, @els,$el3) ( @a1, 5..10, @a2,%h1, $x .. @aX) {
 .blah...
};

:"))) There is endless ways to do it : TIEWTDI
=
iVAN
[EMAIL PROTECTED]
=




Re: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread raptor

> Hmmm. Didn't think about that.  That would be a nice way, that way you can
> manipulate it's behaviour depending with how many aliases you provide.
>
> for my $el1, $el2 ( (@foo, @bar) ) {
> print "$el\n"
>  }
>
> $el1 and $el2 would of course be aliases, right?

]- yes ALIASING will be better, instead of copyng values into $el,$el2
scalars just one point I placed around them "(" ")", so that the arrays
would be flattened :") ... but now as u told it will be beter they to be
aliases.. so may be this is the right one :

for my ($e1,$2,e3...,$eX) ( @a1, @a2, @a3, .. @aX) {
 .blah...
}

and later on the first iteration $el's are aliased to the zero elements of
arrays (if the $el's are more than @a's then the latest  $el's are not
aliased/probably undef'ed/, if @a's are more then all $el's are occuped and
on the next iteration they doesn't start to be aliesed again from the @a1
but from the next @a's !!).
If we have Scalars  in the list, then they behave as array with one element
(just got aliesed every time).
If we have Hashes then VALUES get aliased... if someone wants keys it should
write explictly keys %hash (temporarily has to be created scalars may be),
the same with each.

just one possible solution.. may be there is many things i've not seen..

And then what about this :")

for my ( $e1, @els,$el3) ( @a1, 5..10, @a2,%h1, $x .. @aX) {
 .blah...
};

:"))) There is endless ways to do it : TIEWTDI
=
iVAN
[EMAIL PROTECTED]
=





RE: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Sterin, Ilya

Hmmm. Didn't think about that.  That would be a nice way, that way you can
manipulate it's behaviour depending with how many aliases you provide.  

for my $el1, $el2 ( (@foo, @bar) ) {
print "$el\n"
 }

$el1 and $el2 would of course be aliases, right?

But one though might be, what happens if this is written...

for my $el1, $el2 ( (@foo, @bar, @arr) ) {
print "$el\n"
 }

Does this bahave in the same way as the first one, but we just don't set
@arr elements, since the user forgot to provide a third alias, or should
this croak at compile time?

Ilya


-Original Message-
From: raptor
To: [EMAIL PROTECTED]
Sent: 07/20/2001 3:37 AM
Subject: Re: array/hash manipulation [was :what's with 'with'?]



> So my initial code (which I modified a little...)
>
> for ( @foo, @bar ) {
>   print "$_[0] : $_[1]\n";
> }
>
> for would set each element of the @_ array to correspond to the
arguments
in
> for() , therfore $_[0] will equal to the current element of @foo and
$_[1]
> will equal to the corresponding element of @bar.  As I mentioned
before
this
> can very easily be accomplished through 0..$#foo loop, but people
disagreed
> based on that it would be a nice option, in my opinion it's useless,
but
if
> was implemented this could be a way:)

]- Yes ... and one more option :

 for my $el1, $el2 ( @foo, @bar ) {
print "$el1 : $el2\n"
 }

$el1 will get values from @foo and $el2 from @bar, but the following :

 for my $el ( @foo, @bar ) {
print "$el\n"
 }

will print :
$foo[0]
$bar[0]
$foo[1]
$bar[1]

if people like the other way they can write :

 for my $el ( (@foo, @bar) ) {
print "$el\n"
 }

will print :
$foo[0]
$foo[1]
...$foo[x]
$bar[0]
$bar[1]


is this correct , but now I'm looking at these too...
http://dev.perl.org/rfc/90.pod
http://dev.perl.org/rfc/91.pod
http://dev.perl.org/rfc/148.pod

so may be what must be the order of passing the arguments and other
stuff
should be done via these proposed functions.

PS. I was thinking of that before, what if we have something let's call
it
'transform' for transformation of any structure to other structure.. but
as
i thought it should combine in some way the features of
switch,if-else,for/foeach, do, while, array/hash-slices, assignment
etc  ps I'm talking about DWIM operator. anyway...
is it
possible to really add such "dwim" function/operator that can be
modified on
the fly so that it suit all programmers tastes and don't make real
mess...")
... ok i say it :")))
=
iVAN
[EMAIL PROTECTED]
=



Re: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread Jeremy Howard

"raptor" <[EMAIL PROTECTED]> wrote:
> but now I'm looking at these too...
> http://dev.perl.org/rfc/90.pod
> http://dev.perl.org/rfc/91.pod
> http://dev.perl.org/rfc/148.pod
>
> so may be what must be the order of passing the arguments and other stuff
> should be done via these proposed functions.
>
> PS. I was thinking of that before, what if we have something let's call it
> 'transform' for transformation of any structure to other structure.. but
as
> i thought it should combine in some way the features of
> switch,if-else,for/foeach, do, while, array/hash-slices, assignment
> etc  ps I'm talking about DWIM operator. anyway... is
it
> possible to really add such "dwim" function/operator that can be modified
on
> the fly so that it suit all programmers tastes and don't make real
mess...")

The generalised version of these is here:
  http://dev.perl.org/rfc/81.html

In particular, see this section:
  http://dev.perl.org/rfc/81.html#JUSTIFICATION

This RFC suggests a syntax for list comprehension, which when used as a
slice index provides flexible data structure transformation.

Another useful transformation is provided by the cartesian product operator
described here:
  http://dev.perl.org/rfc/205.html

HTH,
  Jeremy





Re: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread John Porter

raptor wrote:
> 
>  for my $el1, $el2 ( @foo, @bar ) {

Hopefully you mean

   for my $el1, my $el2 ( @foo, @bar ) {

or maybe 

   for [ my $el1, my $el2 ] ( @foo, @bar ) {

And yes, it's an old idea.


> PS. I was thinking of that before, what if we have something let's call it
> 'transform' for transformation of any structure to other structure.. 

This sort of thing should certainly not be in the kernel.

-- 
John Porter




Re: array/hash manipulation [was :what's with 'with'?]

2001-07-20 Thread raptor



> So my initial code (which I modified a little...)
>
> for ( @foo, @bar ) {
>   print "$_[0] : $_[1]\n";
> }
>
> for would set each element of the @_ array to correspond to the arguments
in
> for() , therfore $_[0] will equal to the current element of @foo and $_[1]
> will equal to the corresponding element of @bar.  As I mentioned before
this
> can very easily be accomplished through 0..$#foo loop, but people
disagreed
> based on that it would be a nice option, in my opinion it's useless, but
if
> was implemented this could be a way:)

]- Yes ... and one more option :

 for my $el1, $el2 ( @foo, @bar ) {
print "$el1 : $el2\n"
 }

$el1 will get values from @foo and $el2 from @bar, but the following :

 for my $el ( @foo, @bar ) {
print "$el\n"
 }

will print :
$foo[0]
$bar[0]
$foo[1]
$bar[1]

if people like the other way they can write :

 for my $el ( (@foo, @bar) ) {
print "$el\n"
 }

will print :
$foo[0]
$foo[1]
...$foo[x]
$bar[0]
$bar[1]


is this correct , but now I'm looking at these too...
http://dev.perl.org/rfc/90.pod
http://dev.perl.org/rfc/91.pod
http://dev.perl.org/rfc/148.pod

so may be what must be the order of passing the arguments and other stuff
should be done via these proposed functions.

PS. I was thinking of that before, what if we have something let's call it
'transform' for transformation of any structure to other structure.. but as
i thought it should combine in some way the features of
switch,if-else,for/foeach, do, while, array/hash-slices, assignment
etc  ps I'm talking about DWIM operator. anyway... is it
possible to really add such "dwim" function/operator that can be modified on
the fly so that it suit all programmers tastes and don't make real mess...")
... ok i say it :")))
=
iVAN
[EMAIL PROTECTED]
=