[24157] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6351 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Apr 1 09:10:41 2004

Date: Thu, 1 Apr 2004 06:10:13 -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           Thu, 1 Apr 2004     Volume: 10 Number: 6351

Today's topics:
        Question for List of hash.... <letnomanenterhere@yahoo.co.kr>
    Re: Question for List of hash.... <tore@aursand.no>
    Re: Question for List of hash.... <kuujinbo@hotmail.com>
    Re: read a paragraph via scrit? (Anno Siegel)
        splitting string with unknown number of similar substri <jan.biel@tu-clausthal.de>
    Re: splitting string with unknown number of similar sub <bernard.el-haginDODGE_THIS@lido-tech.net>
    Re: splitting string with unknown number of similar sub <jan.biel@tu-clausthal.de>
    Re: splitting string with unknown number of similar sub <bernard.el-haginDODGE_THIS@lido-tech.net>
    Re: TCP Connection redirector with password? <Joe.Smith@inwap.com>
        test <letnomanenterhere@yahoo.co.kr>
    Re: test <tadmc@augustmail.com>
    Re: Tough (for me) regex case <kuujinbo@hotmail.com>
    Re: trapping errors <simalt@totalise.co.uk>
    Re: trapping errors <simalt@totalise.co.uk>
    Re: trapping errors <tadmc@augustmail.com>
    Re: trapping errors <tadmc@augustmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 1 Apr 2004 14:36:55 +0900
From: "CatcherInTheRye" <letnomanenterhere@yahoo.co.kr>
Subject: Question for List of hash....
Message-Id: <c4g9pl$b63@netnews.proxy.lucent.com>

hi all,

I am trying to get "cell"and "sector" having  the same pn value.
Temporarily, I make the following script. but don't work..
How do I do to make it works.

Thanks in advance,



#!/usr/bin/perl

@mscfci = (
    { cell   => '3',
      sector => '1',
      pn     => '200' },

    { cell   => '4',
      sector => '2',
      pn     => '300' },

    { cell   => '8',
      sector => '2',
      pn     => '300' },

    { cell   => '9',
      sector => '2',
      pn     => '110' },

    { cell   => '12',
      sector => '3',
      pn     => '300' },

);

foreach $fci ( @mscfci ) {

   my @pns = @{ $fci->{pn} };

   if (@pns > 1) {
      foreach $pn (@pns) {
         print $fci->{pn},"\n";
      }
   }
}




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

Date: Thu, 01 Apr 2004 10:28:47 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Question for List of hash....
Message-Id: <pan.2004.04.01.08.28.01.489896@aursand.no>

On Thu, 01 Apr 2004 14:36:55 +0900, CatcherInTheRye wrote:
> I am trying to get "cell"and "sector" having  the same pn value.

How come you ended up with thw wrong data?

> Temporarily, I make the following script. but don't work..

You should tell us _why_ it doesn't work (ie. any error messages), and/or
_what_ goes wrong.

> #!/usr/bin/perl

Should be:

  #!/usr/bin/perl
  #
  use strict;
  use warnings;

> @mscfci = (

Should be:

  my @mscfci = (

> foreach $fci ( @mscfci ) {
>    my @pns = @{ $fci->{pn} };

The value which '$fci->{pn}' refers to isn't an array.  It's just a
ordinary scalar.

>    if (@pns > 1) {
>       foreach $pn (@pns) {
>          print $fci->{pn},"\n";
>       }
>    }
> }

It looks to me that you're just trying to list all the 'pn' values.  That
could easily be done this way;

  foreach my $fci ( @mscfci ) {
      print $fci->{pn} . "\n";
  }

The code above will list the 'pn' value for each (hash) entry in the
'@mscfci' array.

I think you need to clarify for us what you _really_ want to do.  English
isn't my primary language, so I could use that as an excuse for why I
didn't understand your problem. :)


-- 
Tore Aursand <tore@aursand.no>
"I didn't have time to write a short letter, so I wrote a long one
 instead." -- Mark Twain


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

Date: Thu, 01 Apr 2004 19:23:52 +0900
From: ko <kuujinbo@hotmail.com>
Subject: Re: Question for List of hash....
Message-Id: <c4gqjq$2hc711$1@ID-227975.news.uni-berlin.de>

CatcherInTheRye wrote:
> hi all,
> 
> I am trying to get "cell"and "sector" having  the same pn value.
> Temporarily, I make the following script. but don't work..
> How do I do to make it works.
> 
> Thanks in advance,
> 
> #!/usr/bin/perl
> 
> @mscfci = (
>     { cell   => '3',
>       sector => '1',
>       pn     => '200' },
> 
>     { cell   => '4',
>       sector => '2',
>       pn     => '300' },
> 
>     { cell   => '8',
>       sector => '2',
>       pn     => '300' },
> 
>     { cell   => '9',
>       sector => '2',
>       pn     => '110' },
> 
>     { cell   => '12',
>       sector => '3',
>       pn     => '300' },
> 
> );

As has already been pointed out, if you had were using 'strict' and 
'warnings', you would have received a number of useful error/diagnostic 
messages.

[snip code]

One way to do what you want:

#!/usr/bin/perl
use strict;
use warnings;

my %summary;

foreach my $pn ( @mscfci ) {
   push @{ $summary{ $pn->{pn} }->{cell} },   $pn->{cell};
   push @{ $summary{ $pn->{pn} }->{sector} }, $pn->{sector};
}

while ( my $key = each %summary ) {
   print <<_PN_;
     PN: $key
   CELL: @{ $summary{$key}->{cell} }
SECTOR: @{ $summary{$key}->{sector} }

_PN_
}

If you need to order the PN values, use a 'foreach' and sort routine 
while iterating over the keys of %summary. And you'll also have to add 
another condition if you don't want duplicate CELL and SECTOR values.

If you don't understand everything, 'perldoc perldsc' from your shell 
for the relevant documentation.

HTH - keith


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

Date: 1 Apr 2004 13:25:02 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: read a paragraph via scrit?
Message-Id: <c4h57e$6eb$1@mamenchi.zrz.TU-Berlin.DE>

NNTP <news8080@yahoo.com> wrote in comp.lang.perl.misc:
> I'd lile to read the following file and output everything from 
> 'NetBIOS Name Table for Host 192.168.5.10' to the first occurance of 
> "----------------------------------------"
> 
> can perl do that? here is the file (long file but only the portion of
> it is shown here)
> 
> 
> 
> 
> ----------------------------------------
> data
> data
> ----------------------------------------
> NetBIOS Name Table for Host 192.168.5.11
> lines1
> line2
> 
> Adapter address: 00-00-00-00-00-00
> ----------------------------------------
> 
> NetBIOS Name Table for Host 192.168.5.10
> 
> Adapter address: 00-a8-34-35-c3-aa
> ----------------------------------------
> more data
> more data
> ----------------------------------------

Make the line of dashes the record terminator:

    $/ = "----------------------------------------\n";
    /\QNetBIOS Name Table for Host 192.168.5.10/ and print while <DATA>;

Anno


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

Date: Thu, 1 Apr 2004 12:17:49 +0200
From: "Jan Biel" <jan.biel@tu-clausthal.de>
Subject: splitting string with unknown number of similar substrings
Message-Id: <c4gq36$3s4$1@ariadne.rz.tu-clausthal.de>

Hi!

I'm trying to split a string into an array of tokens. Some of the tokens are
similar but I don't know how many will be in the string.

Example: (just imagine \n instead of line breaks, this is really one string)

CREATE TABLE personal (
    personal_id integer NOT NULL,
    vorname varchar(50) NOT NULL,
    nachname varchar(50) NOT NULL,
    privattelefon varchar(30),
    email varchar(130),
    datenschutzcode bigint NOT NULL,
    CONSTRAINT emailkorrekt_chk CHECK ((email ~~ '%@%'::text))
);

So the string always has a head

"CREATE TABLE personal ("

and a tail

");"


The tokens in between always have the form ".*," except the last one which
is omitting the "," but it is not known beforehand how many tokens exist.

My idea of a fitting perl expression looks like this:
my @table = split /(CREATE TABLE)(.*?)(\()(.*?,)*?(.*?\);)/s, $table;


But it does not work. From the tokens inbetween it only finds

"datenschutzcode bigint NOT NULL,"

and the one after that.


I have tried

my @table = split /(CREATE
TABLE)(.*?)(\()(.*?,)(.*?,)(.*?,)(.*?,)(.*?,)(.*?,)(.*?\);)/s, $table;

Which splits exactly at the correct places. Unluckily with this solution I
have to know the number of tokens beforehand. Additionally I consider it
really bad style.

I hope you can help,
Janbiel



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

Date: Thu, 1 Apr 2004 13:00:34 +0200
From: "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net>
Subject: Re: splitting string with unknown number of similar substrings
Message-Id: <Xns94BE83958FB62elhber1lidotechnet@62.89.127.66>

"Jan Biel" <jan.biel@tu-clausthal.de> wrote:

> Hi!
> 
> I'm trying to split a string into an array of tokens. Some of the
> tokens are similar but I don't know how many will be in the
> string. 
> 
> Example: (just imagine \n instead of line breaks, this is really
> one string) 
> 
> CREATE TABLE personal (
>     personal_id integer NOT NULL,
>     vorname varchar(50) NOT NULL,
>     nachname varchar(50) NOT NULL,
>     privattelefon varchar(30),
>     email varchar(130),
>     datenschutzcode bigint NOT NULL,
>     CONSTRAINT emailkorrekt_chk CHECK ((email ~~ '%@%'::text))
> );
> 
> So the string always has a head
> 
> "CREATE TABLE personal ("
> 
> and a tail
> 
> ");"
> 
> 
> The tokens in between always have the form ".*," except the last
> one which is omitting the "," but it is not known beforehand how
> many tokens exist. 


If the data always fits that description then this should work:


my @tokens = m/(?:,|CREATE TABLE personal \()([^,]*)/g;


-- 
Cheers,
Bernard


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

Date: Thu, 1 Apr 2004 13:31:13 +0200
From: "Jan Biel" <jan.biel@tu-clausthal.de>
Subject: Re: splitting string with unknown number of similar substrings
Message-Id: <c4gucp$adv$1@ariadne.rz.tu-clausthal.de>

Bernard El-Hagin wrote:

> If the data always fits that description then this should work:
>
>
> my @tokens = m/(?:,|CREATE TABLE personal \()([^,]*)/g;

Thanks for the tip, but could you elaborate?
I'm a newbie and don't fully understand

The expression looks for a match (m/)

I see two groups
  (?:,|CREATE TABLE personal \()
and
  ([^,]*)

I suppose any of the matches is copied in an array field.

The first group either detects "CREATE TABLE personal (" or "?:,", but I
have no idea what the latter means. :(

[^,]* is also a mystery to me.

Just trying to understand instead of blindly copy-pasting. Could you please
clear it up?

Thanks a lot,
Janbiel



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

Date: Thu, 1 Apr 2004 13:53:02 +0200
From: "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net>
Subject: Re: splitting string with unknown number of similar substrings
Message-Id: <Xns94BE8C7B16F99elhber1lidotechnet@62.89.127.66>

"Jan Biel" <jan.biel@tu-clausthal.de> wrote:

> Bernard El-Hagin wrote:
> 
>> If the data always fits that description then this should work:
>>
>>
>> my @tokens = m/(?:,|CREATE TABLE personal \()([^,]*)/g;
> 
> Thanks for the tip, but could you elaborate?
> I'm a newbie and don't fully understand


Sure. See below.

 
> The expression looks for a match (m/)
> 
> I see two groups
>   (?:,|CREATE TABLE personal \()
> and
>   ([^,]*)
> 
> I suppose any of the matches is copied in an array field.


Matches within () are captured (if you don't know what this means 
read


  perldoc perlretut


before continuing), but if the opening ( is followed by ?: then that 
specific group is not captured. So the regex matches a comma or the 
string 'CREATE TABLE personal (' followed by any string which doesn't 
contain a comma. The "any string which doesn't contain a comma" bit 
is then captured by the second pair of (). When evaluated in list 
context m//g will return all captured matches. For example,


my $input = 'a1b2c3d4';
my @digits = $input =~ m/(\d)/g;


And now @digits contains the digits 1 2 3 4.


So in the regex I gave you we're capturing everything (except ,) 
which follows either a comma or the text 'CREATE TABLE personal ('. 
This should leave only the tokens you're interested in.


More on the m// operator can be found in the docs:


  perldoc perlop


> The first group either detects "CREATE TABLE personal (" or "?:,",
> but I have no idea what the latter means. :(


The (?: means we don't want to capture with these particular 
parentheses.


  perldoc perlretut
  perldoc perlre

 
> [^,]* is also a mystery to me.


Everything except a comma zero or more times.


  perldoc perlretut
  perldoc perlre

 
> Just trying to understand instead of blindly copy-pasting. Could
> you please clear it up?


I hope you get it now.


-- 
Cheers,
Bernard


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

Date: Thu, 01 Apr 2004 10:28:24 GMT
From: Joe Smith <Joe.Smith@inwap.com>
Subject: Re: TCP Connection redirector with password?
Message-Id: <c9Sac.151505$1p.1955666@attbi_s54>

Ignoramus26612 wrote:

> I have access to a particular NNTP service. I would like to write a
> script that would listen on a port. Upon connection, it would connect
> to the NNTPservice and supply my credentials (authinfo user/pass), and
> then simply forewrd whatever data from one socket to another.
> 
> Any idea how to do it?

http://www.inwap.com/mybin/miscunix/list-files.pl?tcp-proxy
	-Joe


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

Date: Thu, 1 Apr 2004 14:13:07 +0900
From: "CatcherInTheRye" <letnomanenterhere@yahoo.co.kr>
Subject: test
Message-Id: <c4g8b5$akp@netnews.proxy.lucent.com>

test




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

Date: Thu, 1 Apr 2004 07:54:47 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: test
Message-Id: <slrnc6o7p7.2qk.tadmc@magna.augustmail.com>

CatcherInTheRye <letnomanenterhere@yahoo.co.kr> wrote:

> test


Please do not abuse our newsgroup. It will annoy people.

We discuss Perl here.

There are other newsgroups for testing news software.


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


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

Date: Thu, 01 Apr 2004 16:16:58 +0900
From: ko <kuujinbo@hotmail.com>
Subject: Re: Tough (for me) regex case
Message-Id: <c4gfld$2hetum$1@ID-227975.news.uni-berlin.de>

Rob Perkins wrote:
> Hello,
> 
> I know I'm not a regular, and I'm new to the arcana of regular
> expressions, so I'm a little stuck with two specific cases and I'm
> hoping for a genius:
> 
> The case I'm most stumped on is an input string like this:
> 
> The "quick" brown "fox jumped ""over"" the" lazy dog.
> 
> Where what I want to have matched is the quoted strings, except that
> paired doublequotes don't count, and I don't want to capture the
> quotemarks. In other words, my desired matches are:
> 
> <>
> quick
> fox jumped ""over"" the
> </>
> 
> If I use: /".+?"/, I get:
> 
> <>
> "quick"
> "fox jumped "
> "over"
> " the "
> </>
> 
> ...which isn't right. If I use /".+"/, I get:
> 
> <>
> "quick" brown "fox jumped ""over"" the"
> </>
> 
> ...which also isn't right. So I don't know how to proceed and get the
> match of the strings contained in doublequotes, with the paired
> doublequotes escaped, and the matches without the quotes.
> 
> How would you do it?
> 
> Rob

Take a look at the Text::Balanced module - here's a short example:

use strict;
use warnings;
use Text::Balanced qw[ extract_delimited ];

my $text = q[The "quick" brown "fox jumped ""over"" the" lazy dog.];

while (my ($extracted, $remainder) =
       extract_delimited($text, '"', '[^"]+', '"') )
       {
         last unless $extracted =~ s#^"(.*)"$#$1#;
         print "EXTRACTED TEXT: $extracted\n";
         $text = $remainder;
       }

HTH - keith



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

Date: Thu, 1 Apr 2004 13:43:35 +0100
From: "Simon" <simalt@totalise.co.uk>
Subject: Re: trapping errors
Message-Id: <406c0e78$1@primark.com>

Thanks all will try your suggestions

Brad,

    This is my other problem that it counts the "." and ".." and two dirs
that don't really exist in the count of files.

Is there a way for me to avoid it, sorry I am only a beginner in Perl.

An short example would help to understand.

Much appreciate your help.

Simon
"Brad Baxter" <bmb@ginger.libs.uga.edu> wrote in message
news:Pine.A41.4.58.0403311833260.13576@ginger.libs.uga.edu...
> On Wed, 31 Mar 2004, Brad Baxter wrote:
> > for( @hold_paths ) {
> >     if( opendir(DIR, $_) ) {
> >         my $file_count = readdir(DIR);
> >         push @hold_all, "$_ $file_count\n";
> >         closedir DIR || die "can't closedir $_: $!";
> >     }
> > }
> >
> Oops, apologies. Make that:
>
>         my $file_count = () = readdir(DIR);
>
> But again, that count will include '.' and '..' as is.
>
> Regards,
>
> Brad




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

Date: Thu, 1 Apr 2004 14:52:07 +0100
From: "Simon" <simalt@totalise.co.uk>
Subject: Re: trapping errors
Message-Id: <406c1e88$1@primark.com>

This works well Brad.

How do I suppress the count of . and ..?

Also what is the syntax for perldoc to tell if I have a subdirectory instead
of a file with in this code?

Many Thanks

Simon
"Simon" <simalt@totalise.co.uk> wrote in message
news:406c0e78$1@primark.com...
> Thanks all will try your suggestions
>
> Brad,
>
>     This is my other problem that it counts the "." and ".." and two dirs
> that don't really exist in the count of files.
>
> Is there a way for me to avoid it, sorry I am only a beginner in Perl.
>
> An short example would help to understand.
>
> Much appreciate your help.
>
> Simon
> "Brad Baxter" <bmb@ginger.libs.uga.edu> wrote in message
> news:Pine.A41.4.58.0403311833260.13576@ginger.libs.uga.edu...
> > On Wed, 31 Mar 2004, Brad Baxter wrote:
> > > for( @hold_paths ) {
> > >     if( opendir(DIR, $_) ) {
> > >         my $file_count = readdir(DIR);
> > >         push @hold_all, "$_ $file_count\n";
> > >         closedir DIR || die "can't closedir $_: $!";
> > >     }
> > > }
> > >
> > Oops, apologies. Make that:
> >
> >         my $file_count = () = readdir(DIR);
> >
> > But again, that count will include '.' and '..' as is.
> >
> > Regards,
> >
> > Brad
>
>




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

Date: Thu, 1 Apr 2004 07:30:36 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: trapping errors
Message-Id: <slrnc6o6bs.2qk.tadmc@magna.augustmail.com>

Brad Baxter <bmb@ginger.libs.uga.edu> wrote:
> On Wed, 31 Mar 2004, Brad Baxter wrote:

>>         my $file_count = readdir(DIR);

> Oops, apologies. Make that:
> 
>         my $file_count = () = readdir(DIR);


Or make that:

   my($file_count) = readdir(DIR);

:-)


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


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

Date: Thu, 1 Apr 2004 07:34:04 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: trapping errors
Message-Id: <slrnc6o6ic.2qk.tadmc@magna.augustmail.com>


[ Please do not top-post.
  Please do not quote the entire article.
]


Simon <simalt@totalise.co.uk> wrote:
> "Brad Baxter" <bmb@ginger.libs.uga.edu> wrote in message
> news:Pine.A41.4.58.0403311833260.13576@ginger.libs.uga.edu...
>> On Wed, 31 Mar 2004, Brad Baxter wrote:


>> Oops, apologies. Make that:
>>
>>         my $file_count = () = readdir(DIR);
>>
>> But again, that count will include '.' and '..' as is.


>     This is my other problem that it counts the "." and ".." and two dirs
> that don't really exist in the count of files.
> 
> Is there a way for me to avoid it, sorry I am only a beginner in Perl.


   my($file_count) = readdir(DIR);
   $file_count -= 2;                           #   :-)

or

   my $file_count = grep { $_ ne '.' and $_ ne '..' } readdir(DIR);


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


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

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


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