[24950] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7200 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Oct 2 00:06:56 2004

Date: Fri, 1 Oct 2004 21:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 1 Oct 2004     Volume: 10 Number: 7200

Today's topics:
        Dynamic data within regex pattern? <elektrophyte-yahoo>
    Re: Dynamic data within regex pattern? <noreply@gunnar.cc>
    Re: Dynamic data within regex pattern? <mritty@gmail.com>
        How do I convert TIME into Cookie and last-modified-tim <123@abc.xyz>
    Re: How do I convert TIME into Cookie and last-modified <perl&nntp@rhesa.com>
    Re: How do I convert TIME into Cookie and last-modified <glex_nospam@qwest.invalid>
    Re: How to merge .wav files <jarsonk@nospam.com>
    Re: how to put constaints on coefficients obtained from <see@sig.invalid>
    Re: How to: Build Dynamic loading Perl <mritty@gmail.com>
        making easy things difficult: @a-@b (wana)
    Re: making easy things difficult: @a-@b <perl&nntp@rhesa.com>
    Re: making easy things difficult: @a-@b <jurgenex@hotmail.com>
    Re: making easy things difficult: @a-@b <noreply@gunnar.cc>
    Re: making easy things difficult: @a-@b <abigail@abigail.nl>
    Re: making easy things difficult: @a-@b <lv@aol.com>
        removing empty split() fields (dutone)
    Re: removing empty split() fields <noreply@gunnar.cc>
    Re: removing empty split() fields <tadmc@augustmail.com>
        Return top-N of Hashes - hash splice? <ewijaya@singnet.com.sg.removethis>
    Re: Return top-N of Hashes - hash splice? <tadmc@augustmail.com>
    Re: Return top-N of Hashes - hash splice? <see@sig.invalid>
        Scripting trouble (y)
    Re: Scripting trouble <jgibson@mail.arc.nasa.gov>
    Re: Scripting trouble <tadmc@augustmail.com>
    Re: While query <kkeller-usenet@wombat.san-francisco.ca.us>
    Re: While query <abigail@abigail.nl>
    Re: While query <abigail@abigail.nl>
    Re: While query <usa1@llenroc.ude.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 01 Oct 2004 12:53:19 -0700
From: DM <elektrophyte-yahoo>
Subject: Dynamic data within regex pattern?
Message-Id: <415dbf2b$0$10704$2c56edd9@news.cablerocket.com>

Is there a way to set up a regex substitution so that the pattern is dynamically 
generated at runtime?

I.e., something along the lines of...

$output =~ s/$someVar/$someOtherVar/g;

I read in my Perl book that the replacement string is variable interpolated, but 
it didn't say anything about that being the case for the regex pattern. So I 
guess in the situation above, the regex would match the end of a line followed 
by "someVar". And if you escaped the dollar sign, it would match "$someVar" 
litterally.

Any assistance would be greatly appreciated.


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

Date: Fri, 01 Oct 2004 23:18:07 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Dynamic data within regex pattern?
Message-Id: <2s600cF1hobkuU1@uni-berlin.de>

DM wrote:
> Is there a way to set up a regex substitution so that the pattern
> is dynamically generated at runtime?
> 
> I.e., something along the lines of...
> 
> $output =~ s/$someVar/$someOtherVar/g;

What happened when you tried it?

> I read in my Perl book that the replacement string is variable 
> interpolated, but it didn't say anything about that being the case
> for the regex pattern. So I guess in the situation above, the regex
> would match the end of a line followed by "someVar".

You guessed wrong.

> Any assistance would be greatly appreciated.

You don't need any assistance at this stage. You need to play with
Perl, and figure out what 'works' and what doesn't. And maybe read a
little more.

Come back here if you fail in solving a particular problem.

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


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

Date: Fri, 01 Oct 2004 18:30:08 -0400
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Dynamic data within regex pattern?
Message-Id: <cjklq5$q6t$1@misc-cct.server.rpi.edu>

DM wrote:

> Is there a way to set up a regex substitution so that the pattern is 
> dynamically generated at runtime?
> 
> I.e., something along the lines of...
> 
> $output =~ s/$someVar/$someOtherVar/g;
> 
> I read in my Perl book that the replacement string is variable 
> interpolated, but it didn't say anything about that being the case for 
> the regex pattern. 

What Perl book was that, may I ask?

> So I guess in the situation above, the regex would 
> match the end of a line followed by "someVar".

Why just guess?  Why not actually try it?

> And if you escaped the 
> dollar sign, it would match "$someVar" litterally.
> 
> Any assistance would be greatly appreciated.

Read the section "Regexp Quote-Like Operators" from perldoc perlop. 
Specifically, the paragraph beginning "PATTERN may contain variables,"

Paul Lalli


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

Date: Fri, 1 Oct 2004 14:05:38 -0400
From: <123@abc.xyz>
Subject: How do I convert TIME into Cookie and last-modified-time format?
Message-Id: <ZsCdncfeSp4MAcDcRVn-tg@rogers.com>

First of all, I want to know if Cookie's expiration time and HTTP's header's
last modified time are using the same format.

Secondly, how do I format the time from a 10923023324324 kind of number into
the cookie and http header format(s)?




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

Date: Fri, 01 Oct 2004 20:34:36 +0200
From: Rhesa Rozendaal <perl&nntp@rhesa.com>
Subject: Re: How do I convert TIME into Cookie and last-modified-time format?
Message-Id: <415da339$0$78279$e4fe514c@news.xs4all.nl>

123@abc.xyz wrote:
> First of all, I want to know if Cookie's expiration time and HTTP's header's
> last modified time are using the same format.

Yes, they are. It's RFC 1123 format.

> Secondly, how do I format the time from a 10923023324324 kind of number into
> the cookie and http header format(s)?

use HTTP::Date;
print time2str( 1092302332 );
# prints Thu, 12 Aug 2004 09:18:52 GMT


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

Date: Fri, 01 Oct 2004 14:28:15 -0500
From: "J. Gleixner" <glex_nospam@qwest.invalid>
Subject: Re: How do I convert TIME into Cookie and last-modified-time format?
Message-Id: <jdi7d.44$rO6.34487@news.uswest.net>

123@abc.xyz wrote:
> First of all, I want to know if Cookie's expiration time and HTTP's header's
> last modified time are using the same format.
> 
> Secondly, how do I format the time from a 10923023324324 kind of number into
> the cookie and http header format(s)?

Perl has cookies??.. Cool, I'm hungry.


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

Date: Fri, 1 Oct 2004 14:01:33 -0400
From: "Jarson" <jarsonk@nospam.com>
Subject: Re: How to merge .wav files
Message-Id: <0Yg7d.21069$MD5.1260083@news20.bellglobal.com>


"Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> wrote in message 
news:2s5d0kF1h213qU1@uni-berlin.de...
>[ Please don't top-post ]
>
> Also sprach Jarson:
>
>
> Yes, the first 44 bytes of a wav-file is the header. If you want to
> append one wave-file to another, you first have to strip off the first
> 44 bytes of the stream to be appended. After that, you measure the size
> of the stream in bytes (it's filesize minus 44 for obvious reasons).
> Then you append the stream to the file. The last thing you have to do is
> update two fields in the wave-header of the first stream. Those two
> fields denote the size in bytes of the file and the stream:
>
>    #!/usr/bin/perl -w
>
>    use Fcntl qw/:seek/;
>
>    my ($file1, $file2) = @ARGV;
>
>    open WAV1, "+<", $file1 or die $!;
>    open WAV2, $file2 or die $!;
>
>    binmode WAV1;
>    binmode WAV2;
>
>    # the length of the second stream without header
>    my $size = -s WAV2 - 44;
>
>    $/ = \4; # four bytes on each <>
>
>    seek WAV1, 4, SEEK_SET;
>    my $filesize = unpack "V", <WAV1>;     # it's little-endian AFAIK
>    seek WAV1, 32, SEEK_CUR;
>    my $streamsize = unpack "V", <WAV1>;
>
>    seek WAV1, 4, SEEK_SET;
>    print WAV1 pack "V", $filesize + $size;
>    seek WAV1, 32, SEEK_CUR;
>    print WAV1 pack "V", $streamsize + $size;
>
>    seek WAV1, 0, SEEK_END;
>
>    $/ = \4096; # increase block-size a bit
>
>    # skip header of second file
>    seek WAV2, 44, SEEK_SET;
>
>    # append
>    print WAV1 $_ while <WAV2>;
>
>    close WAV1;
>    close WAV2;
>
> The above is totally untested so byte-offsets and such might be a bit
> off. If you get hold of a description of a wave-header you should be
> able to understand what the above does.
>
> Needless to say, you can only concatenate two wave-files when they have
> the same format. Otherwise, you have to convert the one you append
> first.
>
> Tassilo

Fantastic!  I modified Kevin C's code snippet to effectively do as you 
described and the joined
wave file works fine.  Thanks for the insight.   Now I'm riding the wave!
Jarson 




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

Date: Fri, 01 Oct 2004 23:28:18 -0400
From: Bob Walton <see@sig.invalid>
Subject: Re: how to put constaints on coefficients obtained from regression.pm
Message-Id: <415e1e66$1_5@127.0.0.1>

pj wrote:

> Hi, 
> 
> I use regression package to do a multivariate linear regression fit to
> my data. Regression.pm generates coefficient values (thetas), but I
> want to make them always possitive. How do I put constraints (all
> coefficients are positive) when using regression package? any ideas?
> thanks

Sounds like you want a program like lp_solve:

    http://www.cs.sunysb.edu/~algorith/implement/lpsolve/implement.shtml

along with a bunch of other sites.

That's one that you probably don't want to reinvent in Perl.
-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


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

Date: Fri, 01 Oct 2004 18:10:12 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: How to: Build Dynamic loading Perl
Message-Id: <84h7d.262$gS1.158@trndny04>

"Paula" <pcapacio@amfam.com> wrote in message
news:bd052363.0410010632.24acce29@posting.google.com...
> Basic recipe needed for building a dynamic loading Perl on HP/UX V11.
> We have perl 5.006001 installed and have since installed other modules
> but when attempting to use them we get: You may need to build a new
> perl executable which either supports dynamic loading or has the
> Date::Calc module statically linked into it.
>
> I don't know anything about installing or configuring Perl on UNIX or
> much about UNIX in general.  I use ActiveState's Active Perl and PPM
> on Windows and that IS dynamic already.  Any help to get us started
> would be greatly appreciated.

You're using an ancient perl.  Upgrading is definately recommended.
http://www.perl.com/CPAN/ports/index.html#hpux  would seem to be the
port for you.  All source code distributions include installation
instructions in the file named INSTALL.  Perhaps you should start here
and try to follow those directions?

Paul Lalli




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

Date: 1 Oct 2004 13:21:20 -0700
From: ioneabu@yahoo.com (wana)
Subject: making easy things difficult: @a-@b
Message-Id: <bf0b47ca.0410011221.5ab4c4d1@posting.google.com>

Suppose I want to create an array made up of all of the elements of @a
except for those that are also contained in @b.  For example:

(apple, banana, peach, pear) - (apple, peach) = (banana, pear)

I was hoping that @c = @a - @b; would work.  This would really be
making an easy task easy.

Searching the faqs and docs, the solution seems to be a confusing (to
me at least) use of either a hash or grep.  I looked at the map
function which seemed appropriate, but I couldn't think of an obvious
way to do it.

I finally settled for the old C way of doing it of having a loop in a
loop to compare each value of one array against each value of the
other.

Is there a better way?  Would it be wrong for Perl to make @a-@b
legal?


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

Date: Fri, 01 Oct 2004 22:47:03 +0200
From: Rhesa Rozendaal <perl&nntp@rhesa.com>
Subject: Re: making easy things difficult: @a-@b
Message-Id: <415dc243$0$78749$e4fe514c@news.xs4all.nl>

wana wrote:
> Suppose I want to create an array made up of all of the elements of @a
> except for those that are also contained in @b.  For example:
> 
> (apple, banana, peach, pear) - (apple, peach) = (banana, pear)
> 
> I was hoping that @c = @a - @b; would work.  This would really be
> making an easy task easy.

If it would do anything, I'd expect it to do vector subtraction:

	push @c, (shift(@a) - shift(@b))  while( @a or @b );

But I am not a set theorist.

> Searching the faqs and docs, the solution seems to be a confusing (to
> me at least) use of either a hash or grep.  I looked at the map
> function which seemed appropriate, but I couldn't think of an obvious
> way to do it.
> 
> I finally settled for the old C way of doing it of having a loop in a
> loop to compare each value of one array against each value of the
> other.
> 
> Is there a better way?  Would it be wrong for Perl to make @a-@b
> legal?

In general yes, because I have a different idea of arrays than you ;)

Have a look at Set::Array, 
http://search.cpan.org/~djberg/Set-Array-0.11/Array.pm
It implements a - that you seek.

Rhesa


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

Date: Fri, 01 Oct 2004 21:04:09 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: making easy things difficult: @a-@b
Message-Id: <dDj7d.694$Xk1.259@trnddc02>

wana wrote:
> Suppose I want to create an array made up of all of the elements of @a
> except for those that are also contained in @b.  For example:
>
> (apple, banana, peach, pear) - (apple, peach) = (banana, pear)
>
> I was hoping that @c = @a - @b; would work.  This would really be
> making an easy task easy.

Well, if @a = (apple, banana, peach, pear) and @b = (apple, peach) then I 
would expect @a - @b to be (0, 0, 0, 0) because the numerical because the 
numerical values of every singe element is 0 and 0 - 0 is 0.
But what do I know about vector arithmetic...

If a binary minus on arrays would have such a semantic as you indicated, 
then I wonder what you expect to happen if e.g. @a contains the value 
"apple" twice. Would both occurences be removed? Only the first? Only the 
last?

If you don't want a vector but a set then don't use an array but a hash.

> Searching the faqs and docs, the solution seems to be a confusing (to
> me at least) use of either a hash or grep.

You don't say _which_ FAQ you checked, but "perldoc -q intersection" seems 
to be pretty clear to me.

> I looked at the map
> function which seemed appropriate, but I couldn't think of an obvious
> way to do it.

> I finally settled for the old C way of doing it of having a loop in a
> loop to compare each value of one array against each value of the
> other. Is there a better way?

Absolutely. See the FAQ.

> Would it be wrong for Perl to make @a-@b legal?

Wrong? No, why? But it probably wouldn't do what you seem to expect it to 
do.
If there would ever be a binary minus on arrays it would more likely be used 
for vector substraction because there are other data structures, which are 
better suited for set operations than arrays.

jue 




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

Date: Fri, 01 Oct 2004 23:29:08 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: making easy things difficult: @a-@b
Message-Id: <2s60kuF1gi8kcU1@uni-berlin.de>

wana wrote:
> Suppose I want to create an array made up of all of the elements of
> @a except for those that are also contained in @b.  For example:
> 
> (apple, banana, peach, pear) - (apple, peach) = (banana, pear)
> 
> I was hoping that @c = @a - @b; would work.  This would really be 
> making an easy task easy.
> 
> Searching the faqs and docs, the solution seems to be a confusing
> (to me at least) use of either a hash or grep.

Why not both?

     my %b = map { $_, 1 } @b;
     my @c = grep !$b{$_}, @a;

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


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

Date: 01 Oct 2004 21:45:14 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: making easy things difficult: @a-@b
Message-Id: <slrnclrjva.hv.abigail@alexandra.abigail.nl>

wana (ioneabu@yahoo.com) wrote on MMMMXLIX September MCMXCIII in
<URL:news:bf0b47ca.0410011221.5ab4c4d1@posting.google.com>:
//  Suppose I want to create an array made up of all of the elements of @a
//  except for those that are also contained in @b.  For example:
//  
//  (apple, banana, peach, pear) - (apple, peach) = (banana, pear)
//  
//  I was hoping that @c = @a - @b; would work.  This would really be
//  making an easy task easy.
//  
//  Searching the faqs and docs, the solution seems to be a confusing (to
//  me at least) use of either a hash or grep.  I looked at the map
//  function which seemed appropriate, but I couldn't think of an obvious
//  way to do it.
//  
//  I finally settled for the old C way of doing it of having a loop in a
//  loop to compare each value of one array against each value of the
//  other.

That's very inefficient. If both arrays contain 1000 elements, you
would be doing 1_000_000 comparisons.

The solutions from the faq are (nearly) linear.

//  Is there a better way?  Would it be wrong for Perl to make @a-@b
//  legal?

@a - @b *is* legal. It returns the number of elements in @a minus the
number of elements in @b.

I'd go for:

    @c = do {my %t; @t {@a} = (); delete @t {@b}; keys %t};

or, if the order needs to be maintained:

    @c = do {my %t; @t {@b} = (); grep {!exists $t {$_}} @a};


Abigail
-- 
perl -wle 'sub _ "Just another Perl Hacker"; print prototype q ^_^'


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

Date: Fri, 01 Oct 2004 22:20:15 -0500
From: l v <lv@aol.com>
Subject: Re: making easy things difficult: @a-@b
Message-Id: <415e1e66$1_6@corp.newsgroups.com>

wana wrote:
> Suppose I want to create an array made up of all of the elements of @a
> except for those that are also contained in @b.  For example:
> 
> (apple, banana, peach, pear) - (apple, peach) = (banana, pear)
> 
> I was hoping that @c = @a - @b; would work.  This would really be
> making an easy task easy.
> 
> Searching the faqs and docs, the solution seems to be a confusing (to
> me at least) use of either a hash or grep.  I looked at the map
> function which seemed appropriate, but I couldn't think of an obvious
> way to do it.
> 
> I finally settled for the old C way of doing it of having a loop in a
> loop to compare each value of one array against each value of the
> other.
> 
> Is there a better way?  Would it be wrong for Perl to make @a-@b
> legal?

Try List::Compare::Functional

Len


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 100,000 Newsgroups - 19 Different Servers! =-----


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

Date: 1 Oct 2004 16:54:44 -0700
From: dutone@hotmail.com (dutone)
Subject: removing empty split() fields
Message-Id: <7d56a4da.0410011554.68c1b116@posting.google.com>

sample file:
 +123n
blah blah blah
+123+
more blah
sasas
assa
+123+

open IN, "data" or die $!;
my @data = <IN>;
my @splitData = split /\+123(?:\+)?/, "@data";   
for(@splitData) {  print "LINE ** $_"; }

prints:
LINE ** LINE ** n
 blah blah blah
 LINE ** 
 more blah
 sasas
 assa
 LINE ** 

now i understand that split returns the number of matched items plus 1
if no limit is given.
so in order to weed the empty elements out will i always have to do
somthing like the following:

my @splitData = grep { $_ and length $_ > 0 and  $_ !~ /^\s+$/  }
split /\+123(?:\+)?/, "@data";

or get a count on how many times /\+123(?:\+)?/ matches in @data and
use that for a limit?

thanks


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

Date: Sat, 02 Oct 2004 02:29:13 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: removing empty split() fields
Message-Id: <2s6b6qF1h52siU1@uni-berlin.de>

dutone wrote:
> now i understand that split returns the number of matched items
> plus 1 if no limit is given.

What's wrong with the description in "perldoc -f split":

"Splits a string into a list of strings and returns that list. By
default, empty leading fields are preserved, and empty trailing ones
are deleted."

> so in order to weed the empty elements out will i always have to do
> somthing like the following:
> 
> my @splitData = grep { $_ and length $_ > 0 and  $_ !~ /^\s+$/  }
> split /\+123(?:\+)?/, "@data";
> 
> or get a count on how many times /\+123(?:\+)?/ matches in @data
> and use that for a limit?

It would be easier to help you if you let us know exactly what it is
you are trying to do. I for one do not fully understand.

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


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

Date: Fri, 1 Oct 2004 20:22:46 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: removing empty split() fields
Message-Id: <slrncls0n6.20i.tadmc@magna.augustmail.com>

dutone <dutone@hotmail.com> wrote:
> sample file:
>  +123n
> blah blah blah
> +123+
> more blah
> sasas
> assa
> +123+
> 
> open IN, "data" or die $!;
> my @data = <IN>;
> my @splitData = split /\+123(?:\+)?/, "@data";   
                                        ^     ^
                                        ^     ^
                                        ^     ^
> for(@splitData) {  print "LINE ** $_"; }
> 
> prints:
> LINE ** LINE ** n
>  blah blah blah
>  LINE ** 
>  more blah
>  sasas
>  assa
>  LINE ** 


Yes...

 ... and what is it that you want it to print instead?

Should we all just guess?

I'll assume you don't want the extra space characters at the
beginning of the lines after the first line?


> now i understand that split returns the number of matched items plus 1
> if no limit is given.


That is not related to your problem in any way.


> so in order to weed the empty elements


There are no "empty elements" here. Each element in the list
returned by split() is non-empty (ie. has length()).



You put the extra space characters in yourself by interpolating
the array.

If you don't want them in there, then don't put them in there.  :-)


   my @splitData = split /\+123(?:\+)?/, join "", @data;

or

   my @splitData;
   { local $" = "";
      @splitData = split /\+123(?:\+)?/, "@data";
   }

or, do without the @data temporary array altogether:

   my @splitData;
   { local $/;
      @splitData = split /\+123(?:\+)?/, <IN>;
   }



Yours is a Frequently Asked Question:

   perldoc -q spaces

       Why do I get weird spaces when I print an array of lines?


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 02 Oct 2004 11:05:42 +0800
From: "Edward Wijaya" <ewijaya@singnet.com.sg.removethis>
Subject: Return top-N of Hashes - hash splice?
Message-Id: <opse72vsxdavcff0@news.singnet.com.sg>

Hi

With this hashes:

$hash = {
           '1-1' => 3,
           '2-3' => 2,
           '2-2' => 1,
           '1-2' => 6,
           '1-3' => 3
         };

What's the best to retrieve the top-N hashes
 from it. So, with N = 3 it will become:

$hash_top3 = {
           '1-1' => 3,
           '2-3' => 2,
           '2-2' => 1,
         };

Thanks beforehand.

Regards,
Edward WIJAYA
SINGAPORE


-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


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

Date: Fri, 1 Oct 2004 22:20:44 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Return top-N of Hashes - hash splice?
Message-Id: <slrncls7kc.1gi.tadmc@magna.augustmail.com>

Edward Wijaya <ewijaya@singnet.com.sg.removethis> wrote:

> With this hashes:
> 
> $hash = {
>            '1-1' => 3,
>            '2-3' => 2,
>            '2-2' => 1,
>            '1-2' => 6,
>            '1-3' => 3
>          };
> 
> What's the best to retrieve the top-N hashes


Hashes do not have a "top". They are UNordered.

If you need to preserve the order, then you have chosen
the wrong data structure.


What is it that you are actually trying to accomplish?


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 02 Oct 2004 00:00:46 -0400
From: Bob Walton <see@sig.invalid>
Subject: Re: Return top-N of Hashes - hash splice?
Message-Id: <415e2603$1_2@127.0.0.1>

Edward Wijaya wrote:

 ...

> With this hashes:
> 
> $hash = {
>           '1-1' => 3,
>           '2-3' => 2,
>           '2-2' => 1,
>           '1-2' => 6,
>           '1-3' => 3
>         };
> 
> What's the best to retrieve the top-N hashes
> from it. So, with N = 3 it will become:
> 
> $hash_top3 = {
>           '1-1' => 3,
>           '2-3' => 2,
>           '2-2' => 1,
>         };
 ...

> Edward WIJAYA
 ...
Not sure why you want the first three hash entries, since they are in 
pseudo-random order -- but try:

$hash = {
           '1-1' => 3,
           '2-3' => 2,
           '2-2' => 1,
           '1-2' => 6,
           '1-3' => 3
         };
$n=3;
@$hash_top3{(keys %$hash)[0..$n-1]}=@$hash{(keys %$hash)[0..$n-1]};
use Data::Dumper;
print Dumper($hash_top3);

-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


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

Date: 1 Oct 2004 15:14:14 -0700
From: ayush.a@gmail.com (y)
Subject: Scripting trouble
Message-Id: <213ce376.0410011414.62bf01bc@posting.google.com>

I am unable to run scripts on OSX.  Here is what I did:

I created a file script (no extensions) as follows:
   #!/usr/bin/perl
   echo "Hello"

And then I save it.  After that I go to my shell and try running this
file and I get the following message:
   bash: /Users/Y/script: /usr/bin/perl: bad interpreter: Permission
denied

Any idea what I am doing wrong, and how I can fix this?


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

Date: Fri, 01 Oct 2004 16:44:33 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Scripting trouble
Message-Id: <011020041644330329%jgibson@mail.arc.nasa.gov>

In article <213ce376.0410011414.62bf01bc@posting.google.com>,
<ayush.a@gmail.com> wrote:

> I am unable to run scripts on OSX.  Here is what I did:
> 
> I created a file script (no extensions) as follows:
>    #!/usr/bin/perl
>    echo "Hello"
> 
> And then I save it.  After that I go to my shell and try running this
> file and I get the following message:
>    bash: /Users/Y/script: /usr/bin/perl: bad interpreter: Permission
> denied
> 
> Any idea what I am doing wrong, and how I can fix this?

Did you set your file to be executable with the 'chmod +x filename'
command?
Does the /usr/bin/perl file exist and is executable?
Did you know that perl does not have an echo function?
Are you trying to run a perl program or a bash script?
What version of OSX are you running, and on what platform?

The answers to these questions may help someone help you.


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

Date: Fri, 1 Oct 2004 20:05:19 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Scripting trouble
Message-Id: <slrnclrvmf.20i.tadmc@magna.augustmail.com>

y <ayush.a@gmail.com> wrote:


> I created a file script (no extensions) as follows:
>    #!/usr/bin/perl
>    echo "Hello"
     ^^^^
     ^^^^

   perldoc -f echo


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Fri, 01 Oct 2004 12:03:37 -0700
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: While query
Message-Id: <99m032x5ic.ln2@goaway.wombat.san-francisco.ca.us>

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

On 2004-10-01, Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us> wrote:
>
> use strict;
> use warnings;
>
> my @array=('','2');
>
> foreach (reverse @array)
> {
> 	print "''popped'' [$_]\n";
> }

Oops--this won't actually empty the array as the various pop-based
methods will.  Depending on what the goal is, that may or may not be an
issue.  (It does continue to loop if an element is undef, which also may
or may not be a concern.)

- --keith

- -- 
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBXaoHhVcNCxZ5ID8RAqCOAJ9G4Sj5dAS+0IoIpY3kf64SsPt4OgCfRFw8
W60xtsNDp4oagNSYjUBisZo=
=ENhP
-----END PGP SIGNATURE-----


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

Date: 01 Oct 2004 21:37:20 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: While query
Message-Id: <slrnclrjgf.hv.abigail@alexandra.abigail.nl>

A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMXLIX September
MCMXCIII in <URL:news:Xns957588191B864asu1cornelledu@132.236.56.8>:
!!  "P.R.Brady" <iss025@bangor.ac.uk> wrote in
!!  news:415D9051.60300@bangor.ac.uk: 
!!  
!! > So to empty and process my array/stack I need to:
!! > while (scalar(@array)) {
!! >      $_=pop(@array);
!! >          ...
!! > }
!!  
!!  You could do 
!!  
!!  use strict;
!!  use warnings;
!!  
!!  my @array = ('hello', '');
!!  
!!  while(@array and my ($x) = pop @array) {
!!      print "Popped: [$x]\n";
!!  }
!!  
!!  __END__


Tricky. And not very nice to post this without explaining why this
works. (It won't work if you remove the parens around the '$x').


Abigail
-- 
:$:=~s:$":Just$&another$&:;$:=~s:
:Perl$"Hacker$&:;chop$:;print$:#:


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

Date: 01 Oct 2004 21:39:40 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: While query
Message-Id: <slrnclrjks.hv.abigail@alexandra.abigail.nl>

Keith Keller (kkeller-usenet@wombat.san-francisco.ca.us) wrote on
MMMMXLIX September MCMXCIII in <URL:news:99m032x5ic.ln2@goaway.wombat.san-francisco.ca.us>:
||  -----BEGIN xxx SIGNED MESSAGE-----
||  Hash: SHA1
||  
||  On 2004-10-01, Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us> wrote:
|| >
|| > use strict;
|| > use warnings;
|| >
|| > my @array=('','2');
|| >
|| > foreach (reverse @array)
|| > {
|| > 	print "''popped'' [$_]\n";
|| > }
||  
||  Oops--this won't actually empty the array as the various pop-based
||  methods will.  Depending on what the goal is, that may or may not be an
||  issue.  (It does continue to loop if an element is undef, which also may
||  or may not be a concern.)


    printf "Popped: %s\n" => pop @array while @array;


Abigail
-- 
A perl rose:  perl -e '@}>-`-,-`-%-'


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

Date: 2 Oct 2004 03:53:16 GMT
From: "A. Sinan Unur" <usa1@llenroc.ude.invalid>
Subject: Re: While query
Message-Id: <Xns9575F3378167Aasu1cornelledu@132.236.56.8>

Abigail <abigail@abigail.nl> wrote in 
news:slrnclrjgf.hv.abigail@alexandra.abigail.nl:

> A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMXLIX September
> MCMXCIII in <URL:news:Xns957588191B864asu1cornelledu@132.236.56.8>:

> !!  "P.R.Brady" <iss025@bangor.ac.uk> wrote in
> !!  news:415D9051.60300@bangor.ac.uk: 
 
> !! > So to empty and process my array/stack I need to:
> !! > while (scalar(@array)) {
> !! >      $_=pop(@array);
> !! >          ...
> !! > }
> !!  
> !!  You could do 
> !!  
> !!  use strict;
> !!  use warnings;
> !!  
> !!  my @array = ('hello', '');
> !!  
> !!  while(@array and my ($x) = pop @array) {
> !!      print "Popped: [$x]\n";
> !!  }
> !!  
> !!  __END__
> 
> Tricky. And not very nice to post this without explaining why this
> works. (It won't work if you remove the parens around the '$x').

Well, I know you know why it works, but I also see the point of your 
remark, so here it goes. Consider the condition in the while statement:

@array and my ($x) = pop @array

@array in this context returns the number of elements in the array. Only if 
the number of elements in @array is greater than zero will the right hand 
side above will be evaulated.

my ($x) = pop @array;

pops an element off @array, puts it in $x, and returns the single element 
list ($x). Hence, the RHS is always true, if @array has any elements. This 
way, the while-loop does not prematurely abort if one of the elements in 
the list is undefined.

I am really not suggesting this as a good solution. In fact, I would 
probably be inclined to use the first answer I posted:

while (@array) {
    my $x = pop @array;
    # do something with $x
}

I only posted the solution above to show how the test can be constructed as 
a single compund statement that does not abort in the middle if one of the 
list elements is undefined.

Hope this is clearer and I did not make too many errors above.

Sinan.


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 7200
***************************************


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