[22694] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4915 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Apr 30 11:06:18 2003

Date: Wed, 30 Apr 2003 08:05:14 -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, 30 Apr 2003     Volume: 10 Number: 4915

Today's topics:
    Re: another beginner question <krahnj@acm.org>
    Re: another beginner question <barryk2@SPAM-KILLER.mts.net>
    Re: Attribute inheritance, accessor methods & namespace (Dave Pointon)
    Re: books from beginner to guru - reading order (Randal L. Schwartz)
    Re: books from beginner to guru - reading order (qanda)
    Re: books from beginner to guru - reading order <goedicke@goedsole.com>
        building perl-5.8.0 on solaris (newbie) (Prashanth)
    Re: complex text/file search. help please!!! <krahnj@acm.org>
    Re: complex text/file search. help please!!! (supportgeek)
    Re: Convert hex to dec (Randal L. Schwartz)
    Re: Data::Dumper::SortKeys  (not working for 2nd level  (Anno Siegel)
        Elegant test for A in B OR B in A? (Sara)
    Re: Elegant test for A in B OR B in A? (Anno Siegel)
    Re: Elegant test for A in B OR B in A? (Hunter Johnson)
    Re: Elegant test for A in B OR B in A? ctcgag@hotmail.com
    Re: Elegant test for A in B OR B in A? <andreas.buehmann@web.de>
    Re: Elegant test for A in B OR B in A? <usenet@tinita.de>
    Re: file search logic <barryk2@SPAM-KILLER.mts.net>
        grep-mediated autocreation: bug or feature? <jill_krugman@yahoo.com>
    Re: grep-mediated autocreation: bug or feature? <uri@stemsystems.com>
        how to do namespace aliasing? <burch@burcarpat.com>
    Re: how to do namespace aliasing? <bart.lateur@pandora.be>
    Re: Is it possible to pass @somethig and $something_els <fxn@hashref.com>
    Re: Is it possible to pass @somethig and $something_els <anthony@movielink.net.au>
    Re: Is it possible to pass @somethig and $something_els <uri@stemsystems.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 30 Apr 2003 08:21:53 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: another beginner question
Message-Id: <3EAF879F.4DF3D5D5@acm.org>

Herb Burnswell wrote:
> 
> Sorry if this is a basic question but I can't seem to find the correct
> syntax.  I would like to do something similar to:
> 
> @names = qw(name1 name2 name3 name4 name5 name6 name7);
> 
> for ( @names[0-6] )

for ( @names[ 0 .. 6 ] )

> {
> 
> do something.....
> 
> }
> 
> Can anyone point me in the correct direction here?  perldoc?  Is there
> a better way to go about doing this?  Any assistance is greatly
> appreciated.

perldoc perlop for the range operator.
perldoc perldata for array slices


John
-- 
use Perl;
program
fulfillment


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

Date: Wed, 30 Apr 2003 08:18:41 -0500
From: Barry Kimelman <barryk2@SPAM-KILLER.mts.net>
Subject: Re: another beginner question
Message-Id: <MPG.19198927f5bba1369897b4@news.mts.net>

[This followup was posted to comp.lang.perl.misc]

In article <3b38898e.0304292028.362013e8@posting.google.com>, Herb 
Burnswell (patyoung13@hotmail.com) says...
> Hi,
> 
> Sorry if this is a basic question but I can't seem to find the correct
> syntax.  I would like to do something similar to:
> 
> 
> 
> @names = qw(name1 name2 name3 name4 name5 name6 name7);
> 
> for ( @names[0-6] )
> {
> 
> do something.....
> 
> }
> 
> 
> Can anyone point me in the correct direction here?  perldoc?  Is there
> a better way to go about doing this?  Any assistance is greatly
> appreciated.
> 
> TIA,
> 
> Herb
> 

foreach $name ( @names ) {
    # do something with $name
}


-- 
---------

Barry Kimelman
Winnipeg, Manitoba, Canada
email : bkimelman@hotmail.com


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

Date: 30 Apr 2003 02:53:29 -0700
From: david.pointon@tesco.net (Dave Pointon)
Subject: Re: Attribute inheritance, accessor methods & namespaces
Message-Id: <80fceb32.0304300153.4d506a7c@posting.google.com>

david.pointon@tesco.net (Dave Pointon) wrote in message news:<80fceb32.0304240220.528a5884@posting.google.com>...
> Hi all,
> 
> I've implemented a version of the _Initialiseable class (Damina
> Conway, OO Perl, pages 173-4) as a module. The design captures
> encapsulates attributes in a class hash variable and requires
> attribute access via methods.
> 
> In search of this (to me:) utopian ideal, I've attempted to
> incorporate an
> auto-accessor method generation method, within _Initialiseable, which
> generates a method for each attribute - this works fine until 2
> disparate
> classes had an attribute name in common. I found that the dynmically
> generated
> accessor methods were created in the _Initialiseable package namespace
> - not
> in the inheriting package. 
> 
> Question is, am I following the right track, or is their a better way
> of
> achieving data encapsulation within an OO framework. If I am on the
> right
> track, does anybody know how to extend the appropriate namespace ?? 
> 
> All/any help greatly appreciated,
> 
> TIA,
> 
> Dave P

Hi all,

Methinks I have the solution to my problem !!

Admittedly not yet tested to destruction, but I re-read the para's regarding
name clashes and decided on a variant of Damians' suggested solution/workround
whereby, rather than an abbreviation, I prefixed the attribute name with the
full package path i.e. the string returned by ref(), ... 

no strict 'refs';
my $_sRef = ref($_self) . "::$_sym";
*$_sRef = { ... }; 
use strict 'refs';

Lo and behold, the symbol appears to be created in the namespace of the owning
class ... result !! :-)) 

Rgds,

DP


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

Date: Wed, 30 Apr 2003 10:03:23 GMT
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: books from beginner to guru - reading order
Message-Id: <cdf900a48a4018975061eb659d92a095@TeraNews>

>>>>> "Andrew" == Andrew Lee <spamtrap@nowhere.com> writes:

Andrew> On 28 Apr 2003 08:23:43 -0700, fumail@freeuk.com (qanda) wrote:
>> Hi all
>> 
>> Would you be able to give me a list of books and the reading order
>> that a programmer would need to learn Perl, from the basics through to
>> advanced topcs.  

Andrew> ALso, don't forget to look at the online tutorials

Andrew> perlretut, perlreftut, perldebtut, perlopentut, perlpacktut, perlboot,
Andrew> perltoot, perbot, perldsc

And don't forget the 165+ columns I've written, all available for
free at http://www.stonehenge.com/merlyn/columns.html.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


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

Date: 30 Apr 2003 06:17:00 -0700
From: fumail@freeuk.com (qanda)
Subject: Re: books from beginner to guru - reading order
Message-Id: <62b4710f.0304300517.356f8fc6@posting.google.com>

Andrew Lee <spamtrap@nowhere.com> wrote in message > 
> ALso, don't forget to look at the online tutorials
> 
> Books are not meant as a replacement for online documentation.  Online
> documentation is not a replacement for books.  They work hand in hand.
> Using perdoc is a good habit to learn at the outset.
> 
Yep, I agree but I'm still confused.  The aim of the post was to get a
list of books and reading order recommended by the experts in the
group.  The list of books would enable existing programmers (junior,
intermediate and senior) to take them from beginner Perl programmers
to advanced.  I have found many sources of information but wanted a
single list agreed by several gurus.

If you're one of the gurus and have time can you put a list together
(or add/remove from the following) and give a recommened reading order
so we end up with a small list of complete books?

Here's a few books I know about aleady...

Learning Perl
Programming Perl
Perl Cookbook
Elements of Programming with Perl
Advanced Perl Programming
The Programmers Companion
Perl Debugged
Efficient Perl Programming

Thanks again.


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

Date: Wed, 30 Apr 2003 14:18:15 GMT
From: William Goedicke <goedicke@goedsole.com>
Subject: Re: books from beginner to guru - reading order
Message-Id: <m3of2ooyqo.fsf@mail.goedsole.com>

Dear qanda - 

> If you're one of the gurus and have time can you put a list together
> (or add/remove from the following) and give a recommened reading
> order so we end up with a small list of complete books?

Well, I'm no guru, but I've read a lot of books (hmmm, maybe I read
them in the wrong order).  Anyway here's my recommended version of
your ordered list.

                                        > Learning Perl
                                        > Perl Cookbook
You didn't have this; it's a must-read  > Object-Oriented Perl
                                        > Advanced Perl Programming

> Programming Perl

I use this book more as reference material so I don't include it in
the reading order.  As to your "purchasing order" get it on the first
trip to the bookstore.  Other key reference books are:

Mastering Algorithms with Perl
Mastering Regular Expressions

There are various special-purpose books that I'd recommend but you
should determine the order yourself based on what you're working on.
Be aware that I have a zealous penchant for O'Reilly books and that's
reflected in my recommendations.  The only exceptions are
Object-Oriented Perl and Win32 PERL Scripting from Manning and New
Riders respectively.  Never waste your time with SAMS or anything "for
Dummies" unless ...

Perl and LWP
Win32 PERL Scripting
Perl and XML
Mastering Perl/Tk
CGI Programming with Perl
Programming Web Services with Perl
Programming the Perl DBI
Writing Apache Modules with Perl and C
Programming Web Services with Perl

> Elements of Programming with Perl
> The Programmers Companion
> Perl Debugged
> Efficient Perl Programming

I'm unfamiliar with those.  I'll leave it to the "gurus" where they
belong in the ordered list.

     Yours -      Billy

============================================================
     William Goedicke     goedicke@goedsole.com            
                          http://www.goedsole.com:8080      
============================================================

          Lest we forget:

Good software development involves analyzing and automating the input
and output of the required business functionality.

		- William Goedicke


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

Date: 30 Apr 2003 03:49:05 -0700
From: npengr@yahoo.com (Prashanth)
Subject: building perl-5.8.0 on solaris (newbie)
Message-Id: <fff5a729.0304300249.2b8093d9@posting.google.com>

Hi,

I'm building perl for the first time. I'm trying to build for Solaris
64bit. It would be great if anybody can help me with the following
error.

"perlio.c", line 2761: undefined struct/union member: _ptr
"perlio.c", line 2769: improper member use: _ptr
"perlio.c", line 2866: undefined struct/union member: _base
"perlio.c", line 2873: undefined struct/union member: _cnt
"perlio.c", line 2873: undefined struct/union member: _ptr
"perlio.c", line 2873: undefined struct/union member: _base
"perlio.c", line 2882: undefined struct/union member: _ptr
"perlio.c", line 2889: undefined struct/union member: _cnt
"perlio.c", line 2898: undefined struct/union member: _ptr
"perlio.c", line 2898: warning: improper pointer/integer combination:
op "="
"perlio.c", line 2918: undefined struct/union member: _cnt

Am I missing something in configuration?

Thanks,
Prashanth.


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

Date: Wed, 30 Apr 2003 08:42:37 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: complex text/file search. help please!!!
Message-Id: <3EAF8C7C.6B4C2807@acm.org>

supportgeek wrote:
> 
> Ok. Here's the problem I've been having, "forever!". Thanks so much to
> anyone who can just point me in the right direction "again!" on this
> one.
> 
> I have a large file full of reports. Each report is separated by a
> form feed \f character. Here is what I need to do:
> 
> I need to search the file for the the lines that match /^7002/ and if
> there is a match I need to print those lines and their matching
> account number. If there is not match of /^7002/, I do not want to
> pull any data from that record. Below is an example of the file, and
> then an example of the data I need to pull.
> 
> [snip data]
> 
> As you can see I do not want to bother with the second record because
> it has no line line that starts with ^7002. I Would prefer to pull
> just the account number, but the whole line will do.
> 
> Thank You in advance for any help on this. If you need any more info
> from me please let me know.

Something like this should work:

$/ = "\f";
while ( <> ) {
    my ( $account_no ) = /(\b\d{3}-\d{7,8}-\d{3}\b)/
    print "$account_no\n" if /^7002\b/m;
    }

If you DO want the whole line as well:

$/ = "\f";
while ( <> ) {
    my ( $account_no ) = /(\b\d{3}-\d{7,8}-\d{3}\b)/
    if ( /^(7002\b.*)/m ) {
        print "$account_no $1\n";
        }
    }


John
-- 
use Perl;
program
fulfillment


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

Date: 30 Apr 2003 06:18:09 -0700
From: molivier@caregroup.harvard.edu (supportgeek)
Subject: Re: complex text/file search. help please!!!
Message-Id: <da063e5d.0304300518.6bd29980@posting.google.com>

> 
> this should get you started:
> 
> open (D, "data") or die ("Can't open data: $!");
> local $/ = "\f";
> while (<D>) {
>   if (/^7002/m) {
>     my @lines = split /\n/;
>     print shift @lines, "\n";
>     foreach (@lines) {
>       print "$_\n" if /^7002/;
>     }
>   }
> }
> 
> hth-

Michael. 

That code works great. It's just what I need to get started. Thanks a
million.
If you get a chance, would you mind explaining to me how it works. I
feel guilty using it w/o fully understanding it!

I looked up $/ the input record separator, but I'm not sure how your
getting it to print out the first line, of the record (which is the
line that I need). Is that just how the $/ works. Also would it be
possible for me to print the entire record if I fine the /^7002/ match
in it. Thanks again.


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

Date: Wed, 30 Apr 2003 10:03:26 GMT
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Convert hex to dec
Message-Id: <6bc7c0c33d08f1032a844c8816efd816@TeraNews>

>>>>> "Tony" == Tony Curtis <tony_curtis32@yahoo.com> writes:

>> Hi, How can I convert a heximal number into a decimal

Tony>                           (hexadecimal)

>> number, is there a function available or do I have to
>> calculate this.

Tony> s/number/numeral/g

Tony> "perldoc -f hex"

Of course, that only converts it from hex to "a number".  You convert
that number to decimal by "print". :)

That's a common mis-speaking that I think messes up some thinking.
Perl isn't dealing with decimal when it calculates.  It's working with
numbers that aren't in any base (although the underlying architecture
is likely dealing with those as binary values).  The I/O routines read
and print those numbers as decimal for our convenience.

So hex($string) doesn't convert hex to decimal, any more than
sprintf("%x", $number) converts from decimal to hex.  You must
s/decimal/number/g there.

print "Just another Perl hacker,"

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


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

Date: 30 Apr 2003 09:18:57 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Data::Dumper::SortKeys  (not working for 2nd level sort)
Message-Id: <b8o4e1$egk$2@mamenchi.zrz.TU-Berlin.DE>

TruthXayer  <TruthXayer@yahoo.com> wrote in comp.lang.perl.misc:
> This worked for the simple testcase(sor by value,key) on
> first level hash. But on expansion it fails to sort by
> second level 
> value.

So what do you see?  For me (perl 5.8.0, DD 2.12) the code below
dumps the hash in descending order of the ratio values: B, A, C.

Anno

> 
> 
>  #!/perl -w
>  use strict;
>  use Data::Dumper;
>  
>          $Data::Dumper::Sortkeys = \&my_filter2;
>  
>          my %hash = (
>                          "A" => {
>                                    ratio => 34
>                                  },
>                          "B" => {
>                                    ratio => 75
>                                  },
>                          "C" => {
>                                    ratio => 21
>                                  },
>                     );
>  
>          print Dumper ( \%hash );
>  
>          sub my_filter2 {
>               my ($hash) = shift;
>               # return an array ref containing the hash keys
> to dump
>               # in the order that you want them to be dumped
>  
>               return [   sort { $hash->{$b}->{ratio} <=>
>  			$hash->{$a}->{ratio} } (keys  %$hash) ];
>           }


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

Date: 30 Apr 2003 06:08:00 -0700
From: genericax@hotmail.com (Sara)
Subject: Elegant test for A in B OR B in A?
Message-Id: <776e0325.0304300508.28c4fba7@posting.google.com>

Good Morn Perlistas:

I have an array of names and a scalar. Basically I want to know if the
scalar is in the array of names, but unfortunately, either the scalar
or the array elements can be abreviated.

 SO:
# this is match type 1
  $x = 'cat';
  @a = qw(catbird mouse eel);

#this is match type 2
  $x = 'catbird';
  @a = qw(cat mouse eel);

My solution (hold down laughter please) was this:

      $match = 0;
      $x = 'cat';
      @names = qw(catbird mouse eel);

# match type 1
      $match = grep /^$x/, @names; 

# match type 2 if 1 failed
      unless ($match)
	{for (@names)
	   {$match = 1 if $name =~ /^$_/;}


It seems to work but it's none too pretty, and its too pretty a
morning here to be looking at such ugly code! Is there a way to do
this in one step?


Cheers,
Gx


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

Date: 30 Apr 2003 14:11:07 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Elegant test for A in B OR B in A?
Message-Id: <b8olhr$7h$1@mamenchi.zrz.TU-Berlin.DE>

Sara <genericax@hotmail.com> wrote in comp.lang.perl.misc:
> Good Morn Perlistas:
> 
> I have an array of names and a scalar. Basically I want to know if the
> scalar is in the array of names, but unfortunately, either the scalar
> or the array elements can be abreviated.
> 
>  SO:
> # this is match type 1
>   $x = 'cat';
>   @a = qw(catbird mouse eel);
> 
> #this is match type 2
>   $x = 'catbird';
>   @a = qw(cat mouse eel);
> 
> My solution (hold down laughter please) was this:
> 
>       $match = 0;
>       $x = 'cat';
>       @names = qw(catbird mouse eel);
> 
> # match type 1
>       $match = grep /^$x/, @names; 
> 
> # match type 2 if 1 failed
>       unless ($match)
> 	{for (@names)
> 	   {$match = 1 if $name =~ /^$_/;}
> 
> 
> It seems to work but it's none too pretty, and its too pretty a
> morning here to be looking at such ugly code! Is there a way to do
> this in one step?

I don't think so.  The problem of finding abbreviations either way
comes up occasionally, but no strikingly elegant solution has been
proposed.

One technique is to find the longest common prefix of all pairs
and see if it covers all of one of the candidates (length comparison).
If so, we have a match.  The common prefix can be found by XOR-ing
the strings and seeing how many null characters appear at the beginning
of the result.

    sub match_either_way {
        my ( $x, @a) = @_;
        my $match = 0;
        for ( @a ) {
            ( $x ^ $_) =~ /^\0*/;
            $match = ( $+[ 0] == length $x or $+[ 0] == length $_);
            last if $match;
        }
        $match;
    }


With "@names = qw(catbird mouse eel)" we get

    match_either_way( 'cat', @names)        # true
    match_either_way( 'cati', @names)       # false
    match_either_way( 'eelephant', @names); # true

Anno


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

Date: 30 Apr 2003 14:22:27 GMT
From: jhunterj@lexis-nexis.com (Hunter Johnson)
Subject: Re: Elegant test for A in B OR B in A?
Message-Id: <b8om73$9ir$1@mailgate2.lexis-nexis.com>

In article <776e0325.0304300508.28c4fba7@posting.google.com>,
Sara <genericax@hotmail.com> wrote:
> Good Morn Perlistas:

> I have an array of names and a scalar. Basically I want to know if
> the scalar is in the array of names, but unfortunately, either the
> scalar or the array elements can be abreviated.

>  SO:
> # this is match type 1
>   $x = 'cat';
>   @a = qw(catbird mouse eel);
> 
> #this is match type 2
>   $x = 'catbird';
>   @a = qw(cat mouse eel);
> 
> My solution (hold down laughter please) was this:
> 
>       $match = 0;
>       $x = 'cat';
>       @names = qw(catbird mouse eel);
> 
> # match type 1
>       $match = grep /^$x/, @names; 
> 
> # match type 2 if 1 failed
>       unless ($match)
> 	{for (@names)
> 	   {$match = 1 if $name =~ /^$_/;}
> 

> It seems to work but it's none too pretty, and its too pretty a
> morning here to be looking at such ugly code! Is there a way to do
> this in one step?

$match = (grep /^$x/, @names) || ($x =~ /^(${\join('|', @names)})/);

I'm not claiming it's any prettier, but it is one step.

If you don't mind building the regexp ahead of time, you could instead use

$regexp = '^(' . join('|', @names) . ')';
$match = ($regexp =~ /\b$x/) || ($x =~ /$regexp/);

if all of your names are made up solely of word characters
(alphanumerics).  That strikes me as prettier.

Hunter
--
J. Hunter Johnson        | "A little consistent wholesome modeling and
jhunterj@lexisnexis.com  |   costly servanthood are worth millions of
(937) 865-6800 x55385    |    true words harshly spoken."
Lexis-Nexis, Dayton, OH  |                               -- Ron Sider


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

Date: 30 Apr 2003 14:33:21 GMT
From: ctcgag@hotmail.com
Subject: Re: Elegant test for A in B OR B in A?
Message-Id: <20030430103321.494$xY@newsreader.com>

genericax@hotmail.com (Sara) wrote:
>
> It seems to work but it's none too pretty, and its too pretty a
> morning here to be looking at such ugly code! Is there a way to do
> this in one step?

Yes.  Make a subroutine that does it. Stuff the subroutine into a
module of useful but too ugly to comtemplate code, then forget about
the code and just remember the beautiful subroutine call.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service              New Rate! $9.95/Month 50GB


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

Date: Wed, 30 Apr 2003 14:40:11 +0000 (UTC)
From: Andreas =?iso-8859-15?Q?B=FChmann?= <andreas.buehmann@web.de>
Subject: Re: Elegant test for A in B OR B in A?
Message-Id: <slrnbavnt5.6pb.andreas.buehmann@forelle284.wohnheim.uni-kl.de>

Sara wrote:
> I have an array of names and a scalar. Basically I want to know if the
> scalar is in the array of names, but unfortunately, either the scalar
> or the array elements can be abreviated.
> 
>  SO:
> # this is match type 1
>   $x = 'cat';
>   @a = qw(catbird mouse eel);
> 
> #this is match type 2
>   $x = 'catbird';
>   @a = qw(cat mouse eel);
> 
> My solution (hold down laughter please) was this:
> 
>       $match = 0;
>       $x = 'cat';
>       @names = qw(catbird mouse eel);
> 
> # match type 1
>       $match = grep /^$x/, @names; 
> 
> # match type 2 if 1 failed
>       unless ($match)
> 	{for (@names)
> 	   {$match = 1 if $name =~ /^$_/;}
> 
> 
> It seems to work but it's none too pretty, and its too pretty a
> morning here to be looking at such ugly code! Is there a way to do
> this in one step?

You could use the following if you don't care much about performance.
It's a combination of your solution for match type 1 and the subject. :-)

  $match = grep {/^\Q$x/ or $x =~ /^\Q$_/} @names;

Andreas


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

Date: 30 Apr 2003 15:02:58 GMT
From: Tina Mueller <usenet@tinita.de>
Subject: Re: Elegant test for A in B OR B in A?
Message-Id: <b8ooj2$bv76v$1@ID-24002.news.dfncis.de>

Sara <genericax@hotmail.com> wrote:

> I have an array of names and a scalar. Basically I want to know if the
> scalar is in the array of names, but unfortunately, either the scalar
> or the array elements can be abreviated.

for (@array) {
 print unless index($x,$_) && index ($_,$x)
}

hth, tina
-- 
http://www.tinita.de/     \  enter__| |__the___ _ _ ___
http://Movies.tinita.de/   \     / _` / _ \/ _ \ '_(_-< of
http://www.perlquotes.de/   \    \ _,_\ __/\ __/_| /__/ perception
http://www.tinita.de/peace/link.html - Spread Peace


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

Date: Wed, 30 Apr 2003 08:16:39 -0500
From: Barry Kimelman <barryk2@SPAM-KILLER.mts.net>
Subject: Re: file search logic
Message-Id: <MPG.191988b52a0e8c039897b3@news.mts.net>

[This followup was posted to comp.lang.perl.misc]

In article <3EAEA617.8A6B7CFE@miel.mot.com>, P S Mahesh 
(psmahesh@miel.mot.com) says...
> Hi Folks,
> 
> I have a file which contains a certain pattern "get_clear_data" and
> some lines in between them.
> 
> get_clear_data  0 0 0
> -------
> --
> get_clear_data 0 1 0
> --
> 
> I need to search  in between this two occurences and find if a
> string"XXX" is present and if it is present. I need to print the
> previous get_clear_data pattern.  I tried a couple of logics, could not
> get a head start. Can anyone suggest a proper logic.
> 
> thanks,
> mahesh.

while ( $buffer = <INPUT> ) {
    if ( $buffer =~ m/get_clear_data 0 0 0/ ) {
        last;
    }
}

while ( $buffer = <INPUT> ) {
    if ( $buffer =~ m/get_clear_data 0 1 0/ ) {
        last;
    }
    if ( $buffer =~ m/XXX/ ) {
        print $buffer;
    }
}


-- 
---------

Barry Kimelman
Winnipeg, Manitoba, Canada
email : bkimelman@hotmail.com


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

Date: Wed, 30 Apr 2003 13:38:51 +0000 (UTC)
From: J Krugman <jill_krugman@yahoo.com>
Subject: grep-mediated autocreation: bug or feature?
Message-Id: <b8ojlb$qq5$1@reader1.panix.com>




  DB<1> $x = $s{'a'}

  DB<2> x \%s
0  HASH(0x8387220)
     empty hash
  DB<3> @x = @s{qw(a b c)}

  DB<4> x \%s
0  HASH(0x8387220)
     empty hash
  DB<5> @x = grep $_, @s{qw(a b c)}

  DB<6> x \%s
0  HASH(0x8387220)
   'a' => undef
   'b' => undef
   'c' => undef


Bug or feature?



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

Date: Wed, 30 Apr 2003 14:45:04 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: grep-mediated autocreation: bug or feature?
Message-Id: <x7n0i8xcwg.fsf@mail.sysarch.com>

>>>>> "JK" == J Krugman <jill_krugman@yahoo.com> writes:

  JK>   DB<1> $x = $s{'a'}

  JK>   DB<2> x \%s
  JK> 0  HASH(0x8387220)
  JK>      empty hash
  JK>   DB<3> @x = @s{qw(a b c)}

those elements don't exist so they return undef. the values are not
autovivified as they are not ever accessed. 

  JK>   DB<4> x \%s
  JK> 0  HASH(0x8387220)
  JK>      empty hash
  JK>   DB<5> @x = grep $_, @s{qw(a b c)}

$_ is aliased to the values of those hash keys. so they must have an
actual value and so are autovivified to undef. that means the keys will
be created too.

  JK>   DB<6> x \%s
  JK> 0  HASH(0x8387220)
  JK>    'a' => undef
  JK>    'b' => undef
  JK>    'c' => undef

  JK> Bug or feature?

it's a feature and an interesting example of autovivification. for more
on this read my tutorial at:

	http://www.sysarch.com/perl/tutorials/autoviv.txt

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: Wed, 30 Apr 2003 04:35:23 -0700
From: "Burc Arpat" <burch@burcarpat.com>
Subject: how to do namespace aliasing?
Message-Id: <b8ocdt$h77$1@news.Stanford.EDU>

hi,

might be kind of a newbie question; sorry if this was too obvious...

how can i create a c++ namespace alias-like thingie in perl?  let's say i
have a package called "Some::Module".  but, i also want to access this
package via a different name, say "Another".  so, basically, if i have a
function in Some::Module named Some::Module::MyFunction, i also want to
access this function via Another::MyFunction.  is this possible? ( actually,
i want to access the function using Module::MyFunction, i.e. if the user
desires to do so, i want to promote the inner namespace... )

i found one package called "namespace" by albert micheev but for some reason
that doesn't work...

thanks in advance!

burch




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

Date: Wed, 30 Apr 2003 12:51:00 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: how to do namespace aliasing?
Message-Id: <logvavcu2vjkpilfkjidmge7pip6pjlm3m@4ax.com>

Burc Arpat wrote:

>might be kind of a newbie question; sorry if this was too obvious...

Trust me, it's not.

>how can i create a c++ namespace alias-like thingie in perl?  let's say i
>have a package called "Some::Module".  but, i also want to access this
>package via a different name, say "Another".  so, basically, if i have a
>function in Some::Module named Some::Module::MyFunction, i also want to
>access this function via Another::MyFunction.  is this possible?

The general idea is called "importing". Perl provides a way to have it
done automatically, for example using the module Exporter. See the docs
for those. Basically, you require() or use() Exporter in Some::Module,
and add it to its @ISA, so you can inherit import() from it. Next, also
set the (package) variables (no "my"!) @EXPORT and @EXPORT_OK to your
liking... and when you "use" that module, the stuff you specified will
be imported in the package you "used" it from. Like:

	package Some::Module;
	use vars qw(@ISA @EXPORT @EXPORT_OK);
	use Exporter;
	@ISA = qw(Exporter);
	@EXPORT = qw(func1);
	@EXPORT_OK = qw(func2);

	sub func1 {
	    print "This is func1\n";
	}

	sub func2 {
	    print "This is func2\n";
	}


You call it from your main script as

	use Some::Module;
	func1;

or

	use Some::Module qw(func2);
	func2;

Note that specifying a list to the use() call prevents the default
export mechanism. Thus, in the second case, func1() is not available as
before, by default.

n.b. For this to work, Some::Module must be in its own proper module
file, Some/Module.pm, i.e. the file "Module.pm" in a subdirectory "Some"
to a root dir in @INC.


If you don't mind a pure manual way of doing it, try:

	*Another::MyFunction = \&Some::Module::MyFunction;

That's the concept Exporter is built upon, anyway (plus it's using
symbolic references). You do need the parens for the function call now,
because the import now happens at runtime, and it's too late for the
compiler to see it. If you'd wrap the above in a BEGIN block, it'd start
working again.

>( actually,
>i want to access the function using Module::MyFunction, i.e. if the user
>desires to do so, i want to promote the inner namespace... )

That's not the Perl way. There's no relation between Module:: and
Some::Module::. Both are top level namespaces, er, package names. Perl
has nothing but top level packages, despite what it might look like.

-- 
	Bart.


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

Date: Wed, 30 Apr 2003 06:16:42 +0000 (UTC)
From: Xavier Noria <fxn@hashref.com>
Subject: Re: Is it possible to pass @somethig and $something_else to sub
Message-Id: <b8npoa$aj$1@news.ya.com>

In article <pan.2003.04.30.05.41.33.793363@movielink.net.au>, Tony wrote:

: my @list = ("something","something_else","something_more",...could go on")
: my $string = $string_from_other_part_of_program;
: 
: I wish to send the $string together with @list to a sub process
: like this:
: 
: process($string,@list);
: 
: sub process {
: 
:   my $local_string = $_[0];
:   my @local_list = @_;
:   ...
:   Process $local_list in various ways depending on value of $local_string 
: }
: 
: This does not seem to do what I want, the docs say something
: about everything being "flattened" when passing to a sub.

What happens is that the array is flattened so that if @list had, say, 7
elements, and you called process() this way

    process($string, @list);
    
inside process @_ would have length 8. The first element would be $string,
and the following elements would be the ones in @list. You get everything
ordered, but in a unique array.

Common idioms for that situation are

    sub process {
        my ($string, @list) = @_;
	# ...
    }
    
or

    sub process {
        my $string = shift;
	my @list   = @_;
	# ...
    }
    
See how it works?

-- fxn
    
PS BTW @list is not a list, but an array. @array would be more appropriate.
   See perldoc -q 'What is the difference between a list and an array?'.


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

Date: Wed, 30 Apr 2003 20:07:19 +1000
From: "Tony" <anthony@movielink.net.au>
Subject: Re: Is it possible to pass @somethig and $something_else to sub
Message-Id: <pan.2003.04.30.10.07.18.476507@movielink.net.au>


On Wed, 30 Apr 2003 02:04:04 -0400, Rich wrote:

> Hi,
> 
> Well... there are a few ways...
> 

Thanks to all who replied,

This one was the one that worked out best:-)

my @list = ("something","$something_else");
my $string = "a_string";

process( $string,\@list);

sub process
	{
	my ( $str, $a_ref ) = @_;
	my @Array = @{$a_ref};
	print "$str @Array\n;
	}

For some reason the print function (To let me see things)
showed the @Array list without the quotes so I thaught it was cactus...
when passing things on after processing, all turned out fine:-)

Tony


-- 
--------------------------------------------------------------
To reply directly send to: anthony AT movielink DOT net DOT au
Replace AT and DOT with @ and . and mail will get through.
Any spammers will be persued until they get booted off the net



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

Date: Wed, 30 Apr 2003 14:40:39 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Is it possible to pass @somethig and $something_else to sub
Message-Id: <x7ptn4xd3s.fsf@mail.sysarch.com>

>>>>> "T" == Tony  <anthony@movielink.net.au> writes:

  T> This one was the one that worked out best:-)

  T> my @list = ("something","$something_else");
  T> my $string = "a_string";

  T> process( $string,\@list);

  T> sub process
  T> 	{
  T> 	my ( $str, $a_ref ) = @_;
  T> 	my @Array = @{$a_ref};

why copy the array if all you want is to print it? you can just access
it in the print like you do here.

  T> 	print "$str @Array\n;

	print "$str @{$a_ref}\n;

  T> For some reason the print function (To let me see things)
  T> showed the @Array list without the quotes so I thaught it was cactus...

what quotes? print never adds quote so why are you expecting them?

  T> when passing things on after processing, all turned out fine:-)

again, don't do the extra copy as there is no reason (unless you modify
the local copy of the array). it will be a bad habit to get into.

also learn the other standard way of passing scalars and a single
array. you just put the array at the end of the asignment. this is a
very common idiom and you must know it.

	sub foo {
		my( $scalar, @array ) = @_ ;

the way you chose is not better or worse, but different. it is a pass by
reference and this is a pass by value. those are standard computer
science terms that are well known.

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: 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.  

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


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