Re: cool tricks for inserting element into array?

2005-07-21 Thread John W. Krahn
Tom Allison wrote:
> John W. Krahn wrote:
> 
>> for ( my $i = @tmp; --$i; ) {
>> splice @tmp, $i, 0, '|';
>> }
> 
> How does this fare for large arrays?

According to my tests it does poorly on large arrays.


John

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: cool tricks for inserting element into array?

2005-07-21 Thread Tom Allison

Bryan R Harris wrote:


As a sidenote, are questions like this appropriate for the list?  I really
don't *need* help with this, I was just hoping to expand my personal toolbox
with a better way to do something.  Every time John, Wiggins, Luke, Bob,
Jeff, etc. respond to my emails I know I'm going to learn something.



Yeah, so am I.
Isn't that why we're here?  :)

It's a heck of a lot better than my questions so far...
But I'm not always sure what I'm doing in the first place... ;)

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: cool tricks for inserting element into array?

2005-07-20 Thread John W. Krahn
John W. Krahn wrote:
> Bryan R Harris wrote:
>>I'd like to turn array @tmp from:
>>
>>   (1,2,3)
>>
>>to
>>
>>   (1,"|",2,"|",3)
>>
>>I'm using:
>>
>>  @tmp = split(' ', join(" | ", @tmp));
>>
>>... but it seems like a waste to create a string and then split it back up
>>again.

FYI, I ran a benchmark on the different algorithms and

splice @tmp, $_, 0, '|' for reverse 1 .. $#tmp;

is the fastest on small arrays but is the slowest on large arrays while

push @tmp, $_ ? '|' : (), shift @tmp for 0 .. $#tmp;

is second fastest on small arrays and the fastest on large arrays.

And if you want something that is really fast on small arrays then a C
style for loop is the fastest:

for ( my $i = @tmp; --$i; ) {
splice @tmp, $i, 0, '|';
}



John

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: cool tricks for inserting element into array?

2005-07-20 Thread John W. Krahn
John W. Krahn wrote:
> John W. Krahn wrote:
>>Bryan R Harris wrote:
>>>I'd like to turn array @tmp from:
>>>
>>>  (1,2,3)
>>>
>>>to
>>>
>>>  (1,"|",2,"|",3)
>>>
>>>I'm using:
>>>
>>> @tmp = split(' ', join(" | ", @tmp));
>>>
>>>... but it seems like a waste to create a string and then split it back up
>>>again.
>>I can think of two ways to do it:
>>
>>splice @tmp, $_, 0, '|' for reverse 1 .. $#tmp;
>>
>>
>>@tmp = ( map( ($_, '|'), @tmp[ 0 .. $#tmp - 1 ] ), $tmp[ -1 ] );
> 
> Another way to do it:
> 
> @tmp = splice @{[ map( ('|', $_), @tmp ) ]}, 1;

Or just using a list:

@tmp = ( map +( $_, '|' ), @tmp )[ 0 .. 2 * $#tmp ];



John

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: cool tricks for inserting element into array?

2005-07-20 Thread John W. Krahn
Jay Savage wrote:
> On 7/20/05, Bryan R Harris <[EMAIL PROTECTED]> wrote:
>>
>>I'd like to turn array @tmp from:
>>
>>   (1,2,3)
>>
>>to
>>
>>   (1,"|",2,"|",3)
>>
>>I'm using:
>>
>>  @tmp = split(' ', join(" | ", @tmp));
>>
>>... but it seems like a waste to create a string and then split it back up
>>again.
> 
> I would do something like:
> 
>unshift(@ar, $_ == 1 ? pop @ar : (pop @ar, '|')) foreach [EMAIL PROTECTED];
> 
> map might be faster; benchmark it.

You can simplifiy that a bit:

unshift( @ar, $_ ? (pop @ar, '|') : pop @ar ) for 0 .. $#ar;

Which also leads to:

unshift @ar, (pop @ar, $_ ? '|' : ()) for 0 .. $#ar;

Or also:

push @ar, ($_ ? '|' : (), shift @ar) for 0 .. $#ar;



John

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: cool tricks for inserting element into array?

2005-07-20 Thread Jay Savage
On 7/20/05, Bryan R Harris <[EMAIL PROTECTED]> wrote:
> 
> 
> I'd like to turn array @tmp from:
> 
>(1,2,3)
> 
> to
> 
>(1,"|",2,"|",3)
> 
> I'm using:
> 
>   @tmp = split(' ', join(" | ", @tmp));
> 
> ... but it seems like a waste to create a string and then split it back up
> again.

I would do something like:

   unshift(@ar, $_ == 1 ? pop @ar : (pop @ar, '|')) foreach [EMAIL PROTECTED];

map might be faster; benchmark it.

HTH,

-- jay
--
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: cool tricks for inserting element into array?

2005-07-20 Thread John W. Krahn
John W. Krahn wrote:
> Bryan R Harris wrote:
>>I'd like to turn array @tmp from:
>>
>>   (1,2,3)
>>
>>to
>>
>>   (1,"|",2,"|",3)
>>
>>I'm using:
>>
>>  @tmp = split(' ', join(" | ", @tmp));
>>
>>... but it seems like a waste to create a string and then split it back up
>>again.
> 
> I can think of two ways to do it:
> 
> splice @tmp, $_, 0, '|' for reverse 1 .. $#tmp;
> 
> 
> @tmp = ( map( ($_, '|'), @tmp[ 0 .. $#tmp - 1 ] ), $tmp[ -1 ] );

Another way to do it:

@tmp = splice @{[ map( ('|', $_), @tmp ) ]}, 1;



John

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: cool tricks for inserting element into array?

2005-07-20 Thread John W. Krahn
John W. Krahn wrote:
> Bryan R Harris wrote:
>>I'd like to turn array @tmp from:
>>
>>   (1,2,3)
>>
>>to
>>
>>   (1,"|",2,"|",3)
>>
>>I'm using:
>>
>>  @tmp = split(' ', join(" | ", @tmp));
>>
>>... but it seems like a waste to create a string and then split it back up
>>again.
> 
> I can think of two ways to do it:
> 
> splice @tmp, $_, 0, '|' for reverse 1 .. $#tmp;
> 
> 
> @tmp = ( map( ($_, '|'), @tmp[ 0 .. $#tmp - 1 ] ), $tmp[ -1 ] );

Or to put that another way:

@tmp = ( $tmp[ 0 ], map( ('|', $_), @tmp[ 1 .. $#tmp ] ) );


John

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: cool tricks for inserting element into array?

2005-07-20 Thread John W. Krahn
Bryan R Harris wrote:
> 
> I'd like to turn array @tmp from:
> 
>(1,2,3)
> 
> to
> 
>(1,"|",2,"|",3)
> 
> I'm using:
> 
>   @tmp = split(' ', join(" | ", @tmp));
> 
> ... but it seems like a waste to create a string and then split it back up
> again.

I can think of two ways to do it:

splice @tmp, $_, 0, '|' for reverse 1 .. $#tmp;


@tmp = ( map( ($_, '|'), @tmp[ 0 .. $#tmp - 1 ] ), $tmp[ -1 ] );



John

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: cool tricks for inserting element into array?

2005-07-20 Thread Bryan R Harris


Neat, thanks!

- Bryan



> On Jul 20, Bryan R Harris said:
> 
>> I'd like to turn array @tmp from:
>> 
>>   (1,2,3)
>> to
>>   (1,"|",2,"|",3)
> 
>>  @tmp = split(' ', join(" | ", @tmp));
>> 
>> ... but it seems like a waste to create a string and then split it back up
>> again.
> 
> It would also break if elements of @tmp had spaces in them.
> 
> Well, here's one sneaky way.  It assumes that no two elements in the array
> are references to the same data:
> 
>my @array = ( 1 .. 3 );
>my @with_pipes = map { \$_ == \$array[-1] ? $_ : ($_, "|") } @array;
> 
> The trick it uses is seeing if a reference to the current element is the
> same as a reference to the LAST element.  If it's not, we insert an
> element after it.
> 
> You could also do it in two steps:
> 
>my @with_pipes = map { ($_, "|") } @array;
>pop @with_pipes;
> 
>> As a sidenote, are questions like this appropriate for the list?  I really
>> don't *need* help with this, I was just hoping to expand my personal toolbox
>> with a better way to do something.  Every time John, Wiggins, Luke, Bob,
>> Jeff, etc. respond to my emails I know I'm going to learn something.
> 
> I don't think there's anything wrong with asking such questions.




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: cool tricks for inserting element into array?

2005-07-20 Thread Bob Showalter
Bryan R Harris wrote:
> I'd like to turn array @tmp from:
> 
>(1,2,3)
> 
> to
> 
>(1,"|",2,"|",3)
> 
> I'm using:
> 
>   @tmp = split(' ', join(" | ", @tmp));
> 
> ... but it seems like a waste to create a string and then split it
> back up again.

I'll bet that's pretty efficient. The problem would come if any of the
original elements contained an embedded space.

Here's a way, but I doubt it's very efficient:

   splice @tmp, $_, 0, '|' for reverse 1 .. @tmp-1;

Another way:

   @tmp = map +($_, '|'), @tmp; pop @tmp;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: cool tricks for inserting element into array?

2005-07-20 Thread Jeff 'japhy' Pinyan

On Jul 20, Bryan R Harris said:


I'd like to turn array @tmp from:

  (1,2,3)
to
  (1,"|",2,"|",3)



 @tmp = split(' ', join(" | ", @tmp));

... but it seems like a waste to create a string and then split it back up
again.


It would also break if elements of @tmp had spaces in them.

Well, here's one sneaky way.  It assumes that no two elements in the array 
are references to the same data:


  my @array = ( 1 .. 3 );
  my @with_pipes = map { \$_ == \$array[-1] ? $_ : ($_, "|") } @array;

The trick it uses is seeing if a reference to the current element is the 
same as a reference to the LAST element.  If it's not, we insert an 
element after it.


You could also do it in two steps:

  my @with_pipes = map { ($_, "|") } @array;
  pop @with_pipes;


As a sidenote, are questions like this appropriate for the list?  I really
don't *need* help with this, I was just hoping to expand my personal toolbox
with a better way to do something.  Every time John, Wiggins, Luke, Bob,
Jeff, etc. respond to my emails I know I'm going to learn something.


I don't think there's anything wrong with asking such questions.

--
Jeff "japhy" Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: cool tricks for inserting element into array?

2005-07-20 Thread Moon, John
Subject: RE: cool tricks for inserting element into array?

Subject: cool tricks for inserting element into array?



I'd like to turn array @tmp from:

   (1,2,3)

to

   (1,"|",2,"|",3)

I'm using:

  @tmp = split(' ', join(" | ", @tmp));

... but it seems like a waste to create a string and then split it back up
again.

As a sidenote, are questions like this appropriate for the list?  I really
don't *need* help with this, I was just hoping to expand my personal toolbox
with a better way to do something.  Every time John, Wiggins, Luke, Bob,
Jeff, etc. respond to my emails I know I'm going to learn something.

- Bryan


Not sure if this helps but ...

perl -e '@tmp=(1..4); @tmp = map {$_, "|"} @tmp; print "@tmp";'


Oops...
Add pop @tmp after map... 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: cool tricks for inserting element into array?

2005-07-20 Thread Moon, John
Subject: cool tricks for inserting element into array?



I'd like to turn array @tmp from:

   (1,2,3)

to

   (1,"|",2,"|",3)

I'm using:

  @tmp = split(' ', join(" | ", @tmp));

... but it seems like a waste to create a string and then split it back up
again.

As a sidenote, are questions like this appropriate for the list?  I really
don't *need* help with this, I was just hoping to expand my personal toolbox
with a better way to do something.  Every time John, Wiggins, Luke, Bob,
Jeff, etc. respond to my emails I know I'm going to learn something.

- Bryan


Not sure if this helps but ...

perl -e '@tmp=(1..4); @tmp = map {$_, "|"} @tmp; print "@tmp";'

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]