[16408] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3820 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 27 11:05:37 2000

Date: Thu, 27 Jul 2000 08:05:11 -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: <964710311-v9-i3820@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Thu, 27 Jul 2000     Volume: 9 Number: 3820

Today's topics:
    Re: Byte swapping: Win32 Perl script reading UNIX file (Greg Bacon)
    Re: Capturing info accross multiple pages in one form (Nobody)
    Re: CGI passing parameters <glodalec@yahoo.com>
        CGI SCRIPT PROBLEM <igorleal@dcc.ufmg.br>
    Re: CGI SCRIPT PROBLEM <andre@UltraShell.Net>
    Re: CGI->new(*FORM) Problem... <zampino@squidco.com>
        CGI-Perl question - Form results writing to a file (Liam)
    Re: CGI-Perl question - Form results writing to a file (Marcel Grunauer)
    Re: difference between two times in seconds <sariq@texas.net>
        empty hash reference vnguyen_1999@my-deja.com
    Re: exactly WHEN will PP-3rd be in bookstores, etc?  An (Tom Christiansen)
    Re: find.pl & error_log <weberr2202@my-deja.com>
        HELP needed in e-mail notification...  <dae@chello.se>
    Re: HELP needed in e-mail notification...  (Marcel Grunauer)
        HELP NEEDED! BADLY... E-mail notification <dae@chello.se>
    Re: HELP NEEDED! BADLY... E-mail notification (Marcel Grunauer)
    Re: Making and extracting from XML-like string (Nobody)
        newbie question: assign array to hash <berndt.zeitler@tu-berlin.de>
    Re: newbie question: assign array to hash (Marcel Grunauer)
    Re: newbie question: assign array to hash <aqumsieh@hyperchip.com>
    Re: Nobody loves me? (Anthony Peacock)
        Power? <srh104@york.ac.uk>
    Re: Power? (Marcel Grunauer)
        Regex alternation question (Marcel Grunauer)
    Re: Regex alternation question (Greg Bacon)
    Re: return value of `eval "use lib"' <sumus@aut.dk>
    Re: Saving space in a hash <russ_jones@rac.ray.com>
    Re: Suggestion for syntax change <iltzu@sci.invalid>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Thu, 27 Jul 2000 14:48:35 GMT
From: gbacon@HiWAAY.net (Greg Bacon)
Subject: Re: Byte swapping: Win32 Perl script reading UNIX file
Message-Id: <so0iu3h83j1148@corp.supernews.com>

In article <964698103.11197.0.nnrp-03.9e98e5bc@news.demon.co.uk>,
    Clyde Ingram <cingram-at-pjocs-dot-demon-dot-co-dot-uk> wrote:

: Using od -x on an NT PC to look at a UNIX file over the LAN, I see that
: bytes are swapped when od displays the file contents, compared to running
: the UNIX od -c command on the UNIX system where the same file lives.

Are the implementations different?  Consider using PPT's od:

    <URL:http://language.perl.com/ppt/src/od/>

: I believe this is stopping file seek calls from working properly when a
: Win32 Perl program reads a UNIX log file.

Does the log file contain binary or text data?

: Can anyone suggest how to get the 'read' function to quietly do any
: necessary byte swapping?  How would the undelying software know, in any
: case, that the file being read is on platform that uses different
: byte-ordering (little- vs. big-endian) compared to the local platform
: running the script?

You could tie the filehandle (see the perltie manpage).  It would seem
more straightforward to use Perl's unpack operator to specify byte
order.

Greg
-- 
As a general rule, don't solve puzzles that open portals to Hell. 
    -- Ralph Mason


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

Date: 27 Jul 2000 14:44:21 GMT
From: nobody@contract.East.Sun.COM (Nobody)
Subject: Re: Capturing info accross multiple pages in one form
Message-Id: <8lphs5$8u2$1@eastnews1.east.sun.com>

In article <snu4qejh3j1148@corp.supernews.com>,
Monk <wmonk@documentsolutions.com> wrote:
>
>"Andy Barlow" <andybarlow70NOanSPAM@hotmail.com.invalid> wrote in message
>news:138115fb.cc6205b9@usw-ex0107-055.remarq.com...
>> Cheers Wes.
>>
>> Could you explain in more detail.  You sound like you know a
>> hell of a lot more than me.
>>
>> Cheers
>>
>> Andy
>>
>>
>Andy,
>    I can not say it any better than the man himself.  Here is an article
>that Randal Schwartz wrote for Webtechniques.
>
>http://www.webtechniques.com/archives/1999/08/perl/
>
>This is where I learned the technique.
>

This is a good technique.  Another similar way to do this is to 
build a "hidden" page (by using a frame that is 100% of your
screen) and setting up a set of hidden variables there.  Each 
time you move to the next page, you call a function that copies all
of the form inputs to their respective hidden counterparts on the
other page.  Then, your input forms can all be straight html pages
and you can do your actual "submit" to the cgi that will process the
data at the end.  You can write similar code for your own "go back"
button, to fill in the values of the previous page, or add context-
sensitive help.  Granted, this isn't entirely a Perl solution, but
it came in handy on a project I did once that was working over a dog-slow
network connection to an overworked NT server (doesn't take much to
overwork those puppies :-).  This technique simply shifts some of the 
processing to the client side.

Anita





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

Date: Thu, 27 Jul 2000 15:05:51 +0200
From: Mouse <glodalec@yahoo.com>
Subject: Re: CGI passing parameters
Message-Id: <398033AF.62DA@yahoo.com>

Bart Lateur wrote:
> 
> Eelke Kleijn wrote:
> 
> >the way I did this was using socket.pm to connect to another
> >webserver.
> 
> Gee, that looks complicated.  Why not skip the low-level stuff, and use
> LWP, like LWP::Simple for the straightforward stuff, and LWP::UserAgent
> for less trivial tasks?
> 
>     use LWP::Simple;
>     $result = get("http://www.server.invalid/cgi-bin/script.pl?get=go");
> 
> --
>         Bart.
Actually, I have main security script which has only few options.
cmd=<command>,msg=<message>,sys=<system>

In this script, I have array of commands

$c=param(cmd);
$s=param(sys);
$CMD{<command} = "real command";
$CMD{<command} = "real command";
$CMD{<command} = "real command";
$CMD{<command} = "real command";
$CMD{cat}      = "cat $s";

if ($c && $CMD{$c}) 
{
   @MYARRAY=`$CMD{$c}`; 
}
 ...


If I would make a script for CATting any file, using ?file=<name>
parameter, that would be security hazardeus, since someone can change
<name> manually.


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

Date: Thu, 27 Jul 2000 09:59:16 -0300
From: Igor Campos Leal <igorleal@dcc.ufmg.br>
Subject: CGI SCRIPT PROBLEM
Message-Id: <Pine.GSO.4.21.0007270953060.10793-100000@turmalina.dcc.ufmg.br>

	Hello everybody!
	I have a problem:  I'm doing a CGI script to get the information
in a form, it's working very well, and those informations are sent by
e-mail, using sendmail. But my client want the information not in the body
of the e-mail, but in a text-file. 
	The script writes the informations in the file yet, but I don't
know how I make a attachment in the e-mail with this file. I've looked for
that explanation everywhere in the net, and I do not find some example
about that.
	If someone knows something about it, please help me.
	If you prefer, mail me directly.
	Thanks,

                    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
                    \                                      /
                    /           Igor Campos Leal           \
                    \        Ciencia da Computacao         /
                    /              DCC - UFMG              \
                    \     e-mail:   igorleal@dcc.ufmg.br   /
                    /               igor@acesso.com.br     \
                    \             ICQ# 37936364            /
                    /                                      \
                    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



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

Date: Thu, 27 Jul 2000 14:55:49 GMT
From: Andre van Straaten <andre@UltraShell.Net>
Subject: Re: CGI SCRIPT PROBLEM
Message-Id: <V3Yf5.8587$gU5.222890@news-east.usenetserver.com>

MIME::Lite should have an option for text attchments, too.
A simple example of MIME::Lite and hints to install it locally is on my
Web site (see footer).

-- avs

Igor Campos Leal <igorleal@dcc.ufmg.br> wrote:
> 	Hello everybody!
> 	I have a problem:  I'm doing a CGI script to get the information
> in a form, it's working very well, and those informations are sent by
> e-mail, using sendmail. But my client want the information not in the body
> of the e-mail, but in a text-file. 
> 	The script writes the informations in the file yet, but I don't
> know how I make a attachment in the e-mail with this file. I've looked for
> that explanation everywhere in the net, and I do not find some example
> about that.
> 	If someone knows something about it, please help me.
> 	If you prefer, mail me directly.
> 	Thanks,

>                     /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
>                     \                                      /
>                     /           Igor Campos Leal           \
>                     \        Ciencia da Computacao         /
>                     /              DCC - UFMG              \
>                     \     e-mail:   igorleal@dcc.ufmg.br   /
>                     /               igor@acesso.com.br     \
>                     \             ICQ# 37936364            /
>                     /                                      \
>                     \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/


-- 

Andre van Straaten
http://www.vanstraatensoft.com
______________________________________________
flames please to /dev/null@vanstraatensoft.com



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

Date: Thu, 27 Jul 2000 08:30:08 -0400
From: Phil Zampino <zampino@squidco.com>
Subject: Re: CGI->new(*FORM) Problem...
Message-Id: <zampino-97A5CB.08300827072000@enews.newsguy.com>

In article <slrn8nvilv.h8o.efflandt@efflandt.xnet.com>, 
efflandt@xnet.com wrote:

> Apparently you are using function method to originally get the data from
> the form, so param('foo') works, but when you save it to a file and then
> reload it, I think you have to use object method ($query->param('foo')) 
> or
> at least if you want to distinguish just submitted params from params
> loaded from a file.  I have used an array to load multiple sets of data:
> 
> $i = 0;
> while (!eof(FILE)) { $q[$i++] = new CGI(FILE); }
> foreach $q (@q) {
>     foreach ($q->param) {
>         print "$_ = ",$q->param($_),"\n";
>     }
>     print "\n";
> }


Still a bit of a curiousity to me, but it's clear to me that I should 
stick with the method that works in both cases (function and file) so 
$q->param("myVar") it is.  

Thanks so much for your assistance, I have a lot of cgi's to convert to 
this method, but I feel much more confident that I'm doing it for the 
last time now.

philz


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

Date: Thu, 27 Jul 2000 13:18:42 GMT
From: lkenny@fisheries.org (Liam)
Subject: CGI-Perl question - Form results writing to a file
Message-Id: <39803687.4422746@209.247.210.101>

i can't get these form results to write to a file.  any help anyone
can offer is appreciated.  i am including the script below

#!/usr/local/bin/perl

# read in query
read (STDIN, $email, $ENV{"CONTENT_LENGTH"});

# open email-list file and write new address to file
open (em, "email.txt");

while (<em>) {
    print >>"$email"."\;\n";
}

#close email list-file
close (em);

# http header
print "Content-type: text/html\n\n";

# print out confirmation page
print "<html><head><title>E-Mail Subscription Confirmation</title>\n";
print "</head><body><h1>Thank you for subscribing!</h1><br>\n";
print "<p><br>\n";
print "Click <a href=\"/index.html\">here</a> to return to
homepage.<br>\n";
print "</body></html>\n";




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

Date: Thu, 27 Jul 2000 13:27:01 GMT
From: marcel@codewerk.com (Marcel Grunauer)
Subject: Re: CGI-Perl question - Form results writing to a file
Message-Id: <slrn8o0egb.1d6.marcel@gandalf.local>

On Thu, 27 Jul 2000 13:18:42 GMT, Liam <lkenny@fisheries.org> wrote:

>i can't get these form results to write to a file.  any help anyone
>can offer is appreciated.  i am including the script below

>#!/usr/local/bin/perl

No.

    #!/usr/local/bin/perl -w

    use strict;

>read (STDIN, $email, $ENV{"CONTENT_LENGTH"});

No. Use CGI.pm (see 'perldoc CGI').

># open email-list file and write new address to file
>open (em, "email.txt");

If you want to open the file for writing, you need to say that. Also
you need to check the result of the system call. And filehandles are
all caps by convention.

	open(EM, ">email.txt") or die "can't create email.txt: $!\n";

>
>while (<em>) {

Now you want to read from the file? No. With CGI.pm, you get the form
contents via param() (see the CGI.pm documentation).

>    print >>"$email"."\;\n";

Where did you get that syntax from? The '>>' is strange, as is the
escaping of the semicolon. Why do you quote the $email scalar? Methinks
you mean

	print EM $email, "\n";

>}
>
># http header
>print "Content-type: text/html\n\n";
>
># print out confirmation page
>print "<html><head><title>E-Mail Subscription Confirmation</title>\n";
>print "</head><body><h1>Thank you for subscribing!</h1><br>\n";
>print "<p><br>\n";
>print "Click <a href=\"/index.html\">here</a> to return to
>homepage.<br>\n";
>print "</body></html>\n";

Please use the CGI module's HTML shortcuts to do that. But _if_ ever you
need to print a lot of text in this fashion, consider using a different
quote operator or a here document (cf. 'perldoc perlop' and "perldoc -q
'here doc'").

Anyway, using CGI.pm that becomes something like

	print header, start_html('E-Mail Subscription Confirmation'),
		h1('Thank you for subscribing!');

etc.

Also remember to test you program locally, either on the local web server
or on the command line (again, CGI.pm provides for that). And check your
HTML. The <H1> shouldn't be followed by paragraph or line breaks.

-- 
Marcel
sub AUTOLOAD{($_=$AUTOLOAD)=~s;^.*::;;;y;_; ;;print} Just_Another_Perl_Hacker();


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

Date: Thu, 27 Jul 2000 08:11:19 -0500
From: Tom Briles <sariq@texas.net>
Subject: Re: difference between two times in seconds
Message-Id: <398034F7.47478C11@texas.net>

Uri Guttman wrote:
> 
> >>>>> "h" == hayati  <hayati@math.berkeley.edu> writes:
> 
>   h> In article <slrn8nv26d.vcg.abigail@alexandra.foad.org>,
>   h>   abigail@foad.org wrote:
> 
>   >> sub datediff {
>   >> local ($_, $:) = map {1 x $_} $_ [0] > $_ [1] ? @_ : reverse @_;
>   >> s/$:// and length;
>   >> }
>
> cute, but not as complicated as many of abigail's japh's.

It must not be.  I could parse it...

- Tom


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

Date: Thu, 27 Jul 2000 14:26:07 GMT
From: vnguyen_1999@my-deja.com
Subject: empty hash reference
Message-Id: <8lpgpo$iha$1@nnrp1.deja.com>

Hello everyone

Try to catch an empty hash reference from the result set of query. Just
want to know how I can check if $sth->fetch() is empty.

Thanks



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


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

Date: 27 Jul 2000 07:23:57 -0700
From: tchrist@perl.com (Tom Christiansen)
Subject: Re: exactly WHEN will PP-3rd be in bookstores, etc?  Anyone know?
Message-Id: <398037ed@cs.colorado.edu>

In article <397ee793@news.jakinternet.co.uk>, Andy <andy@andy.co.uk> wrote:

>I have a copy ordered at amazon, and it said they expect it on 7th August

It's a bit tough to find the Third Edition on Amazon right now:
their search engine, oddly enough, pulls up Second Edition instead.  But

    http://www.amazon.com/exec/obidos/ASIN/0596000278/

does work.  

Quite a few people got theirs at TPC, where ORA was selling the
book at 40% off as a special conference-only discount.

--tom


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

Date: Thu, 27 Jul 2000 14:14:45 GMT
From: Ralf Weber <weberr2202@my-deja.com>
Subject: Re: find.pl & error_log
Message-Id: <8lpg4g$hvt$1@nnrp1.deja.com>

In article <u9u2dcj0pe.fsf@wcl-l.bham.ac.uk>,
  nobull@mail.com wrote:
> Ralf Weber <weberr2202@my-deja.com> writes:
>
> > Subject: Re: find.pl & error_log
>
> A better subject would have been: find.pl & Apache+mod_perl
>
> > in a perl file I use require find.pl
>
> Bad idea. The Perl4-style find.pl interface to the File::Find module
> is provided for legacy code.  New code should use the File::Find
> module directly.
>
> One problem with the find.pl kludge is that if your program consits of
> serveral packages (modules) and more than one of theses packages
> requires find.pl then your program will fail because only the first
> package will actually get the symbols from File::Find imported.
>
> > This works fine under windows, but under linux (SUSE 6.3) I get the
> > following error
> > in the error_log:
> > Fri Jul 14 19:01:51 2000] [error] Undefined subroutine
> >
&Apache::ROOT::cgi_2dbin::cgiuebungen::searchindex::sicherhomepage::rfin
> > dex_2epl::find called at
> > /apache/cgi-bin/cgiuebungen/searchindex/sicherhomepage/rfindex.pl
line
> > 27.
> >
> > I absolutely don't understand ;-)
>
> Under Apache+mod_perl your CGI scripts don't really run as separate
> scripts in separate instances of the Perl interpreter.  Instead they
> are loaded as packages in a single instance of the Perl interpreter.
> You therefore fall foul of the problem with find.pl mentioned above.
>
> Either run the scripts as real CGI scripts or stop requiring old
> Perl4-style libraries and start using Perl5-style modules.
>
> You may also want to read the Apache+mod_perl docs to learn what other
> things that are safe in stand-alone CGI are no longer safe.  There are
> not all that many but I pays to be aware of them.

Thanks a lot, I'll soon try out! Really helped me out, thanks.
I never would have known that....

Greetings,
Ralf
--
Ralf  Weber
Klingenstr. 18
70186 Stuttgart
E-Mail: weberr@machno.hbi-stuttgart.de
Internet: http://machno.hbi-stuttgart.de/~weberr


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


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

Date: Thu, 27 Jul 2000 14:49:33 GMT
From: "MusikSthlm" <dae@chello.se>
Subject: HELP needed in e-mail notification... 
Message-Id: <1_Xf5.6$Ba.334@nntp1.chello.se>

Hi...
I wonder if anyone in here can help me make a Perl Script that gets todays
date and then search a subfolder called "../messages/"....

The files in ../messages/ are organized like this:

_________________________________________________
notice: [date every year] ex. 05-11 (may the 11th every year)
user: test@test.com
________________________________________________

then I want it to send a mail to test@test.com if it is 05-11 and give him a
notice "one of your contracts is expiring" and also automaticly send the
same message to manager@test.com I would be very grateful if someone could
help me with this! Thanks alot =)

/Daniel


PS. If i can assist you in any way possible. Please let me know DS.







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

Date: Thu, 27 Jul 2000 14:52:02 GMT
From: marcel@codewerk.com (Marcel Grunauer)
Subject: Re: HELP needed in e-mail notification... 
Message-Id: <slrn8o0jf9.1d6.marcel@gandalf.local>

On Thu, 27 Jul 2000 14:49:33 GMT, MusikSthlm <dae@chello.se> wrote:

>I wonder if anyone in here can help me make a Perl Script that gets todays
>date and then search a subfolder called "../messages/"....
[blah]
>PS. If i can assist you in any way possible. Please let me know DS.


Repeatedly posting the same question doesn't get you higher in the list of
priorities. It can take hours for your message to get to people, who might
then have something else to do than immediately answer it. So be patient.

Besides, did you have any issues with the answer already given?


-- 
Marcel
sub AUTOLOAD{($_=$AUTOLOAD)=~s;^.*::;;;y;_; ;;print} Just_Another_Perl_Hacker();


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

Date: Thu, 27 Jul 2000 14:19:13 GMT
From: "Daniel" <dae@chello.se>
Subject: HELP NEEDED! BADLY... E-mail notification
Message-Id: <BxXf5.4$Ba.199@nntp1.chello.se>

Hi...
I wonder if anyone in here can help me make a Perl Script that gets todays
date and then search a subfolder called "../messages/"....

The files in ../messages/ are organized like this:

_________________________________________________
notice: [date every year] ex. 05-11 (may the 11th every year)
user: test@test.com
________________________________________________

then I want it to send a mail to test@test.com if it is 05-11 and give him a
notice "one of your contracts is expiring" and also automaticly send the
same message to manager@test.com I would be very grateful if someone could
help me with this! Thanks alot =)

/Daniel








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

Date: Thu, 27 Jul 2000 14:20:04 GMT
From: marcel@codewerk.com (Marcel Grunauer)
Subject: Re: HELP NEEDED! BADLY... E-mail notification
Message-Id: <slrn8o0him.1d6.marcel@gandalf.local>

On Thu, 27 Jul 2000 14:19:13 GMT, Daniel <dae@chello.se> wrote:

>I wonder if anyone in here can help me make a Perl Script that gets todays
>date and then search a subfolder called "../messages/"....
>
>The files in ../messages/ are organized like this:
>
>_________________________________________________
>notice: [date every year] ex. 05-11 (may the 11th every year)
>user: test@test.com
>________________________________________________
>
>then I want it to send a mail to test@test.com if it is 05-11 and give him a
>notice "one of your contracts is expiring" and also automaticly send the
>same message to manager@test.com I would be very grateful if someone could
>help me with this! Thanks alot =)

Sure.

To get the day and month, use localtime ('perldoc -f localtime'). Just
remember that months get counted from 0, not 1.

To get files in a directory, use opendir() and readdir(), or a glob
( <../messages/*> ).

To search for the date in the file contents, use a regex maybe combined
with a grep ('perldoc -f grep', 'perldoc perlre').

To send mail, read "How do I send mail?" in perlfaq9 ("perldoc -q 'send
mail'"). Or use a module like MIME::Lite, which you get from CPAN.

Good luck with it!

-- 
Marcel
sub AUTOLOAD{($_=$AUTOLOAD)=~s;^.*::;;;y;_; ;;print} Just_Another_Perl_Hacker();


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

Date: 27 Jul 2000 14:30:37 GMT
From: nobody@contract.East.Sun.COM (Nobody)
Subject: Re: Making and extracting from XML-like string
Message-Id: <8lph2d$8h9$1@eastnews1.east.sun.com>

In article <8ln5ol$1bu@junior.apk.net>,
Mark W. Schumann <catfood@apk.net> wrote:
>In article <397F05B8.49DF4BE5@eurodyn.com>,

<SNIP/>

>>On the other way, I've need to extract field_1, field_2,...,
>>student_name, professor_name, school_name from already crated XML-like
>>string.
>
>I would start by downloading and reading the documentation for
>Bundle::XML from CPAN.
>

I agree.  The XML modules should handle that nicely.  Or, if it's not
too large of a parsing job, just write your own.  I did that a couple of
years ago (wasn't aware of the XML mods, if they were even available yet).
Just write a simple regex to look for <tag> and recursively parse until 
that tag is closed.  Kind of quick and dirty, and I'm sure the modules 
handle it much more cleanly :-)

Anita


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

Date: Thu, 27 Jul 2000 15:41:59 +0200
From: "berndt zeitler" <berndt.zeitler@tu-berlin.de>
Subject: newbie question: assign array to hash
Message-Id: <8lpe38$9m3$1@mamenchi.zrz.TU-Berlin.DE>

Hi again,

i would like to know how to assign data from an @array to a key and to
values of a hash.
i've tried like this, but it didn't work.

  @FreqIeq{@alldata[0]}=@alldata[1];


@alldata is data from a file
  open(DATAFILE,"$datafile") || die "can't create $datafile";
  @alldata=<DATAFILE>;

this way the whole line is one key and one value. in other words how so i
separate the data in the array @alldata.

i tried with
  @temp0=split(/ /,@alldata[0]);
  @temp1=split(/ /,@alldata[1]);
too.

thanks a lot
ciao for now,
berndt






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

Date: Thu, 27 Jul 2000 13:46:03 GMT
From: marcel@codewerk.com (Marcel Grunauer)
Subject: Re: newbie question: assign array to hash
Message-Id: <slrn8o0fj2.1d6.marcel@gandalf.local>

On Thu, 27 Jul 2000 15:41:59 +0200, berndt zeitler
<berndt.zeitler@tu-berlin.de> wrote:

>i would like to know how to assign data from an @array to a key and to
>values of a hash.

Muuuuch simpler than you thought:

	%hash = @arr;

Since a hash can be unrolled into an array and vice versa, element pairs
are simply interpreted to be a hash key and its value. Just be sure that
the array has an even number of elements.

So:

    my @arr = qw/k1 v1 k2 v2 k3 v3/;

    my %hash = @arr;

    use Data::Dumper;
    print Dumper(\%hash);

    __END__

    $VAR1 = {
	      'k1' => 'v1',
	      'k2' => 'v2',
	      'k3' => 'v3'
	    };

-- 
Marcel
sub AUTOLOAD{($_=$AUTOLOAD)=~s;^.*::;;;y;_; ;;print} Just_Another_Perl_Hacker();


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

Date: Thu, 27 Jul 2000 14:47:19 GMT
From: Ala Qumsieh <aqumsieh@hyperchip.com>
Subject: Re: newbie question: assign array to hash
Message-Id: <7abszjhbpi.fsf@merlin.hyperchip.com>


"berndt zeitler" <berndt.zeitler@tu-berlin.de> writes:

> i would like to know how to assign data from an @array to a key and to
> values of a hash.
> i've tried like this, but it didn't work.
> 
>   @FreqIeq{@alldata[0]}=@alldata[1];

You seem to be confused by Perl's data structures. @alldata[0] is not
the first element of the array @alldata. It is an array slice that
contains one element which is equal to the first element of @alldata. If
you want to refer to the first element of @alldata, you have to prefix
it with a '$':

	$alldata[0]

For more info checkout perlfaq4:

	What is the difference between $array[1] and @array[1]?

> @alldata is data from a file
>   open(DATAFILE,"$datafile") || die "can't create $datafile";
>   @alldata=<DATAFILE>;
> 
> this way the whole line is one key and one value. in other words how so i
> separate the data in the array @alldata.
> 
> i tried with
>   @temp0=split(/ /,@alldata[0]);
>   @temp1=split(/ /,@alldata[1]);
> too.

Make sure you try and understand this code before using it. It is very
simple.

	for my $data (@alldata) {
		chomp $data;
		@temp = split ' ', $data;
		$FreqIeq{$temp[0]} = $temp[1];
	}

--Ala


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

Date: 27 Jul 2000 13:17:37 GMT
From: a.peacock@chime.ucl.ac.uk (Anthony Peacock)
Subject: Re: Nobody loves me?
Message-Id: <8lpcph$ir6$1@uns-a.ucl.ac.uk>

In article <397F4902.AD365854@mybowie.com>, jason@mybowie.com says...
>
>John Hascall wrote:
>
>> Jason Dixon  <fuzzyping@freewwweb.net> wrote:
>> >Ok, I don't get it.  I've posted this question now for the 3rd time, 
but
>> >no one will reply.  Am I sending this to the wrong group?  I've been
>> >posting this to both comp.lang.perl.misc and comp.lang.perl.modules, 
but
>> >haven't been able to get any assistance or direction.  PLEASE, SOMEONE
>> >LOVE ME AND REPLY.  :-)
>> >
>> >I've written a script to email a list of people with updates on my
>> >site.  The script works great, except I'm having difficulty 
customizing
>> >the "From" section of the header (no problems at all with the
>> >"Subject").  Instead of changing the From header to my variable, it 
uses
>> >the "real" header (from a machine that is NOT in my domain), then
>> >inserts the new header into the body of the email (???).
>>
>>    If you are not running as a 'trusted user' (see the sendmail conf
>>    file docs) then sendmail will not let you lie about 'From'.
>>
>
>That's a thought... however, I ran it as root and it still did the same
>thing....

Don't forget that root may not be a sendmail trusted user.  You will need 
to check your sendmail configuration to determine who are trusted users.



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

Date: Thu, 27 Jul 2000 14:20:54 +0100
From: "Simon H." <srh104@york.ac.uk>
Subject: Power?
Message-Id: <39803736.46A85B0B@york.ac.uk>

Is there a quick way to find a number n, which is a to the power of b? I
know there must be a function for this somewhere...

Cheers,
Simon H

--

http://atlas.cs.york.ac.uk/~srh104/




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

Date: Thu, 27 Jul 2000 13:28:01 GMT
From: marcel@codewerk.com (Marcel Grunauer)
Subject: Re: Power?
Message-Id: <slrn8o0ehr.1d6.marcel@gandalf.local>

On Thu, 27 Jul 2000 14:20:54 +0100, Simon H. <srh104@york.ac.uk> wrote:

>Is there a quick way to find a number n, which is a to the power of b? I
>know there must be a function for this somewhere...

$a ** $b

-- 
Marcel
sub AUTOLOAD{($_=$AUTOLOAD)=~s;^.*::;;;y;_; ;;print} Just_Another_Perl_Hacker();


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

Date: Thu, 27 Jul 2000 14:04:05 GMT
From: marcel@codewerk.com (Marcel Grunauer)
Subject: Regex alternation question
Message-Id: <slrn8o0gkb.1d6.marcel@gandalf.local>


I'm missing something about alternations or backtracking in a regex. I
thought the greedy '*' quantifier tries to match the longest of all
alternations, but apparently not.

The string I'm matching against contains double-quoted bits that are
interesting, which themselves can contain escaped double-quotes. First
I've used a negative lookbehind assertion, which was fine. But when
trying to do it without negative lookbehind, I've tried an alternation
(see below). But swapping the two alternating options produces different
results:


    local $/;
    $_ = <DATA>;

    print "Negative lookbehind assertion: ";
    print (/(".*?(?<!\\)")/s);
    print "\nStandard regex: ";
    print (/("(?:\\"|[^"])*")/s);
    print "\nStandard regex with swapped alternatives: ";
    print (/("(?:[^"]|\\")*")/s);
    print "\n";

    __DATA__
    result: "abc=\"123\" def, ghi=\"456\" jkl"
    result: "ABC=\"123\" DEF, GHI=\"456\" JKL"

produces:

    Negative lookbehind assertion: "abc=\"123\" def, ghi=\"456\" jkl"
    Standard regex: "abc=\"123\" def, ghi=\"456\" jkl"
    Standard regex with swapped alternatives: "abc=\"

What am I missing? (BTW, this is 5.6.0 on Linux).

-- 
Marcel
sub AUTOLOAD{($_=$AUTOLOAD)=~s;^.*::;;;y;_; ;;print} Just_Another_Perl_Hacker();


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

Date: Thu, 27 Jul 2000 14:41:27 GMT
From: gbacon@HiWAAY.net (Greg Bacon)
Subject: Re: Regex alternation question
Message-Id: <so0ign733j113@corp.supernews.com>

In article <slrn8o0gkb.1d6.marcel@gandalf.local>,
    Marcel Grunauer <marcel@codewerk.com> wrote:

: I'm missing something about alternations or backtracking in a regex. I
: thought the greedy '*' quantifier tries to match the longest of all
: alternations, but apparently not.

POSIX requires this of NFA implementations, but Perl breaks with POSIX
on this issue:

    Alternatives are tried from left to right, so the first alternative
    found for which the entire expression matches, is the one that is
    chosen. This means that alternatives are not necessarily greedy.
    For example: when matching `foo|foot' against "barefoot", only the
    "foo" part will match, as that is the first alternative tried, and
    it successfully matches the target string. (This might not seem
    important, but it is important when you are capturing matched text
    using parentheses.)

The above excerpt is from the perlre manpage.

Greg
-- 
Drugs may lead to nowhere, but at least it's the scenic route.


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

Date: 27 Jul 2000 16:08:04 +0200
From: Jakob Schmidt <sumus@aut.dk>
Subject: Re: return value of `eval "use lib"'
Message-Id: <punzr7i3.fsf@macforce.sumus.dk>

sweth+perl@gwu.edu (Sweth Chandramouli) writes:

> In article <puo07hkx.fsf@macforce.sumus.dk>,
> Jakob Schmidt  <sumus@aut.dk> wrote:
> >sweth+perl@gwu.edu (Sweth Chandramouli) writes:
>
> 	Except that eval() changes the definition of compile-time
> a little--inside of an eval(), the use() is executed not during compile-time
> per se, but instead (as I understand it) during the compile-time phase of a
> block of code that is called during run-time.

Didn't I say that?

Jakob:
> >The eval() is a way to defer compilation (and running) of a string of code
> >'til sometime during the runtime of the surrounding code

 ...

> execution is a no-op doesn't mean that the eval() couldn't trap the failure
> of
> the compilation and be smart enough to set its exit status accordingly.

Of course that's a possibility. Only you must acknowledge that it would
break the consistency in the behavior of eval().

> >BTW, remember that require is executed during runtime - so there's no reason
> >for eval() unless you want your program to tolerate a failure.
> 	Or unless the contents of the require() are being determined at 
> runtime, as I indicated was the case for my code.

No. Require is runtime. It even takes $_ as default argument. You can determine
the contents during runtime anytime.

The problem in your case is that you need import lib $somelib which doesn't
give you any (easy) chance of testing for success.

-- 
Jakob


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

Date: Thu, 27 Jul 2000 08:55:47 -0500
From: Russ Jones <russ_jones@rac.ray.com>
Subject: Re: Saving space in a hash
Message-Id: <39803F63.B0B9E53F@rac.ray.com>

Ilmari Karonen wrote:
> 
> In article <397F5D6C.2E5330C4@mail.tju.edu>, George M. Kuetemeyer wrote:
> >Russ Jones wrote:
> >> I'm building a hash that contains the same line of data, referenced
> >> three different ways, like this:
> >>
> >>         $snmp{$ip}    = $snmp_info;
> >>         $snmp{$dns}   = $snmp_info;
> >>         $snmp{$alias} = $snmp_info;
> >>
> >>         (repeat for about five thousand different ip's)
> >> Is there some more efficient way of doing this?
> 
> First question: What kind of scalars are the values?
> 
> If they're references, then the actual data already gets stored only
> once.  If they're strings, and significantly longer than the keys, you
> could try storing references to those strings instead.
> 
> >You could play around with a compound key approach:
> >Access could be via grep:
> >You would reduce your entries to 1/3 of the original. On the other hand,
> >access would not be as direct and maybe not as fast.
> 
> Maybe?  In this case it would be over five thousand times slower.
> 

Speed when I'm creating the lookup table is only of minor concern.
Within reason, I can take as long as I want. (I'd like the job to
complete before it's scheduled to run the next time, but that's about
it.

But the program that does the lookup wants to go pretty fast. It gets
executed when a monitor program suspects that a node is down, and then
it sends out a page to the network analysts so they can schlep over to
wherever the device is physically located and take a look.

The data I'm working with here are the snmp sysName and sysLocation
fields. I want to include them in the text of the pager message
because that can help the analyst find the device more quickly than
just a dotted IP or a dns name. I have to gather the snmp information
in advance because if the node really is down, it sure as heck won't
be responding to snmpwalk requests!

I ended up storing the scalars (strings in this case) that the hash
was referring to in an array, and then the hash contained indexes into
that array, kind of like this: 

(This snippet was typed, NOT cut and pasted.)

$msg = "some string from 20 to 40 characters long";
# save the string in an array
$snmp_data[++$ix] = $msg;
$ip = "127.0.0.1";	# for example
# since a dotted ip can take from 7 to 15 characters,
# pack them down to 4
@octet = split('\.',$ip,4);
$addr = pack('C4',@octet);
$hash{$addr} = $ix;		# store the index into the array
# also I want the dns name
$name = gethostbyaddr($addr,2);
($name,$x) = split("\.',$name);	# keep the first word of the dns name
				# I can ignore the domain part and
				# pretend to reconstruct it later.
$hash{$name} = $ix;		# it also points to the string array

Then I used Storable store_fd to store @snmp_data and %hash. When I
need the values in the other program, I just pack a dotted ip and do
the lookup, or if I have a dns name and not an ip, I can do the split
on it like I did above and look the string up that way. It made my
total file size about 23% of what it was before when I stored the same
string for each of ip, dns and "alias". Most of the savings was in
storing the data string only once instead of three times, but packing
the IP and not storing the domain part of the dns name saved another 7
or 8 percent.

My benchmarking shows that the time saved by loading the much smaller
file more than made up for the extra processing time used to create
the packed addresses and use a lookup table.

Thanks everyone for your input on this! It was pretty fun. Also the
golfing that everyone did on my other question, about selecting only
lines with two dotted ip's and eliminating everything else from an
input file, that was for this project too, thanks again!

-- 
Russ Jones - HP OpenView IT/Operatons support
Raytheon Aircraft Company, Wichita KS
russ_jones@rac.ray.com 316-676-0747

Quae narravi, nullo modo negabo. - Catullus


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

Date: 27 Jul 2000 14:24:14 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: Suggestion for syntax change
Message-Id: <964707568.2037@itz.pp.sci.fi>

In article <MPG.13eaa2e8babbb2cb989692@news>, jason wrote:
>HOWEVER .. it doesn't solve the behaviour that Keith is proposing where 
>within list index context the negative arguments of '..' are evaluated 
>first (to be indices relative to the end of the list) and THEN the '..' 
>is evaluated to produce a forward list

 ..for which I've already proposed a better (less klugy) alternative,
namely having a special variable contain the last index of the list
for the innermost surrounding [].

This wouldn't break existing code, which Keith's proposal might do.
It also involves less magic, and is therefore easier to understand.

-- 
Ilmari Karonen - http://www.sci.fi/~iltzu/
"The screwdriver *is* the portable method."  -- Abigail
Please ignore Godzilla and its pseudonyms - do not feed the troll.





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

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


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