[12605] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 10 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jul 2 20:57:44 1999

Date: Fri, 2 Jul 1999 17:50:06 -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           Fri, 2 Jul 1999     Volume: 9 Number: 10

Today's topics:
        Help with Pattern Matching <shaddy@shaddy.com>
    Re: Help with Pattern Matching (Jason Stapels)
    Re: Help with Pattern Matching <rervadodkarmove@siebel.com>
        Help with regex... <shaffer2@ptd.net>
    Re: Help with regex... (Andreas Fehr)
    Re: Help with regex... (Andrew Johnson)
    Re: Help with regex... (Andreas Fehr)
    Re: Help with regex... <swiftkid@bigfoot.com>
    Re: Help with regex... <dodger@dodger.org>
    Re: Help with regex... (Tad McClellan)
    Re: Help with regex... (Abigail)
    Re: hiring perl programmers <uri@sysarch.com>
    Re: hiring perl programmers (Bart Lateur)
    Re: hiring perl programmers (Neko)
    Re: hiring perl programmers <tchrist@mox.perl.com>
    Re: hiring perl programmers (Randal L. Schwartz)
    Re: hiring perl programmers (Abigail)
    Re: hiring perl programmers (Abigail)
    Re: hiring perl programmers <tchrist@mox.perl.com>
    Re: How do I do an FTP PUT with LWP? <swiftkid@bigfoot.com>
    Re: How to put small icons at the start menu bar <gellyfish@gellyfish.com>
        HTML form help - need an image for <input type="reset"> finnadat@my-deja.com
        I need a skript like which one which  is used by come.t <Philipp_Goetzinger@gmx.de>
    Re: I need a skript like which one which  is used by co <gellyfish@gellyfish.com>
        Digest Administrivia (Last modified: 1 Jul 99) (Perl-Users-Digest Admin)

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

Date: Fri, 02 Jul 1999 16:10:42 GMT
From: "Shaddy International Ltd." <shaddy@shaddy.com>
Subject: Help with Pattern Matching
Message-Id: <377CE434.395CFFA3@shaddy.com>

This is a multi-part message in MIME format.
--------------75E85564DC14A2546A65EB57
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hey

    I'm trying to write a little script that'll replace all the " in a ~
delimited text file with a space. The following code is what I got. I
can't figure out way it won't replace. It'll find all the "'s in the
fill.

Code:
$old_file = "j:\\scripts\\pricelist.txt";
$search = '"';
$replace = "";
open(PRICELIST, "$old_file");
while ($line = <PRICELIST>)
{
 ($partno, $desc, $unit, $price) = split(/~/,$line,4);
 $desc =~ s/$search/$replace/g;
}
close(PRICELIST);

Sample Line from Data File:
65211~MOUSE "MOUSE-IN-A-BOX" GREY~"U"~42.67

Could somebody please give me some insight into this. I'm new to this
Pattern Matching stuff

Thanks
Jason

--------------75E85564DC14A2546A65EB57
Content-Type: text/x-vcard; charset=us-ascii;
 name="shaddy.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Shaddy International Ltd.
Content-Disposition: attachment;
 filename="shaddy.vcf"

begin:vcard 
n:;Shaddy Int'l Marketing LTD.
x-mozilla-html:FALSE
version:2.1
email;internet:shaddy@shaddy.com
x-mozilla-cpt:;0
fn:Shaddy Int'l Marketing LTD.
end:vcard

--------------75E85564DC14A2546A65EB57--



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

Date: 2 Jul 1999 16:44:55 GMT
From: jmstapel_n0spam@mtu.edu (Jason Stapels)
Subject: Re: Help with Pattern Matching
Message-Id: <7liqa7$ahv$1@campus1.mtu.edu>

I think your going to have to explain your problem a little more
clearly, it seemed to work just fine when I tried it.

Jason


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

Date: Fri, 2 Jul 1999 12:03:11 -0700
From: "Rahul" <rervadodkarmove@siebel.com>
Subject: Re: Help with Pattern Matching
Message-Id: <377d0d40$0$209@nntp1.ba.best.com>

You are changing the variable alright, but are not writing it anywhere ! Try
this version of the code

$old_file = "pricelist.txt";
$new_file = "newPricelist.txt";
$search = '"';
$replace = "";
open(PRICELIST, "$old_file");
open(NEWPRICELIST,">$new_file");
while ($line = <PRICELIST>)
{
 ($partno, $desc, $unit, $price) = split(/~/,$line,4);
 $desc =~ s/$search/$replace/g;
 print NEWPRICELIST $partno,"~",$desc,"~",$unit,"~",$price;
}
close(PRICELIST);
close(NEWPRICELIST);

You will see that the newpricelist.txt will contain the results.

Good luck


Shaddy International Ltd. <shaddy@shaddy.com> wrote in message
news:377CE434.395CFFA3@shaddy.com...
> Hey
>
>     I'm trying to write a little script that'll replace all the " in a ~
> delimited text file with a space. The following code is what I got. I
> can't figure out way it won't replace. It'll find all the "'s in the
> fill.
>
> Code:
> $old_file = "j:\\scripts\\pricelist.txt";
> $search = '"';
> $replace = "";
> open(PRICELIST, "$old_file");
> while ($line = <PRICELIST>)
> {
>  ($partno, $desc, $unit, $price) = split(/~/,$line,4);
>  $desc =~ s/$search/$replace/g;
> }
> close(PRICELIST);
>
> Sample Line from Data File:
> 65211~MOUSE "MOUSE-IN-A-BOX" GREY~"U"~42.67
>
> Could somebody please give me some insight into this. I'm new to this
> Pattern Matching stuff
>
> Thanks
> Jason
>




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

Date: Fri, 02 Jul 1999 07:55:04 GMT
From: "dave shaffer" <shaffer2@ptd.net>
Subject: Help with regex...
Message-Id: <sf_e3.5137$I72.574281@nnrp1.ptd.net>

HELP!!!

I need a regular expression to match/remove html tags -actually anything in
greater/lessthan brackets "<all characters >" and on multiple lines.  For
example, the following text would be matched:

Before:
        <yada yada> this is my doc <yada
        yada> it is pretty cool <yada

        yada

                     yada> reality,
       it is!!!


After:
       this is my doc
       it is pretty cool
       reality
       it is!!!

I could use something like this: $text =~ tr/<.*>//;  but how do you match
when spanning multiple lines.

Thanks,
   Dave Shaffer









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

Date: Fri, 02 Jul 1999 08:25:28 GMT
From: backwards.saerdna@srm.hc (Andreas Fehr)
Subject: Re: Help with regex...
Message-Id: <377c755d.10588154@news.uniplus.ch>

On Fri, 02 Jul 1999 07:55:04 GMT, "dave shaffer" <shaffer2@ptd.net>
wrote:

>HELP!!!
>
>I need a regular expression to match/remove html tags -actually anything in
>greater/lessthan brackets "<all characters >" and on multiple lines.  For
>example, the following text would be matched:
>
>Before:
>        <yada yada> this is my doc <yada
>        yada> it is pretty cool <yada
>
>        yada
>
>                     yada> reality,
>       it is!!!
>
>
>After:
>       this is my doc
>       it is pretty cool
>       reality
>       it is!!!
>
>I could use something like this: $text =~ tr/<.*>//;  but how do you match
>when spanning multiple lines.
>
>Thanks,
>   Dave Shaffer
>

tr/<.*>//; wont work because .* is greedy and eats all from the first
< to the last >.

"blah <yada yada> this is my doc <yada> blah" would get "blah  blah"
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I'd suggest tr/<[^>]*>//; this eats everything not a closing bracket.
You might be successfull with the nongreede tr/<.*?>//;

Use tr///s to span multiple lines.
I think there are other ways spanning lines...

There is good book on regexp from ORA:
http://www.oreilly.com/catalog/regex/

Andreas



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

Date: Fri, 02 Jul 1999 08:33:59 GMT
From: andrew-johnson@home.com (Andrew Johnson)
Subject: Re: Help with regex...
Message-Id: <XP_e3.790$SH6.45386@news1.rdc2.on.home.com>

In article <377c755d.10588154@news.uniplus.ch>,
 Andreas Fehr <backwards.saerdna@srm.hc> wrote:

[snip]
 
! tr/<.*>//; wont work because .* is greedy and eats all from the first
! < to the last >.
! 
! "blah <yada yada> this is my doc <yada> blah" would get "blah  blah"
!       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tr/// doesn't use regular expressions and would certainly not
do what you say it does above.

use s/// 
But first read the perlre and perlop manpages.

regards
andrew
 


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

Date: Fri, 02 Jul 1999 08:45:48 GMT
From: backwards.saerdna@srm.hc (Andreas Fehr)
Subject: Re: Help with regex...
Message-Id: <377c7bb7.12214433@news.uniplus.ch>

On Fri, 02 Jul 1999 08:33:59 GMT, andrew-johnson@home.com (Andrew
Johnson) wrote:

>In article <377c755d.10588154@news.uniplus.ch>,
> Andreas Fehr <backwards.saerdna@srm.hc> wrote:
>
>[snip]
> 
>! tr/<.*>//; wont work because .* is greedy and eats all from the first
>! < to the last >.
>! 
>! "blah <yada yada> this is my doc <yada> blah" would get "blah  blah"
>!       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
>tr/// doesn't use regular expressions and would certainly not
>do what you say it does above.
>
>use s/// 
>But first read the perlre and perlop manpages.
>

Your right, I mixed things up. Sorry! The following code is better:


my $temp = "blah <yada yada> this \n is my doc <yada> blah";
$temp =~ s/<[^>]*>//g;
print "$temp\n";

Andreas



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

Date: Fri, 2 Jul 1999 15:40:42 +0500
From: "Faisal Nasim" <swiftkid@bigfoot.com>
Subject: Re: Help with regex...
Message-Id: <7lj892$7ek6@news.cyber.net.pk>

> my $temp = "blah <yada yada> this \n is my doc <yada> blah";
> $temp =~ s/<[^>]*>//g;

$temp =~ s/<.+?>//gs;

?




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

Date: Thu, 1 Jul 1999 04:19:02 -0700
From: "Dodger" <dodger@dodger.org>
Subject: Re: Help with regex...
Message-Id: <7li5ck$44o$1@ancalagon.dragon-net.net>

no,
$temp =~ s/<.*?>//gs;
otherwise it would leave <> and he said any < or >.

Dodger
--
I don't care if it rains or freezes
Long as I've got my Strap-on Jesus...

The Necrosoft Network is now online!
Add your site and help make it the net's premiere Gothic Search Engine!
http://www.gothic-classifieds.com/necrosoft/
Faisal Nasim wrote in message <7lj892$7ek6@news.cyber.net.pk>...
>> my $temp = "blah <yada yada> this \n is my doc <yada> blah";
>> $temp =~ s/<[^>]*>//g;
>
>$temp =~ s/<.+?>//gs;
>
>?
>
>




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

Date: Fri, 2 Jul 1999 10:08:11 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Help with regex...
Message-Id: <b4hil7.ef3.ln@magna.metronet.com>

dave shaffer (shaffer2@ptd.net) wrote:

: HELP!!!


   FAQ!!!


: I need a regular expression to match/remove html tags


   Perl FAQ, part 9:

      "How do I remove HTML from a string?"


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: 2 Jul 1999 19:40:19 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: Help with regex...
Message-Id: <slrn7nqmv2.31h.abigail@alexandra.delanet.com>

dave shaffer (shaffer2@ptd.net) wrote on MMCXXXI September MCMXCIII in
<URL:news:sf_e3.5137$I72.574281@nnrp1.ptd.net>:
** HELP!!!
** 
** I need a regular expression to match/remove html tags -actually anything in
** greater/lessthan brackets "<all characters >" and on multiple lines.  For
** example, the following text would be matched:

FAQ.

** I could use something like this: $text =~ tr/<.*>//;  but how do you match
** when spanning multiple lines.

Oh. You want to delete from the very first < in your document, to the
very last >? That's possible, but you should use s/// then.



Abigail
-- 
perl -e '$a = q 94a75737420616e6f74686572205065726c204861636b65720a9 and
         ${qq$\x5F$} = q 97265646f9 and s g..g;
         qq e\x63\x68\x72\x20\x30\x78$&eggee;
         {eval if $a =~ s e..eqq qprint chr 0x$& and \x71\x20\x71\x71qeexcess}'


  -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----


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

Date: 02 Jul 1999 01:08:39 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: hiring perl programmers
Message-Id: <x7g1373ag8.fsf@home.sysarch.com>

>>>>> "IC" == Iain Chalmers <bigiain@mightymedia.com.au> writes:

  IC> The Fake Uri wrote:
  >> >>>>> "IC" == Iain Chalmers <bigiain@mightymedia.com.au> writes:
  >> 
  IC> Uri wrote:
  >> >> NOTE: another reply without snide comments. i must be on a roll.
  >> >> must be the ones who get the rude answers are the ones who don't have a
  >> >> sense of humor or self worth. interesting how that works out.
  >> 
  IC> Hey! what have you done with the *real* Uri??? :-)
  >> 
  >> i have kidnapped him and i am holding him hostage. i will release him
  >> when all the whiners in this group acknowledge him to be a perl god and
  >> the proper burnt offerings of newbies are made.

  IC> Cool! I'm rounding up a few newbies as I type this - will photos of the
  IC> pyre do, or should I snail mail the remains somewhere???

it sounds like a good start either way.

  IC> Does it matter if the newbie-roast is contaminated by a whiner or two?

please, keep it pure. perl gods are very picky.

  IC> (wondering if he'll get away with slipping a few VB newbies in too)

unclean!!! begone from the temple of perl lest ye be made into the
sisyphus of redmond.

the false god, uri


-- 
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering
uri@sysarch.com  ---------------------------  Perl, Internet, UNIX Consulting
Have Perl, Will Travel  -----------------------------  http://www.sysarch.com
The Best Search Engine on the Net -------------  http://www.northernlight.com


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

Date: Fri, 02 Jul 1999 08:53:55 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: hiring perl programmers
Message-Id: <377d777c.5846162@news.skynet.be>

Tom Christiansen wrote:

>    1) You have an array of non-negative integers.  Move all the
>       zero-valued elements to the front of that array.

Oh. First thought, if the remainder needs to be in the same order:

	foreach (@ary) {
		if($_) {
			push @result, $_; # at end
		} else {
			unshift @result, $_; # at front
		}
	}

If the order of the rest doesn't matter, an in-place replacement would
do:

	my $k=0; # zero count
	for(my $i=0;$i<=$#ary;$i++) {
	    unless($ary[$i]) { 
	        # item i is zero
	        $ary[$i] = $ary[$k]; # move item k to position i
	        $ary[$k++] = 0; # move zero into item k (cheating :)
		# and increment zero count
	    }
	}

>    2) You have the head node of a doubly linked list and a target value.
>       Delete the first node in the list whose value matches your target.

You mean "low level" doubly linked list? With actual "previous" and
"next" fields? Yuck.

I assume that the "items" are hash refs.

	$previous = $header; # special header node
	while(my $this = $previous->{NEXT}) {
	    if($this->{VALUE} eq $value) {
	        # bingo!
	        my $next  = $this->{NEXT};
	        $previous->{NEXT} = $next; # forward
	        $next->{PREVIOUS} = $previous if $next; # back
	        return $this;
	    }
	    $previous = $this;
	}

I think this still won't work OK for the first node.

>Furthermore, they were told to pay especial attention to the space and
>time complexities of their algorithms.

You mean the O(fn(N)) stuff? Blerk. I always hated that. However, I
don't think I did that bad. My inplace replacement "zero to front"
routine uses minimal space, and looks O(N) to me. The other one is
worse. I think push() is O(1). It could be that unshift() is O(N), so I
think it is O(N*M), with M the number of zero items.

It does create a second array, which is just as large as the original
one.

The doubly linked list looks rather optimal to me. That's how I'd do it
in assembly (I dispise C). I assume O(N), and no extra memory used apart
from some scalars.

	Bart.


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

Date: 2 Jul 1999 18:48:56 GMT
From: tgy@chocobo.org (Neko)
Subject: Re: hiring perl programmers
Message-Id: <7lj1io$pgu$0@216.39.141.200>

On 01 Jul 1999 22:38:03 -0400, Uri Guttman <uri@sysarch.com> wrote:

>>>>>> "N" == Neko  <tgy@chocobo.org> writes:
>
>  N> On 30 Jun 1999 11:31:14 -0700, Tom Christiansen <tchrist@mox.perl.com> wrote:
>  >> 1) You have an array of non-negative integers.  Move all the
>  >> zero-valued elements to the front of that array.
>
>  N> My first thought was a simple sort:
>
>  N>     @array = sort @array;  # One round of Perl golf :)
>
>but that is O(NlogN) and i bet there are faster algorithms that tom was
>looking for.

I wasn't considering space or time for this one.  And the next snippet of
code is one such faster algorithm.

>  N> Then I finished reading the instructions:
>
>  N>     my $size = @array;
>  N>     @array = grep $_, @array;
>  N>     unshift @array, (0) x ($size - @array);
>
>this is worse as it is O(N**2).

I see a constant in getting the size of the array and one N in the grep.  Is
there another N in the unshift?  I can imagine each element of the array
being moved to make room.  Is this how it works?

However, I don't get penalized for it in my benchmarks below.  'Sort' and
'Grep' are the codes above, and 'Grep' is faster than 'Sort' even though it
is O(N**2) versus O(NlogN).

'Ordered' and 'Inplace' are from Bart Lateur's post, where 'Ordered' doesn't
change the order of non-zero elements (same with 'Grep') and 'Inplace'
modifies the array in-place.

And 'Copy' just copies one array into another, which I do in all the
subroutines.  It is necessary for 'Inplace' and the others do it just to be
fair.

I run the same benchmark for several different N, the size of @array.
Roughly half the array elements are zero.

#!/usr/bin/perl -w

use Benchmark;

my @array = map { $_ > 100 ? 0 : $_ } map { int rand 200 } 1..100000;

timethese 10, {
    Copy => sub {
        my @temp = @array;
    },
    Sort => sub {
        my @temp = @array;
        @temp = sort @temp;
    },
    Grep => sub {
        my @temp = @array;
        my $size = @temp;
        @temp = grep $_, @temp;
        unshift @temp, (0) x ($size - @temp);
    },
    Ordered => sub {
        my @ary = @array;
        my @result;
        foreach (@ary) {
            if($_) {
                push @result, $_; # at end
            } else {
                unshift @result, $_; # at front
            }
        }
    },
    Inplace => sub {
        my @ary = @array;
        my $k=0; # zero count
        for(my $i=0;$i<=$#ary;$i++) {
            unless($ary[$i]) { 
                # item i is zero
                $ary[$i] = $ary[$k]; # move item k to position i
                $ary[$k++] = 0; # move zero into item k (cheating :)
                # and increment zero count
            }
        }
    },
};

__END__

N=10
Benchmark: timing 100000 iterations of Copy, Grep, Inplace, Ordered, Sort...
      Copy:  2 wallclock secs ( 2.03 usr +  0.00 sys =  2.03 CPU)
      Grep:  8 wallclock secs ( 6.97 usr +  0.00 sys =  6.97 CPU)
   Inplace:  7 wallclock secs ( 7.08 usr +  0.00 sys =  7.08 CPU)
   Ordered:  7 wallclock secs ( 6.76 usr +  0.00 sys =  6.76 CPU)
      Sort: 22 wallclock secs (21.97 usr +  0.00 sys = 21.97 CPU)

N=100
Benchmark: timing 10000 iterations of Copy, Grep, Inplace, Ordered, Sort...
      Copy:  2 wallclock secs ( 1.27 usr +  0.00 sys =  1.27 CPU)
      Grep:  4 wallclock secs ( 4.07 usr +  0.00 sys =  4.07 CPU)
   Inplace:  7 wallclock secs ( 7.25 usr +  0.00 sys =  7.25 CPU)
   Ordered:  6 wallclock secs ( 5.54 usr +  0.00 sys =  5.54 CPU)
      Sort: 18 wallclock secs (18.68 usr +  0.00 sys = 18.68 CPU)

N=1000
Benchmark: timing 1000 iterations of Copy, Grep, Inplace, Ordered, Sort...
      Copy:  2 wallclock secs ( 1.59 usr +  0.00 sys =  1.59 CPU)
      Grep:  5 wallclock secs ( 4.89 usr +  0.00 sys =  4.89 CPU)
   Inplace:  7 wallclock secs ( 7.25 usr +  0.00 sys =  7.25 CPU)
   Ordered:  7 wallclock secs ( 7.03 usr +  0.00 sys =  7.03 CPU)
      Sort: 19 wallclock secs (19.17 usr +  0.00 sys = 19.17 CPU)

N=10000
Benchmark: timing 100 iterations of Copy, Grep, Inplace, Ordered, Sort...
      Copy:  2 wallclock secs ( 1.86 usr +  0.00 sys =  1.86 CPU)
      Grep:  5 wallclock secs ( 5.44 usr +  0.00 sys =  5.44 CPU)
   Inplace:  8 wallclock secs ( 7.75 usr +  0.00 sys =  7.75 CPU)
   Ordered: 35 wallclock secs (34.87 usr +  0.00 sys = 34.87 CPU)
      Sort: 21 wallclock secs (21.48 usr +  0.00 sys = 21.48 CPU)

N=100000
Benchmark: timing 10 iterations of Copy, Grep, Inplace, Ordered, Sort...
      Copy:  2 wallclock secs ( 1.92 usr +  0.00 sys =  1.92 CPU)
      Grep:  6 wallclock secs ( 6.26 usr +  0.00 sys =  6.26 CPU)
   Inplace:  8 wallclock secs ( 8.24 usr +  0.00 sys =  8.24 CPU)
   Ordered: 452 wallclock secs (451.82 usr +  0.00 sys = 451.82 CPU)
      Sort: 24 wallclock secs (24.22 usr +  0.00 sys = 24.22 CPU)

'Grep' and 'Inplace' did okay.  'Sort' was slow as expected.  But 'Ordered'
was the only one that became unruly for large N.  Did I do something wrong?
Are there other algorithms?

As for doubly linked lists, Bart Lateur just has hash refs pointing to each
other.  I can't think of anything more straight forward.  

-- 
Neko | tgy@chocobo.org | I want a moogle stuffy!


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

Date: 2 Jul 1999 14:13:47 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: hiring perl programmers
Message-Id: <377d1d7b@cs.colorado.edu>

You're all missing the point.  The point wasn't to see how well you
know the standard library.  The goal was first of all, to see whether
you can understand basic algorithms, and second of all, whether you've
a clue what it means to be parsimonious with respect to time and space
in designing those algorithms.  I'm afraid that anyone who called a
built-in sort function *flunked* miserably.

--tom
-- 
	    Refer does not rhyme with deafer.
	    Foeffer does, and zephyr, heifer.


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

Date: 02 Jul 1999 14:54:25 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: hiring perl programmers
Message-Id: <m14sjmsooe.fsf@halfdome.holdit.com>

>>>>> "Tom" == Tom Christiansen <tchrist@mox.perl.com> writes:

Tom>     1) You have an array of non-negative integers.  Move all the
Tom>        zero-valued elements to the front of that array.

Here's what I came up with in five minutes, not having looked at the
rest of the thread.  Apologies if it duplicates anything else. :)

    @array = map { int rand 10 } 1..25;

    for (my ($left,$right) = (0, 0); $right <= $#array; ++$right) {
      print "@array\n"; ## debug
      if ($array[$right] == 0) {
	@array[$left,$right] = @array[$right,$left] unless $left == $right;
	++$left;
      }
    }
    print "@array\n";

print "Just another Perl hacker,"

-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@teleport.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me


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

Date: 2 Jul 1999 18:29:17 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: hiring perl programmers
Message-Id: <slrn7nqipr.31h.abigail@alexandra.delanet.com>

Uri Guttman (uri@sysarch.com) wrote on MMCXXXI September MCMXCIII in
<URL:news:x7u2rn3hf8.fsf@home.sysarch.com>:
'' >>>>> "N" == Neko  <tgy@chocobo.org> writes:
'' 
''   N> On 30 Jun 1999 11:31:14 -0700, Tom Christiansen <tchrist@mox.perl.com> wrote:
''   >> Rather tangential to your point, but not to the grandparent
''   >> article, standard programming problems we made the interviewee write
''   >> extemporanously, although in any programming language they cared to use,
''   >> often included these:
''   >> 
''   >> 1) You have an array of non-negative integers.  Move all the
''   >> zero-valued elements to the front of that array.
'' 
''   N> My first thought was a simple sort:
'' 
''   N>     @array = sort @array;  # One round of Perl golf :)
'' 
'' but that is O(NlogN) and i bet there are faster algorithms that tom was
'' looking for.

Easy to port to other languages answer:
     for (my $i = my $j = 0; $i < @array; $i ++) {
         next if $array [$i];
         if ($i != $j) {@array [$i, $j] = @array [$j, $i];}
         $j ++;
     }

Perlish answer:
     @array = (grep {!$_} @array), grep {$_} @array;

'' 
''   N> Then I finished reading the instructions:
'' 
''   N>     my $size = @array;
''   N>     @array = grep $_, @array;
''   N>     unshift @array, (0) x ($size - @array);
'' 
'' this is worse as it is O(N**2).

That would be a very, very scary implementation of unshift. There's
no reason unshift to be worse than O (N + M), for adding an M length
list to an N length array.

But if it really is, here's an adaption:

     my $size = @array;
     @array   = reverse grep $_, @array;
     push @array, (0) x ($size - @array);
     @array   = reverse @array;


''   >> 2) You have the head node of a doubly linked list and a target value.
''   >> Delete the first node in the list whose value matches your target.
'' 
''   N> Is this cheating?
'' 
''   N>     my $node = $head_node;
''   N>     while (1) {
''   N>         $node->delete, last if $node->value eq $target_value;
''   N>         $node = $node->next;
''   N>     }
'' 
'' no but it illegal perl code and it doesn't solve the problem. where is
'' delete or next defined? they do the work so their code should be part
'' of the solution.

Assume nodes are 3 element anon arrays, with element 0 a reference
to the previous node, element 1 to the key, and element 2 a reference
to the next node. Also, assume the keys are numeric. Let $head be
a pointer to the first node; let $query be the element we look for.
Assume the next pointer of the last node is undef, and the previous
pointer of the first node is undef.

    use constant PREV => 0;
    use constant KEY  => 1;
    use constant NEXT => 2;

    for (my $node = $head; $node; $node = $node -> [NEXT]) {
        next unless $node -> [KEY] == $query;
        unless ($node -> [PREV]) {
            # Special case, deleting the first element.
            $head = $node -> [NEXT];
        }
        else {
            $node -> [PREV] -> [NEXT] = $node -> [NEXT];
        }
        unless ($node -> [NEXT]) {
            # Special case, deleting last element.
            # Uncomment the following line if you have a tail pointer.
            # $tail = $node -> [PREV];
        }
        else {
            $node -> [NEXT] -> [PREV] = $node -> [PREV];
        }
        last;
    }


Next question: do the same, but for a double linked, cyclid list.




Abigail
-- 
sub A::TIESCALAR{bless\my$x=>A};package B;@q=qw/Hacker Another
Perl Just/;use overload'""'=>sub{pop @q};sub A::FETCH{bless\my
$y=>B}; tie my $shoe => 'A';print "$shoe $shoe $shoe $shoe\n";


  -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----


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

Date: 2 Jul 1999 18:39:02 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: hiring perl programmers
Message-Id: <slrn7nqjc4.31h.abigail@alexandra.delanet.com>

Bart Lateur (bart.lateur@skynet.be) wrote on MMCXXXI September MCMXCIII
in <URL:news:377d777c.5846162@news.skynet.be>:
~~ Tom Christiansen wrote:
~~ 
~~ >    1) You have an array of non-negative integers.  Move all the
~~ >       zero-valued elements to the front of that array.
~~ 
~~ Oh. First thought, if the remainder needs to be in the same order:
~~ 
~~ 	foreach (@ary) {
~~ 		if($_) {
~~ 			push @result, $_; # at end
~~ 		} else {
~~ 			unshift @result, $_; # at front
~~ 		}
~~ 	}

That's a quadratic algorithm.

~~ If the order of the rest doesn't matter, an in-place replacement would
~~ do:
~~ 
~~ 	my $k=0; # zero count
~~ 	for(my $i=0;$i<=$#ary;$i++) {
~~ 	    unless($ary[$i]) { 
~~ 	        # item i is zero
~~ 	        $ary[$i] = $ary[$k]; # move item k to position i
~~ 	        $ary[$k++] = 0; # move zero into item k (cheating :)
~~ 		# and increment zero count
~~ 	    }
~~ 	}
~~ 
~~ >    2) You have the head node of a doubly linked list and a target value.
~~ >       Delete the first node in the list whose value matches your target.
~~ 
~~ You mean "low level" doubly linked list? With actual "previous" and
~~ "next" fields? Yuck.
~~ 
~~ I assume that the "items" are hash refs.
~~ 
~~ 	$previous = $header; # special header node
~~ 	while(my $this = $previous->{NEXT}) {
~~ 	    if($this->{VALUE} eq $value) {
~~ 	        # bingo!
~~ 	        my $next  = $this->{NEXT};
~~ 	        $previous->{NEXT} = $next; # forward
~~ 	        $next->{PREVIOUS} = $previous if $next; # back
~~ 	        return $this;
~~ 	    }
~~ 	    $previous = $this;
~~ 	}
~~ 
~~ I think this still won't work OK for the first node.

Indeed.

~~ 
~~ >Furthermore, they were told to pay especial attention to the space and
~~ >time complexities of their algorithms.
~~ 
~~ You mean the O(fn(N)) stuff? Blerk. I always hated that. However, I

But it is important! How could you ever decide which algorithm to use
if you don't know this stuff?

~~ don't think I did that bad. My inplace replacement "zero to front"
~~ routine uses minimal space, and looks O(N) to me. The other one is
~~ worse. I think push() is O(1). It could be that unshift() is O(N), so I
~~ think it is O(N*M), with M the number of zero items.

It's Omega (N * M) worst case indeed. push() isn't O(1), it can be linear.
However, starting from an array of size N, M pushes will have a total
running time of O (N + M), making it O(1) *amortized* time.

~~ It does create a second array, which is just as large as the original
~~ one.
~~ 
~~ The doubly linked list looks rather optimal to me. That's how I'd do it
~~ in assembly (I dispise C). I assume O(N), and no extra memory used apart
~~ from some scalars.


Yes, but incorrect algorithms don't score at all. 


Abigail
-- 
sub _'_{$_'_=~s/$a/$_/}map{$$_=$Z++}Y,a..z,A..X;*{($_::_=sprintf+q=%X==>"$A$Y".
"$b$r$T$u")=~s~0~O~g;map+_::_,U=>T=>L=>$Z;$_::_}=*_;sub _{print+/.*::(.*)/s}
*_'_=*{chr($b*$e)};*__=*{chr(1<<$e)};
_::_(r(e(k(c(a(H(__(l(r(e(P(__(r(e(h(t(o(n(a(__(t(us(J())))))))))))))))))))))))


  -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----


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

Date: 2 Jul 1999 18:14:16 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: hiring perl programmers
Message-Id: <377d55d8@cs.colorado.edu>

     [courtesy cc of this posting mailed to cited author]

In comp.lang.perl.misc, abigail@delanet.com writes:
:     for (my $i = my $j = 0; $i < @array; $i ++) {
:         next if $array [$i];
:         if ($i != $j) {@array [$i, $j] = @array [$j, $i];}
:         $j ++;
:     }

We considered:

    t    = a[i];
    a[i] = a[j];
    a[j] = t;

To be suboptimal.  Think about it. :-)

--tom
-- 
    Believe it or not, this signature was chosen randomly 
    from a file of hundreds!


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

Date: Fri, 2 Jul 1999 15:31:38 +0500
From: "Faisal Nasim" <swiftkid@bigfoot.com>
Subject: Re: How do I do an FTP PUT with LWP?
Message-Id: <7lj7o2$7ek5@news.cyber.net.pk>

> Hi,
>
> I am trying to put a file on an ftp server using LWP.

Try Net::FTP

--
Faisal Nasim (the Whiz Kid)
Web: http://wss.hypermart.net/
AOL: Whiz Swift  ICQ: 4265451
FAX: (815) 846-2877




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

Date: 2 Jul 1999 19:59:26 -0000
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: How to put small icons at the start menu bar
Message-Id: <7lj5mu$1e6$1@gellyfish.btinternet.com>

On Tue, 29 Jun 1999 04:33:21 GMT smnayeem@my-deja.com wrote:
> Does anyone know how I can install a small icon on the start menu like
> those of say ICQ, Antivirus programs, Pager etc? 

I think you mean you want to place an icon in the 'System Tray' - you
will probably want to look at the information that is available at 
<http://developer.microsoft.com> and use the module Win32::API - you
almost certainly wont get much help here I'm afraid you might ask in
the microsoft groups ...

/J\
-- 
Jonathan Stowe <jns@gellyfish.com>
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
Hastings: <URL:http://www.newhoo.com/Regional/UK/England/East_Sussex/Hastings>


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

Date: Fri, 02 Jul 1999 22:44:59 GMT
From: finnadat@my-deja.com
Subject: HTML form help - need an image for <input type="reset">
Message-Id: <7ljfd9$ooo$1@nnrp1.deja.com>

I got -

<input type="image" border=0 src="../images/send.gif" value="Send Form">
<input type="reset" border=0 src="../images/reset.gif"></p>

and it doesn't work - I have had some success using the <button>
command from w3.org, but I "need" to have an image for my reset button -
I've seen some postings that include javascripts (from FP 3.0) and
others, but haven't been able to get them to work - any ideas or am I
screwed - could I maybe href to an input type button that is hidden
somewhere??

Jason
finnadat@yahoo.com


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


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

Date: Fri, 2 Jul 1999 08:11:36 +0200
From: "Philipp Götzinger" <Philipp_Goetzinger@gmx.de>
Subject: I need a skript like which one which  is used by come.to/user or fly.to/ (url forwarding skript)
Message-Id: <377c5838@isoit370.bbn.hp.com>

where can i get some like this
thanx

CU




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

Date: 2 Jul 1999 12:58:09 -0000
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: I need a skript like which one which  is used by come.to/user or fly.to/ (url forwarding skript)
Message-Id: <7lid11$1a7$1@gellyfish.btinternet.com>

On Fri, 2 Jul 1999 08:11:36 +0200 "Philipp Götzinger" wrote:
> where can i get some like this

Please dont rely on the Subject: to convey your question as it might
not be visible in its entirey on some newsreaders.

As to your question - I dont think that this is necessarily done by
a script (and even if it was it might not be Perl).  You might want
to ask in the group comp.infosystems.www.servers.unix to find how
that might be done by the server itself.

Even if it was done by a script and even if it was Perl then this group
might still not be the place to ask - there are many archive available
that may contain such a script.

/J\
-- 
Jonathan Stowe <jns@gellyfish.com>
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
Hastings: <URL:http://www.newhoo.com/Regional/UK/England/East_Sussex/Hastings>


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

Date: 1 Jul 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 1 Jul 99)
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.misc (and this Digest), send your
article to perl-users@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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V9 Issue 10
************************************


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