[13403] in Perl-Users-Digest
Perl-Users Digest, Issue: 813 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Sep 15 22:17:26 1999
Date: Wed, 15 Sep 1999 19:10:10 -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 Wed, 15 Sep 1999 Volume: 9 Number: 813
Today's topics:
Re: PLEASE HELP! SymRefs (answer to EVERYBODY) makau@multimania.com
Re: Redirect STDOUT to variable bernfarr@my-deja.com
Re: Text Parsing - How to Multi-Line? <makkulka@cisco.com>
Re: Text Parsing - How to Multi-Line? <makkulka@cisco.com>
Re: trimming spaces from a string <wellhaven@worldnet.att.net>
Re: trimming spaces from a string (elephant)
Re: trimming spaces from a string (Martien Verbruggen)
Re: trimming spaces from a string (Abigail)
Re: trimming spaces from a string (Larry Rosler)
Re: Two dimensional array (Abigail)
Digest Administrivia (Last modified: 1 Jul 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 16 Sep 1999 01:15:28 GMT
From: makau@multimania.com
Subject: Re: PLEASE HELP! SymRefs (answer to EVERYBODY)
Message-Id: <7rpgb8$ck9$1@nnrp1.deja.com>
Thanks to everybody for the help, or otherwise, advice!
Huge Thanks to Kragen!!!
I have to admit that your code is bloody awesome - exactly what I
needed! You have to be one of the most talented Perl coders out there
(no one else gave me an answer to my question arguing that using
symbolic references in my case was not clean. Yet I know they did NOT
know how to make my code work using symrefs!).
For those who asked why I needed like hell to use symrefs in place
of hashes, I will explain. I firstly created a single perl script -
which worked perfectly. Then my boss asked me to use modules instead. So
I created this friggin .pm file and copied the main subroutines (from
the orignial script) to it. But the problem is: I was using *MANY*
scalar variables inside those subroutines!!! So since all variables are
transfered to the module subroutines through the SINGLE object $self, if
i wanted so keep the same names for my former scalar variables, I had to
reconstruct them at the very begining of the subroutine!
ie:
------------------------------------------------------
my $self = shift;
foreach (keys %$self) {
eval "use vars '\$$_'; \$$_ = '$self->{$_}';";
};
------------------------------------------------------
[... nothing else to change in the subroutine]
And then all the scalarvars of the subroutines wouldn't have to be
changed (and heck they are numerous!). Needless to say, if Kragen hadn't
helped, I'd have had to change all those scalarvars to hash-entries.
ie:
------------------------------------------------------
my $self = shift;
my %hash;
foreach (keys %$self) {
$hash{$_} = $self->{$_};
};
------------------------------------------------------
[... I have to rename all the scalarvars to the corresponding hash
entries]
To the guy who said my code doesn't even work with 'use strict'
enabled, here is the modified code :
---------------------------------
#!/usr/local/bin/perl -w
%hash = (
hello => 'world',
perl => 'rOckz',
);
foreach (keys %hash) {
${$_} = $hash{$_};
};
print "The $hello $perl !";
---------------------------------
I can't tell you my exact version of Perl right now since I don't have
access to my unix station for the moment.
Once again thanks to everybody for trying to help! I am always
amazed when I see so many good-willing people giving a hand to newbies!
Needless to say it's with this behaviour that we will have the Perl
community grown even bigger and towards the right direction.
Makau. (joined this group 2 days ago)
In article <3oPD3.12650$N77.938851@typ11.nn.bcandid.com>,
kragen@dnaco.net (Kragen Sitaker) wrote:
> In article <7rnphr$3ik$1@nnrp1.deja.com>, <makau@multimania.com>
wrote:
> >I have already tried this so many times....
> >Removing the 'my' doesn't solve the problem.
> >The first error message disappears by the others remain!
>
> You can get rid of the others with use vars, or by doing as it says
and
> talking about $main::hello instead of $hello.
>
> I thought strict 'refs' prohibited symbolic references.
>
> You can, of course, use eval STRING.
>
> #!/usr/bin/perl -w
> use strict;
> BEGIN {
> my %vars = ('x' => 'y', 'z' => 'elephant@squirrel');
> for (keys %vars) {
> eval "use vars '\$$_'; \$$_ = '$vars{$_}';";
> }
> }
> print "x is $x and z is $z\n";
>
> Output is:
> x is y and z is elephant@squirrel
>
> I hope that at some point you decide to avoid eval STRING and symrefs
:)
>
> Kragen
> --
> <kragen@pobox.com> Kragen Sitaker
<http://www.pobox.com/~kragen/>
> Tue Sep 14 1999
> 55 days until the Internet stock bubble bursts on Monday, 1999-11-08.
> <URL:http://www.pobox.com/~kragen/bubble.html>
>
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
------------------------------
Date: Thu, 16 Sep 1999 00:41:42 GMT
From: bernfarr@my-deja.com
Subject: Re: Redirect STDOUT to variable
Message-Id: <7rpec4$b89$1@nnrp1.deja.com>
In article <37DEB8FE.5ECE505E@cisco.com>,
Makarand Kulkarni <makkulka@cisco.com> wrote:
> [ bernfarr@my-deja.com wrote:
>
> >
> > Does anyone have a way to do this? I've searched for redirect and
> > STDOUT, etc., in the normal forums without any success.
>
> Do it yourself. It is easy. Concatenate all HTML output to a string
> variable.
>
> use CGI ;
> my $page=new CGI ;
> my $string = "";
> print $page->header();
> $string.=$page->start_html();
> $string.=$page->end_html();
> print $string ; # print entire HTML when </HTML> tag is written to
$string.
>
> ==
Makarand
Thank you for the answer (I know it's obvious in retrospect, can you
tell I'm running on empty) and for not telling me to RTFM, etc.
Bernard
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
------------------------------
Date: Wed, 15 Sep 1999 17:04:54 -0700
From: Makarand Kulkarni <makkulka@cisco.com>
Subject: Re: Text Parsing - How to Multi-Line?
Message-Id: <37E03425.7F63A5CF@cisco.com>
[ Scott Davis wrote:
> Newbie here. I'm attempting to parse a multi-line input to a single
> line output. Is this possible? Any quick tips would be appreciated.
> Here's a sample:
> Line 1 text
> I want something in this line, fixed characters here, variable here
> Line 3 text
> Line 4 text
> I want something in this line, variable text here, fixed characters here
> Line 6 text
> Line 7 test
>
> My code is:
> { code snipped.. }
> What I'm trying to do is find line 1 text ending with specific "char is
> (3)" and line 2 text starting with "IP Address". If I can find that,
> I'll be happy as a clam. All other text is to be ignored.
I am assuming that if line1 ended with (3) and line2 started with 'IP
Address'
and line5 followed some syntax then you want to ignore lines 3, 4, 6 and 7
and print out all other lines ( 1,2 and 5) to the results file.
while ( $line1 = <DATA> )
{
if ( $line1 =~/\(3\)$/ ) # line1 is ending with (3)
{
$line2 = <DATA> ;
if ( $line2 =~/^IP Address/ )
{
$line3 = <DATA>; # lose this line
$line4 = <DATA>; #lose this line
$line5 = <DATA> ;
$line6 = <DATA> ; #lose this line
$line7 = <DATA> ; #lose this line
print $line1. $line2. $line5 ; # you can
check line5 sytax here if you
want to
}
}
}
__DATA__
Line 1 text(3)
IP Address=127.0.0.1
Line 3 text
Line 4 text
I want something in this line, variable text here, fixed characters here
Line 6 text
Line 7 test
-- Results--
Line 1 text(3)
IP Address=127.0.0.1
I want something in this line, variable text here, fixed characters here
------------------------------
Date: Wed, 15 Sep 1999 17:46:07 -0700
From: Makarand Kulkarni <makkulka@cisco.com>
Subject: Re: Text Parsing - How to Multi-Line?
Message-Id: <37E03DCE.C8513914@cisco.com>
[ Scott Davis wrote:
> Newbie here. I'm attempting to parse a multi-line input to a single
> line output. Is this possible? Any quick tips would be appreciated.
> Here's a sample:
> Line 1 text
> I want something in this line, fixed characters here, variable here
> Line 3 text
> Line 4 text
> I want something in this line, variable text here, fixed characters here
> Line 6 text
> Line 7 test
>
> My code is:
> { code snipped.. }
> What I'm trying to do is find line 1 text ending with specific "char is
> (3)" and line 2 text starting with "IP Address". If I can find that,
> I'll be happy as a clam. All other text is to be ignored.
I am assuming that if line1 ended with (3) and line2 started with 'IP
Address'
and line5 followed some syntax then you want to ignore lines 3, 4, 6 and 7
and print out all other lines ( 1,2 and 5) to the results file.
while ( $line1 = <DATA> )
{
if ( $line1 =~/\(3\)$/ ) # line1 is ending with (3)
{
$line2 = <DATA> ;
if ( $line2 =~/^IP Address/ )
{
$line3 = <DATA>; # lose this line
$line4 = <DATA>; #lose this line
$line5 = <DATA> ;
$line6 = <DATA> ; #lose this line
$line7 = <DATA> ; #lose this line
print $line1. $line2. $line5 ; # you can
check line5 sytax here if you
want to
}
}
}
__DATA__
Line 1 text(3)
IP Address=127.0.0.1
Line 3 text
Line 4 text
I want something in this line, variable text here, fixed characters here
Line 6 text
Line 7 test
-- Results--
Line 1 text(3)
IP Address=127.0.0.1
I want something in this line, variable text here, fixed characters here
==
------------------------------
Date: Wed, 15 Sep 1999 20:50:07 -0400
From: "Dolly & Will Cardwell" <wellhaven@worldnet.att.net>
Subject: Re: trimming spaces from a string
Message-Id: <7rpepc$m34$1@bgtnsc02.worldnet.att.net>
Thanks so much for all the helpful and educational responses. I want to
apologies that I did not intend to show an unequal number of embedded
spaces, and I do not want to discard them. Looking at the responses, I like
the following, but I'd like to combine the two statements into one. - which
I am still working on:
$s=~s/^ *//;
$s=~s/ *$//;
... and similarly for $t.
I believe s/^ *$// worked in unix shells I have worked on in the past, but
not in Win32 perl which I am getting started in now.
Being a unix shell and awk person, new to perl, your responses helped me get
the sense of the =~ operator which I had not used in the perl way before.
Actually, for many years I was a Fortran and Control Data systems
programmer, and as they say, a Fortran programmer progarms in Fortran,
whatever the language.
As for my second question, metaquote($sep) as suggested by several of you
works great and I have put it into production. Thanks!
Thank you again and keep on perling. I'd walk a mile for a camel.
Will Cardwell
Dolly & Will Cardwell <wellhaven@worldnet.att.net> wrote in message
news:7rmr41$4lv$1@bgtnsc01.worldnet.att.net...
> #Given:
>
> $s=' aa bcdef ';
> $t='aa bcdef';
>
> # I'd like them to match.
> # That is, I want to match as though leading and and trailing spaces were
> removed.
> # For example:
>
> if ( $s eq $t) { # This of course needs help
> print "They match.\n";
> }else{
> print "They don't match.\n";
> }
>
> # -------------------------------------------------------
> # While I have your attention, May I sneek in another?
> # Feel free to leave one undone.
>
> #Given:
>
> $s='abc*def * ghi';
> $sep='*'; # $sep may vary, and asterisk is a candidate.
> @flds=split /$sep/, $s; # This won't split on * but works for non-spcl
> chars
>
> print "@flds\n";
>
> # @flds = split /\*/, $s; # works OK - i.e. when the asterisk is a
> literal
> # but not .../\$sep/....
>
>
> # Hope I have all more trivial bugs removed. Don't have my compiler
> # readily available.
>
> # Thank you so much for your help!
> # Will Cardwell
> #
>
>
>
------------------------------
Date: Thu, 16 Sep 1999 12:32:31 +1000
From: elephant@squirrelgroup.com (elephant)
Subject: Re: trimming spaces from a string
Message-Id: <MPG.124ae5a98de9d90d989ccd@news-server>
Larry Rosler writes ..
>In article <x3ywvtsdq2p.fsf@tigre.matrox.com> on Wed, 15 Sep 1999
>13:47:42 -0400, Ala Qumsieh <aqumsieh@matrox.com> says...
>> You also need:
>>
>> $t =~ s/^\s+|\s+$/g; # just in case
>
>Neither of you seems to have read perlfaq4: "How do I strip blank space
>from the beginning/end of a string?" It says some rather
>uncomplimentary things about these approaches.
not to mention the fact that C<$t =~ s/^\s+|\s+$/g;> is not valid perl
*please* test the code you post here (it'll not be long before someone
writes something to sift through all the posts searching for stuff that
looks like perl, parsing it, and if it didn't parse killfiling the
poster *8^)
--
jason - elephant@squirrelgroup.com -
------------------------------
Date: Thu, 16 Sep 1999 01:33:17 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: trimming spaces from a string
Message-Id: <xNXD3.112$E%1.6244@nsw.nnrp.telstra.net>
In article <7rpepc$m34$1@bgtnsc02.worldnet.att.net>,
"Dolly & Will Cardwell" <wellhaven@worldnet.att.net> writes:
> Thanks so much for all the helpful and educational responses. I want to
> apologies that I did not intend to show an unequal number of embedded
> spaces, and I do not want to discard them. Looking at the responses, I like
> the following, but I'd like to combine the two statements into one. - which
> I am still working on:
>
> $s=~s/^ *//;
> $s=~s/ *$//;
Don't combine them. One of the responses referred to the perlfaq.
# perldoc perlfaq4
=head2 How do I strip blank space from the beginning/end of a string?
This explains why _not_ to combine the two.
Martien
PS. Next time you post, put your replies _after_ the text you respond
to, and please, remove all the quoted text from your reply that is not
directly related to what you are adding.
--
Martien Verbruggen |
Interactive Media Division | In a world without fences, who needs
Commercial Dynamics Pty. Ltd. | Gates?
NSW, Australia |
------------------------------
Date: 15 Sep 1999 20:49:38 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: trimming spaces from a string
Message-Id: <slrn7u0jah.i4j.abigail@alexandra.delanet.com>
Dolly & Will Cardwell (wellhaven@worldnet.att.net) wrote on MMCCVII
September MCMXCIII in <URL:news:7rpepc$m34$1@bgtnsc02.worldnet.att.net>:
<> Thanks so much for all the helpful and educational responses. I want to
<> apologies that I did not intend to show an unequal number of embedded
<> spaces, and I do not want to discard them. Looking at the responses, I like
<> the following, but I'd like to combine the two statements into one. - which
<> I am still working on:
<>
<> $s=~s/^ *//;
<> $s=~s/ *$//;
Why the urge to combine the statements? If you really want to, you could
do something like:
$s =~ s/^ +// xor $s =~ s/ +$//;
You could put it in one regex, but unless you're playing Perl golf, I
advice you against that.
<> ... and similarly for $t.
s/^ +// + s/ +$// for ($s, $t);
<> I believe s/^ *$// worked in unix shells I have worked on in the past, but
<> not in Win32 perl which I am getting started in now.
What have shells to do with this?
Abigail
--
sub _'_{$_'_=~s/$a/$_/}map{$$_=$Z++}Y,a..z,A..X;*{($_::_=sprintf+q=%X==>"$A$Y".
"$b$r$T$u")=~s~0~O~g;map+_::_,U=>T=>L=>$Z;$_::_}=*_;sub _{print+/.*::(.*)/s}
*_'_=*{chr($b*$e)};*__=*{chr(1<<$e)};
_::_(r(e(k(c(a(H(__(l(r(e(P(__(r(e(h(t(o(n(a(__(t(us(J())))))))))))))))))))))))
-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
http://www.newsfeeds.com The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including Dedicated Binaries Servers ==-----
------------------------------
Date: Wed, 15 Sep 1999 18:47:09 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: trimming spaces from a string
Message-Id: <MPG.1249ebfcc05487fb989f7f@nntp.hpl.hp.com>
In article <7rpepc$m34$1@bgtnsc02.worldnet.att.net> on Wed, 15 Sep 1999
20:50:07 -0400, Dolly & Will Cardwell <wellhaven@worldnet.att.net>
says...
> Thanks so much for all the helpful and educational responses. I want to
> apologies that I did not intend to show an unequal number of embedded
> spaces, and I do not want to discard them. Looking at the responses, I like
> the following, but I'd like to combine the two statements into one. - which
> I am still working on:
>
> $s=~s/^ *//;
> $s=~s/ *$//;
>
> ... and similarly for $t.
s/^ +//, s/ +$// for $s, $t;
You don't want to use '*', because why do unnecessary work?
...
> Dolly & Will Cardwell <wellhaven@worldnet.att.net> wrote in message
> news:7rmr41$4lv$1@bgtnsc01.worldnet.att.net...
<SNIP> of long, superfluous, out-of-order quote of original submision.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 15 Sep 1999 19:05:04 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: Two dimensional array
Message-Id: <slrn7u0d6f.i4j.abigail@alexandra.delanet.com>
Scott D. Wells (shadowsystems@ibm.net) wrote on MMCCVI September MCMXCIII
in <URL:news:37E009EF.BF28CA69@ibm.net>:
"" Does PERL have any kind of implimentation of two dimensional arrays?
No, but it has arrays of references to arrays, which should do what
you want.
Details are found in the manual.
Abigail
--
perl -wlpe '}{$_=$.' file # Count the number of lines.
-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
http://www.newsfeeds.com The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including Dedicated Binaries Servers ==-----
------------------------------
Date: 1 Jul 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 1 Jul 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.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq" from
almanac@ruby.oce.orst.edu. The real FAQ, as it appeared last in the
newsgroup, can be retrieved with the request "send perl-users FAQ" from
almanac@ruby.oce.orst.edu. Due to their sizes, neither the Meta-FAQ nor
the FAQ are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq" from
almanac@ruby.oce.orst.edu.
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 813
*************************************