[31288] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2533 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Aug 2 18:09:44 2009

Date: Sun, 2 Aug 2009 15:09:08 -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           Sun, 2 Aug 2009     Volume: 11 Number: 2533

Today's topics:
    Re: FAQ 7.29 How can I use a variable as a variable nam <kst-u@mib.org>
    Re: FAQ 7.29 How can I use a variable as a variable nam <uri@stemsystems.com>
        open file, delete 4 lines if they meet criteria <fgnowfg@gmail.com>
    Re: open file, delete 4 lines if they meet criteria <ben@morrow.me.uk>
    Re: open file, delete 4 lines if they meet criteria <fgnowfg@gmail.com>
    Re: open file, delete 4 lines if they meet criteria <tadmc@seesig.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 02 Aug 2009 12:47:34 -0700
From: Keith Thompson <kst-u@mib.org>
Subject: Re: FAQ 7.29 How can I use a variable as a variable name?
Message-Id: <ln8wi2q9pl.fsf@nuthaus.mib.org>

PerlFAQ Server <brian@stonehenge.com> writes:
[..]
> 7.29: How can I use a variable as a variable name?
>
>     Beginners often think they want to have a variable contain the name of a
>     variable.
>
>             $fred    = 23;
>             $varname = "fred";
>             ++$$varname;         # $fred now 24
>
>     This works *sometimes*, but it is a very bad idea for two reasons.
>
>     The first reason is that this technique *only works on global
>     variables*.
[snip]

This FAQ should probably mention that you *can* have a variable
containing a variable name, if you use eval, like this:
========================================
#!/usr/bin/perl

use strict;
use warnings;

my $foo="hello";
my $variable_name = 'foo';

print eval "\$$variable_name", "\n";
========================================

or even like this:
========================================
#!/usr/bin/perl

use strict;
use warnings;

my $foo="hello";
my $variable_name = '$foo';

print eval($variable_name), "\n";
========================================

It should also mention that it's almost always a bad idea.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


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

Date: Sun, 02 Aug 2009 16:38:38 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: FAQ 7.29 How can I use a variable as a variable name?
Message-Id: <878wi2rlwx.fsf@quad.sysarch.com>

>>>>> "KT" == Keith Thompson <kst-u@mib.org> writes:

  KT> It should also mention that it's almost always a bad idea.

agreed. my edit which could be put in is this:

Using a string for a variable name (via symbolic references or eval)
means you are using the symbol table (which is a specialized hash tree)
for a data structure. This has many flaws including action at a
distance, the inability to isolate your data and it is slower. Instead
you should just use your own data structures composed of hashes and
hash/array references. Read perllol and perldsc for more on how to do
this. The symbol table should only be modified if you are actually
manipulating symbols and not just your data.

(that can be improved upon i am sure. but the gist is pretty good IMO).

and yes, i know that eval can even access lexicals with strings for
names but that is even nastier than accessing globals!

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Sat, 1 Aug 2009 09:43:33 -0700 (PDT)
From: Faith Greenwood <fgnowfg@gmail.com>
Subject: open file, delete 4 lines if they meet criteria
Message-Id: <fe3ddf44-c8e4-4048-a77d-5e3df11c169f@24g2000yqm.googlegroups.com>

OK, I know how to open a file and delete a specific line, but how can
I delete a section of lines? for instance, I have:

here's the contents of my <FILE>:

the red fox
and the brown fox
quickly and quietly
jumped over
the fence
but for some reason
the fence was
too high
I guess they
will have to try and try again
the farmer saw the goat
and the goat was happy

--end of file

I want my ending file to be as follows:

the red fox
and the brown fox
quickly and quietly
jumped over
the fence
the farmer saw the goat
and the goat was happy

The part I will be deleting will obviously be:
but for some reason
the fence was
too high
I guess they
will have to try and try again

and will alway start with "but for some reason" and end with "will
have to try and try again" but the middle contents will change....how
can I do this in perl?


I have this:

while (<FILE>){
      next if /but for some reason/;
      last if /will have to try and try again/;
print $_;

}

but this gives me:

the red fox
and the brown fox
quickly and quietly
jumped over
the fence
the fence was
too high
I guess they


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

Date: Sat, 1 Aug 2009 17:56:41 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: open file, delete 4 lines if they meet criteria
Message-Id: <9n8ek6-0ln2.ln1@osiris.mauzo.dyndns.org>


Quoth Faith Greenwood <fgnowfg@gmail.com>:
> OK, I know how to open a file and delete a specific line, but how can
> I delete a section of lines? for instance, I have:

You want the .. operator in scalar context.

<snip>
> The part I will be deleting will obviously be:
> but for some reason
> the fence was
> too high
> I guess they
> will have to try and try again
> 
> and will alway start with "but for some reason" and end with "will
> have to try and try again" but the middle contents will change....how
> can I do this in perl?

    perl -ne'print unless /but for/../will have to/'

or the equivalent while loop.

Ben



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

Date: Sat, 1 Aug 2009 10:51:15 -0700 (PDT)
From: Faith Greenwood <fgnowfg@gmail.com>
Subject: Re: open file, delete 4 lines if they meet criteria
Message-Id: <c2856868-da05-41e8-a612-db154b20a7fc@r38g2000yqn.googlegroups.com>

On Aug 1, 12:56=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Faith Greenwood <fgno...@gmail.com>:
>
> > OK, I know how to open a file and delete a specific line, but how can
> > I delete a section of lines? for instance, I have:
>
> You want the .. operator in scalar context.
>
> <snip>
>
> > The part I will be deleting will obviously be:
> > but for some reason
> > the fence was
> > too high
> > I guess they
> > will have to try and try again
>
> > and will alway start with "but for some reason" and end with "will
> > have to try and try again" but the middle contents will change....how
> > can I do this in perl?
>
> =A0 =A0 perl -ne'print unless /but for/../will have to/'
>
> or the equivalent while loop.
>
> Ben

dude, that is too easy! now that I see it I feel ashamed for even
asking. thx!
-Faith


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

Date: Sat, 01 Aug 2009 18:49:00 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: open file, delete 4 lines if they meet criteria
Message-Id: <slrnh79kma.8vl.tadmc@tadmc30.sbcglobal.net>

Faith Greenwood <fgnowfg@gmail.com> wrote:
> On Aug 1, 12:56 pm, Ben Morrow <b...@morrow.me.uk> wrote:
>> Quoth Faith Greenwood <fgno...@gmail.com>:
>>
>> > how can
     ^^^ ^^^
>> > I delete a section of lines?
     ^                     ^^^^^

    perldoc -q how

        How can I pull out lines between two patterns that are 
        themselves on different lines?

    perldoc -q can

        How can I pull out lines between two patterns that are 
        themselves on different lines?

    perldoc -q I

        How can I pull out lines between two patterns that are 
        themselves on different lines?

    perldoc -q lines

        How can I pull out lines between two patterns that are 
        themselves on different lines?

>>     perl -ne'print unless /but for/../will have to/'


> I feel ashamed for even
> asking.


That is appropriate.

You should always try searching the FAQ on a subject before doing 
your part to ensure that the Question remains Frequently Asked...


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

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 V11 Issue 2533
***************************************


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