[25834] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8071 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 11 21:05:24 2005

Date: Wed, 11 May 2005 18:05:03 -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, 11 May 2005     Volume: 10 Number: 8071

Today's topics:
    Re: Games.pl again <hackeras@gmail.com>
    Re: Games.pl again <tadmc@augustmail.com>
    Re: how to recursively search through files (not dirs)? <vdheuv@kabelfoon.nl>
    Re: how to recursively search through files (not dirs)? <joe@inwap.com>
    Re: how to recursively search through files (not dirs)? <someone@example.com>
    Re: Loading a hash from a string containing newlines <skuo@mtwhitney.nsc.com>
    Re: Loading a hash from a string containing newlines <someone@example.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 11 May 2005 21:11:15 +0300
From: Nikos <hackeras@gmail.com>
Subject: Re: Games.pl again
Message-Id: <d5ths2$a3s$1@nic.grnet.gr>

Tad McClellan wrote:

> It also helps to get an answer if you actually _ask_ some question...
> 
> 
> (questions are easily identified, as they end with a question mark.)

my ($gamename, $gamedesc, $gamecounter);
my $select = $dbh->prepare( "SELECT count(*) FROM games WHERE gamename=?" );
my $insert = $dbh->prepare( "INSERT INTO games (gamename, gamedesc, 
gamecounter) VALUES (?, ?, ?)" );
my $update = $dbh->prepare( "UPDATE games SET gamedesc=?, 
gamecounter=?+1 where gamename=?" );

open (FILE, "<../data/games/descriptions.txt") or die $!;
     while (<FILE>) {
         chomp;

         ($gamename, $gamedesc) = split /\t/;

         $select->execute($gamename);
         my $record_exists = $select->fetchrow_array();

         if ($record_exists) {
            $update->execute( $gamedesc, $gamecounter, $gamename );
         }
         else {
            $insert->execute( $gamename, $gamedesc, 0 );
         }
     }
close (FILE);

Well yes.
Something wrong with update. the last gamename from description.txt 
always updates and not the others.

Code seems fine though and i made it as straightforward as i could.
I think it explains very well what i am up to.
But please if you have questions on what iam trying to do just ask me....

Or maybe we can write it even better.
you can always check how it is run by visiting http://www.nikolas.tk 
going bottom down & clicking the fun ballon button. Then games.pl loads.
IUnfortunately i cant explain better why its nor runnign as expected.
you can see for yourself!


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

Date: Wed, 11 May 2005 14:00:12 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Games.pl again
Message-Id: <slrnd84lhs.i3u.tadmc@magna.augustmail.com>

Nikos <hackeras@gmail.com> wrote:
> Tad McClellan wrote:

>> (questions are easily identified, as they end with a question mark.)

[snip text, none of it ending with a question mark]


It helps to get an answer if you actually _ask_ some question...

Is there anybody in there? Just nod if you can hear me.


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


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

Date: Wed, 11 May 2005 22:56:12 +0200
From: "Theo  van den Heuvel" <vdheuv@kabelfoon.nl>
Subject: Re: how to recursively search through files (not dirs)?
Message-Id: <4282716c$0$75708$58c7af7e@news.kabelfoon.nl>


"David Combs" <dkcombs@panix.com> schreef in bericht 
news:d5th6n$9hj$1@panix1.panix.com...
> What I want to do traverse files that recursively
> "include" or (as in csh .cshrc-files) "source"
> other files -- then come back and continue
> to process the "sourcing" file.

[lots of frustration snipped]


> ----------------- here it is (ugh!):
>
>
> #!/me-FIRST-in-PATH-bin/perl-5.8.6/bin/perl5.8.6  -w 
> traverse-via-SOURCE-stmts.pl
> use strict;
> use diagnostics;
> use warnings;

good start.

> use Carp;
> use IO::Handle;  # test of 11may05:  (recursive opens)
> use FileHandle;  # ---- from pg 895 in camel 3rd:
>
> # ------------------------------------------------------------------------
> # ------------------------------------------------ processOneFile():::
> # ------------------------------------------------------------------------
>
> # cperl-indent-region on the entire sub, complains (unfortunately
> #   the err-msg doesn't make it to *Messages*):
> #      Scan error: "Unbalanced parentheses", 3002, 5180"
>
>
> sub processOneFile {
>  my $fileName = shift;
>  my $level = shift;
>
>  print("\n========================= ENTERING processOneFile(\"$fileName\", 
> $level\n");
>
> # ---- from pg 895 in camel 3rd,  (with "my" added):
>
>  my $fh = new FileHandle;
>  die "Cannot open \"$fileName\"!" if (! fh->open( $fileName ));
                                         ^

Make this into $fh. Perl complained about that when I ran it.
Now it seems to run.
BTW, I find it more clear to say
   ... unless $fh->open($fileName);


[rest of code snipped]

>
> Thanks!
>
> David
>
>

You're welcome.

Theo van den Heuvel




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

Date: Wed, 11 May 2005 14:22:01 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: how to recursively search through files (not dirs)?
Message-Id: <L4idnVYJh9bn6h_fRVn-ig@comcast.com>

David Combs wrote:
> I originally had it at least opening the files in
> the right order, but not restoring the prior
> filehandle on returning 

That's not a problem if you use lexical file handles.
Not too many books show how to use lexical file handles.
They eliminate the need to "use FileHandle;".

>   my $fh = new FileHandle;
>   die "Cannot open \"$fileName\"!" if (! fh->open( $fileName ));

  open my $fh,'<',$fileName or return warn "Cannot open $fileName: $!\n";

>     if ( ( $line =~ /^source / ) && 
> 	 ( $line =~ /^source\s+([A-Za-z0-9._-]+)/ ) ) { # dive-in:
>     #  CPERL-MODE won't bounce on either rparen   ^ ^ -- WHY?
>             my $diveIntoFName = $1;

     if ($line =~ /^source\s+(\S+)/) {
        my $diveIntoFName = $1;

		-Joe


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

Date: Thu, 12 May 2005 00:30:22 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: how to recursively search through files (not dirs)?
Message-Id: <ysxge.36505$0X6.32934@edtnps90>

David Combs wrote:
> What I want to do traverse files that recursively
> "include" or (as in csh .cshrc-files) "source"
> other files -- then come back and continue
> to process the "sourcing" file.
> 
> Files like these:
> 
> 375 ====> head -5000 test{1,2,3}.source
> ==> test1.source <==
> foo
> source test2.source
> bar
> bletch
> 
> ==> test2.source <==
> this is test2.source, line ONE
> source test3.source
> this is test2.source, line THREE
> this is test2.source, line FOUR
> 
> ==> test3.source <==
> this is test3.source, line ONE.
> this is test3.source, line TWO.
> 376 ====> !! >! foo.out
> head -5000 test{1,2,3}.source > ! foo.out
> 377 ====> 

This works for me:

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


source( 'test1.source' );

sub source {
     local *ARGV;
     @ARGV = shift;
     my $level = 1 + shift;
     while ( <> ) {
         if ( /^source\s+(.+)/ ) {
             source( $1, $level )
             }
         else {
             print "$level: $_"
             }
         }
     }

__END__



John
-- 
use Perl;
program
fulfillment


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

Date: Wed, 11 May 2005 13:10:34 -0700
From: Steven Kuo <skuo@mtwhitney.nsc.com>
Subject: Re: Loading a hash from a string containing newlines
Message-Id: <Pine.GSO.4.21.0505111304590.25445-100000@mtwhitney.nsc.com>

On Wed, 11 May 2005, it was written:

> Hi,
> 
> my $var = "TESTID: A\nB\nOBJECTIVE: C\nD\nPROCEDURE: E\nF\nRESULTS: G\nH\n";
> my %hash = $var =~ /(TESTID|OBJECTIVE|PROCEDURE|RESULTS):(.+)/mg;
> print $hash{"TESTID"};
> print $hash{"OBJECTIVE"};
> print $hash{"PROCEDURE"};
> print $hash{"RESULTS"};
> 
> 1.  How can I get $hash{"TESTID"} = "A\nB", etc.?
> 
> 2.  (Minor) How can I make the hash keys lowercase?  The search needs to be 
> case-sensitive, and the text must be in upper case?
> 
> Sorry if this is in the docs (just point me to it).  I searched the doc, 
> Googled, and skimmed a couple O'Reilly books before posting.
> 
> Thanks,
> Scott 



Among the many ways to do this:

package LCKeys;
require Tie::Hash;
@ISA = ('Tie::StdHash');

sub STORE
{
    $_[0]{  lc($_[1])  } = $_[2];
}
1;

package main;
use Data::Dumper;

my %h1;
tie %h1, 'LCKeys';

my $var = "TESTID: A\nB\nOBJECTIVE: C\nD\nPROCEDURE: E\nF\nRESULTS: G\nH\n";
%h1 = grep $_, split(/(TESTID|OBJECTIVE|PROCEDURE|RESULTS):/, $var);

print Dumper \%h1;

my %h2;
my $key;

require 5.8.0;
open (my $fh, '<', \$var)
    or die $!;

while (<$fh>)
{
    if (/(TESTID|OBJECTIVE|PROCEDURE|RESULTS):(.+)/s)
    {
        $key = lc($1);
        $h2{$key} = $2;
    }
    else
    {
        $h2{$key} .= $_;
    }
}

print Dumper \%h2;


For details, see: 

perldoc -f open
perldoc -f split
perldoc Tie::Hash

-- 
Hope this helps,
Steven



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

Date: Wed, 11 May 2005 20:22:34 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Loading a hash from a string containing newlines
Message-Id: <eQtge.61543$tg1.45306@edtnps84>

Scott Bass wrote:
> 
> my $var = "TESTID: A\nB\nOBJECTIVE: C\nD\nPROCEDURE: E\nF\nRESULTS: G\nH\n";
> my %hash = $var =~ /(TESTID|OBJECTIVE|PROCEDURE|RESULTS):(.+)/mg;
> print $hash{"TESTID"};
> print $hash{"OBJECTIVE"};
> print $hash{"PROCEDURE"};
> print $hash{"RESULTS"};
> 
> 1.  How can I get $hash{"TESTID"} = "A\nB", etc.?
> 
> 2.  (Minor) How can I make the hash keys lowercase?  The search needs to be 
> case-sensitive, and the text must be in upper case?
> 
> Sorry if this is in the docs (just point me to it).  I searched the doc, 
> Googled, and skimmed a couple O'Reilly books before posting.

john@perl Combs $ perl -MData::Dumper -e'
my $var = "TESTID: A\nB\nOBJECTIVE: C\nD\nPROCEDURE: E\nF\nRESULTS: G\nH\n";
my %hash = map { ($a,$b) = split /:/; lc $a, $b }
     $var =~ /((?:TESTID|OBJECTIVE|PROCEDURE|RESULTS):.+?)(?=\n\w+:|$)/sg;
print Dumper \%hash;
'
$VAR1 = {
           'testid' => ' A
B',
           'procedure' => ' E
F',
           'objective' => ' C
D',
           'results' => ' G
H'
         };



John
-- 
use Perl;
program
fulfillment


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

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


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