[25417] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7662 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jan 17 14:10:22 2005

Date: Mon, 17 Jan 2005 11:10:16 -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           Mon, 17 Jan 2005     Volume: 10 Number: 7662

Today's topics:
    Re: Need help with Perl and MySQL database data load <oscar@nowhere.com>
    Re: Need help with Perl and MySQL database data load <toreau@gmail.com>
    Re: Need help with Perl and MySQL database data load <1usa@llenroc.ude.invalid>
    Re: passing a hash reference to subroutine <abc@invalid.com>
    Re: Perl error <travisq@gmail.com>
        Rearrange graphically a perl module: my poorman solutio s_p_a_m_mob@hotmail.com
        regex: how to %hash2 = grep {/KEY/} %hash1 (Gerhard M)
    Re: regex: how to %hash2 = grep {/KEY/} %hash1 <mritty@gmail.com>
    Re: regex: how to %hash2 = grep {/KEY/} %hash1 <noreply@gunnar.cc>
    Re: regex: how to %hash2 = grep {/KEY/} %hash1 <mritty@gmail.com>
    Re: regex: how to %hash2 = grep {/KEY/} %hash1 (Anno Siegel)
    Re: regex: how to %hash2 = grep {/KEY/} %hash1 <perl@my-header.org>
        regex: listing all textstrings to be found more that 2  tools55@bluemail.ch
    Re: regex: listing all textstrings to be found more tha <phaylon@dunkelheit.at>
    Re: regex: listing all textstrings to be found more tha <noreply@gunnar.cc>
    Re: regex: listing all textstrings to be found more tha <nobull@mail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 17 Jan 2005 16:19:32 GMT
From: Oscar <oscar@nowhere.com>
Subject: Re: Need help with Perl and MySQL database data load
Message-Id: <oARGd.24685$Z%.24446@fe1.texas.rr.com>

Tore,

I'm not sure what top-post refers to.  Hope I'm not doing it now.
Here is the complete script:

#!/perl/bin/perl -w
#insert data into mysql
use strict;
use DBI;

package MySQL;

my $dsn   = 'DBI:mysql:database=sd_tst;host=localhost;port=3306';
my $user  = 'root';
my $pass  = 'xxxxxxx';
my %args  = ( RaiseError => 1,
               AutoCommit => 1 );

my $dbh = DBI->connect( $dns, $user, $pass, \%args )
           or die "Can't connect; $DBI:errstr";


$dbh->do("LOAD DATA LOCAL INFILE '/sd_data/audit_20041208.txt' INTO TABLE
sd_tst FIELDS TERMINATED BY '\|' LINES TERMINATED BY '\r\n';");


$dbh->disconnect ();
#EOF

Thanks,
Oscar


Tore Aursand wrote:
> Oscar wrote:
> 
>> Thanks for the responses.
> 
> 
> Please don't top-post.
> 
>> I modified my script to declare
>> my $user
>> my $dsn
>> my $pass
>> my %args
>> and modified my connect command to accomodate the new variables.
>> When I ran the script I got the message:
>> "Global symbol "$dns" requires explicit package name at sqltst2.pl 
>> line 13.
> 
> 
> It means that I had a typo in my example. It should be ease for you to 
> correct yourself, as Perl tells you on what line the typo is, right?
> 
>> I then added the line
>> package MySQL;
>> at the beginning of my script and got the same error.
> 
> 
> Sure. Why should "converting" your script to a package named "MySQL" do 
> any good?
> 
> Please post a _complete_ script detailing your problems.
> 
> 


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

Date: Mon, 17 Jan 2005 18:11:13 +0100
From: Tore Aursand <toreau@gmail.com>
Subject: Re: Need help with Perl and MySQL database data load
Message-Id: <KkSGd.4842$IW4.99516@news2.e.nsc.no>

Oscar wrote:
> I'm not sure what top-post refers to.  Hope I'm not doing it now.

You are. Top-posting means that you are posting your answer in front of 
the part you are answering. That's a bad habit; it's like answering 
someone before you've even heard the question.

> #!/perl/bin/perl -w
> #insert data into mysql
> use strict;
> use DBI;

If you're running a newer version of Perl, you should change this to:

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

> package MySQL;

Don't do that. You are telling Perl that you're creating a package named 
'MySQL' with this line. You aren't doing that, are you?

> my $dsn   = 'DBI:mysql:database=sd_tst;host=localhost;port=3306';
> my $user  = 'root';
> my $pass  = 'xxxxxxx';
> my %args  = ( RaiseError => 1,
>               AutoCommit => 1 );
> 
> my $dbh = DBI->connect( $dns, $user, $pass, \%args )
>           or die "Can't connect; $DBI:errstr";
> 
> $dbh->do("LOAD DATA LOCAL INFILE '/sd_data/audit_20041208.txt' INTO TABLE
> sd_tst FIELDS TERMINATED BY '\|' LINES TERMINATED BY '\r\n';");
> 
> $dbh->disconnect ();

What error message(s) do you get? I haven't imported files this way in a 
long time, so I'm not sure if the syntax is right, but I'm sure you've 
read the documentation?


-- 
Tore Aursand <tore@aursand.no>
"When you see a good idea look for a better one. You should never play
  the first good move that comes into your head." (Bruce Pandolfine)


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

Date: 17 Jan 2005 17:39:00 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Need help with Perl and MySQL database data load
Message-Id: <Xns95E180B37F425asu1cornelledu@132.236.56.8>

Oscar <oscar@nowhere.com> wrote in news:oARGd.24685$Z%.24446
@fe1.texas.rr.com:

> Tore,
> 
> I'm not sure what top-post refers to.  

There is always Google you know:

http://www.google.com/search?q=top-posting

> Hope I'm not doing it now.

Yes you are.

Please read the posting guidelines for this group.

Sinan.


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

Date: Mon, 17 Jan 2005 16:52:07 GMT
From: ktom <abc@invalid.com>
Subject: Re: passing a hash reference to subroutine
Message-Id: <X2SGd.23843$Ta2.4587@fe2.texas.rr.com>

A. Sinan Unur wrote:
> ktom <abc@invalid.com> wrote in
> news:QfYFd.7077$_56.2931@fe2.texas.rr.com: 
> 
> 
>>is this a valid way to pass a hash to a subroutine?
>>
>>&getBest( \%{$eventHash{$eventCnt}} ) ;
> 
> 
> First, read 
> 
> perldoc perlsub 

yikes, this was far more informative that i had expected.  it would have 
saved me the hour of perusing web pages for the answer that was in the 
first 100 lines..

> 
> on why you should not use the & above unless you need the specific features 
> it provides.
> 
> Second, please post the a short complete script that exhibits the problem 
> you are trying to solve rather than a mish-mash of punctuation.

i though the above usage was the problem, in actuality, access the hash 
ref in the subroutine was the real problem.  the %{$_[0]} usage.

thanks for the pointers..

kevin

> 
> You might find Data::Dumper useful in visualizing what gets passed to your 
> sub.
> 
> Sinan


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

Date: 17 Jan 2005 06:14:31 -0800
From: "quartet" <travisq@gmail.com>
Subject: Re: Perl error
Message-Id: <1105971271.193656.51190@z14g2000cwz.googlegroups.com>

Sorry for the delay, I thought I would receive an email once i got a
response to my question. The code that was posted is the actual code,
there isn't another mystery script. I figure out the problem though, it
appears that the function, "KeyAttr=>'sku'" was need for accessing the
complex data structure in memory, as far as line '16' I have no idea
why the script was complaining on this, here is the finshed code that
appears to be working:

#!/usr/bin/perl -wT
use strict;

use XML::Simple;
use Data::Dumper;
use Carp;

my ($xml,$data);

#$xml = new XML::Simple (ForceArray => 1);
$xml = new XML::Simple (KeyAttr=>'sku');

$data = $xml->XMLin("/tmp/config.xml");
#print Dumper($data);

print "$data->{UnixMachine}->{PostBindUID}\n";



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

Date: 17 Jan 2005 08:46:05 -0800
From: s_p_a_m_mob@hotmail.com
Subject: Rearrange graphically a perl module: my poorman solution
Message-Id: <1105980365.417477.84190@f14g2000cwb.googlegroups.com>

The aim is to rearrange graphically a perl module, to move down a
method, move up some pod, etc.

My solution is to add structure to the perl/pod code with html and use
the navigator feature of OpenOffice to graphically move the blocks.

1. add structure to perl/pod code, with following script (perl2html.pl)

---

#!/usr/bin/perl -w


use strict;
use IO::File;
use HTML::Entities;


$ARGV[0] or die qq(Usage: $0  Module.pm > /tmp/Module.html
Then:
~/OpenOffice.org1.1.0/soffice /tmp/Module.html &
rearrange with OO explorer, save as txt file /tmp/Module.txt
perl -pi -e "s/# space//gc" /tmp/Module.txt
cp /tmp/Module.txt /tmp/Module,2.pm
);


my $fh = IO::File->new();
$fh->open("< ".$ARGV[0]) or die "$!";


my $content="";
my $line;


while ($line = <$fh>){


# escape entities
$line = encode_entities($line);


my $tmp;
my $elemName;


if($line =~ m/^(=head(\d) \w.*?)$/ or $line =~ m/^(=cut.*?)$/ or
$line =~ m/^(sub .*?)$/){


$tmp = $1;
$elemName = $2 ? "h$2" : "h1";
# suppress  \n
$tmp =~ s/\n$//;


# add structure for =head1, =head2, etc
$line = qq(<$elemName>$tmp</$elemName>);


}elsif($line =~ m/^[\t ]*\n$/){
# avoid n blank lines -> n-1 blank lines with OO html->txt
$line = "# space<br>";
}else{
$line =~ s/\n$//;
$line ="<pre>".$line."</pre>";
}


# add \n
$content = $content.$line."\n";
}

# add html and
body markup
$content = qq(<html><body>).$content.qq(</body></html>);
print $content;

---

Output to Module.html

2. Open the resulting html file with OpenOffice, and its navigator
feature

(Edit > Navigator)

3. Save as txt file (Module.txt)

4.  perl -pi -e "s/# space//gc" Module.txt

It's done.

Marc-Olivier BERNARD

Hendrik Maryns wrote:

> I'm not sure wether I understand exactly what you want, but eclipse
with
> the EPIC extension for Perl, does something that sounds like this...
It
> does syntax-highlighting and checking too.
That tool seems pretty complicate...



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

Date: 17 Jan 2005 06:51:39 -0800
From: notruf_1102003@yahoo.de (Gerhard M)
Subject: regex: how to %hash2 = grep {/KEY/} %hash1
Message-Id: <942c5b0d.0501170651.135f4b94@posting.google.com>

Hi,
currently i'm using 
 
 %what_i_want = map {$_=>$opts->{$_}} grep {/KEY/} keys %$opts;

or 
 
 %what_i_want = map {/KEY/? ($_=>$opts->{$_}) : ()} keys %$opts;

to extract some keys with it's values out of one hash. 
Is there an easier/nicer way to do this?

Regards 
Gerhard


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

Date: Mon, 17 Jan 2005 15:05:37 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: regex: how to %hash2 = grep {/KEY/} %hash1
Message-Id: <5vQGd.1329$cx2.768@trndny03>

"Gerhard M" <notruf_1102003@yahoo.de> wrote in message
news:942c5b0d.0501170651.135f4b94@posting.google.com...
> currently i'm using
>
>  %what_i_want = map {$_=>$opts->{$_}} grep {/KEY/} keys %$opts;
> or
>  %what_i_want = map {/KEY/? ($_=>$opts->{$_}) : ()} keys %$opts;
>
> to extract some keys with it's values out of one hash.
> Is there an easier/nicer way to do this?

I'm not sure I understand exactly what you're looking for, but I *think*
you want to create a new hash with only the key/value pairs from the
original hash in which the key matches /KEY/.  Is that correct?

I guess you could use a hash slice, but I'm no really convinced this is
either easier or nicer...

my %what_i_want = @$opts{grep /KEY/, keys %$opts};

Paul Lalli



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

Date: Mon, 17 Jan 2005 16:34:15 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: regex: how to %hash2 = grep {/KEY/} %hash1
Message-Id: <3524dfF4h6cmuU1@individual.net>

Paul Lalli wrote:
> I guess you could use a hash slice, but I'm no really convinced this is
> either easier or nicer...
> 
> my %what_i_want = @$opts{grep /KEY/, keys %$opts};

It's not nicer, since it doesn't do what the OP wants.

This is a hash slice construct that works:

     my %what_i_want;
     @what_i_want{ grep /KEY/, keys %$opts } =
       @$opts{grep /KEY/, keys %$opts};

but that's certainly not nicer either. ;-)

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


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

Date: Mon, 17 Jan 2005 16:37:13 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: regex: how to %hash2 = grep {/KEY/} %hash1
Message-Id: <ZQRGd.5345$1l2.4716@trndny05>

"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:3524dfF4h6cmuU1@individual.net...
> Paul Lalli wrote:
> > I guess you could use a hash slice, but I'm no really convinced this
is
> > either easier or nicer...
> >
> > my %what_i_want = @$opts{grep /KEY/, keys %$opts};
>
> It's not nicer, since it doesn't do what the OP wants.
>
> This is a hash slice construct that works:
>
>      my %what_i_want;
>      @what_i_want{ grep /KEY/, keys %$opts } =
>        @$opts{grep /KEY/, keys %$opts};
>
> but that's certainly not nicer either. ;-)

Wow, I was completely off there.  My apologies to the OP for not testing
my code before posting.  Thanks Gunnar for the correction.

Paul



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

Date: 17 Jan 2005 16:38:34 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: regex: how to %hash2 = grep {/KEY/} %hash1
Message-Id: <csgpma$rgf$1@mamenchi.zrz.TU-Berlin.DE>

Paul Lalli <mritty@gmail.com> wrote in comp.lang.perl.misc:
> "Gerhard M" <notruf_1102003@yahoo.de> wrote in message
> news:942c5b0d.0501170651.135f4b94@posting.google.com...
> > currently i'm using
> >
> >  %what_i_want = map {$_=>$opts->{$_}} grep {/KEY/} keys %$opts;
> > or
> >  %what_i_want = map {/KEY/? ($_=>$opts->{$_}) : ()} keys %$opts;
> >
> > to extract some keys with it's values out of one hash.
> > Is there an easier/nicer way to do this?
> 
> I'm not sure I understand exactly what you're looking for, but I *think*
> you want to create a new hash with only the key/value pairs from the
> original hash in which the key matches /KEY/.  Is that correct?
> 
> I guess you could use a hash slice, but I'm no really convinced this is
> either easier or nicer...
> 
> my %what_i_want = @$opts{grep /KEY/, keys %$opts};

The right hand side is a list of the *values* whose keys match /KEY/, in
unspecified order.  Assigning that to a hash doesn't make sense.

Using a hash slice is possible, but unattractive:

    my %what_i_want;
    my @sel = grep /KEY/, keys %$opts;
    @what_i_want{ @sel} = @$opts{ @sel};

Except when there are multiple selections to make from multiple hashes
(so that @sel could be re-used), I'd stick with the OP's first solution.

Anno


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

Date: Mon, 17 Jan 2005 18:16:16 +0100
From: Matija Papec <perl@my-header.org>
Subject: Re: regex: how to %hash2 = grep {/KEY/} %hash1
Message-Id: <vqrnu0hodq08kv9s5jsfjhqpsfu9tiinud@4ax.com>

X-Ftn-To: Gerhard M 

notruf_1102003@yahoo.de (Gerhard M) wrote:
>Hi,
>currently i'm using 
> 
> %what_i_want = map {$_=>$opts->{$_}} grep {/KEY/} keys %$opts;
>
>or 
> 
> %what_i_want = map {/KEY/? ($_=>$opts->{$_}) : ()} keys %$opts;
>
>to extract some keys with it's values out of one hash. 
>Is there an easier/nicer way to do this?

Not really,
$what_i_want{$_}=$opts->{$_} for grep /KEY/, keys %$opts;

but it could make sense to wrap your first example into function,
%what_i_want = MySlice(sub {/KEY/}, $opts);

#untested
sub MySlice {
  my ($sub, $href) = @_;
  my %hash =
    map {$_=>$href->{$_}} 
    grep $sub->(), 
    keys %$href;
  return wantarray ? %hash : \%hash;
}


-- 
Matija


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

Date: 17 Jan 2005 08:41:47 -0800
From: tools55@bluemail.ch
Subject: regex: listing all textstrings to be found more that 2 times in a file
Message-Id: <1105980107.428307.32370@z14g2000cwz.googlegroups.com>

Hi,

is there a way to have(regex) listed all text strings that
are found more than 2/two times in a file?
How could such a regex look like ?
 .
Any tip is appreciated very much. Thank`s, Bill.



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

Date: Mon, 17 Jan 2005 17:39:38 +0100
From: phaylon <phaylon@dunkelheit.at>
Subject: Re: regex: listing all textstrings to be found more that 2 times in a file
Message-Id: <pan.2005.01.17.16.39.38.13029@dunkelheit.at>

tools55 wrote:

> is there a way to have(regex) listed all text strings that are found more
> than 2/two times in a file? How could such a regex look like ?

What do you mean by "text string"?

What should "get me me some beer" give as result? ('e', ' ', 'm')?

g,
Robert

-- 
http://www.dunkelheit.at/
That is not dead, which can eternal lie,
and with strange aeons even death may die.
		-- H.P. Lovecraft



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

Date: Mon, 17 Jan 2005 18:15:11 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: regex: listing all textstrings to be found more that 2 times in a file
Message-Id: <352abeF4hgqp8U1@individual.net>

tools55@bluemail.ch wrote:
> is there a way to have(regex) listed all text strings that
> are found more than 2/two times in a file?

As Robert pointed out, you need to make clear what you mean by "text 
string".

> How could such a regex look like ?

Not just a regex, but maybe something like this:

     my $string = qr(\b[a-z]+\b)i;
     my %seen;
     while (<FILE>) { $seen{$1}++ while /($string)/g }
     print "$_\n" for grep $seen{$_} > 2, keys %seen;

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


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

Date: Mon, 17 Jan 2005 17:54:51 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: regex: listing all textstrings to be found more that 2 times in a file
Message-Id: <csgtr2$88o$1@sun3.bham.ac.uk>

tools55@bluemail.ch wrote:

> is there a way to have(regex) listed all text strings that
> are found more than 2/two times in a file?

A regex works on a string not a file.  So I shall assume you first slurp 
the file into a string (with File::Slurp or suchlike).

/(.+)(?=.*\1.*\1)/gs

Note this is quite inefficient and almost certainly not want you wanted.

Note also this only find the longest non-overlapping match at each 
starting postion.  i.e. in 'foofoodfool' it will find 'foo' but not 'oo' 
and 'o', 'fo' and 'f' all of which also appear at least 3 times.

To get _all_ matches for a pattern you'd want to use the trick I 
described[1] in my Usenet Gems talk at YAPC::Europe::20042[2].

[1] I described it but if you find the original thread you'll see most 
of the credit goes to Abigail.

[2] http://birmingham.pm.org/talks/YAPC-Europe-2003-Gems.pdf



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

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


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