[22176] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4397 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jan 13 18:11:21 2003

Date: Mon, 13 Jan 2003 15:10:12 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 13 Jan 2003     Volume: 10 Number: 4397

Today's topics:
        Substitution of ampersand with a plus symbol stan@temple.edu
    Re: Substitution of ampersand with a plus symbol <bongie@gmx.net>
    Re: suggested revisions for the Posting Guidelines (Tad McClellan)
    Re: undef of large Hashes/Arrays took a very long time <pa@panix.com>
        Why does this NOT work?? (ashok)
    Re: Why does this NOT work?? <skuo@mtwhitney.nsc.com>
    Re: Why does this NOT work?? (Tad McClellan)
    Re: Why does this NOT work?? (Anno Siegel)
        WINS update script.. outdated? (Matt Williamson)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 13 Jan 2003 20:32:03 GMT
From: stan@temple.edu
Subject: Substitution of ampersand with a plus symbol
Message-Id: <avv7o3$2n8$2@cronkite.temple.edu>

I have a data file which I am parsing with perl on a Unix system.
The output file is fed to another program that I prefer not to touch.
The program that works with the output file is choking on any text
that has an ampersand in it. What I want to do in my perl parsing
script is substitute a plus symbol for any occurrence of an ampersand
in the string in question before I output it.

The problem is that no matter what I try with the "s" and "tr"
operators, the ampersands still show up.

For example:

  suppose $mystring = "This & That" and I want to end up with
  $mystring = "This + That". I have tried numerous things such as:

  $mystring =~ s/\&/+/g;

  and

  $mystring =~ s/&/+/g;

do nothing.

What am I doing wrong?


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

Date: Mon, 13 Jan 2003 22:22:51 +0100
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: Substitution of ampersand with a plus symbol
Message-Id: <1219170.VPsArZBPNb@nyoga.dubu.de>

stan@temple.edu wrote:
>   $mystring =~ s/&/+/g;

This works perfectly, except that
        $mystring =~ tr/&/+/;
will be faster.

> What am I doing wrong?

The problem must be elsewhere.  Are you working on the right string?  

Ciao,
        Harald
-- 
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Rottweiler
  Bad, bad doggie! Play dead! -- blam! -- yipe! Good dog!
                                (From the Quake I manual.txt)



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

Date: Mon, 13 Jan 2003 12:54:59 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: suggested revisions for the Posting Guidelines
Message-Id: <slrnb262s3.3lh.tadmc@magna.augustmail.com>

Brian McCauley <nobull@mail.com> wrote:

> OK applying the same tightening to some of the other metainformation pragraphs...
> 
>     A note to newsgroup "regulars":
> 
>        Do not use these guidelines as a "license to flame" or other
>        meanness. It is possible that a poster is unaware of things
>        discussed here.  Give them the benefit of the doubt, and just
>        help them learn how to post, rather than assume that they are
>        being the "bad kind" of Lazy.
> 
>     A note about technical terms used here:
> 
>        In this document, we use words like "must" and "should" as
>        they're used in technical conversation (such as you will
>        encounter in this newsgroup). When we say that you *must* do
>        something, we mean that if you don't do that something, then
>        it's unlikely that you will benefit much from this group.
>        We're not bossing you around; we're making the point without
>        lots of words.


Done.


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


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

Date: Mon, 13 Jan 2003 19:19:59 +0000 (UTC)
From: Pierre Asselin <pa@panix.com>
Subject: Re: undef of large Hashes/Arrays took a very long time
Message-Id: <avv3gs$78o$1@reader1.panix.com>

In <avu283$iugd2$1@ID-2265.news.dfncis.de> Jan Schubert <Jan.Schubert@GMX.li> writes:

>Pierre Asselin wrote:
>> Ok, so the OP should try assigning () and see if that helps.

>So i did it, but no success at all. The same behaviour as before. The 
>array has approximately 35 Mio. entries and is using ca. 1.6 GB in Memory.

If your work is all done, you can code {kill 9, $$;}  to exit in a hurry.
It returns an error to the OS, but I assume that doesn't matter.



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

Date: 13 Jan 2003 11:38:45 -0800
From: ashokc@qualcomm.com (ashok)
Subject: Why does this NOT work??
Message-Id: <c13619fd.0301131138.604c62a2@posting.google.com>

Hi,

I have the following 'test' code that works fine as follows.

#!/usr2/bin/perl -w
#

use strict qw(subs refs) ;

%hash = () ;
$number = 0 ;
while ($number < 5) {
  $number++ ;
  @list = ('a' . $number ,'b' . $number,'c' . $number) ;
  $hash{$number} = \@list ;
  print "\t\t$number : @{$hash{$number}}\n" ;
}

Prints:

                1 : a1 b1 c1
                2 : a2 b2 c2
                3 : a3 b3 c3
                4 : a4 b4 c4
                5 : a5 b5 c5

But the same idea split into a main code & a module does not!

The Module:

package test;

use Exporter;
@ISA = qw(Exporter) ;
@EXPORT_OK = qw() ;

use 5.004;
use strict qw(subs refs) ;

sub new {
   my $proto = shift ;
   $class = ref($proto) || $proto ;
   my $self = {} ;
   bless ($self, $class) ;
   return $self ;
}

sub getHashRef() {
  my $self = shift ;

  %hash = () ;
  $number = 0 ;
  while ($number < 5) {
    $number++ ;
    @list = ('a' . $number ,'b' . $number,'c' . $number) ;
    $hash{$number} = \@list ;
  }
  return \%hash ;
}

1 ;

The code:

#!/usr/bin/perl -w
#

use Carp ;
use lib qw(/usr2/ashokc/myperl/lib/dev) ;
use test ;

use strict qw(subs refs) ;

$Test = new test() ;

$hashref = $Test->getHashRef() ;

foreach $key (sort keys %{$hashref}) {
  print "$key\n" ;
  foreach $item ( @{$hashref{$key}} ) {
    print "$item\n" ;
  }
}

I get:

1
2
3
4
5

It seems that the array reference "$hashref{$number}" is undefined. Why is that?
Thanks

- ashok


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

Date: Mon, 13 Jan 2003 13:10:36 -0800
From: Steven Kuo <skuo@mtwhitney.nsc.com>
Subject: Re: Why does this NOT work??
Message-Id: <Pine.GSO.4.21.0301131300230.35-100000@mtwhitney.nsc.com>

On 13 Jan 2003, ashok wrote:

> Hi,
> 
> I have the following 'test' code that works fine as follows.
> 
> #!/usr2/bin/perl -w
> #
> 
> use strict qw(subs refs) ;
> 
> %hash = () ;
> $number = 0 ;
> while ($number < 5) {
>   $number++ ;
>   @list = ('a' . $number ,'b' . $number,'c' . $number) ;
>   $hash{$number} = \@list ;
>   print "\t\t$number : @{$hash{$number}}\n" ;
> }
> 
> Prints:
> 
>                 1 : a1 b1 c1
>                 2 : a2 b2 c2
>                 3 : a3 b3 c3
>                 4 : a4 b4 c4
>                 5 : a5 b5 c5
> 




Instead of printing inside the while loop, try this outside the while
loop:

foreach my $key (sort keys %hash) {
    print "$key => @{$hash{$key}}\n";
}

I think you're confused about references in Perl (see perldoc
perlref) and how they "work".



> But the same idea split into a main code & a module does not!
> 
> The Module:
> 
> package test;
> 
> use Exporter;
> @ISA = qw(Exporter) ;
> @EXPORT_OK = qw() ;
> 
> use 5.004;
> use strict qw(subs refs) ;
> 
> sub new {
>    my $proto = shift ;
>    $class = ref($proto) || $proto ;
>    my $self = {} ;
>    bless ($self, $class) ;
>    return $self ;
> }
> 
> sub getHashRef() {
>   my $self = shift ;
> 
>   %hash = () ;
>   $number = 0 ;
>   while ($number < 5) {
>     $number++ ;
>     @list = ('a' . $number ,'b' . $number,'c' . $number) ;
>     $hash{$number} = \@list ;
>   }
>   return \%hash ;
> }
> 
> 1 ;
> 
> The code:
> 
> #!/usr/bin/perl -w
> #
> 
> use Carp ;
> use lib qw(/usr2/ashokc/myperl/lib/dev) ;
> use test ;
> 
> use strict qw(subs refs) ;
> 
> $Test = new test() ;
> 
> $hashref = $Test->getHashRef() ;
> 
> foreach $key (sort keys %{$hashref}) {
>   print "$key\n" ;
>   foreach $item ( @{$hashref{$key}} ) {
>     print "$item\n" ;
>   }
> }
> 
> I get:
> 
> 1
> 2
> 3
> 4
> 5
> 
> It seems that the array reference "$hashref{$number}" is undefined. Why is that?
> Thanks
> 
> - ashok
> 


That's because the dereferencing a hash reference is accomplished by:

$hashref->{$number}

not

$hashref{$number}.


This goes to show that you should use strict (and warnings) as much
as possible.

Another minor issue: you seem to be using an OO method without doing
much with the object itself ($self).


-- 
HTH,
Steven



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

Date: Mon, 13 Jan 2003 15:40:40 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Why does this NOT work??
Message-Id: <slrnb26cio.46m.tadmc@magna.augustmail.com>

ashok <ashokc@qualcomm.com> wrote:

> Subject: Why does this NOT work??


Because you unwisely thought you could "drive without a seatbelt",
aka without strictures.


> I have the following 'test' code that works fine as follows.
> 
> #!/usr2/bin/perl -w
> #
> 
> use strict qw(subs refs) ;


Why not have "strict vars" too?

You should only turn off strictures when you need to, and
you do not need to.

And when you _do_ turn them off, you should turn them off in
the smallest possible scope.


> %hash = () ;

>   print "\t\t$number : @{$hash{$number}}\n" ;
                           ^^^^^^^^^^^^^^

Here "hash" really _is_ a hash.


> The Module:
> 
> package test;
> 
> use Exporter;
> @ISA = qw(Exporter) ;
> @EXPORT_OK = qw() ;
> 
> use 5.004;
> use strict qw(subs refs) ;


   use strict;


> sub new {
>    my $proto = shift ;
>    $class = ref($proto) || $proto ;


   my $class = ref($proto) || $proto ;


>    my $self = {} ;
>    bless ($self, $class) ;
>    return $self ;
> }
> 
> sub getHashRef() {
>   my $self = shift ;
> 
>   %hash = () ;
>   $number = 0 ;


   my %hash = () ;
   my $number = 0 ;


>   while ($number < 5) {
>     $number++ ;
>     @list = ('a' . $number ,'b' . $number,'c' . $number) ;


   my @list = ('a' . $number ,'b' . $number,'c' . $number) ;


>     $hash{$number} = \@list ;
>   }
>   return \%hash ;
> }
> 
> 1 ;
> 
> The code:
> 
> #!/usr/bin/perl -w
> #
> 
> use Carp ;
> use lib qw(/usr2/ashokc/myperl/lib/dev) ;
> use test ;
> 
> use strict qw(subs refs) ;


   use strict;


> $Test = new test() ;


   my $Test = new test() ;


> $hashref = $Test->getHashRef() ;


   my $hashref = $Test->getHashRef() ;


> foreach $key (sort keys %{$hashref}) {


   foreach my $key (sort keys %{$hashref}) {


>   print "$key\n" ;
>   foreach $item ( @{$hashref{$key}} ) {


   foreach my $item ( @{$hashref{$key}} ) {
                        ^^^^^^^^^^^^^

Whoop!

Global symbol "%hashref" requires explicit package name at ...

Here "hashref" is a reference, not a real hash. You must deref it:

      foreach my $item ( @{$hashref->{$key}} ) {


"strict vars" would have saved you from yourself.


> It seems that the array reference "$hashref{$number}" is undefined. Why is that?


Because you do not have a %hashref hash anywhere in your code.


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


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

Date: 13 Jan 2003 21:41:34 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Why does this NOT work??
Message-Id: <avvbqe$g1j$1@mamenchi.zrz.TU-Berlin.DE>

ashok <ashokc@qualcomm.com> wrote in comp.lang.perl.misc:

What a subject!  Please put something meaningful there.

> Hi,
> 
> I have the following 'test' code that works fine as follows.
> 
> #!/usr2/bin/perl -w
> #
> 
> use strict qw(subs refs) ;

Why not plain strict?  By omitting 'vars' you keep Perl from pointing
out what's wrong.

> %hash = () ;
> $number = 0 ;
> while ($number < 5) {
>   $number++ ;
>   @list = ('a' . $number ,'b' . $number,'c' . $number) ;
>   $hash{$number} = \@list ;
>   print "\t\t$number : @{$hash{$number}}\n" ;
> }
> 
> Prints:
> 
>                 1 : a1 b1 c1
>                 2 : a2 b2 c2
>                 3 : a3 b3 c3
>                 4 : a4 b4 c4
>                 5 : a5 b5 c5
> 
> But the same idea split into a main code & a module does not!
> 
> The Module:
> 
> package test;
> 
> use Exporter;
> @ISA = qw(Exporter) ;
> @EXPORT_OK = qw() ;
> 
> use 5.004;
> use strict qw(subs refs) ;
> 
> sub new {
>    my $proto = shift ;
>    $class = ref($proto) || $proto ;
>    my $self = {} ;

Why is this and only this variable a lexical?  All your variables should
be lexicals, unless there is a compelling reason to decide otherwise.
Except for the system variables @ISA and @EXPORT_OK there is none.

>    bless ($self, $class) ;
>    return $self ;
> }
> 
> sub getHashRef() {
>   my $self = shift ;
> 
>   %hash = () ;
>   $number = 0 ;
>   while ($number < 5) {
>     $number++ ;
>     @list = ('a' . $number ,'b' . $number,'c' . $number) ;

In particular, using a package variable for @list here is a mistake.

>     $hash{$number} = \@list ;
>   }
>   return \%hash ;
> }
> 
> 1 ;
> 
> The code:
> 
> #!/usr/bin/perl -w
> #
> 
> use Carp ;
> use lib qw(/usr2/ashokc/myperl/lib/dev) ;
> use test ;
> 
> use strict qw(subs refs) ;
> 
> $Test = new test() ;
> 
> $hashref = $Test->getHashRef() ;
> 
> foreach $key (sort keys %{$hashref}) {
>   print "$key\n" ;
>   foreach $item ( @{$hashref{$key}} ) {
                      ^^^^^^^^^^^^^^
Again the absence of "strict 'vars'" bites you.  If you take a closer
look at "$hashref{$key}", you will notice that this doesn't access the
scalar $hashref you want, but the hash %hashref you didn't even think
of.  "strict 'vars'" would have complained, but you didn't let it.

Next you use $hashref{ $key}, which is, of course, undefined, as an
arrayref.  Autovivification then creates a reference to an empty array
in this place, so the loop is empty.  Replace "$hashref{ $key}" with
"$hashref->{ $key}" and you will be one step closer.

     print "$item\n" ;
>   }
> }
> 
> I get:
> 
> 1
> 2
> 3
> 4
> 5
> 
> It seems that the array reference "$hashref{$number}" is undefined. Why is that?

Explained above.  However, even after that correction you will see that
all five arrays in your hash print "a5 b5 c5".  This happens because
you didn't make @list in your module a lexical, s noted above.  The way
it is, you deposit a reference to the same variable @list in all positions
of your hash, so they all contain the same elements.  When you print
them, the array content is still the last one used when building the
hash, so you see this value in all positions.  Declare @list with
"my" inside the loop.  Then each time through a new variable is allocated
and you get five different references, capable of holding five different
values.

Anno


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

Date: 13 Jan 2003 13:45:26 -0800
From: jmw@hcb.com (Matt Williamson)
Subject: WINS update script.. outdated?
Message-Id: <ba05412e.0301131345.7ba92b8a@posting.google.com>

I found the following script to update static WINS entries, but I
can't get it to work. Part of the problem is the Nt.ph file. I can't
find it anywhere. I looking on the Resource CD and scoured the web /
ftp sites looking for it. That led me to believe that it was obsolete,
so I grabbed the Win32::registry module and tried to adapt it, but it
isn't going too well. I'm very new to Perl as well. I have the Blue
Camel but it's hard to follow since I'm working with ActivePerl under
WinNT4. I've read all the FAQ's and documentation I can, but I'm
stuck. Has anyone written a WINS update script similar to this one or
can someone point me in the right direction as to how they would
approach this situation if they were in it?

Here is the script I found at the Winnetmag.com site:

use Win32;
require 'NT.ph';

(@ARGV) or &PrintHelp;

$primary = $ARGV[0];
$secondary = $ARGV[1];
if ($ARGV[2]) {
  if (lc($ARGV[2]) eq "remote") {
    @machines = ($ARGV[3]);
  } elsif (lc($ARGV[2]) eq "file") {
    open(FILELIST, "<$ARGV[3]") or
        die "Unable to read file $ARGV[3] for list of ".
            "computernames.\n";
    @machines = grep(/\S/, map {s/\s//g; $_} <FILELIST>);
    close FILELIST;
  } else {
    die "Third command line option $ARGV[2] illegal.\n";
  }
} else {
  @machines = ($ENV{COMPUTERNAME});
}

foreach $machine (@machines) {
  unless (Win32::RegConnectRegistry($machine,
      &HKEY_LOCAL_MACHINE, $key_LM)) {
    print "$machine: ERROR, unable to connect to ".
        "remote registry.\n";
    next;
  }
  unless (Win32::RegOpenKeyEx($key_LM,
      "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Adapters",
      &NULL, &KEY_ALL_ACCESS,  $key_Adapters)) {
    print "$machine: ERROR, unable to connect to open ".
        "NetBT\\Adapters.\n";
    Win32::RegCloseKey($key_LM);
    next;
  }
  $i = 0;
  while (Win32::RegEnumKey($key_Adapters, $i++,
      $adaptername)) {
    ($adaptername =~ /NdisWan/i) and next;
    unless (Win32::RegOpenKeyEx($key_Adapters, $adaptername,
        &NULL, &KEY_ALL_ACCESS,  $key_Adapter)) {
      print "$machine: ERROR, unable to connect to open ".
          "NetBT\\Adapters\\$adaptername.\n";
      next;
    }
    unless (Win32::RegSetValueEx($key_Adapter, "NameServer",
        &NULL, &REG_SZ, $primary)) {
      print "$machine: ERROR, unable to set primary WINS ".
          "address or $adaptername.\n";
      Win32::RegCloseKey($key_Adapter);
      next;
    }
    unless (Win32::RegSetValueEx($key_Adapter,
        "NameServerBackup", &NULL, &REG_SZ, $secondary)) {
      print "$machine: ERROR, unable to set secondary WINS ".
          "address for $adaptername.\n";
      Win32::RegCloseKey($key_Adapter);
      next;
    }
    print "$machine: Set primary and secondary WINS ".
        "addresses to $primary and $secondary for ".
        "adapter $adaptername.\n";
    Win32::RegCloseKey($key_Adapters);
  }
  Win32::RegCloseKey($key_LM);
}

sub PrintHelp {
  print <<END;
This program is designed to update the WINS entries on either the 
local host, a remote host, or a list of remote hosts. In all 
cases, the first two parameters are the desired primary and 
secondary WINS server IP addresses.  In the local host case, pass 
no more parameters.  In the remote host case, pass the string 
"remote" (no quotes) followed by a space and the hostname. In the 
list of remote hosts case, pass the string "file" (no quotes) 
followed by the name of a file containing one hostname per line. 
In all cases, the program will return information about whether 
it was successful.
END
  exit;
}

TIA

Matt


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

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.  

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 4397
***************************************


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