[25161] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7410 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Nov 16 14:06:03 2004

Date: Tue, 16 Nov 2004 11:05:06 -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, 16 Nov 2004     Volume: 10 Number: 7410

Today's topics:
    Re: A preference in the style guide (Anno Siegel)
    Re: Advice needed: threads and queues <noreply@gunnar.cc>
    Re: Advice needed: threads and queues <usenet@morrow.me.uk>
        Break large file down into smaller parts (Brian F.)
    Re: Break large file down into smaller parts <for-spammers-only@web.de>
    Re: Break large file down into smaller parts <peter@semantico.com>
    Re: Break large file down into smaller parts <toreau@gmail.com>
    Re: Break large file down into smaller parts <jwillmore@fastmail.us>
        FAQ 4.20: How do I unescape a string? <comdog@panix.com>
    Re: hi,something about thread in perl <usenet@morrow.me.uk>
    Re: How to work (?{code}) <nobull@mail.com>
    Re: How to work (?{code}) <mritty@gmail.com>
    Re: OPEN command <tadmc@augustmail.com>
        perl efficiency -- fastest grepping? <bryankrone@comcast.net>
    Re: perl efficiency -- fastest grepping? <wyzelli@yahoo.com>
    Re: perl efficiency -- fastest grepping? <jwillmore@fastmail.us>
    Re: perl efficiency -- fastest grepping? <tadmc@augustmail.com>
    Re: perl efficiency -- fastest grepping? <perl@my-header.org>
    Re: perl efficiency -- fastest grepping? <ddunham@redwood.taos.com>
    Re: perl efficiency -- fastest grepping? <uguttman@athenahealth.com>
        regexp question <nnpospamm@front.org>
    Re: regexp question <toreau@gmail.com>
    Re: regexp question <preynolds___@hotmail.com>
    Re: Text::Autoformat usage for a rookie (Anno Siegel)
    Re: Text::Autoformat usage for a rookie <usenet@morrow.me.uk>
    Re: Text::Autoformat usage for a rookie (Gary Schenk)
        Time::HiRes module and timing a command... (Adam)
    Re: unix perl module install errors (Peteris Krumins)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 16 Nov 2004 13:56:32 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: A preference in the style guide
Message-Id: <cnd0ug$qq7$1@mamenchi.zrz.TU-Berlin.DE>

Ben Morrow  <usenet@morrow.me.uk> wrote in comp.lang.perl.misc:
> 
> Quoth anno4000@lublin.zrz.tu-berlin.de (Anno Siegel):
> > Arndt Jonasson  <do-not-use@invalid.net> wrote in comp.lang.perl.misc:
> > > 
> > > I've been reading the style guide (perldoc perlstyle), and I have
> > > trouble understanding one of the short preferences listed in the
> > > beginning:
> > > 
> > > "Space after last parenthesis matching on current line."
> > > 
> > > Can someone give simple examples where this preference is
> > > followed, and not followed, respectively?
> > 
> > This follows the rule:
> > 
> >     foo(
> >         bar( baz( 3.141)) );
> > 
> > There's a space before the last ")" because the matching opening "("
> > isn't on the same line.
> 
> FWIW I've recently taken to invariably writing the above as
> 
> foo(
>     bar( baz(3.141))
> );

That's what I do too.  In fact, in such cases I would introduce parentheses
in a call I would write without if it fit on one line, so

    map <short_expression>, <short_list>;

becomes

    map(
        <long_expression>,
        <long_list>,
    );

If the function  has more than one argument, I add commas after all of
them, including the last one.

I'm not entirely consistent with this style, however.

    map <long_expression>,
        <long_list>;

is still an alternative if <long_expression> and <long_list> each fit on
one line.  If they don't, it's definitely the parenthesized form.

Anno


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

Date: Tue, 16 Nov 2004 15:42:54 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Advice needed: threads and queues
Message-Id: <2vuighF2olcr3U1@uni-berlin.de>

Swartz wrote:
> Hi all.
> 
> I'm fairly new to Perl

You seem to be new to Usenet as well. Please do not multi-post!
http://www.uwasa.fi/~ts/http/crospost.html

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Tue, 16 Nov 2004 18:33:26 +0000
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Advice needed: threads and queues
Message-Id: <motp62-ju7.ln1@osiris.mauzo.dyndns.org>

Quoth "Swartz" <swartz@inbox.ru>:
> 
> I'm trying to write a small threaded "proof of concept" application.
> Basic ideas:
>  - there is a number of songs users can listen to,
>  - each song can only be accessed by one user at a time
>  - each user can only listen to one song at a time,

You don't use lock to state that a song is being played: use it just to
sync access to variables. You want something more like

my %songs : shared = (
    song1 => {
        name   => "Song 1",
        length => 45,
    },
    song2 => ...
);

# create login threads

sub login {
    my @toplay = map { "song$_" }
                map { rand 8 + 1 } 
                1..8;
    
    SONG: while (@toplay) {
        for (@toplay) {
            if (get_song $_) {
                play_song $_;
                put_song $_;
                delete $_;
                next SONG;
            }
        }
    }
}

sub get_song {
    my $song = shift;
    lock %songs;
    
    if ($songs{$song}{locked}) {
        return;
    }
    else {
        $songs{$song}{locked} = threads->tid;
        return $songs{$song};
    }
}

sub put_song {
    my $song = shift;
    lock %songs;

    $songs{$song}{locked} == threads->tid or die <<DIE;
Thread @{[threads->tid]} attempted to put song $song which
it did not own!
DIE

    $songs{$song}{locked} = undef;
}

sub play_song {
    my $song = shift;
    sleep $song->{length};
}

# <all untested>

Ben

-- 
        I must not fear. Fear is the mind-killer. I will face my fear and
        I will let it pass through me. When the fear is gone there will be 
        nothing. Only I will remain.
ben@morrow.me.uk                                          Frank Herbert, 'Dune'


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

Date: 16 Nov 2004 08:10:45 -0800
From: brian@cv.net (Brian F.)
Subject: Break large file down into smaller parts
Message-Id: <2bbba31c.0411160810.488e6e18@posting.google.com>

Greets,

I have a 2million+ line file that gets generated twice a day, and was
wondering if there would be a way to read in the amount of lines and
split the file into several (say 5) parts with different file names?

so instead of having list.txt with 2 million lines, i'd end up with
file1.txt, file2.txt, file3.txt..... each with an equal (or nearly
equal) amount of data from the original.

Brian F.


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

Date: Tue, 16 Nov 2004 17:32:05 +0100
From: Toni Erdmann <for-spammers-only@web.de>
Subject: Re: Break large file down into smaller parts
Message-Id: <cnda25$k8s$1@news.mch.sbs.de>

Brian F. wrote:
> Greets,
> 
> I have a 2million+ line file that gets generated twice a day, and was
> wondering if there would be a way to read in the amount of lines and
> split the file into several (say 5) parts with different file names?
> 
> so instead of having list.txt with 2 million lines, i'd end up with
> file1.txt, file2.txt, file3.txt..... each with an equal (or nearly
> equal) amount of data from the original.

man split

split --lines=NUMBER

Toni


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

Date: Tue, 16 Nov 2004 16:32:36 +0000
From: Peter Hickman <peter@semantico.com>
Subject: Re: Break large file down into smaller parts
Message-Id: <419a2c1f$0$19656$afc38c87@news.easynet.co.uk>

If you are using unix or the like there is a command called split that will do 
it for you.


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

Date: Tue, 16 Nov 2004 17:35:39 +0100
From: Tore Aursand <toreau@gmail.com>
Subject: Re: Break large file down into smaller parts
Message-Id: <pan.2004.11.16.16.35.39.598707@gmail.com>

On Tue, 16 Nov 2004 08:10:45 -0800, Brian F. wrote:
> I have a 2million+ line file that gets generated twice a day, and was
> wondering if there would be a way to read in the amount of lines and
> split the file into several (say 5) parts with different file names?
> 
> so instead of having list.txt with 2 million lines, i'd end up with
> file1.txt, file2.txt, file3.txt..... each with an equal (or nearly
> equal) amount of data from the original.

1. Count the number of lines in the file; 'perldoc -q lines'
2. Decide on how many parts you want.
3. Iterate through the file, opening, writing to and closing
   each file as appropriate.


-- 
Tore Aursand <toreau@gmail.com>
"A car is not the only thing that can be recalled by its maker."
 (Unknown)


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

Date: Tue, 16 Nov 2004 11:58:36 -0500
From: James Willmore <jwillmore@fastmail.us>
Subject: Re: Break large file down into smaller parts
Message-Id: <pan.2004.11.16.16.58.34.159941@fastmail.us>

On Tue, 16 Nov 2004 08:10:45 -0800, Brian F. wrote:

> Greets,
> 
> I have a 2million+ line file that gets generated twice a day, and was
> wondering if there would be a way to read in the amount of lines and
> split the file into several (say 5) parts with different file names?
> 
> so instead of having list.txt with 2 million lines, i'd end up with
> file1.txt, file2.txt, file3.txt..... each with an equal (or nearly
> equal) amount of data from the original.

If the file you're reading isn't being written to as this script runs,
then the example should do what you want.  If the file you want to read
*is* being written to while you're reading it, that opens up a whole host
of other issues (like losing information while reading).

(example - may need work)
#!/usr/bin/perl

use strict;
use warnings;

my $prefix_for_chunks = '/tmp/testing';
my $chunk_count = 1;
my $chunk_size = 100000;
my $file_to_read = '/var/log/messages';

open IN, $file_to_read  or die "Can't open $file_to_read: $!\n";

my $current_output_file = sprintf "%s%04d.txt", $prefix_for_chunks,
$chunk_count;

open OUT, '+>', $current_output_file
  or die "Can't open $current_output_file for writing: $!\n";

while(<IN>) {
    if(!(  $. % $chunk_size ) ){
      close OUT;
      $current_output_file = sprintf "%s%s.txt", $prefix_for_chunks,
$chunk_count++;
      open OUT, '+>', $current_output_file
  or die "Can't open $current_output_file for writing: $!\n";
    }
    print OUT $_;
}

close IN;
close OUT;

=cut

HTH

Jim



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

Date: Tue, 16 Nov 2004 17:03:06 +0000 (UTC)
From: PerlFAQ Server <comdog@panix.com>
Subject: FAQ 4.20: How do I unescape a string?
Message-Id: <cndbsa$ovd$1@reader1.panix.com>

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.

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

4.20: How do I unescape a string?

    It depends just what you mean by ``escape''. URL escapes are dealt with
    in perlfaq9. Shell escapes with the backslash ("\") character are
    removed with

        s/\\(.)/$1/g;

    This won't expand "\n" or "\t" or any other special escapes.



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

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.

  AUTHOR AND COPYRIGHT

    Copyright (c) 1997-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. All rights 
    reserved.

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.


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

Date: Tue, 16 Nov 2004 10:22:40 +0000
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: hi,something about thread in perl
Message-Id: <g01p62-715.ln1@osiris.mauzo.dyndns.org>


Quoth XiaotingHua <x.t.hua@163.com>:
> i 'd like to write threaded perl program in a IBM machine which contain
> 8 cpu and install AIX 5.1 .
> 
> i look at the perlthrtut , and find perl thread is just like the fork in
>   unix, which is expensive.

Yup :)

> is perl thread in user-mode or kernel mode? AIX 5.1 support kernel
> thread, and can automatic put the task to all processers.but user thread
>   only can do all work in one processer. and if perl thread is only
> running in one processer, it 's senceless for me to do thread program.

Perl threads under unix will use your pthreads (POSIX threads)
implementation, so if your system pthreads library uses kernel threads
(and I'd bet it does, on AIX) then perl will too.

However, as you say, a perl thread is not a thread by any reasonable
definition: it's a userland fork. Without copy-on-write, or any of the
other optimizations the kernel can provide. The ithreads concept
originated in a project to emulate fork on systems which don't support
it, and IMHO on a system which supports fork you'd be *much* better off
with that. You *may* find that if you can create all your threads at
startup that over the course of a long-running program saving the
context switches needed for multiprocess is a significant benefit over
fork, but I doubt it; especially if you aren't forking hundereds of
process but just ~1 per processor.

If you want to use the threads::shared interface while forking, see the
forks module on CPAN.

Ben

-- 
               We do not stop playing because we grow old; 
                  we grow old because we stop playing.
                            ben@morrow.me.uk


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

Date: Tue, 16 Nov 2004 13:37:25 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: How to work (?{code})
Message-Id: <cncvhl$brg$1@sun3.bham.ac.uk>

Tad McClellan wrote:

> Mark <admin@asarian-host.net> wrote:
> 
> 
>>I was wondering if someone could explain the proper working of (?{code}) to
>>me. 
> 
> perlre.pod does a pretty good job of that I think:
> 
>    This zero-width assertion evaluate any embedded Perl code.  It
>    always succeeds, and its C<code> is not interpolated.

> It is *supposed to* always return true.

IMHO this was a poor design decision.  It would be much more intuative 
and useful if it only succeeded when the code returned a true (as the OP 
seems to expect).

Even though this is still called an "experimental feature - subject to 
change" this the default behaiour unlikely now to change but there may 
in future be an option to include some option flags between the closing 
brackets (?{ CODE }xxx) that modify the behaviour.

See 
http://groups.google.com/groups?threadm=u9pt9krvwa.fsf%40wcl-l.bham.ac.uk
	
>>Obviously, I am misunderstanding something. I hope
>>someone can tell me what I missed.
> 
> 
> 
> You are missing a question mark.  :-)
> 
> You want  (?? { code })  rather than  (? { code }).

Surely the OP really wants the hideous but widely used idiom:

(?? { (code) ? '' : '(?!)' })



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

Date: Tue, 16 Nov 2004 13:53:05 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: How to work (?{code})
Message-Id: <5Dnmd.5035$%M4.4845@trndny08>

"Mark" <admin@asarian-host.net> wrote in message
news:ididnadyDpAE1QTcRVn-2w@giganews.com...
> "Paul Lalli" <mritty@gmail.com> wrote in message
> news:K49md.10035$tI3.3096@trndny01...
>
> > Did you try the documentation for the feature you're using?
> >
> > perldoc perlre:
> >     "(?{ code })"
> >                WARNING: This extended regular expression feature
> >                is considered highly experimental, and may be
> >                changed or deleted without notice.
> >
> >                This zero-width assertion evaluate any embedded
> >                Perl code.  It always succeeds, and its "code" is
> >                not interpolated.
> >
> > In other words, (?{code()}) has no intrinsic effect on your regular
> > expression.  It is *always* true.  The result of the code is not
> > examined by the regexp engine to try to match anything.
>
> Yes, I read the docs (some older ActiveState Perl docs, here not at
home). I
> just focussed on the 'experimental', and figured I was not bothered by
that
> if it worked.

I wasn't referring to the 'experimental' warning.  I was referring to
the description of the feature itself - which flat out says it doesn't
do what you thought it did.

Paul Lalli



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

Date: Tue, 16 Nov 2004 08:11:49 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: OPEN command
Message-Id: <slrncpk2l5.3u4.tadmc@magna.augustmail.com>

Soxos <soxos@libero.it> wrote:


> I've already spent some hours on my problem, perhaps searching in the  
> wrong places, 


Where _did_ you search?

Maybe we can suggest some better places...


> I will search on PERLDOC (THIS is a good suggestion, 


Case *still* matters. The name of the program is "perldoc".


You asked how open() works, and I assumed that the 1st place you
would look would be the documentation for open().

Since you still posted asking how a pipe open works, I assumed
that you had not spent any time at all trying to find the
answer on your own.

Sorry if my assumption was not correct.


> without you have to  
> remark that open is not a command!).


I don't see anything offensive it that, but I think that you do.

open() is not a "command" and you did not know that, so I let
you know it. You did come here to learn, yes?




Have you seen the Posting Guidelines that are posted here frequently?


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


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

Date: Tue, 16 Nov 2004 05:57:25 -0600
From: Bryan Krone <bryankrone@comcast.net>
Subject: perl efficiency -- fastest grepping?
Message-Id: <Y92dnaeOkvA3dwTcRVn-2w@comcast.com>

I have a stream of data comming off a serial port at 19200. I am wondering
what is the most efficient way to grep through the data in realtime? I have
20 or so different strings I need to find. All of which are ~15 characters
or less. Currently I'm using code that looks like this:                

forever loop
{
sysread the serial buffer into $newdata

if( defined $newdata )
{
        $inString =~ s/^.*(.{32})$/$1/o;
        $inString .= $newdata;
}



if( $inString =~ /.*ResetPF.*/o || $inString =~ /.*[gG][oO].*/o || $inString
=~ /.*reset.*/o || $inString =~ /.*sysinit.*/o )
{
        set some flag;
}
}
Is there a more efficient way to grep for the strings to set some flag? This
works pretty well but this is only 4 strings. I would like to add a lot
more but the program slows down after 10 or more strings. Any ideas would
be greatly appreciated.

Thanks


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

Date: Tue, 16 Nov 2004 12:27:13 GMT
From: "Peter Wyzl" <wyzelli@yahoo.com>
Subject: Re: perl efficiency -- fastest grepping?
Message-Id: <Bmmmd.38298$K7.6105@news-server.bigpond.net.au>

"Bryan Krone" <bryankrone@comcast.net> wrote in message 
news:Y92dnaeOkvA3dwTcRVn-2w@comcast.com...
>I have a stream of data comming off a serial port at 19200. I am wondering
> what is the most efficient way to grep through the data in realtime? I 
> have
> 20 or so different strings I need to find. All of which are ~15 characters
> or less. Currently I'm using code that looks like this:

No doubt others more qualified than I will comment as well, but a couple of 
things...

> forever loop
> {
> sysread the serial buffer into $newdata
>
> if( defined $newdata )
> {
> $inString =~ s/^.*(.{32})$/$1/o;

Why are you using the 'o' switch to the regex?  You have you variable being 
interpolated.

> $inString .= $newdata;


Anyway, I believe you will find substr to be significantly faster for this 
operation, simply discarding everything except the last 32 characters in a 
string.

$inString = substr( $inString, -32) . $newdata;

Read about that in perlfunc


> }
>
>
>
> if( $inString =~ /.*ResetPF.*/o || $inString =~ /.*[gG][oO].*/o || 
> $inString
> =~ /.*reset.*/o || $inString =~ /.*sysinit.*/o )

Ooo!!  Your regexen will be VERY inefficient because of the .* causing huge 
amounts of backtracking (specially at both ends).  Since you are only 
looking to match the string, you can discard both sets of .* for a BIG 
performance boost (particularly across multiple regexen).  Again, you have 
the unnecessary 'o' switches, and that second regex can be written using the 
'i' switch (case insensitive).

Yielding:

if( $inString =~ /ResetPF/ || $inString =~ /go/i || $inString =~ /reset/ || 
$inString =~ /sysinit/ ){

I think you need to read up a bit more on regexes, particularly switches and 
how the regex engine works.

HTH
-- 
Wyzelli
#!/usr/bin/perl -w
use strict;
eval reverse ';"n\rekcaH lreP rehtona tsuJ" tnirp';




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

Date: Tue, 16 Nov 2004 08:33:58 -0500
From: James Willmore <jwillmore@fastmail.us>
Subject: Re: perl efficiency -- fastest grepping?
Message-Id: <pan.2004.11.16.13.33.57.143547@fastmail.us>

On Tue, 16 Nov 2004 05:57:25 -0600, Bryan Krone wrote:

<snip>
> if( $inString =~ /.*ResetPF.*/o || $inString =~ /.*[gG][oO].*/o ||
$inString
> =~ /.*reset.*/o || $inString =~ /.*sysinit.*/o ) {
>         set some flag;
> }
> }
> Is there a more efficient way to grep for the strings to set some flag?
> This works pretty well but this is only 4 strings. I would like to add a
> lot more but the program slows down after 10 or more strings. Any ideas
> would be greatly appreciated.

First, if you can do without the regular expressions, do so.  You can use
either 'unpack' or 'split' and place the results into an array.  Then you
can use 'grep' to find what you need.

Second, I'm going to throw this out here and see what happens.

If you can't get away from using regular expressions ... and because there
are *specific* matches to be performed ... and with each match there might
be a specific flag to be set (or action to be performed based upon the
match), I'd (maybe) use a lookup table. This method may or may not be any
better than the way you're doing it now.  I haven't benchmarked it and ...
my benchmarks would be useless against what you're trying to do.  

For example:

#!/usr/bin/perl 

use strict;
use warnings;

my $inString = 'reset the switch now please';

my %lookup = (
  qr{ResetPF} => \&do_resetpf,
  qr{go}i  => \&do_go,
  qr{reset}   => \&do_reset,
  qr{sysinit} => \&do_sysinit,
);

while( my($key,$value) = each %lookup ) {
  if( $inString =~ $key) {
    $value->();
  }
}

sub do_resetpf {
  print "ResetPF matched\n";
}

sub do_go {
  print "GgOo matched\n";
}

sub do_reset {
  print "reset matched\n";
}

sub do_sysinit {
  print "sysinit matched\n";
}

HTH

Jim



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

Date: Tue, 16 Nov 2004 08:37:21 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: perl efficiency -- fastest grepping?
Message-Id: <slrncpk451.3u4.tadmc@magna.augustmail.com>

Peter Wyzl <wyzelli@yahoo.com> wrote:


> I think you need to read up a bit more on regexes, particularly switches and 
> how the regex engine works.


See also:

   "How Regexes Work"

   http://perl.plover.com/Regex/


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


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

Date: Tue, 16 Nov 2004 18:41:22 +0100
From: Matija Papec <perl@my-header.org>
Subject: Re: perl efficiency -- fastest grepping?
Message-Id: <5hekp014q32v9vbko7nhg8g7scemjr3rer@4ax.com>

X-Ftn-To: Bryan Krone 

Bryan Krone <bryankrone@comcast.net> wrote:
>if( $inString =~ /.*ResetPF.*/o || $inString =~ /.*[gG][oO].*/o || $inString
>=~ /.*reset.*/o || $inString =~ /.*sysinit.*/o )
>{
>        set some flag;
>}
>}
>Is there a more efficient way to grep for the strings to set some flag? This

If you're checking against plain strings (ResetPF, reset..) you can speed up
things with perldoc -f index,

if (1+index($inString, "ResetPF") or ..) {}



-- 
Matija


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

Date: Tue, 16 Nov 2004 18:27:11 GMT
From: Darren Dunham <ddunham@redwood.taos.com>
Subject: Re: perl efficiency -- fastest grepping?
Message-Id: <3Ermd.21966$6q2.15041@newssvr14.news.prodigy.com>

Peter Wyzl <wyzelli@yahoo.com> wrote:

> if( $inString =~ /ResetPF/ || $inString =~ /go/i || $inString =~ /reset/ || 
> $inString =~ /sysinit/ ){

> I think you need to read up a bit more on regexes, particularly switches and 
> how the regex engine works.

And while those may be replacable with index, if you can't do so in the
general case, moving all the matches into a single regex can be
significantly faster...

if( $inString =~ /ResetPF|(?i:go)|reset|sysinit/ )

You can even generalize that by building the regex at runtime..

my @match_items = qw{ ResetPF (?i:go) reset sysinit };
my $regex = join('|', @match_items);
# ...
if ($inString =~ /$regex/) { ....

-- 
Darren Dunham                                           ddunham@taos.com
Senior Technical Consultant         TAOS            http://www.taos.com/
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >


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

Date: Tue, 16 Nov 2004 13:39:22 -0500
From: Uri Guttman <uguttman@athenahealth.com>
Subject: Re: perl efficiency -- fastest grepping?
Message-Id: <m3lld17i5h.fsf@lap.athenahealth.com>

>>>>> "DD" == Darren Dunham <ddunham@redwood.taos.com> writes:

  DD> Peter Wyzl <wyzelli@yahoo.com> wrote:

  >> if( $inString =~ /ResetPF/ || $inString =~ /go/i || $inString =~
  >> /reset/ || $inString =~ /sysinit/ ){

  DD> And while those may be replacable with index, if you can't do so in the
  DD> general case, moving all the matches into a single regex can be
  DD> significantly faster...

  DD> if( $inString =~ /ResetPF|(?i:go)|reset|sysinit/ )

and alternation of lots of strings in a regex can be very slow as well.

the OP didn't give a proper spec for the problem IMO. if the string in
question has a token in a know place, the fastest way to check for it is
to grab it with a simple regex and then look it up in a hash. so the
data read from the serial line needs to be properly specified with some
way to define where this match string is located. then extraction should
be easy and a hash can be made of the desired strings.

uri


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

Date: Tue, 16 Nov 2004 16:09:54 GMT
From: "Sam" <nnpospamm@front.org>
Subject: regexp question
Message-Id: <mDpmd.61$CK.1@twister.nyroc.rr.com>

Hello,

I'm have the following problem: I have pair of numbers like 111.1234 and
111.1235. But I want to display that than as 111.1234|5 (to save space on
the screen).
Other examples:

111.98 and 114.0 -> 111.98|4.0
1.2345 and 1.2945 -> 1.2345|945

I have sort of a solution but it's quiet ugly, maybe someone has an idea for
an elegant solution.

Thanks,
Sam




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

Date: Tue, 16 Nov 2004 17:32:17 +0100
From: Tore Aursand <toreau@gmail.com>
Subject: Re: regexp question
Message-Id: <pan.2004.11.16.16.32.16.995415@gmail.com>

On Tue, 16 Nov 2004 16:09:54 +0000, Sam wrote:
> I'm have the following problem: I have pair of numbers like 111.1234 and
> 111.1235. But I want to display that than as 111.1234|5 (to save space
> on the screen).
> Other examples:
> 
> 111.98 and 114.0 -> 111.98|4.0
> 1.2345 and 1.2945 -> 1.2345|945

If you're only dealing with two numbers at a time, you should store the
difference between the numbers instead; I refuse to believe that your
screen is _that_ small. :-)


-- 
Tore Aursand <toreau@gmail.com>
"Leadership is doing what is right when no one is watching." (George
 Van Valkenburg)


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

Date: Tue, 16 Nov 2004 18:56:05 GMT
From: "Patty Reynolds" <preynolds___@hotmail.com>
Subject: Re: regexp question
Message-Id: <93smd.86$CK.52@twister.nyroc.rr.com>


"Tore Aursand"  wrote
> On Tue, 16 Nov 2004 16:09:54 +0000, Sam wrote:
> > I'm have the following problem: I have pair of numbers like 111.1234 and
> > 111.1235. But I want to display that than as 111.1234|5 (to save space
> > on the screen).
> > Other examples:
> >
> > 111.98 and 114.0 -> 111.98|4.0
> > 1.2345 and 1.2945 -> 1.2345|945
>
> If you're only dealing with two numbers at a time, you should store the
> difference between the numbers instead

but with the difference he has still to deal with other problems:
e.g 111.98 and 114.0
would be 111.98|2.02 and not 111.98|4.0

But I agree that you can not easily solve that with a regular expression.

Patty




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

Date: 16 Nov 2004 11:31:01 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Text::Autoformat usage for a rookie
Message-Id: <cncodl$lcj$1@mamenchi.zrz.TU-Berlin.DE>

Gary Schenk  <gwschenk@fuzz.socal.rr.com> wrote in comp.lang.perl.misc:

[...]

> I have an immense text file, with very long lines, sometimes over
> 900 characters long. I would like to format this so that the
> lines are around 77 characters.

[...]

> I discovered Text::Autoformat. I have not been able to get it to
> work. I get a usage error. I have Googled trying to find
> examples of usage, and read up on it on CPAN, but I don't get
> it. It seems as though people use it to format simple strings to
> STDOUT. I want to format files.

That's the "minimal use" described in SYNOPSIS.  What's the
problem?

> Could someone point me in the right direction to getting this
> code to work? I'm missing something terribly basic, I know. I
> need a nudge in the right direction.
> 
> This is the latest version of the program:
> 
> #!/usr/bin/perl -w

No strict, no warnings.  Bad.

> use Text::Autoformat;
> 
> print "\n\nEnter a file to convert, or 'q' to quit: ";
> chomp( $input = <STDIN> );
> 
> if ( $input ne 'q' ) {
>    open( INPUT, "<$input" ) or die( "Can't open $input for
> reading: $!" );
>    open( OUTPUT, ">output.txt" ) or die( "Can't open output.txt:
> $!" );
> 
>    my $fixed = autoformat( <INPUT>, { left => 1, right => 77, all
> => 1 } );

autoformat() expects a single string $text as its first argument,
possibly followed by a hashref of options.  <INPUT> expands to the
list of lines in the file, which is not a single string, confusing
autoformat().

Read the file into a variable

    my $text = do { local $/; <INPUT> };

or use Uri's baby File::Slurp.  Then call autoformat with that variable:

    my $fixed = autoformat( $text, { left => 1, right => 77, all => 1});

>    print OUTPUT $fixed;
> }

That should take you a step further.

Anno


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

Date: Tue, 16 Nov 2004 10:13:46 +0000
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Text::Autoformat usage for a rookie
Message-Id: <qf0p62-715.ln1@osiris.mauzo.dyndns.org>


Quoth Gary Schenk <gwschenk@fuzz.socal.rr.com>:
> #!/usr/bin/perl -w

use warnings; # instead of -w
use strict;

Have you read the Posting Guidelines?

> use Text::Autoformat;
> 
> print "\n\nEnter a file to convert, or 'q' to quit: ";
> chomp( $input = <STDIN> );
        ^^
        my

I would use ^D (EOF) (^Z on Win32) to signal 'quit' instead, as this is
more usual with programs like this; you can detect EOF because $input
will be undef. Also, I would check @ARGV for files before prompting, so
you can call it like

 ./script file1

if you want to. 

> if ( $input ne 'q' ) {

I would use 'unless' here, but you may find that more confusing.

>    open( INPUT, "<$input" ) or die( "Can't open $input for
> reading: $!" );

You have checked the return of open: good.
You have included the file and $! in the error: good. :)
Use lexical FHs.
Use 3-arg open.
Don't use unnecessary parens.

open my $INPUT, '<', $input or die "...";

>    open( OUTPUT, ">output.txt" ) or die( "Can't open output.txt:
> $!" );

This is generally a bad idea... you would be much better off (say)
renaming the old file to "$input~" and overwriting, or creating the new
file as "$input.fmt"; or simply processing STDIN to STDOUT and letting
the user redirect where he will.

>    my $fixed = autoformat( <INPUT>, { left => 1, right => 77, all
> => 1 } );

[I am going to assume at this point that Text::Autoformat::autoformat is
unprototyped... I don't have a copy of the module to hand to check. See
perlsub for prototypes, and type
    perl -MText::Autoformat 
        -le'print prototype \&Text::Autoformat::autoformat'
to check I'm correct (it should print nothing).]

If it is, then its args will be evaluated in list context. Context is
one of the most important concepts in Perl, so try to get your head
around it :). The <> operator in list context returns a list of all the
lines in the file, where 'lines' are delimited with $/ which is "\n" as
you haven't changed it. So this will call autoformat like

autoformat( 
    "line one of file...\n", 
    "line two of file...\n",
    ..., 
    { opts }
)

which does not match the ([text], [options]) it was expecting. You want
to get the whole file into a scalar; there are (at least) two ways of
doing this. The 'obvious' one is 

$/ = undef;  # use local for other than very small scripts
my $fixed = autoformat scalar <INPUT>, {...});

where $/ and the special meaning of undef is explained in perlvar, and
the 'scalar' forces scalar context on the <> (this isn't strictly
necessary in this case, as it will just return one 'line' consisting of
he whole file anyway, but I prefer to add it as documentation that we
are only getting one value back). A better way is to use Uri's
File::Slurp module, like this

use File::Slurp qw/read_file/;

my $text = read_file $input;
my $fixed = autoformat $text, {...};

Read the docs for how to handle errors: the default as above is probably
fine for small scripts. This is better because it is both faster
(read_file uses unbuffered IO where it can, which can make the reading
much faster) and cleaner (there's no need to open the file or mess
around with $/).

Ben

-- 
   Although few may originate a policy, we are all able to judge it.
                                             - Pericles of Athens, c.430 B.C.
  ben@morrow.me.uk


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

Date: 16 Nov 2004 09:48:03 -0800
From: gary_schenk@yahoo.com (Gary Schenk)
Subject: Re: Text::Autoformat usage for a rookie
Message-Id: <81fd27bb.0411160948.12840326@posting.google.com>

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message news:<cncodl$lcj$1@mamenchi.zrz.TU-Berlin.DE>...
<snip>
> 
> > #!/usr/bin/perl -w
> 
> No strict, no warnings.  Bad.
> 
> > use Text::Autoformat;
> > 
> > print "\n\nEnter a file to convert, or 'q' to quit: ";
> > chomp( $input = <STDIN> );
> > 
> > if ( $input ne 'q' ) {
> >    open( INPUT, "<$input" ) or die( "Can't open $input for
> > reading: $!" );
> >    open( OUTPUT, ">output.txt" ) or die( "Can't open output.txt:
> > $!" );
> > 
> >    my $fixed = autoformat( <INPUT>, { left => 1, right => 77, all
> > => 1 } );
> 
> autoformat() expects a single string $text as its first argument,
> possibly followed by a hashref of options.  <INPUT> expands to the
> list of lines in the file, which is not a single string, confusing
> autoformat().
> 
> Read the file into a variable
> 
>     my $text = do { local $/; <INPUT> };
> 
> or use Uri's baby File::Slurp.  Then call autoformat with that variable:
> 
>     my $fixed = autoformat( $text, { left => 1, right => 77, all => 1});
> 
> >    print OUTPUT $fixed;
> > }
> 
> That should take you a step further.
> 
> Anno


That did more than take me a step forward. It solved the problem. This
little program will save me at least a week's worth of work, maybe
more!

Thanks very much, Anno. Not just for the suggestion, but for helping
me learn something about Perl.

Gary


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

Date: 16 Nov 2004 09:54:08 -0800
From: adam_cheney@hotmail.com (Adam)
Subject: Time::HiRes module and timing a command...
Message-Id: <214d0889.0411160954.2f5f4549@posting.google.com>

Hi All,

Another request for your wisdom, if that's OK...

I want to write a small perl program to test the performance of
ClearCase. Searching for ways to do this lead me to the Time::HiRes
module. This isn't present on our system (Solaris), so I downloaded
the tarred, gzipped module from cpan. Unzipped and untarred it, then
tried to build it.

Executing 'perl Makefile.PL' worked perfectly, but then 'make'
returned a whole lot of warnings. These were mostly of the form:
"./ppport.h", line 3298: warning: invalid white space character in
directive
"./ppport.h", line 3404: token not allowed in directive: \
"./ppport.h", line 3405: syntax error before or at: ||
"./ppport.h", line 3420: identifier redefined: defined
        current : function() returning int
        previous: function(int) returning int : "./ppport.h", line
3405
"./ppport.h", line 3420: parameter not in identifier list: my_cxt_sv
"./ppport.h", line 3420: cannot initialize parameter: my_cxt_sv
"./ppport.h", line 3420: undefined symbol: MY_CXT_KEY
"./ppport.h", line 3420: invalid source character: '\'
"./ppport.h", line 3421: warning: improper pointer/integer
combination: arg #2
(There were 541 lines of warnings/errors, so I've just produced a
flavour)

although there was also a:
"/usr/perl5/5.00503/sun4-solaris/CORE/perl.h", line 1384: warning:
operand treated as unsigned: 0x87654321

Then the make failed... If anyone can give me any insights I'd be
grateful.

Now the important bit: s there a way of timing the execution of a
shell command without using Time::HiRes?

Cheers - Adam...


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

Date: 14 Nov 2004 14:15:59 -0800
From: peteris.krumins@gmail.com (Peteris Krumins)
Subject: Re: unix perl module install errors
Message-Id: <6aeaa0c8.0411141415.418433b5@posting.google.com>

jamturtle@hotmail.com (James Marquez) wrote in message news:<8e120ba9.0411112112.7522089c@posting.google.com>...
>
> cc1: Invalid option `-fno-strict-aliasing'
> make: *** [SHA1.o] Error 1
> 
> Any clues would be helpful.
>

You're running an old version of GNU gcc which does not
support this option. Either get rid of the option or
upgrade the compiler.


P.Krumins


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

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


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