[11387] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4986 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 26 16:07:40 1999

Date: Fri, 26 Feb 99 13:02:59 -0800
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, 26 Feb 1999     Volume: 8 Number: 4986

Today's topics:
        Splitting and assigning parts of an array thst@my-dejanews.com
    Re: Splitting and assigning parts of an array (Steve Linberg)
    Re: Splitting and assigning parts of an array <Allan@due.net>
    Re: Splitting and assigning parts of an array (Larry Rosler)
        Strange behavior <jalil@corp.home.net>
    Re: Strange behavior (Steve Linberg)
        syntax error? <paxtond@ix.netcom.com>
        Using a glob with sort (Bill Moseley)
        What is %h=undef (assigning undef to a hash) supposed t <avm@atos.be>
    Re: What is %h=undef (assigning undef to a hash) suppos <wmwilson1@go.com>
    Re: What is %h=undef (assigning undef to a hash) suppos <wmwilson1@go.com>
    Re: What is %h=undef (assigning undef to a hash) suppos <wmwilson1@go.com>
    Re: What is %h=undef (assigning undef to a hash) suppos <wmwilson1@go.com>
    Re: What is %h=undef (assigning undef to a hash) suppos <zenin@bawdycaste.org>
    Re: What is %h=undef (assigning undef to a hash) suppos (Larry Rosler)
        Win32::Registry doesn't work??? <mckang@pacific.net.sg>
        Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)

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

Date: Fri, 26 Feb 1999 19:07:23 GMT
From: thst@my-dejanews.com
Subject: Splitting and assigning parts of an array
Message-Id: <7b6rd2$mda$1@nnrp1.dejanews.com>

Assuming we have an array with 1000 elements. Now I want

1.) to assign the last 200 elements to another array
(@newarray = $oldarray[800 .. 999] doesn't work)

2.) remove the last 200 elements from the original array.
(@oldarray = oldarray[0 .. 799] doesn't work)

How do I do that (most efficiently) ?

Bye

Thomas

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: Fri, 26 Feb 1999 14:51:50 -0500
From: linberg@literacy.upenn.edu (Steve Linberg)
Subject: Re: Splitting and assigning parts of an array
Message-Id: <linberg-2602991451500001@ltl1.literacy.upenn.edu>

In article <7b6rd2$mda$1@nnrp1.dejanews.com>, thst@my-dejanews.com wrote:

> 1.) to assign the last 200 elements to another array
> (@newarray = $oldarray[800 .. 999] doesn't work)

@newarray = @oldarray[800 .. 999];

> 2.) remove the last 200 elements from the original array.
> (@oldarray = oldarray[0 .. 799] doesn't work)

$#oldarray -= 200;

You might want to read up on arrays again.

-- 
Steve Linberg, Systems Programmer &c.
National Center on Adult Literacy, University of Pennsylvania
email: <linberg@literacy.upenn.edu>
WWW: <http://www.literacyonline.org>


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

Date: Fri, 26 Feb 1999 15:16:48 -0500
From: "Allan M. Due" <Allan@due.net>
Subject: Re: Splitting and assigning parts of an array
Message-Id: <7b6v39$f2o$1@camel19.mindspring.com>

thst@my-dejanews.com wrote in message <7b6rd2$mda$1@nnrp1.dejanews.com>...
:Assuming we have an array with 1000 elements. Now I want
:1.) to assign the last 200 elements to another array
:(@newarray = $oldarray[800 .. 999] doesn't work)


You are so close, we are still working with an array (an array slice
actually)

@newarray = @oldarray[800 .. 999];

I am not sure how many versions of perl this works under but you can also:

@newarray = @oldarray[-200 .. -1];

:
:2.) remove the last 200 elements from the original array.
:(@oldarray = oldarray[0 .. 799] doesn't work)


Still need that @ sign, or just shorten the length with  $#.

HTH

AmD

--
$email{'Allan M. Due'} = ' All@n.Due.net ';
--random quote --
'Where such things here as we do speak about?  Or have we eaten on
the insane root that takes the reason prisoner?'
 - Banquo in MacBeth





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

Date: Fri, 26 Feb 1999 12:37:50 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Splitting and assigning parts of an array
Message-Id: <MPG.1140a5f41410971598969a@nntp.hpl.hp.com>

[Posted and a courtesy copy mailed.]

In article <7b6rd2$mda$1@nnrp1.dejanews.com> on Fri, 26 Feb 1999 
19:07:23 GMT, thst@my-dejanews.com <thst@my-dejanews.com> says...
> Assuming we have an array with 1000 elements. Now I want
> 
> 1.) to assign the last 200 elements to another array
> (@newarray = $oldarray[800 .. 999] doesn't work)

   @newarray = @oldarray[800 .. 999];

> 2.) remove the last 200 elements from the original array.
> (@oldarray = oldarray[0 .. 799] doesn't work)

   @oldarray = splice @oldarray, 0, 800;

or (essentially instantaneously, so *much* better):
 
  $#oldarray = 799;

> How do I do that (most efficiently) ?

   @newarray = splice @oldarray, 800;

does both of the above in one swell foop.

-- 
Larry Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Fri, 26 Feb 1999 18:56:09 GMT
From: "Jalil Feghhi" <jalil@corp.home.net>
Subject: Strange behavior
Message-Id: <920055369.832025@zeppelin.svr.home.net>

When I open a file and then call this function, I get two negative offsets
inside the while loop. I am testing on NT. We are not supposed to get
negative values for offsets. Is that right? Could somebody explain this.

You can just open any text file you want (use MCONFIGFH as file handle) and
then call this function.

sub GetPos{
  my ($dscr, $cfile,$relto);
  my ($var1,$var2);
  my $pos;
  my $line;

  $pos = tell(MCONFIGFH);
  print "offset: $pos\n";

  while ($line = <MCONFIGFH>){
    # Get rid of new line and trim the leading/trailing blanks
    $pos = tell(MCONFIGFH);
    print "offset: $pos\n";
  }
  return;
}


-Jalil





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

Date: Fri, 26 Feb 1999 14:41:49 -0500
From: linberg@literacy.upenn.edu (Steve Linberg)
Subject: Re: Strange behavior
Message-Id: <linberg-2602991441490001@ltl1.literacy.upenn.edu>

Are you using -w?  Your example references a filehandle before you have
done anything with it.  If you are setting it up elsewhere, include that
code in your sample.  tell returns -1 on my system when used like this:

$ perl
my $pos = tell(FOO);
print $pos;
__END__
-1

-- 
Steve Linberg, Systems Programmer &c.
National Center on Adult Literacy, University of Pennsylvania
email: <linberg@literacy.upenn.edu>
WWW: <http://www.literacyonline.org>


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

Date: Fri, 26 Feb 1999 15:46:19 -0500
From: "J. Daniel Paxton" <paxtond@ix.netcom.com>
Subject: syntax error?
Message-Id: <36D7081A.A182FCE0@ix.netcom.com>

For some reason I always feel like I am feeding myself to the lions
whenever I post a question to this newsgroup.  I must admit that most
replies have been very helpful in the past so and as I am still a newbie
to PERL I must continue to ask newbie-style questions.  I am trying to
access a dbm file, check for user name, and then decrement for each use
by specific user.  I thought that this would be very easy to do but I am
running into syntax errors.  I think I am blind because I have been
looking at this short script for quite some time and do not see the
error.  Of course the logic of the script may be flawed as well but I
need to get past the syntax error at the line :  "if (exists $timesleft"

Please help:

# Check for user authorization and decrement

tie %timesleft, 'DB_File', 'users.dbm';

if (exists $timesleft{$Account_Number}) {
  $timesleft {$Account_Number} --;
 }
else {
         print "Content-type: text/html\n\n";
                print "<html><head><title>Wrong USER
ID</title></head>\n";
                print "<body><h1>Incorrect Information</h1><br>\n";
                print "I'm sorry, but it would appear that you've made
an error\n";
                print "filling out the $Account_Number field correctly.
Please click\n";
                print "back and try again.\n";
                print "</body></html>\n";
                exit;
        }
if ($timesleft {$Account_Number} <= 0) {
         print "Content-type: text/html\n\n";
                print "<html><head><title>No More
Credit</title></head>\n";
                print "<body><h1>Out of Credit</h1><br>\n";
                print "I'm sorry, but it would appear that you have
used\n";
                print "all available credit for your account.\n";
                print "Please call Michael A. McDaniel. Ph.D. at
804-364-8474 or \n";
        print "email him at McDaniel@Curryinc.com for additional
credit.\n\n";
                print "</body></html>\n";
                exit;
}

untie %timesleft;



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

Date: Fri, 26 Feb 1999 11:40:02 -0800
From: moseley@best.com (Bill Moseley)
Subject: Using a glob with sort
Message-Id: <MPG.1140986979c242409896c1@206.184.139.132>

I'm missing something obvious.

I'm sorting by:
	foreach (sort sub_sort keys %hash) {
	 [...];
	}

And I'm trying to figure out how to create the subroutine on the fly by 
creating anonymous subs.  I've tried a bunch of things, but haven't had 
any luck.

I'm also getting "Variable "@status_sort" will not stay shared"
which I guess I'm getting because I'm not using anonymous subs in this 
example below.

my @status_sort;
@status_sort[ $STATUS_CONFIRMED, 
              $STATUS_NO_SHOW, 
              $STATUS_WAIT_LIST, 
              $STATUS_CANCELED] = (0..4);

SWITCH: for ( $sortby ) {
    /Date/
      && do {
            sub sort_date {
                            $students{$a}{TIME}
                                <=>
                            $students{$b}{TIME}
                           };
            *sub_sort = *sort_date;
            last SWITCH;
          };

    /Status/
      && do {
            sub  sort_status {
                            $status_sort[$students{$a}{STATUS}]
                                <=>
                            $status_sort[$students{$b}{STATUS}]
                            };
             *sub_sort = *sort_status;
            last SWITCH;
          };
} 

I also had tried assigning an anonymous sub to a scalar and then calling 
the sort as foreach (sort $sort->() keys %hash) but that's was off, too.
             


-- 
Bill Moseley mailto:moseley@best.com


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

Date: Fri, 26 Feb 1999 18:26:21 +0100
From: Arnauld Van Muysewinkel <avm@atos.be>
Subject: What is %h=undef (assigning undef to a hash) supposed to do ?
Message-Id: <36D6D93D.EDD099AF@atos.be>

I'm debugging some ugly Perl code written by someone else.

An error as shown up since I upgraded from Perl 5.004_04 to Perl
5.005_02

Somewhere in the code there is something like : %h=undef;
In Perl 5.004_04 it results in %h containing : { } (an empty hash)
In Perl 5.005_05 it results in %h containing : { "" => undef } (a hash
containing one undef element)

It can be tested with the following code :
perl -e '%h=undef;print scalar(%h)'
which prints '0' under Perl 5.004_04 and '1/8' under Perl 5.005_02
(perl -e '%h=(undef);print scalar(%h)' give the same results)

What is the correct behaviour (5.004_04 or 5.005_02) ?
I suspect it is a problem of context conversion (converting the (undef)
array into a hash).

  -- Arnauld Van Muysewinkel (avm@atos.be, avm@dixite.com)

     Atos SA [ http://www.atos.be ]
     DiXiTe SPRL [ http://dixite.com ]



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

Date: Fri, 26 Feb 1999 19:44:56 GMT
From: Mike Wilson <wmwilson1@go.com>
Subject: Re: What is %h=undef (assigning undef to a hash) supposed to do ?
Message-Id: <7b6tjl$og4$1@nnrp1.dejanews.com>

In article <36D6D93D.EDD099AF@atos.be>,
  Arnauld Van Muysewinkel <avm@atos.be> wrote:
> I'm debugging some ugly Perl code written by someone else.
>
> An error as shown up since I upgraded from Perl 5.004_04 to Perl
> 5.005_02
>
> Somewhere in the code there is something like : %h=undef;
I think what that person wanted to do is:

foreach my $key (sort keys(%h)) {

      delete $h{$key};

}

undef'ing a hash is a wrong, ugly way to try to accomplish this.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: Fri, 26 Feb 1999 19:45:21 GMT
From: Mike Wilson <wmwilson1@go.com>
Subject: Re: What is %h=undef (assigning undef to a hash) supposed to do ?
Message-Id: <7b6tke$ohm$1@nnrp1.dejanews.com>

In article <36D6D93D.EDD099AF@atos.be>,
  Arnauld Van Muysewinkel <avm@atos.be> wrote:
> I'm debugging some ugly Perl code written by someone else.
>
> An error as shown up since I upgraded from Perl 5.004_04 to Perl
> 5.005_02
>
> Somewhere in the code there is something like : %h=undef;
I think what that person wanted to do is:

foreach my $key (sort keys(%h)) {

      delete $h{$key};

}

undef'ing a hash is a wrong, ugly way to try to accomplish this.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: Fri, 26 Feb 1999 19:45:28 GMT
From: Mike Wilson <wmwilson1@go.com>
Subject: Re: What is %h=undef (assigning undef to a hash) supposed to do ?
Message-Id: <7b6tkl$oia$1@nnrp1.dejanews.com>

In article <36D6D93D.EDD099AF@atos.be>,
  Arnauld Van Muysewinkel <avm@atos.be> wrote:
> I'm debugging some ugly Perl code written by someone else.
>
> An error as shown up since I upgraded from Perl 5.004_04 to Perl
> 5.005_02
>
> Somewhere in the code there is something like : %h=undef;
I think what that person wanted to do is:

foreach my $key (sort keys(%h)) {

      delete $h{$key};

}

undef'ing a hash is a wrong, ugly way to try to accomplish this.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: Fri, 26 Feb 1999 19:45:12 GMT
From: Mike Wilson <wmwilson1@go.com>
Subject: Re: What is %h=undef (assigning undef to a hash) supposed to do ?
Message-Id: <7b6tk5$ohk$1@nnrp1.dejanews.com>

In article <36D6D93D.EDD099AF@atos.be>,
  Arnauld Van Muysewinkel <avm@atos.be> wrote:
> I'm debugging some ugly Perl code written by someone else.
>
> An error as shown up since I upgraded from Perl 5.004_04 to Perl
> 5.005_02
>
> Somewhere in the code there is something like : %h=undef;
I think what that person wanted to do is:

foreach my $key (sort keys(%h)) {

      delete $h{$key};

}

undef'ing a hash is a wrong, ugly way to try to accomplish this.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: 26 Feb 1999 20:24:46 GMT
From: Zenin <zenin@bawdycaste.org>
Subject: Re: What is %h=undef (assigning undef to a hash) supposed to do ?
Message-Id: <920060652.401251@thrush.omix.com>

Mike Wilson <wmwilson1@go.com> wrote:
	>snip<
: I think what that person wanted to do is:
: foreach my $key (sort keys(%h)) {
:       delete $h{$key};
: }
: undef'ing a hash is a wrong, ugly way to try to accomplish this.

	You're joking right?  God I really hope so...

		%h = ();

	Does the exact same thing much, much, MUCH faster.

	Why the heck is that sort() in there too?  Or did you just
	forget the :-) at the end of that statement?

-- 
-Zenin (zenin@archive.rhps.org)           From The Blue Camel we learn:
BSD:  A psychoactive drug, popular in the 80s, probably developed at UC
Berkeley or thereabouts.  Similar in many ways to the prescription-only
medication called "System V", but infinitely more useful. (Or, at least,
more fun.)  The full chemical name is "Berkeley Standard Distribution".


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

Date: Fri, 26 Feb 1999 12:45:02 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: What is %h=undef (assigning undef to a hash) supposed to do ?
Message-Id: <MPG.1140a7a5f2c596fb98969b@nntp.hpl.hp.com>

[Posted and a courtesy copy mailed.]

In article <7b6tjl$og4$1@nnrp1.dejanews.com> on Fri, 26 Feb 1999 
19:44:56 GMT, Mike Wilson <wmwilson1@go.com> says...
> In article <36D6D93D.EDD099AF@atos.be>,
>   Arnauld Van Muysewinkel <avm@atos.be> wrote:
> > I'm debugging some ugly Perl code written by someone else.
> >
> > An error as shown up since I upgraded from Perl 5.004_04 to Perl
> > 5.005_02
> >
> > Somewhere in the code there is something like : %h=undef;
> 
> I think what that person wanted to do is:
> 
> foreach my $key (sort keys(%h)) {
> 
>       delete $h{$key};
> 
> }
> 
> undef'ing a hash is a wrong, ugly way to try to accomplish this.

You are correct that '%h = undef;' is wrong and ugly.  But what you 
propose instead is incredibly inefficient (including a careful 'sort' 
before deleting every element!) and unnecessary.

Either

  undef %h;

or

  %h = ();

are perfectly acceptable ways to delete the contents of %h.  The first 
way also removes %h from the symbol table.

-- 
Larry Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Sat, 27 Feb 1999 00:42:30 +0800
From: Meng-Chow Kang <mckang@pacific.net.sg>
Subject: Win32::Registry doesn't work???
Message-Id: <36D6CEF6.36BBD79B@pacific.net.sg>

I was trying out some of the Win32 Registry calls such as
RegOpenEx(), with the "use Win32::Registry;" statement in
the begining of the script file. But Perl returns with error
stating that the command RegOpenEx, and other Registry calls
are not defined.

What could be the problem? I am using ActivePerl 5.005_02
build for MSWin32-x86-object.

I browsed through the HTML documentation, and managed to
find those registry calls defined in the Win32API::Registry,
and they only (finally) worked when I use the following
statement (defined in the documentation, sypnosis section)
in the begining of the script file:

use Win32API::Registry 0.13 qw(:ALL); 

Why is it so?

Would appreciate any help.

Thanks,
Meng-Chow Kang


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

Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>


Administrivia:

Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing. 

]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body.  Majordomo will then send you instructions on how to confirm your
]subscription.  This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.

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 V8 Issue 4986
**************************************

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