[25358] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7603 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jan 4 14:10:33 2005

Date: Tue, 4 Jan 2005 11:10:26 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 4 Jan 2005     Volume: 10 Number: 7603

Today's topics:
    Re: Newbie question: "Get substring of line" <leendert@wouter.unitedknowledge.net>
    Re: Newbie question: "Get substring of line" <mikael.petterson@ericsson.se>
    Re: Newbie question: "Get substring of line" <bik.mido@tiscalinet.it>
    Re: Newbie question: "Get substring of line" <jurgenex@hotmail.com>
    Re: Newbie question: "Get substring of line" <jl_post@hotmail.com>
    Re: Newbie question: "Get substring of line" <uri@stemsystems.com>
    Re: Newbie question: "Get substring of line" <leendert@wouter.unitedknowledge.net>
    Re: Newbie question: "Get substring of line" <spamtrap@dot-app.org>
    Re: Program optimisation advice for newbie <tadmc@augustmail.com>
    Re: Program optimisation advice for newbie <bik.mido@tiscalinet.it>
    Re: weird failure with Net::FTP <botfood@yahoo.com>
    Re: Why does this work? (Peter Scott)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 04 Jan 2005 13:42:27 +0100
From: "Leendert Bottelberghs" <leendert@wouter.unitedknowledge.net>
Subject: Re: Newbie question: "Get substring of line"
Message-Id: <pan.2005.01.04.12.42.27.467527@wouter.unitedknowledge.net>

On Tue, 04 Jan 2005 09:09:43 +0100, Petterson Mikael wrote:
> xdt=a2a
> zdt=b2b
> 
> Is there a simple way in perl to assign a $str the substring after the 
> equal sign?

It can be done in several ways. The most efficient way is using regular
expressions. Take a look here:
http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

-leendert bottelberghs


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

Date: Tue, 04 Jan 2005 13:38:38 +0100
From: Petterson Mikael <mikael.petterson@ericsson.se>
Subject: Re: Newbie question: "Get substring of line"
Message-Id: <cre2gl$mg7$1@newstree.wise.edt.ericsson.se>

Sherm Pendley wrote:
> Petterson Mikael wrote:
> 
>> Hi,
>>
>> I am quite new to perl ( but have written numerous shell scripts). I 
>> had a shell script that used awk to get part of a string on a line.
>>
>> Lines looks like this:
>>
>> xdt=a2a
>> zdt=b2b
>> ....
>>
>> Is there a simple way in perl to assign a $str the substring after the 
>> equal sign?
> 
> 
> Have you seen the posting guidelines that appear here frequently?
> 
> Perl comes with *lots* of documentation. Start with "man perl" and "man 
> perlfaq". <http://learn.perl.org> has lots of links to online tutorials 
> and dead trees to help you get started too.
> 
> sherm--
> 

Hi,

Thanks for the pointers.

Here is the code I am trying to get working ( see below).

If the cs_data_file has a line that begins with # it should be treated 
as comment and the next line should be read. However the if-statement is 
reached anyway. What am  I doing wrong here?

//Mikael

CODE
**************


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

Date: Tue, 04 Jan 2005 15:51:17 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Newbie question: "Get substring of line"
Message-Id: <7urkt0lmmf93nkbdspfkci6tsh0asbt7rh@4ax.com>

On Tue, 04 Jan 2005 09:09:43 +0100, Petterson Mikael
<mikael.petterson@ericsson.se> wrote:

>Lines looks like this:
>
>xdt=a2a
>zdt=b2b
>....
>
>Is there a simple way in perl to assign a $str the substring after the 
>equal sign?

  (my $str=$_) =~ s/.*=//;  # e.g.


PS: please, do (a favour to yourself and) accept the other advices you
got notwithstanding the fact that you took them as being rude
answers...


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Tue, 04 Jan 2005 15:05:43 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Newbie question: "Get substring of line"
Message-Id: <bhyCd.25574$tG3.15578@trnddc02>

Leendert Bottelberghs wrote:
> On Tue, 04 Jan 2005 09:09:43 +0100, Petterson Mikael wrote:
>> xdt=a2a
>> zdt=b2b
>>
>> Is there a simple way in perl to assign a $str the substring after
>> the equal sign?
>
> It can be done in several ways. The most efficient way is using
> regular expressions.

Actually I don't think so.
While powerful and sometimes convenient to write regular expressions are 
also complex and costly to evaluate at runtime.

Therefore the humble index() with a substr() is likely to be faster.

No, I did not benchmark it.

jue 




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

Date: 4 Jan 2005 07:47:27 -0800
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: Newbie question: "Get substring of line"
Message-Id: <1104853646.998256.50450@f14g2000cwb.googlegroups.com>

Petterson Mikael wrote:
>
> Lines looks like this:
>
> xdt=a2a
> zdt=b2b
> ....
>
> Is there a simple way in perl to assign a $str the
> substring after the equal sign?


Here is one (not necessarily optimal) solution:

my $string = "xdt=a2a";
my $substring;
$substring = $'  if $string =~ m/=/;

Basically, the $' variable holds the part AFTER a successful match,
so it will be set to everything after the first equal sign of $string.

Some people don't like using the $' variable because it comes at a
performance cost.  If this performance cost boethers you, you can
change that last line to:

$substring = $1  if $string =~ m/=(.*)$/;

which basically puts the part captured by the first parentheses in $1
(which is given to $substring).

If you don't want to do this with regular expressions, I recommend
you read up on the index() and substr() functions, which you can find
by looking them up in the Camel book or by typing:

perldoc -f index
perldoc -f substr

   I hope this helps.

   -- Jean-Luc



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

Date: Tue, 04 Jan 2005 16:16:41 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Newbie question: "Get substring of line"
Message-Id: <x74qhxyxsn.fsf@mail.sysarch.com>

>>>>> "jpc" == jl post@hotmail com <jl_post@hotmail.com> writes:


  jpc> my $string = "xdt=a2a";
  jpc> my $substring;
  jpc> $substring = $'  if $string =~ m/=/;

  jpc> Basically, the $' variable holds the part AFTER a successful match,
  jpc> so it will be set to everything after the first equal sign of $string.

  jpc> Some people don't like using the $' variable because it comes at a
  jpc> performance cost.  If this performance cost boethers you, you can
  jpc> change that last line to:

and it is not commonly used as well. so why did you even bother to
mention it?

nor did you tell the OP where to learn about regexes or whatever.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Tue, 04 Jan 2005 17:29:00 +0100
From: "Leendert Bottelberghs" <leendert@wouter.unitedknowledge.net>
Subject: Re: Newbie question: "Get substring of line"
Message-Id: <pan.2005.01.04.16.29.00.172807@wouter.unitedknowledge.net>

On Tue, 04 Jan 2005 15:05:43 +0000, Jürgen Exner wrote:
> Leendert Bottelberghs wrote:
>> It can be done in several ways. The most efficient way is using
>> regular expressions.
> 
> Actually I don't think so.
> While powerful and sometimes convenient to write regular expressions are 
> also complex and costly to evaluate at runtime.
> 
> Therefore the humble index() with a substr() is likely to be faster.

I think you're right. I actually meant it's the most convenient way for a
lazy programmer.

-leendert bottelberghs


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

Date: Tue, 04 Jan 2005 12:55:05 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Newbie question: "Get substring of line"
Message-Id: <fMydnRj32qLnRUfcRVn-uQ@adelphia.com>

Uri Guttman wrote:

> and it is not commonly used as well. so why did you even bother to
> mention it?

You heard it here first, folks: TMTOWTDI has been deprecated. The 
Official Perl Motto is now "Uri's Way or the Highway." :-)

Okay, seriously - regex solutions had already been suggested, so where's 
the harm in presenting another alternative? The OP might want to compare 
them to see which is better suited to his situation and/or coding style.

> nor did you tell the OP where to learn about regexes or whatever.

Get off your high horse Uri - your own reply wasn't quite the most 
useful comment ever posted here, either.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


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

Date: Tue, 4 Jan 2005 07:10:51 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Program optimisation advice for newbie
Message-Id: <slrnctl5er.38j.tadmc@magna.augustmail.com>

ChocMonk <wolf_gore@yahoo.com.au> wrote:
> Alan Mead wrote:
>> Star date: Mon, 03 Jan 2005 19:55:33 -0800, ChocMonk's log:


>> my %names=();
>> do {
>>   $cset =~ /$(.+?)\@\@(.+?)$/gm;
>>   $names{$1} = $2 if (defined($1));
>> } until (!defined($1));


> I tried your regexp method and the match works great except that the
> loop ends after the first match and $cset remains unmodified. 


What part of the code were you expecting _would_ change $cset?

It is *supposed to be* unchanged, given that code.


> I changed
> the regexp slightly, as perl complained about the initial $ in your
> regexp:


I expect it was supposed to be the beginning-anchor (^) rather
than the ending-anchor ($).

I'm afraid I'd have to characterize Alan's code above as "hokey".

You should *never* use the dollar-digit variables unless you
have first ensured that the match has *succeeded*.

   while ( $cset =~ /(.+)\@\@(.+)/g ) {  # untested also
      $names{$1} = $2;
   }


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


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

Date: Tue, 04 Jan 2005 15:51:21 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Program optimisation advice for newbie
Message-Id: <sf9lt0l2l5msconmqh9pq83dadtv4p5ad2@4ax.com>

On 3 Jan 2005 19:55:33 -0800, "ChocMonk" <wolf_gore@yahoo.com.au>
wrote:

>$descr = "
>asdasdasd@@\main\3
>boohoo@@\main\6
>toodle@@\main\9";

Incidentally rather than adopting a qq// multiline string I'd use an
HERE doc.

>Is this a good way to do it? Any comments on making it more
>efficient/elegant would be much appreciated.

Due to being in a great hurry, I didn't actually read the rest of your
code. For simplicity I'll assume that your input string is actually
coming in from a filhandle (which seems reasonable - if it isn't there
are other WTDI, and you can even "fake" a filehandle):

  my %names = map split /\@\@/, <$fh>;

Of course this is inherently insecure unless you can absolutely rely
on the input's format. If you can't additional checks will be
required. This part is left as an exercise. (If you still want to do
it fundamentally "like the above" you probably want to use the "BLOCK
form" of map().)


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: 4 Jan 2005 07:37:33 -0800
From: "botfood" <botfood@yahoo.com>
Subject: Re: weird failure with Net::FTP
Message-Id: <1104853053.390168.8920@f14g2000cwb.googlegroups.com>

turns out that the *better* way to switch modes is to use the methods:
ascii() and binary() rather than the type()... in fact the type()
method has been removed from the documentation because a LOT of people
did the same thing I did, and used type('binary') rather than
type('I').



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

Date: Tue, 04 Jan 2005 13:48:07 GMT
From: peter@PSDT.com (Peter Scott)
Subject: Re: Why does this work?
Message-Id: <r8xCd.690652$Pl.287323@pd7tw1no>

In article <slrnctjp4d.h3.abigail@alexandra.abigail.nl>,
 Abigail <abigail@abigail.nl> writes:
>Andrew Hamm (ahamm@mail.com) wrote on MMMMCXLIV September MCMXCIII in
><URL:news:33u51vF43nu9uU1@individual.net>:
>%%  Brian McCauley wrote:
>%% >
>%% >    sub foo { 'bar' };
>%% >    print -foo;
>%% >
>%% > The above prints '-bar' not '-foo' and also emits a warning even
>%% > without "use warnings" in effect.
>%%  
>%%  yeah, it says:
>%%  
>%%  Ambiguous use of -foo resolved as -&foo() at ......
[snip]
>I really, really loathe warnings I can't turned
>off.  It's so unperl. *I*, the programmer, and only *I* should be in
>control - and never ever the compiler/language.

You can turn the warning off:

% perl -le 'no warnings; sub foo { "bar" } print -foo'
-bar
%

-- 
Peter Scott
http://www.perldebugged.com/
*** NEW *** http://www.perlmedic.com/


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

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


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