[16675] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4087 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Aug 21 18:20:58 2000

Date: Mon, 21 Aug 2000 15:20:45 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <966896445-v9-i4087@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Mon, 21 Aug 2000     Volume: 9 Number: 4087

Today's topics:
    Re: Template In/File Out <lr@hpl.hp.com>
    Re: Template In/File Out (Malcolm Dew-Jones)
        This is my last question, I swear!!!!!!!!!! pape_98@my-deja.com
    Re: udp socket problem (Teodor Zlatanov)
        using arrays - please help xlr6drone@my-deja.com
    Re: using arrays - please help (Eric Bohlman)
    Re: using arrays - please help <godzilla@stomp.stomp.tokyo>
    Re: using arrays - please help (Jakob Schmidt)
        Variable vanishing? <smerr612@mailandnews.com>
    Re: Variable vanishing? (Mark-Jason Dominus)
    Re: Variable vanishing? <smerr612@mailandnews.com>
    Re: Variable vanishing? <lauren_smith13@hotmail.com>
    Re: Wildcards in DOS 'dir' command (Teodor Zlatanov)
    Re: Xemacs Perl menu <jeff@vpservices.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Mon, 21 Aug 2000 11:23:52 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Template In/File Out
Message-Id: <MPG.140b13967da312f398acae@nntp.hpl.hp.com>

In article <8nrm74$91fqb$4@ID-24002.news.cis.dfn.de> on 21 Aug 2000 
16:43:16 GMT, Tina Mueller <tina@streetmail.com> says...
> James Hunter <arawn_the_hunter@hotmail.com> wrote:
> > I'm trying to read in a template file -- basically, something that has a lot
> > of text I'll be reusing -- and turn it into an actual file.  What I'd like
> > to be able to do is something like:
> > Include in the template file:
> > ...this results in a net profit of $net_profit dollars...
> > Set $net_profit = 1000000 (for example) in the script, and print to the
> > actual file:
> > ...this results in a net profit of 1000000 dollars...
> 
> $text =~ s/\$(\w+)/${$1}/g;

There is a much more comprehensive answer in perlfaq4: "How can I expand 
variables in text strings?"

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: 21 Aug 2000 13:03:48 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: Template In/File Out
Message-Id: <39a18b24@news.victoria.tc.ca>

Larry Rosler (lr@hpl.hp.com) wrote:
: In article <8nrm74$91fqb$4@ID-24002.news.cis.dfn.de> on 21 Aug 2000 
: 16:43:16 GMT, Tina Mueller <tina@streetmail.com> says...
: > James Hunter <arawn_the_hunter@hotmail.com> wrote:
: > > I'm trying to read in a template file -- basically, something that has a lot
: > > of text I'll be reusing -- and turn it into an actual file.  What I'd like
: > > to be able to do is something like:
: > > Include in the template file:
: > > ...this results in a net profit of $net_profit dollars...
: > > Set $net_profit = 1000000 (for example) in the script, and print to the
: > > actual file:
: > > ...this results in a net profit of 1000000 dollars...
: > 
: > $text =~ s/\$(\w+)/${$1}/g;

: There is a much more comprehensive answer in perlfaq4: "How can I expand 
: variables in text strings?"

And CPAN has numerious template / merge modules which do this and more.


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

Date: Mon, 21 Aug 2000 19:13:22 GMT
From: pape_98@my-deja.com
Subject: This is my last question, I swear!!!!!!!!!!
Message-Id: <8nruvu$36o$1@nnrp1.deja.com>

here is my database,

NIH,10B-410,01 36,13 5 26,15 43,1 5 2 5 2 4
NIH,6B-4,01 36,13 5 26,15 43,1 5 2 5 2 4
Suburban,6C-258,52 51,5256,15 13,152
Suburban,ACardiology,52 51,5256,15 13,152
NIH,9B-4,01 36,13 5 26,15 43,1 5 2 5 2 4
NIH,15-410,01 36,13 5 26,15 43,1 5 2 5 2 4
NIH,60B-410,01 36,13 5 26,15 43,1 5 2 5 2 4
NIH,B1D-416,52,135 6,1513,52 hi,
NIH,B1D-43,01 36,13 5 26,15 43,1 5 2 5 2 4
Suburban,10C-58,52 51,5256,15 13,152
Suburban,1B-29,52 51,5256,15 13,152
NIH,B1D-403,01 36,13 5 26,15 43,1 5 2 5 2 4
NIH,B1D-410,52 51 36,135 256,15413,1512
Suburban,6B-281,52 51,5256,15 13,152
Suburban,Office,52 51,5256,15 13,152

here is my script,

#!/usr/bin/perl

use strict;

use vars qw(@unsorted @sorted @new $a $b $c);

open (SORTER,"/applications/apache/cgi-bin/pape/box") or die "Can't
open file: $!\n";

chomp(my @unsorted = <SORTER>);

@sorted = map "${\ (unpack 'na*', $_)[-1] }" => sort map pack
('na*', /B1D-(\d+),/?$1:0, $_ ) => @unsorted;
@sorted = sort { $a cmp $b } @unsorted;
@new = sort { $b <=> $c } @sorted;

close(SORTER);

foreach(@new) {
  print $_ . "\n";

this is the output I get,

NIH,10B-410,01 36,13 5 26,15 43,1 5 2 5 2 4
NIH,15-410,01 36,13 5 26,15 43,1 5 2 5 2 4
NIH,60B-410,01 36,13 5 26,15 43,1 5 2 5 2 4
NIH,6B-4,01 36,13 5 26,15 43,1 5 2 5 2 4
NIH,9B-4,01 36,13 5 26,15 43,1 5 2 5 2 4
NIH,B1D-403,01 36,13 5 26,15 43,1 5 2 5 2 4
NIH,B1D-410,52 51 36,135 256,15413,1512
NIH,B1D-416,52,135 6,1513,52 hi,
NIH,B1D-43,01 36,13 5 26,15 43,1 5 2 5 2 4
Suburban,10C-58,52 51,5256,15 13,152
Suburban,1B-29,52 51,5256,15 13,152
Suburban,6B-281,52 51,5256,15 13,152
Suburban,6C-258,52 51,5256,15 13,152
Suburban,ACardiology,52 51,5256,15 13,152
Suburban,Office,52 51,5256,15 13,152

now my question is, what do I have to change in my script to get the
output in that format but have the numbers sorted out in order (i.e
1,2,10,12,14,22, etc...)

Please help me. I'm new to perl and have no idea what to do here. I've
even tried combining the different sorting techniques.

Any suggestions would be greatly appreciated.
Thanks


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: 21 Aug 2000 15:38:58 -0500
From: tzz@iglou.com (Teodor Zlatanov)
Subject: Re: udp socket problem
Message-Id: <39a18552$1_1@news.iglou.com>

<8nrcm8$cac$1@nnrp1.deja.com>:aloisious@my-deja.com:comp.lang.perl.misc:Mon, 21 Aug 2000 14:00:52 GMT:quote:
: The "listener" complains:
:     Argument "SOCK_DGRAM" isn't numeric in socket at
:     /usr/local/lib/perl5/5.6.0/sun4-solaris/IO/Socket.pm line 74.
:     Could not create socket: Protocol not supported
: 
: ## Here's the "listener":
: #! /usr/local/bin/perl -w
: use strict;
: use IO::Socket;
: my $sock = IO::Socket::INET->new (
:                                  PeerAddr => 'localhost',
:                                  PeerPort => '4321',
:                                  LocalAddr => 'localhost',
:                                  LocalPort => '4321',
:                                  Proto => 'udp',
:                                  Type => 'SOCK_DGRAM',

Type => SOCK_DGRAM

read perldoc IO::Socket::INET.  SOCK_DGRAM is a value, not a string.

:                                  Listen => 5,
:                                  Reuse => 1,
:                                 );
: die "Could not create socket: $!\n" unless $sock;
: my $new_sock = $sock->accept();
: while(<$new_sock>) {
:    print;
: }
: 

-- 
Teodor Zlatanov <tzz@iglou.com>
"Brevis oratio penetrat colos, longa potatio evacuat ciphos." -Rabelais


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

Date: Mon, 21 Aug 2000 19:27:42 GMT
From: xlr6drone@my-deja.com
Subject: using arrays - please help
Message-Id: <8nrvqj$4h9$1@nnrp1.deja.com>

I am still learning the basics of Perl - so bear with me please

I have a form that works fine with scalars but I want to put those
scalars into an array. When I print out the array to my Log file it
prints the actual scalars - for example

"$name","$saddr","$city","$resume"

instead of the users input.



This is what I did with the code.

$name = $formdata{'name'}; # takes variable from parsing data sub routine
$saddr = $formdata{'saddr'};
$city = $formdata{'city'};
$resume = $formdata{'resume'};

# creates an array with my scalars?
@resform = qw($name $saddr $city $resume);

foreach $nospaces(@resform) { # trying to remove all extra spaces from
	$nospaces=~ tr/\n //s;	# each scalar in the array
	}

$origin = $ENV{'HTTP_REFERER'};

if ($origin =~ m#^http://www.domain.com/#) {
open(LOG, ">>/users/www/domains/domains/logs/mailform/log.txt")
|| &ErrorMessage("Error occurred attempting to open file log.txt");

flock(LOG, 2);
foreach $nospaces (@resform) {	# trying to get all of the scalars
	print LOG "\"$nospaces\""; # to print out
	}
flock(LOG, 8);
close (LOG);

} else {
print "Can not run script from this location";
exit;
}


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: 21 Aug 2000 20:06:28 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: using arrays - please help
Message-Id: <8ns244$8sm$6@slb7.atl.mindspring.net>

xlr6drone@my-deja.com wrote:
: I am still learning the basics of Perl - so bear with me please
: 
: I have a form that works fine with scalars but I want to put those
: scalars into an array. When I print out the array to my Log file it
: prints the actual scalars - for example
: 
: "$name","$saddr","$city","$resume"
: 
: instead of the users input.
: 
: 
: 
: This is what I did with the code.
: 
: $name = $formdata{'name'}; # takes variable from parsing data sub routine
: $saddr = $formdata{'saddr'};
: $city = $formdata{'city'};
: $resume = $formdata{'resume'};
: 
: # creates an array with my scalars?
: @resform = qw($name $saddr $city $resume);

Nope.  A visit to the docs will tell you that qw doesn't interpolate.

@resform = ($name, $saddr, $city, $resume);

would work, but a hash slice would be even better: replace the four 
scalar assignments and the one list assignment with

@resform = @formdata{qw /name saddr city resume/};

:foreach $nospaces(@resform) { # trying to remove all extra spaces from
: 	$nospaces=~ tr/\n //s;	# each scalar in the array
: 	}
: 
: $origin = $ENV{'HTTP_REFERER'};
: 
: if ($origin =~ m#^http://www.domain.com/#) {

Checks on HTTP_REFERER are pretty much worthless; it's trivially spoofed.

: open(LOG, ">>/users/www/domains/domains/logs/mailform/log.txt")
: || &ErrorMessage("Error occurred attempting to open file log.txt");
: 
: flock(LOG, 2);

Use constants imported from Fcntl.pm instead of magic numbers.

: foreach $nospaces (@resform) {	# trying to get all of the scalars
: 	print LOG "\"$nospaces\""; # to print out

Use qq or the like to avoid Leaning Toothpick Syndrome.

: 	}
: flock(LOG, 8);

Don't explicitly unlock a file before closing it.

: close (LOG);
: 
: } else {
: print "Can not run script from this location";
: exit;
: }


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

Date: Mon, 21 Aug 2000 13:07:03 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: using arrays - please help
Message-Id: <39A18BE7.E8907765@stomp.stomp.tokyo>

xlr6drone@my-deja.com wrote:
 
> I have a form that works fine with scalars but I want to put those
> scalars into an array. When I print out the array to my Log file it
> prints the actual scalars - for example
 
> "$name","$saddr","$city","$resume"
 
> instead of the users input.
 
(snippage)

Not quite. There are no commas in your print:

"$name""$saddr""$city""$resume"


> # creates an array with my scalars?
> @resform = qw($name $saddr $city $resume);

(snippage)


Make note of the syntax difference between
@Array1 and @Array2 in my test script below.

You have some other problems you will discover
once you straighten out your array.

Godzilla!


TEST SCRIPT:
____________

#!/usr/local/bin/perl

print "Content-Type: text/plain\n\n";

$test1 = "element1";
$test2 = "element2";
$test3 = "element3";
$test4 = "element4";

print "Quote Word Method:\n";

@Array1 = qw ($test1 $test2 $test3 $test4);

foreach $element (@Array1)
 { print "  $element\n"; }


print "\n\nStandard Array Method:\n";

@Array2 = ($test1, $test2, $test3, $test4);

foreach $element (@Array2)
 { print "  $element\n"; }

exit;


PRINTED RESULTS:
________________

Quote Word Method:
  $test1
  $test2
  $test3
  $test4


Standard Array Method:
  element1
  element2
  element3
  element4


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

Date: Mon, 21 Aug 2000 22:09:59 +0200
From: sumus@aut.dk (Jakob Schmidt)
Subject: Re: using arrays - please help
Message-Id: <1efqip5.1q0rv7jgh6symN@[192.168.88.117]>

<xlr6drone@my-deja.com> wrote:

> # creates an array with my scalars?
> @resform = qw($name $saddr $city $resume);

I didn't check the rest of your code, but this is wrong. It creates
a list of the words '$name', '$saddr' etc.

You just want a list:

@resform = ( $name, $saddr, $city, $resume );

Not the qw() operator. Check man perlop to learn more about why.

Just to hint you qw() is a convenient way of getting rid of all the
quotes and commas in the quite frequent case that you need a list of
words (strings with no whitespace).

-- 
Jakob


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

Date: Mon, 21 Aug 2000 20:32:56 GMT
From: Steven Merritt <smerr612@mailandnews.com>
Subject: Variable vanishing?
Message-Id: <8ns3lo$91i$1@nnrp1.deja.com>

Here's semi-working code.

#! /usr/local/bin/perl -w
use strict;

my $From_Date = 20000000000000;
my $To_Date = 20000525135144;
my $Name = "";
my $Oldest = "";
my $Youngest = "";
my $FlName = "";
my @fllist = `ls -1 /archive/??????????????-??????????????.log`;
my @Include = "";
my @LogLines = "";

foreach $FlName (@fllist){
  (undef, $Name) = split /\/archive\//, $FlName;
  $Name =~ s/\.log//;
  ($Oldest, $Youngest) = split /-/, $Name;
  if ($Youngest < $From_Date){
     next;
     }
     elsif ($Oldest > $To_Date){
        next;
        }
  push @Include, $FlName;
  }
print @Include;

foreach $FlName (reverse @Include){
  chomp $FlName;
  open FH, '$FlName' or die "Couldn't open file $FlName $!\n";
     while (defined (<FH>)){
       push @LogLines, $_;
       }
  }

print @LogLines;

The problem is in the second foreach loop. It dies with this output

Couldn't open file /archive/20000523142707-20000703111734.log
No such file or directory

But there _is_ such a file.  And I do have permissions on it.

-rwxr-xr-x   1 bob      jerks       505622 Jul  3 13:21
20000523142707-20000703111734.log

I've tried open (FH, "$FlName")  and a ton of other
quoting/parenthisizing schemes.  Nothing works. And yet this snippet
works just fine(but doesn't do what I need, I need them all in one array
to pass to a sub)

foreach $FlName (reverse @fllist){
  (undef, $Range) = split /\/archive\//, $FlName;
  $Range =~ s/\.log//;
  ($Oldest, $Youngest) = split /-/, $Range;
  next if ($Youngest < $From_Date);
  next if ($Oldest > $To_Date);
  chomp $FlName;
  print "$FlName\n";
  open (FH, "$FlName") or die "Couldn't open file  $!\n";
  @LogLines = <FH>;
}

Surely there's something simple I'm missing here?  Surely there's a way
to do this without making a bunch of temp arrays and concatenating them
with splice?

Steven
--
King of Casual Play
The One and Only Defender of Cards That Blow


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Mon, 21 Aug 2000 20:48:28 GMT
From: mjd@plover.com (Mark-Jason Dominus)
Subject: Re: Variable vanishing?
Message-Id: <39a1959c.3c67$148@news.op.net>

[mailed and posted]

In article <8ns3lo$91i$1@nnrp1.deja.com>,
Steven Merritt  <smerr612@mailandnews.com> wrote:
>  open FH, '$FlName' or die "Couldn't open file $FlName $!\n";

This says to open a file whose name is '$FlName'.  That is, the file's
name is seven characters long and begins with a dollar sign.

You want

        open FH, $FlName or die ...

>I've tried open (FH, "$FlName")  and a ton of other
>quoting/parenthisizing schemes.

Maybe you had some other bug at the same time that led you to believe
that "$FlName"  'didn't work'.


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

Date: Mon, 21 Aug 2000 21:19:10 GMT
From: Steven Merritt <smerr612@mailandnews.com>
Subject: Re: Variable vanishing?
Message-Id: <8ns6bp$cjf$1@nnrp1.deja.com>

In article <39a1959c.3c67$148@news.op.net>,
  mjd@plover.com (Mark-Jason Dominus) wrote:
> [mailed and posted]
>
> In article <8ns3lo$91i$1@nnrp1.deja.com>,
> Steven Merritt  <smerr612@mailandnews.com> wrote:
> >  open FH, '$FlName' or die "Couldn't open file $FlName $!\n";
>
> This says to open a file whose name is '$FlName'.  That is, the file's
> name is seven characters long and begins with a dollar sign.
>
> You want
>
>         open FH, $FlName or die ...
>
> >I've tried open (FH, "$FlName")  and a ton of other
> >quoting/parenthisizing schemes.
>
> Maybe you had some other bug at the same time that led you to believe
> that "$FlName"  'didn't work'.

I went back and changed the open call to open (FH, "$FlName") and now I
get this error message(no other changes to the script)

Couldn't open file  No such file or directory

So it leaves out the variable in the error message(meaning variable is
unfilled?).  I've tried doing a print before the open to see if the
$FlName variable gets filled, and it doesn't.  Hence the "Vanishing
variable" subject.  The @Include array fills nicely, the comparisons
work fine.  when I try to go through each element of the reversed
@Include using $FlName as a temp variable, it dies.

The results for the open call sans parentheses is the same as the open
with parentheses and double quotes.  I've even done undef $FlName
between the first loop and the second.  Nothing seems to work.  It's
really wierd, I'm running perl 5.00503 on Solaris 2.6.  It's  a new
compile and everything tested fine. We've got another version of perl in
another path to support legacy stuff, I'll try it on that one too.

Steven
--
King of Casual Play
The One and Only Defender of Cards That Blow

My newsreader limits sigs to four lines, but I cleverly bypassed this by


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Mon, 21 Aug 2000 14:47:04 -0700
From: "Lauren Smith" <lauren_smith13@hotmail.com>
Subject: Re: Variable vanishing?
Message-Id: <8ns7sq$5tf$1@brokaw.wa.com>


Steven Merritt <smerr612@mailandnews.com> wrote in message
news:8ns6bp$cjf$1@nnrp1.deja.com...
>
> I went back and changed the open call to open (FH, "$FlName") and now I
> get this error message(no other changes to the script)
>
> Couldn't open file  No such file or directory
>

I hate to ask the obvious, but does the file exist?  You can't open a file
for reading if it doesn't exist.

perldoc -f open

Lauren
--
print grep ord $_,map{y/a-z//d;$x.="+ $_";chr(eval $x)}
'J74u43-s2tA1-84n33o45th1er5-12-P3e13-82r48l21H13-a6-76
c40k25er2wx8-y6z13-81'=~m#([^!\n]{3})#g#tr/-0-9//d;print





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

Date: 21 Aug 2000 15:22:37 -0500
From: tzz@iglou.com (Teodor Zlatanov)
Subject: Re: Wildcards in DOS 'dir' command
Message-Id: <39a1817d$1_1@news.iglou.com>

<8noq3a$is8$1@nnrp1.deja.com>:jblatz2@my-deja.com:comp.lang.perl.misc:Sun, 20 Aug 2000 14:31:06 GMT:quote:
: I'm using ActivePerl on a Windows NT 4.0 platform.  I'm trying to use
: the 'dir' command with the '*' wildcard and it won't work.  I'm trying
: something like:
: 
: @dirlist=`dir /b ..\*.log`;
: 
: Tells me it cannot find the file.  I've also tried explicit path
: 
: @dirlist=`dir /b \dir1\*.log`;
: 
: with same results.
: 
: If I execute this command from DOS it works.
: 
: I've read the documentation and it indicates that I'm better off using
: filenames when possible, but it's not possible here.

The other posters mentioned ways to get 'dir' to work, but the more
important question is why not use opendir/readdir?  

perldoc -f readdir

will tell you more about these functions (look in the HTML help if you can't
run perldoc).  They may be better for your script, and faster and easier to
use.

-- 
Teodor Zlatanov <tzz@iglou.com>
"Brevis oratio penetrat colos, longa potatio evacuat ciphos." -Rabelais


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

Date: Mon, 21 Aug 2000 11:08:07 -0700
From: Jeff Zucker <jeff@vpservices.com>
To: Javier Hijas <jhijas@yahoo.es>
Subject: Re: Xemacs Perl menu
Message-Id: <39A17007.1D8B5BF5@vpservices.com>

Javier Hijas wrote:
> 
> where can I find cperl-mode.el and mode-compile.el?

cperl is available on CPAN.  mode-compile.el is available in many public
Lisp repositories, a google search will turn up dozens of places to
download it from.

-- 
Jeff


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

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 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.  

| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

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


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