[24378] in Perl-Users-Digest
Perl-Users Digest, Issue: 6567 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat May 15 09:05:36 2004
Date: Sat, 15 May 2004 06:05:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 15 May 2004 Volume: 10 Number: 6567
Today's topics:
Conditionally assigning several variables <lepor5e@bestweb.net>
Re: Conditionally assigning several variables <mark.clements@kcl.ac.uk>
Re: Conditionally assigning several variables <tadmc@augustmail.com>
Re: problem with string (Walter Roberson)
Re: problem with string <gnari@simnet.is>
Re: regex split conditional <nospam@nospam.net>
Re: RegEx to delete // comments NOT in quotes: ( ' ) OR <abigail@abigail.nl>
Re: RegEx to delete // comments NOT in quotes: ( ' ) OR <tassilo.parseval@rwth-aachen.de>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 15 May 2004 05:39:26 -0400
From: "Mike Lepore" <lepor5e@bestweb.net>
Subject: Conditionally assigning several variables
Message-Id: <10abp8s8uagbf58@corp.supernews.com>
I have been using the following general method to assign several
global variables whose values will depend on one global variable.
Can someone please warn me if this is inefficient, or liable
to cause any problems? By using "do" instead of using
"require" or a function, does this method have the advantage of
keeping the large number of program lines in memory only for a
temporary period of time? Thank you.
# A line in the main program
do 'program2.pl';
# Contents of a different file named program2.pl
if ($a == 652050) {$b="shirt"; $c="small" ; $d="red" ;}
elsif ($a == 971103) {$b="shirt"; $c="medium"; $d="green" ;}
elsif ($a == 615112) {$b="shirt"; $c="medium"; $d="yellow";}
elsif ($a == 814037) {$b="hat" ; $c="small" ; $d="blue" ;}
elsif ($a == 114392) {$b="hat" ; $c="large" ; $d="purple";}
# There are about a thousand lines of this stuff here.
--
Mike Lepore email delete the 5
------------------------------
Date: Sat, 15 May 2004 11:06:07 +0100
From: Mark Clements <mark.clements@kcl.ac.uk>
Subject: Re: Conditionally assigning several variables
Message-Id: <40a5eb8f$1@news.kcl.ac.uk>
Mike Lepore wrote:
> I have been using the following general method to assign several
> global variables whose values will depend on one global variable.
> Can someone please warn me if this is inefficient, or liable
> to cause any problems? By using "do" instead of using
> "require" or a function, does this method have the advantage of
> keeping the large number of program lines in memory only for a
> temporary period of time? Thank you.
>
> # A line in the main program
> do 'program2.pl';
>
> # Contents of a different file named program2.pl
> if ($a == 652050) {$b="shirt"; $c="small" ; $d="red" ;}
> elsif ($a == 971103) {$b="shirt"; $c="medium"; $d="green" ;}
> elsif ($a == 615112) {$b="shirt"; $c="medium"; $d="yellow";}
> elsif ($a == 814037) {$b="hat" ; $c="small" ; $d="blue" ;}
> elsif ($a == 114392) {$b="hat" ; $c="large" ; $d="purple";}
> # There are about a thousand lines of this stuff here.
What you are doing here is inefficient in programmer time and in
execution time. You may want to look at using a database of some
description, preferably one with indexes, though even doing lookups on a
flat CSV file would be superior to doing what you are doing here. Check
out DB_File, DBD::CSV and DBD::SQLite, for starters.
Also:
From perldoc -f do:
Note that inclusion of library modules is better
done with the "use" and "require" operators, which
also do automatic error checking and raise an
exception if there's a problem.
I realise that these variable names may just be there for purposes of
the example, but $a, $b etc are not good variable names: they are pretty
much meaningless at first inspection. Is $a a serial number, the number
of items in stock or a mass? Also, $a and $b have a special meaning in
perl - check out the docs for sort - that will enable them to creep past
use strict checking. If you are relying on global variables then you may
want to have a think about your program design. Many would (and probably
will disagree) but global variables are generally thought of as not
being a good way for passing data between different sections of a program.
Mark
------------------------------
Date: Sat, 15 May 2004 07:01:20 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Conditionally assigning several variables
Message-Id: <slrncac1kg.ia0.tadmc@magna.augustmail.com>
Mike Lepore <lepor5e@bestweb.net> wrote:
> if ($a == 652050) {$b="shirt"; $c="small" ; $d="red" ;}
if ($a == 652050) { ($b, $c, $d) = qw/ shirt small red / }
I would probably put the info in a hash-of-hashes (HoH),
(or even in a real RDBMS):
my %products = (
652050 => { item => 'shirt', size => 'small', color => 'red' }
...
);
then the assignment can be done in a single line using a hash slice:
($b, $c, $d) = @{ $products{$a} }{qw/ item size color /}; # untested
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 15 May 2004 07:47:27 GMT
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Subject: Re: problem with string
Message-Id: <c84huf$l6o$1@canopus.cc.umanitoba.ca>
In article <c84f37$sii$1@ls219.htnet.hr>, <tihana@kata.com> wrote:
:how to make someting like basic command:
:var$="123456789a"
:1.) a$=mid$(var",5,2) :: a$="56"
substr($var,5,2) = '56';
--
Beware of bugs in the above code; I have only proved it correct,
not tried it. -- Donald Knuth
------------------------------
Date: Sat, 15 May 2004 08:36:07 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: problem with string
Message-Id: <c84kmn$bav$1@news.simnet.is>
"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message
news:c84huf$l6o$1@canopus.cc.umanitoba.ca...
> In article <c84f37$sii$1@ls219.htnet.hr>, <tihana@kata.com> wrote:
> :how to make someting like basic command:
> :var$="123456789a"
> :1.) a$=mid$(var",5,2) :: a$="56"
>
> substr($var,5,2) = '56';
surely the OP is asking for
$var='123456789a':
$a=substr($var,5,2); # $a is '56'
gnari
------------------------------
Date: Sat, 15 May 2004 10:30:06 GMT
From: "Jeff Thies" <nospam@nospam.net>
Subject: Re: regex split conditional
Message-Id: <Oimpc.6749$zO3.6340@newsread2.news.atl.earthlink.net>
> > I want to split this on the commas, but not the commas that are
enclosed
> > in the brackets ():
[DE-SOHT (7.5 LBS., 16"), $139.99; DE-HT (8.5 LBS., 30"), $139.99; DE-GB
(17.5 LBS., 40"), $184.99]
>
> > How do I do this?
That would be this:
@new = ();
push(@new, $+) while $text =~ m{"([^\"\\]*(?:\\.[^\"\\]*)*)",? | ([^,]+),?
| ,}gx;
push(@new, undef) if substr($text,-1,1) eq ',';
How far off is this?
@new = ();
push(@new, $+) while $text =~ m{\((.*)\),? | ([^,]+),? | ,}gx;
push(@new, undef) if substr($text,-1,1) eq ',';
I can't really figure out: (?:\\.[^\"\\]*)
Jeff
That just makes my head spin.
Jeff
>
>
> perldoc -q split
>
> How can I split a [character] delimited string except when inside
> [character]? (Comma-separated files)
>
>
> --
> Tad McClellan SGML consulting
> tadmc@augustmail.com Perl programming
> Fort Worth, Texas
------------------------------
Date: 15 May 2004 10:35:07 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: RegEx to delete // comments NOT in quotes: ( ' ) OR (")???
Message-Id: <slrncabsir.4ga.abigail@alexandra.abigail.nl>
W. D. (NewsGroups@US-Webmasters.com) wrote on MMMCMX September MCMXCIII
in <URL:news:40A5B018.6932@US-Webmasters.com>:
--
-- ========================================================================
-- // Here is the breakdown of how this works:
-- // ' // Opening quote to contain the RegEx String
-- // # // Opening RegEx delimiter. Using # because
-- //
-- // is used in the match
-- // ^ // Start at the beginning of the string
-- // ( // Begin capture section 1
-- // [^"\'\/] // Any character except these 3: " ' /
-- // * // Zero or more of the previous
-- // ) // Close capture section 1
-- // // // Must have the double slashes // on the line
-- // [^"\'] // Any character except these 2: " '
-- // * // Zero or more of the previous
-- // [\n\r] // End of line character
-- // $ // End of string marker
-- // # // Closing RegEx delimiter
-- // m // Multi-line string modifier
-- // U // Ungreedy modifier
-- // ' // Closing quote for RegEx string
-- =============================================================================
I've no idea which version of Perl you are using, but I've never heard
of an 'ungreedy' modifier. Anyway, your description suggests that you
don't strip out comments containing quotes. That is, you leave
// This is a "comment"
as is.
Abigail
--
perl -e '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %;
BEGIN {% % = ($ _ = " " => print "Just Another Perl Hacker\n")}'
------------------------------
Date: Sat, 15 May 2004 13:19:01 +0200
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: RegEx to delete // comments NOT in quotes: ( ' ) OR (")???
Message-Id: <2gmcl7F44peeU1@uni-berlin.de>
Also sprach Abigail:
> W. D. (NewsGroups@US-Webmasters.com) wrote on MMMCMX September MCMXCIII
> in <URL:news:40A5B018.6932@US-Webmasters.com>:
> --
> -- ========================================================================
> -- // Here is the breakdown of how this works:
> -- // ' // Opening quote to contain the RegEx String
> -- // # // Opening RegEx delimiter. Using # because
> -- //
> -- // is used in the match
> -- // ^ // Start at the beginning of the string
> -- // ( // Begin capture section 1
> -- // [^"\'\/] // Any character except these 3: " ' /
> -- // * // Zero or more of the previous
> -- // ) // Close capture section 1
> -- // // // Must have the double slashes // on the line
> -- // [^"\'] // Any character except these 2: " '
> -- // * // Zero or more of the previous
> -- // [\n\r] // End of line character
> -- // $ // End of string marker
> -- // # // Closing RegEx delimiter
> -- // m // Multi-line string modifier
> -- // U // Ungreedy modifier
> -- // ' // Closing quote for RegEx string
> -- =============================================================================
>
> I've no idea which version of Perl you are using, but I've never heard
> of an 'ungreedy' modifier.
In case this isn't obvious, the OP is not using Perl. The showing up of
'preg_match' a couple of postings ago suggests he is using PHP. The 'U'
flag means that he is using the PCRE-flavour of regexes in PHP.
Therefore he should not be posting here but in a PHP group.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 6567
***************************************