[24548] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6726 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jun 24 21:05:34 2004

Date: Thu, 24 Jun 2004 18:05:07 -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           Thu, 24 Jun 2004     Volume: 10 Number: 6726

Today's topics:
    Re: [Newbie] Stupid problem need simple answer (Array & <skuo@mtwhitney.nsc.com>
    Re: [Newbie] Stupid problem need simple answer (Array & (Anno Siegel)
    Re: [Newbie] Stupid problem need simple answer (Array & <daedalus@videotron.ca>
    Re: [SortofNewbie] Addusers with script on crontab <tadmc@augustmail.com>
    Re: error logs... <1usa@llenroc.ude>
    Re: error logs... <johnjcarbone@nospam.hotmail.com>
    Re: Find length of files <abigail@abigail.nl>
    Re: Logfile scanning assistance <glex_nospam@qwest.invalid>
    Re: Logfile scanning assistance <jgibson@mail.arc.nasa.gov>
        PPT UNIX Reconstruction Project at: http://www.perl.com <clydenospamorham@nospamorhamgetofftheline.freeservenospamorham.co.uk>
    Re: PPT UNIX Reconstruction Project at: http://www.perl <tadmc@augustmail.com>
        Thanks & End of the story <daedalus@videotron.ca>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 24 Jun 2004 15:19:41 -0700
From: Steven Kuo <skuo@mtwhitney.nsc.com>
Subject: Re: [Newbie] Stupid problem need simple answer (Array & RegExp)
Message-Id: <Pine.GSO.4.21.0406241509180.24156-100000@mtwhitney.nsc.com>

On Thu, 24 Jun 2004, Daedalus wrote:

(snipped)
> >
> > I asume you are talking about the effect of pushing into @array at the
> > same time with for().
> >
> > How do you know that?
> >
> > Bare in mind that the behaviour of for() when iterating over an array
> > that grows or shrinks is _undefined_.  Undefined means anything could
> > happen, and also the thing that happens now may not be the thing that
> > happens in the next version of Perl.
> >
> > If tomorrow the behaviour of pushing onto the array was to cause the
> > loop to reset to the start of the array then it could very well create
> > an infinite loop.
> >
> > > I know it's not the perfect solution (and I'll try to find a better way
> > > while i'm on my learning process), so if someone have any idea... I'd be
> > > glad to correct this misuse.
> >
> > See several better solutions already given in this thread.
> 
> Well sorry but at this time there no solutions in this thread that would
> behave like I want.
> Assuming that @string_list contain one string that is "to the bookstore"
> 
>  foreach (@string_list){
>     my $string = $_;
>       push @string_list, $string if $string =~ s/(^to\b|^the\b)\s*//i;
>  }
> this code would end with ("to the bookstore", "the bookstore", "bookstore")
> and thats exactly the result i'm looking for.
> While every other solutions proposed in the thread would end with ("to the
> bookstore","the bookstore") or ("to the bookstore","bookstore") depending
> wich of s///i or s///gi (or the map) is used.
> 
> Anyway if there's a way to do exactly the same thing with proper codes, it
> would popup in my mind sooner or later, or in this newsgroup.
> 
> Thanks
> DAE
> 




Try something like the algorithms descibed in

perldoc -q 'expand tabs' 
and
perldoc -q 'nesting':


#!/usr/local/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @copy = my @strings = ( 'to the bookstore' );

for (@strings) {
    push @copy, $_ while (s/^(to|the)\b\s*//i);
}

print Dumper \@copy;


__END__
$VAR1 = [
          'to the bookstore',
          'the bookstore',
          'bookstore'
        ];

-- 
Hope this helps,
Steven




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

Date: 24 Jun 2004 22:59:16 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: [Newbie] Stupid problem need simple answer (Array & RegExp)
Message-Id: <cbfmc4$bqr$1@mamenchi.zrz.TU-Berlin.DE>

Daedalus <daedalus@videotron.ca> wrote in comp.lang.perl.misc:
[more attributions lost]

[big snip]

> > If tomorrow the behaviour of pushing onto the array was to cause the
> > loop to reset to the start of the array then it could very well create
> > an infinite loop.
> >
> > > I know it's not the perfect solution (and I'll try to find a better way
> > > while i'm on my learning process), so if someone have any idea... I'd be
> > > glad to correct this misuse.
> >
> > See several better solutions already given in this thread.
> 
> Well sorry but at this time there no solutions in this thread that would
> behave like I want.
> Assuming that @string_list contain one string that is "to the bookstore"
> 
>  foreach (@string_list){
>     my $string = $_;
>       push @string_list, $string if $string =~ s/(^to\b|^the\b)\s*//i;
>  }
> this code would end with ("to the bookstore", "the bookstore", "bookstore")

No, it wouldn't.  You'd need "while" instead of "if" inside the loop.

> and thats exactly the result i'm looking for.

Well, then do it in a way that doesn't violate the rules.  The most
obvious way (already suggested, I think), is to use a copy of the list
to loop over:

    foreach ( my @copy = @string_list ) {
        push @string_list, $_ while s/(^to\b|^the\b)\s*//i;
    }

You don't need to copy $_ inside the loop because you already copied
all the values.

> While every other solutions proposed in the thread would end with ("to the
> bookstore","the bookstore") or ("to the bookstore","bookstore") depending
> wich of s///i or s///gi (or the map) is used.

That would be mostly because your original example (about "a" and "the")
did in no way suggest this requirement.

> Anyway if there's a way to do exactly the same thing with proper codes, it
> would popup in my mind sooner or later, or in this newsgroup.

See above.

Anno


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

Date: Thu, 24 Jun 2004 19:04:41 -0400
From: "Daedalus" <daedalus@videotron.ca>
Subject: Re: [Newbie] Stupid problem need simple answer (Array & RegExp)
Message-Id: <h4JCc.7380$uY3.334236@wagner.videotron.net>

> Try something like the algorithms descibed in
>
> perldoc -q 'expand tabs'
> and
> perldoc -q 'nesting':
>
>
> #!/usr/local/bin/perl
> use strict;
> use warnings;
> use Data::Dumper;
>
> my @copy = my @strings = ( 'to the bookstore' );
>
> for (@strings) {
>     push @copy, $_ while (s/^(to|the)\b\s*//i);
> }

That's exactly what I want ... push into a copy and test the original
"while" instead of "if". (it's a shame... so simple).

Thanks




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

Date: Thu, 24 Jun 2004 18:16:45 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: [SortofNewbie] Addusers with script on crontab
Message-Id: <slrncdmo6t.3as.tadmc@magna.augustmail.com>

Henk <hotten@zonnet.nl> wrote:

> Btw this is what i made up so far ;)


You forgot these 2 lines:

   use warnings;
   use strict;


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


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

Date: 25 Jun 2004 00:05:47 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude>
Subject: Re: error logs...
Message-Id: <Xns9512CC7017C24asu1cornelledu@132.236.56.8>

"John ©" <johnjcarbone@nospam.hotmail.com> wrote in
news:04GCc.4775$A9.1422@nwrdny01.gnilink.net: 

>>Read the FAQ.... oh wait, I'm not allowed to be snide... okay then,

> The comments were aimed at people like this one guy who was like
> "You're a newbie, you're lazy, go read the FAQ.  You should know not
> to post this way, and to post that way."  Just a bunch of rudeness
> without any substance.  No links to where things are, just total
> unhelpfulness. If he's that upset, then just don't respond to me.

The fact that you made you comments before you received any 'snide' remarks 
explains my unhelpfulness. If you had just made a request without a 
preemptive attack, you would have received help from me as well. OTOH, you 
have already received excellent advice, so you probably don't care.

-- 
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)


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

Date: Fri, 25 Jun 2004 00:41:59 GMT
From: "John ©" <johnjcarbone@nospam.hotmail.com>
Subject: Re: error logs...
Message-Id: <rxKCc.4476$L8.3152@nwrdny02.gnilink.net>


> The fact that you made you comments before you received any 'snide'
remarks
> explains my unhelpfulness. If you had just made a request without a
> preemptive attack, you would have received help from me as well. OTOH, you
> have already received excellent advice, so you probably don't care.
>
> -- 
> A. Sinan Unur

A. Sinan,

    I had already received 'snide' comments from a guy in the comp.lang.perl
group ... I was supposed to realize that that newsgroup was defunct.  That
there were FAQ's, that there were guidelines. That because I was a newbie I
was lazy. He even got on my case for typing PERL instead of Perl, among
other things... and that was the first time I had ever posted.

    So again, sorry if you were thinned skinned about a disclaimer that
wasn't aimed at you, but for the people that sincerely are rude out there.

Cheers,
John




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

Date: 24 Jun 2004 22:46:19 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Find length of files
Message-Id: <slrncdmmdr.1ai.abigail@alexandra.abigail.nl>

Tad McClellan (tadmc@augustmail.com) wrote on MMMCML September MCMXCIII
in <URL:news:slrncdka9m.ca4.tadmc@magna.augustmail.com>:
**  
**  I don't like shelling-out for things that are easily done
**  in native Perl.


I don't like doing things in Perl that are far easier, and much faster
done by an external program. Here's a benchmark - the numbers look
absurd because using "wc" just utterly dwarves the Perl solution:

    #!/usr/bin/perl

    use strict;
    use warnings;
    no warnings qw /syntax/;

    use Benchmark qw /cmpthese/;

    our $dir = "/home/abigail/Src/perl-5.8.4/pod/";
    our (@wc, @perl);

    cmpthese -10 => {
        wc       => '@wc   = `wc -w $dir/*.pod`',
        perl     => '@ARGV = <$dir/*.pod>;
                     @perl = ();
                     my $cnt   = 0;
                     my $total = 0;
                     while (<>) {
                        my @words = split;
                        $cnt += @words;
                        if (eof (ARGV)) {
                           push @perl => sprintf "%7d %s\n" => $cnt, $ARGV;
                           $total += $cnt;
                           $cnt = 0;
                        }
                     }
                     push @perl => sprintf "%7d %s\n", $total, "total";'
    };

    die unless "@wc" eq "@perl";

    __END__
           s/iter    perl      wc
    perl     1.51      --   -100%
    wc   7.60e-04 198564%      --


Abigail
-- 
$_ = "\nrekcaH lreP rehtona tsuJ"; my $chop; $chop = sub {print chop; $chop};
$chop -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
-> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()


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

Date: Thu, 24 Jun 2004 17:12:39 -0500
From: "J. Gleixner" <glex_nospam@qwest.invalid>
Subject: Re: Logfile scanning assistance
Message-Id: <qlICc.396$5D1.31346@news.uswest.net>

Jim wrote:
> Thanks for the suggestions and assistance.  I have been working on this 
> since and come up with the following:
> 
> #!/usr/local/bin/perl
use strict;
use warnings;
> 
> $logfile = "/opt/sample.log";
my $logfile = '/opt/sample.log';
> 
> open (LOG, $logfile)
open (LOG, "<$logfile")
>         or die "Can't open logfile, $!\n";
> @reverse_logfile = reverse <LOG>;
> 
> open (MYLOG, ">/opt/processed.log")
>         or die "Can't open my logfile, $!\n";
> 
my $recvd_found;

> for $line (@reverse_logfile) {
foreach (reverse <LOG> )

>         if ( $line =~ m|(Current recvd)| ) {
No need to capture the match.
if ( m|^Current recvd| )
>         $goodstuff = $line;
$recvd_found=1;
next;
>     }
>         if ( $line =~ m|(\d{4}-\d{2}-\d{2})| ) {
if ($recvd_found && m|\d{4}-\d{2}-\d{2}|)
{
	print MYLOG "$user $_"; #what's $user??
	$recvd_found=0;
	next; # If you only want the last one in the file, then no need for 
above line and change this to last;
}

don't need the following 3 lines
>         $date = $line;
>         }
>     print MYLOG $user, $date;
> }
close MYLOG;
close LOG;
> 
> Here's a sample.log:
> 
> 2004-06-23 12:11:22 Wrote 27 bytes
> 2004-06-23 12:11:22 length 12
> 2004-06-23 12:11:22 blocking
> 12:12:30 processing logfile
> 12:13:22 appending logfile
> 12:13:22 reticulating splines
> 12:13:22 initiating connection
> 12:13:22 receiving connection
> 12:14:57 ending connection
> Max recvd: 20
> Current recvd: 1
> 2004-06-23 12:16:44 Wrote 23 bytes
> 2004-06-23 12:16:44 length 5
> 2004-06-23 12:16:44 blocking
> 12:17:11 processing logfile
> 12:17:11 appending logfile
> 12:17:11 reticulating splines
> 12:17:11 initiating connection
> 12:17:11 receiving connection
> 12:18:38 ending connection
> Max recvd: 20
> Current recvd: 5
> 
> I thought it would be easier to reverse the logfile, find a 'Current 
> recvd' line and then find the first timestamp after it.
> 
> The bit I'm having problems with is I'm picking up all the timestamps, 
> rather than the first one after 'Current recvd' is matched.  How can I 
> only match one timestamp?
> 
> Thanks.
> 
> Jim
> 


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

Date: Thu, 24 Jun 2004 16:36:45 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Logfile scanning assistance
Message-Id: <240620041636451533%jgibson@mail.arc.nasa.gov>

In article <cbfdit$3hr$1@gargoyle.oit.duke.edu>, Jim
<squidsonata@hotmail.com> wrote:

> Thanks for the suggestions and assistance.  I have been working on this 
> since and come up with the following:
> 
> #!/usr/local/bin/perl
> 
> $logfile = "/opt/sample.log";
> 
> open (LOG, $logfile)
>          or die "Can't open logfile, $!\n";
> @reverse_logfile = reverse <LOG>;
> 
> open (MYLOG, ">/opt/processed.log")
>          or die "Can't open my logfile, $!\n";
> 
> for $line (@reverse_logfile) {
>          if ( $line =~ m|(Current recvd)| ) {
>     $goodstuff = $line;
>  }
>          if ( $line =~ m|(\d{4}-\d{2}-\d{2})| ) {
>     $date = $line;
>          }
>  print MYLOG $user, $date;
> }
> 
> Here's a sample.log:
> 
> 2004-06-23 12:11:22 Wrote 27 bytes
> 2004-06-23 12:11:22 length 12
> 2004-06-23 12:11:22 blocking
> 12:12:30 processing logfile
> 12:13:22 appending logfile
> 12:13:22 reticulating splines
> 12:13:22 initiating connection
> 12:13:22 receiving connection
> 12:14:57 ending connection
> Max recvd: 20
> Current recvd: 1
> 2004-06-23 12:16:44 Wrote 23 bytes
> 2004-06-23 12:16:44 length 5
> 2004-06-23 12:16:44 blocking
> 12:17:11 processing logfile
> 12:17:11 appending logfile
> 12:17:11 reticulating splines
> 12:17:11 initiating connection
> 12:17:11 receiving connection
> 12:18:38 ending connection
> Max recvd: 20
> Current recvd: 5
> 
> I thought it would be easier to reverse the logfile, find a 'Current 
> recvd' line and then find the first timestamp after it.
> 
> The bit I'm having problems with is I'm picking up all the timestamps, 
> rather than the first one after 'Current recvd' is matched.  How can I 
> only match one timestamp?

You are going to match any line that contains a timestamp. Your problem
is you are not using the information that you have saved: that fact
that you have matched 'Current recvd'.

Here is another approach: read the file and save each timestamp
encounterd. Test each line against your second pattern: 'Current
recvd'. When that matches, print out your currently saved timestamp.

Something like (untested):

   my $date;
   while(<LOG>) {
      if( m|(\d{4}-\d{2}-\d{2})| ) {
         $date = $1;
      }elsif( m|Current recvd| ){
         print $date if $date;
      }
   }

If you are going to save all of the lines in the file in an array,
there is really no point in reversing them. You can traverse the array
in either direction using an index loop:

   for( my $i = 0; $i < @array; $i++ ) {  #forward
      ...
   }

or

   for( my $i = $#array; $i >= 0; $i-- ) {   #reverse
      ...
   }


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

Date: Thu, 24 Jun 2004 23:18:52 +0100
From: "Clyde Ingram" <clydenospamorham@nospamorhamgetofftheline.freeservenospamorham.co.uk>
Subject: PPT UNIX Reconstruction Project at: http://www.perl.com/language/ppt/
Message-Id: <TiJCc.255$da5.203@newsfe2-win>

Does anyone know where this web site has moved to?
I get an O'Reilly Perl page starting:

        Sorry, but the page you requested is unavailable.

No info why.




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

Date: Thu, 24 Jun 2004 18:18:06 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: PPT UNIX Reconstruction Project at: http://www.perl.com/language/ppt/
Message-Id: <slrncdmo9e.3as.tadmc@magna.augustmail.com>

Clyde Ingram <clydenospamorham@nospamorhamgetofftheline.freeservenospamorham.co.uk> wrote:
> Does anyone know where this web site has moved to?


   http://sourceforge.net/projects/ppt/


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


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

Date: Thu, 24 Jun 2004 19:47:27 -0400
From: "Daedalus" <daedalus@videotron.ca>
Subject: Thanks & End of the story
Message-Id: <nIJCc.7765$uY3.355451@wagner.videotron.net>

>> While every other solutions proposed in the thread would end with ("to
the
> >bookstore","the bookstore") or ("to the bookstore","bookstore") depending
> >wich of s///i or s///gi (or the map) is used.
>
> That would be mostly because your original example (about "a" and "the")
> did in no way suggest this requirement.

Yeah that's totaly true. Actually when I posted the original exemple, I
didn't know that my foreach-loop violates the rules. My question was about
"When I write: if ($string =~ s/(^the\b)|(^a\b)|//i)..., do I'm actually
replacing the string within @string_list " And the answer was yes, in
"foreach my $string (@string_list)" $string become an aliase of the current
element of @string_list.
Then you make the point that my loop was illegal, so I had to change my
exemple to explain why I did use the loop that way, and see if someone could
point me to a legal way to obtain the same behavior... and here we are.


Finally, as I said in the topic, it was a stupid and simple thing: using
while with a copy (sorry I didn't thought of "while"). But in fact, why
should I thought of it ? Since I didn't realise the loop I did was illegal.


Don't worry, I'll just be a newb for a while ;)

Thanks to all (and sorry if I seem rude sometimes, english is not my primary
language)

DAE




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

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


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