[28923] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 167 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Feb 26 19:17:08 2007

Date: Fri, 23 Feb 2007 11:09:05 -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           Fri, 23 Feb 2007     Volume: 11 Number: 167

Today's topics:
    Re: delete() on multi level hash <mritty@gmail.com>
    Re: fork()-ing questions <dale.schmitz@offutt.af.mil>
        Help with search and replace <pradeep.bg@gmail.com>
    Re: Help with search and replace <glex_no-spam@qwest-spam-no.invalid>
    Re: Help with search and replace <pradeep.bg@gmail.com>
    Re: Help with search and replace <nobull67@gmail.com>
    Re: Help with search and replace <jurgenex@hotmail.com>
        here is the famous godwin script, donated to the usenet <anonymous@panta-rhei.eu.org>
    Re: new method to test Perl code dnikolayev@gmail.com
    Re: problem cp <bik.mido@tiscalinet.it>
    Re: problem cp <john.swilting@wanadoo.fr>
    Re: problem cp <john.swilting@wanadoo.fr>
    Re: problem cp <abigail@abigail.be>
    Re: Regex: Why is overreaching necessary? <jgibson@mail.arc.nasa.gov>
    Re: Text::CSV and Mysql - invalid number of columns azzi.george@gmail.com
        Text::CSV and Mysql azzi.george@gmail.com
    Re: Text::CSV and Mysql usenet@DavidFilmer.com
        Using Substr and Regular expressions. <kent.westmoreland@gmail.com>
    Re: Using Substr and Regular expressions. <marora@gmail.com>
    Re: Using Substr and Regular expressions. <damercer@comcast.net>
    Re: Using Substr and Regular expressions. <xicheng@gmail.com>
    Re: Using Substr and Regular expressions. <rvtol+news@isolution.nl>
    Re: Using Substr and Regular expressions. <nobull67@gmail.com>
    Re: Using Substr and Regular expressions. <marora@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 23 Feb 2007 06:19:35 -0800
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: delete() on multi level hash
Message-Id: <1172240375.664017.270200@t69g2000cwt.googlegroups.com>

On Feb 23, 5:59 am, "moritz.mai...@googlemail.com"
> foreach (@{$list}) {
>         $temp->{$_->{'foo'}}->{$_->{'bar'}} = $_;

Just to add to what John said - do you realize that after this
statement, both $_ and $temp->{w}->{6} are references to the *same*
hash?  That changes to one affect the other?

>         delete($temp->{$_->{'foo'}}->{$_->{'bar'}}->{'bar'});

So when you remove the key 'bar' from the hash referenced by $temp-
>{w}->{6}, you're also removing it from the hash referenced by $_.

>         delete($temp->{$_->{'foo'}}->{$_->{'bar'}}->{'foo'});

Which means at this point that the hash referenced by $_ does not have
the key 'bar' anymore.  That's why you're getting the results you were
seeing.

Paul Lalli



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

Date: 23 Feb 2007 07:09:26 -0800
From: "Monty" <dale.schmitz@offutt.af.mil>
Subject: Re: fork()-ing questions
Message-Id: <1172243366.878111.44270@s48g2000cws.googlegroups.com>

It would appear that I may have stepped on toes with my process of
learning here.  I had a particular goal in mind when I first posted
this question and unfortunately, that goal wasn't clear until it was
achieved: 1) a working understanding of what fork() does, and 2)
clarification regarding the evaluation of assignment statements in a
conditional context.  Even though that second one may not be complete,
I have a functional understanding, thanks entirely to this group, of
the particulars, pitfalls, and benefits.

I probably committed a breech of protocol by not posting snippets of
discussion that led me to conclusions that I, in turn, posted.  My
apologies, but what I've learned has been gleaned over the course of
discussion by osmosis and 'nuance' that I got from the discussion, and
I felt that my focus on the goal would not have been served, in fact
debilitated, by detailed examination of my path though all of this.

I've accomplished what I wanted here, and even if I didn't learn
exactly what you were trying to teach me, I learned enough to write my
own tests for aspects I was unclear on, and I have a better, more
functional knowledge of what's going on.

So please accept my thanks graciously.  I realize my knowledge is
incomplete, but for this task, it's what I need to know.  And without
the help of this group, I never would have gotten there.



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

Date: 23 Feb 2007 09:27:30 -0800
From: "Deepu" <pradeep.bg@gmail.com>
Subject: Help with search and replace
Message-Id: <1172251650.653531.79960@z35g2000cwz.googlegroups.com>

Hi All,

I am trying to replace a word in a line with a different word.

Example:

File1:
THIS IS AN ERROR TEST
THIS IS A TEST
THIS IS NOT A TEST

I need to go through this file and check for ERROR in any line and
replace with FAIL.

Please help me on this.

Code:

#!/usr/local/bin/perl

$testFile = "File1";

open (FH, "$testFile") || die;

while (<FH>) {
  chomp;
  if ($_ =~ /ERROR/) {
    print "$_\n"; ## I get the line with ERROR here
  }
}

Thanks



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

Date: Fri, 23 Feb 2007 11:40:12 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Help with search and replace
Message-Id: <45df26f4$0$25786$815e3792@news.qwest.net>

Deepu wrote:
> Hi All,
> 
> I am trying to replace a word in a line with a different word.
> 
> Example:
> 
> File1:
> THIS IS AN ERROR TEST
> THIS IS A TEST
> THIS IS NOT A TEST
> 
> I need to go through this file and check for ERROR in any line and
> replace with FAIL.
> 
> Please help me on this.
> 
> Code:
> 
> #!/usr/local/bin/perl
> 
> $testFile = "File1";
> 
> open (FH, "$testFile") || die;
Include why.
open( my $fh, '<', $testFile ) or die "Can't open $testFile: $!";

> 
> while (<FH>) {
while( <$fh> ) {
>   chomp;
That's probably not needed.
>   if ($_ =~ /ERROR/) {
if( /ERROR/ ) {
>     print "$_\n"; ## I get the line with ERROR here
       s/ERROR/FAIL/g;
       print;
>   }
> }
close $fh;

That will print the line, that used to contain ERROR,
and now contains FAIL, to STDOUT.

If you want to change it in the file.

perldoc -q 'How do I change one line'

How do I change one line in a file/delete a line in a file/insert a
        line in the middle of a file/append to the beginning of a file?

        Use the Tie::File module, which is included in the standard 
distribution since Perl 5.8.0.

For information on how to use that module:

perldoc Tie::File



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

Date: 23 Feb 2007 09:51:00 -0800
From: "Deepu" <pradeep.bg@gmail.com>
Subject: Re: Help with search and replace
Message-Id: <1172253060.456206.98110@m58g2000cwm.googlegroups.com>

Thanks for the reply..how can i pass search & replace strings as
arguments to the script



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

Date: 23 Feb 2007 10:03:55 -0800
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Help with search and replace
Message-Id: <1172253834.971454.136070@m58g2000cwm.googlegroups.com>

On Feb 23, 5:51 pm, "Deepu" <pradeep...@gmail.com> wrote without
adequate context:

[ please don't do that ]

> Thanks for the reply..how can i pass search & replace strings as
> arguments to the script

Command line arguments to a Perl script are placed in the special
array variable @ARGV.

Be aware, however, that in the solution given earlier in this thread
'ERROR' is being treated as a regular expression _not_ a plain string.

  s/\Q$ARGV[0]/$ARGV[1]/g;

Or slightly more long winded but maybe clearer:

# Ouside the loop
my ($search,$replace) = @ARGV;

# Inside the loop
  s/\Q$search/$replace/g;




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

Date: Fri, 23 Feb 2007 18:15:21 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Help with search and replace
Message-Id: <ZaGDh.7$tA1.1@trndny02>

Deepu wrote:
> Thanks for the reply..

What reply? I don't see any. What are you talking about?

> how can i pass search & replace strings as
> arguments to the script

The same way as you would pass any other argument to any other program: you 
just type them on the command line after the command or script name.

jue 




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

Date: 23 Feb 2007 15:25:35 -0000
From: Anonymous via Panta Rhei <anonymous@panta-rhei.eu.org>
Subject: here is the famous godwin script, donated to the usenet populous!
Message-Id: <0KU7UHT039136.6844328704@anonymous.poster>

#!/usr/bin/perl
require 5;
$VERSION = '1.0';
use File::Slurp;
use warnings;
use Getopt::Std;
use List::Util ('shuffle');
use News::Article;
use strict 'refs';

my(%option, $inputFilename, $outputFilename, @addBody, $code);
my $ctr = 0;
my(@dropGroups) = ();
my(@followups) = ();

# generate messages to enforce Godwin's law
# example:
# $ grep -r -l -i '\(nazi\|hitler\)' /var/spool/news/alt/what/ever |xargs godwin

# By default this script adds Followup-To: alt.dev.null,
# X-No-Archive: Yes, and a X-Code header to keep it from replying
# to its own messages.

# This script just generates messages, it's up to you how you get
# them onto usenet.  Warning, if you post a lot of these so you
# can be traced, you'll probably get TOSsed!

# -G alt.foo,alt.bar
#    drop some Newsgroups
# -N 
#    do not FU alt.dev.null
# -a 
#    do not set X-No-Archive: Yes
# -b FILE
#    use text from FILE instead of default data
# -c 
#    suppress X-Code: random header
# -d DIRECTORY
#    put output file here instead of pwd
# -f 'Some Name foo@bar'
#    use this From header instead of the default
# -u alt.foo,alt.bar
#    add FUs
# -L FILE
#    ignore files not newer than FILE

# This script is hereby donated to the PUBLIC DOMAIN.

getopts 'd:af:b:u:G:NcL:', \%option;

my $maxAge = $option{L} ? (-M $option{L}) : 0;
my $outputDirectory = $option{'d'} || '.';
my $from = $option{'f'} || 'Godwin <godwin@...>';

if ($option{'G'}) {
    @dropGroups = split(/,/, $option{'G'}, 0);
}

push @followups, 'alt.dev.null' unless $option{'N'};
push @followups, split(/,/, $option{'u'}, 0) if $option{'u'};

if ($option{'b'}) {
    @addBody = read_file($option{'b'});
}
else {
    @addBody = <DATA>;
}

foreach $inputFilename (@ARGV) {
    if (($option{L}) && (-M $inputFilename >= $maxAge)) {
        print "$inputFilename  ignored (too old)\n";
    }
    else {
	my $input = 'News::Article'->new($inputFilename);
	if ($input->header('X-Code')) {
	    print "$inputFilename  ignored (X-Code)\n";
	}
	else {
	    my $output = 'News::Article'->new;
	    $output->set_headers('Subject', reSubject($input), 
				 'From', $from, 
				 'References', reRefs($input));
	    $output->set_headers('X-No-Archive', 'Yes') unless $option{'a'};
	    unless ($option{'c'}) {
		$code = `mcookie`;
		chomp $code;
		$output->set_headers('X-Code', $code);
	    }
	    $output->set_body(reBody($input));
	    my(@groupings) = reGroup($input);
	    my $groups;
	    while (@groupings) {
		$outputFilename = getFilename($outputDirectory, $inputFilename);
		$groups = shift @groupings;
		$output->set_headers('Newsgroups', $groups);
		$output->set_headers('Followup-To', fu($groups)) if @followups > 0;
		open OUTFILE, ">$outputFilename";
		$output->write(\*OUTFILE);
		close OUTFILE;
		print "$inputFilename  -->  $outputFilename\n";
	    }
	}
    }
}

sub reSubject {
    my $subject = $_[0]->header('Subject');
    $subject = 'Re: ' . $subject unless $subject =~ /\s*(\[.*\])?\s*Re:/i;
    return $subject;
}

sub reRefs {
    my(@refs) = $_[0]->header('Message-ID');
    my(@oldRefs) = split(/\s+/, $_[0]->header('References') || '', 0);
    unshift @refs, pop @oldRefs if @oldRefs;
    unshift @refs, shift @oldRefs if @oldRefs;
    return join(' ', @refs);
}

sub getFilename {
    my $orig = $_[1];
    $orig =~ s[^.*/][];
    my $fn;
    do {
        $fn = $_[0] . '/' . $orig . '-' . $$ . $ctr++ . '.output'
    } while -f $fn;
    return $fn;
}

sub reGroup {
    my(@groups) = shuffle(split(/,/, $_[0]->header('Newsgroups'), 0));
    if ($option{'G'}) {
        @groups = dropGroups(@groups);
    }
    my(@output) = ();
    while (@groups > 3) {
        push @output, join(',', @groups[0, 1, 2]);
        shift @groups;
        shift @groups;
        shift @groups;
    }
    push @output, join(',', @groups);
    return @output;
}

sub dropGroups {
    my(%groups) = map({$_, 1;} @_);
    foreach my $item (@dropGroups) {
        delete $groups{$item};
    }
    return keys %groups;
}

sub fu {
    my(@groups) = split(/,/, $_[0], 0);
    unshift @groups, @followups;
    if (@groups > 3) {
        @groups = @groups[0, 1, 2];
    }
    return join(',', shuffle(@groups));
}

sub reBody {
    my(@old) = $_[0]->body;
    my(@new) = map({"> $_";} grep({$_ =~ /nazi|hitler/i;} @old));
    push @new, '', @addBody;
    return @new;
}

__DATA__
This thread is terminated by Godwin's Law.

Further posts are prohibited.

Thank you for co-operating.
~~~~~~~~~~~~~~~~~~~~~
This message was posted via one or more anonymous remailing services.
The original sender is unknown.  Any address shown in the From header
is unverified. You need a valid hashcash token to post to groups other
than alt.test and alt.anonymous.messages. Visit www.panta-rhei.eu.org
for abuse and hashcash info.





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

Date: 23 Feb 2007 10:55:10 -0800
From: dnikolayev@gmail.com
Subject: Re: new method to test Perl code
Message-Id: <1172256910.599060.268670@m58g2000cwm.googlegroups.com>

Hello,

Just tested your code.. :)
It works ok..
And Then, I tested your code in default tab - BASIC
You ran this code using BASIC language :))

Well, using your cite..
If you are running Perl program in Basic tab than you are....well....
a newbie :-)))))))))))))))))

Try to run this in basic better:

for i=1 to 10
circle (100,100), i*10
next

Use specific PERL tab or create PERL-type file: .cgi or .pl to run
Perl programs!

As for modules - we support standart modules and CGI..
We are working on this now..
All of this project is done in Perl.. And this is very strange, that
Perl community here is not interested in growing this :)






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

Date: Fri, 23 Feb 2007 15:18:59 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: problem cp
Message-Id: <emttt2pu37hhthh4gaf3sl677oqaq385tc@4ax.com>

On Fri, 23 Feb 2007 13:39:12 +0100, "john.swilting"
<john.swilting@wanadoo.fr> wrote:

>open (FH , $old_file);

unchecked open();

>select(FH);
>rename $old_file ,$new_file;

unchecked rename();

>`cp $new_file /image1`;

backticks in void context;


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Fri, 23 Feb 2007 15:32:04 +0100
From: "john.swilting" <john.swilting@wanadoo.fr>
Subject: Re: problem cp
Message-Id: <45defae4$0$27411$ba4acef3@news.orange.fr>

Abigail wrote:

> Michele Dondi (bik.mido@tiscalinet.it) wrote on MMMMCMXXIV September
> MCMXCIII in <URL:news:vtntt2ptjjljafukbpp1bsiqse57i1vbg0@4ax.com>:
> &&  On Fri, 23 Feb 2007 12:54:05 +0100, "john.swilting"
> && <john.swilting@wanadoo.fr> wrote:
> &&
> && >open (FH , $old_fichier);
> && >select(FH);
> && >rename $new_fichier, $old_fichier;
> && >`cp $new_ficier /image1`
> && >
> && >its nice that
> &&
> &&  IMHO you want either to do it all in Perl or use OS native commands
> &&  altogether. Personally, I would perfer the former.
> 
> 
> I very much prefer the latter.
> 
> 
> 
> Abigail
why


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

Date: Fri, 23 Feb 2007 15:34:26 +0100
From: "john.swilting" <john.swilting@wanadoo.fr>
Subject: Re: problem cp
Message-Id: <45defb71$0$27411$ba4acef3@news.orange.fr>

john.swilting wrote:

> Abigail wrote:
> 
>> Michele Dondi (bik.mido@tiscalinet.it) wrote on MMMMCMXXIV September
>> MCMXCIII in <URL:news:vtntt2ptjjljafukbpp1bsiqse57i1vbg0@4ax.com>:
>> &&  On Fri, 23 Feb 2007 12:54:05 +0100, "john.swilting"
>> && <john.swilting@wanadoo.fr> wrote:
>> &&
>> && >open (FH , $old_fichier);
>> && >select(FH);
>> && >rename $new_fichier, $old_fichier;
>> && >`cp $new_ficier /image1`
>> && >
>> && >its nice that
>> &&
>> &&  IMHO you want either to do it all in Perl or use OS native commands
>> &&  altogether. Personally, I would perfer the former.
>> 
>> 
>> I very much prefer the latter.
>> 
>> 
>> 
>> Abigail
> why
I do not know how I will program the change image to order. on sale


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

Date: 23 Feb 2007 15:01:08 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: problem cp
Message-Id: <slrnetu0d1.gq5.abigail@alexandra.abigail.be>

john.swilting (john.swilting@wanadoo.fr) wrote on MMMMCMXXIV September
MCMXCIII in <URL:news:45defae4$0$27411$ba4acef3@news.orange.fr>:
,,  Abigail wrote:
,,  
,, > Michele Dondi (bik.mido@tiscalinet.it) wrote on MMMMCMXXIV September
,, > MCMXCIII in <URL:news:vtntt2ptjjljafukbpp1bsiqse57i1vbg0@4ax.com>:
,, > &&  On Fri, 23 Feb 2007 12:54:05 +0100, "john.swilting"
,, > && <john.swilting@wanadoo.fr> wrote:
,, > &&
,, > && >open (FH , $old_fichier);
,, > && >select(FH);
,, > && >rename $new_fichier, $old_fichier;
,, > && >`cp $new_ficier /image1`
,, > && >
,, > && >its nice that
,, > &&
,, > &&  IMHO you want either to do it all in Perl or use OS native commands
,, > &&  altogether. Personally, I would perfer the former.
,, > 
,, > 
,, > I very much prefer the latter.
,, > 
,, > Abigail
,,  why


Easy. 'system cp => @files, $destination' is just one line (regardless of
the number of files), works flawlessly, and is flexible.

use File::Copy;
copy $file => $destination;

needs two lines just to copy one file, is broken, is hopelessly inflexible,
and will delete your files if you think 'copy' might mimic the 'cp' command.

If you value your time, and your files, you wouldn't be using File::Copy.


Abigail
-- 
perl -e '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 
         % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %;
         BEGIN {% % = ($ _ = " " => print "Just Another Perl Hacker\n")}'


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

Date: Fri, 23 Feb 2007 09:24:12 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Regex: Why is overreaching necessary?
Message-Id: <230220070924128010%jgibson@mail.arc.nasa.gov>

In article <1172196516.875062.189660@h3g2000cwc.googlegroups.com>,
Shannon Jacobs <Shannon.Jacobs.nospam@gmail.com> wrote:

Please do not top-post.

> I'm not sure how to take your curiosity, but as I noted, here is a
> minimal example of a test case for the latest (but not greatest)
> problem.
> 
>
> http://shanenj.tripod.com/cgi-bin/procbook0.cgi?a_SEARCH_VALUE=2471%7C2396&sea
> rchfields=authornums&sensecase=nocase&sorttype=none&datetype=comp&numorname=au
> thnums
> 
>
> http://shanenj.tripod.com/cgi-bin/procbook0.cgi?a_SEARCH_VALUE=2396%7C2471&sea
> rchfields=authornums&sensecase=nocase&sorttype=none&datetype=comp&numorname=au
> thnums

No one is likely to help you without seeing the code behind these
queries, plus the database on which they are executed. Since, as you
have previously pointed out, that would require posting a great deal of
irrelevant code and data, you need to first reduce the problem to the
minimal amount necessary to reproduce the problem. I suggest that if
you want additional help, you write a small, self-contained test
program that demonstrates the problem with your regex. If you can do
that, someone here will surely be able to help you. It is also possible
that in doing so, you will discover the source of your problem (and it
is not likely to be the fact that /2471|2396/ returns different results
from /2396|2471/).

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


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

Date: 23 Feb 2007 06:31:36 -0800
From: azzi.george@gmail.com
Subject: Re: Text::CSV and Mysql - invalid number of columns
Message-Id: <1172241096.637739.16940@p10g2000cwp.googlegroups.com>

On Feb 23, 12:46 am, "Mumia W." <paduille.4060.mumia.w
+nos...@earthlink.net> wrote:
> On 02/22/2007 07:59 PM, azzi.geo...@gmail.com wrote:
>
> > Dear Perl and MySQL/Perl Gurus,
>
> > Good evening. Am having a problem loading data from a CSV file into a
> > test database. [...]
>
> > my $sql_start = "INSERT INTO Start VALUES (?);";
>
> You need as many question marks as you have values to insert. If you
> have three values to insert, you need three question marks:
>
> ... VALUES (?, ?, ?);
>
> HTH
>
> > [...]
> > DBD::mysql::st execute failed: Column count doesn't match value count
> > at row 1 at ./dbi_file.pl line 45, <GEN0> line 1.
> > [...]
>
> --
> Windows Vista and your freedom in conflict:http://www.regdeveloper.co.uk/2006/10/29/microsoft_vista_eula_analysis/


Sorry I put in the commented out test lines, and didn't want to
include all of the checks to make the script as small as possible.
Either way, appreciate the help. Thanks.



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

Date: 23 Feb 2007 10:14:17 -0800
From: azzi.george@gmail.com
Subject: Text::CSV and Mysql
Message-Id: <1172254457.506011.260150@s48g2000cws.googlegroups.com>

Dear Perl Programmers,

Good morning. Am having a problem loading data from a CSV file into a
test database. Below should be all of the information to duplicate my
error. Please let me know what I am doing wrong as unsure as to how to
remove the beginning [ and ] brackets.  Thanks, and await for your
tested resolution.

Pierre

PERL SCRIPT
---------------------------------------------------------------------------
#!/usr/local/bin/perl

use strict;
use warnings;

use Text::CSV;
use IO::File;
use Data::Dumper;
use DBI;

my $csv = Text::CSV->new;

my $db_host = "x.x.x.x";
my $db_name = "test_db";
my $db_user = "user";
my $db_pass = "";
my $dbh = DBI->connect("dbi:mysql:$db_name:$db_host","$db_user");
                        ; #or die "Cannot connect to the Mysql
database $db_name: $DBI:errstr\n";

my $file = "file_csv.txt";
my $fh = new IO::File "<$file" || die "Could not open $file: $! \n";

while (<$fh>) {

my $line = $_;
my $stts = $csv->parse($_);
my @data = Dumper([$csv->fields()]);

if ($line =~ /^START/) {
my $sql_start = "INSERT INTO Start VALUES (@data);";
my $sth_start = $dbh->prepare($sql_start);
  $sth_start->execute();
}
else ($line =~ /^STOP/) {
my $sql_stop = "INSERT INTO Stop VALUES (@data);";
my $sth_stop = $dbh->prepare($sql_stop);
 $sth_stop->execute();
}

} #end of while loop

FILE CONTENTS BEING IMPORTED
----------------------------------------------------------
START,intent,jobs.,"testing,,can..this,be,a,fieldbyitself,",testing,end,
0,
START,tent,job,"can..this,be,a,field-by-itself,",testing,end,0,1
STOP,,tent,,,job,"can..this,be,a,field-by-itself,",,0,1

MySQL Database Structure
-------------------------------------------------------------------------------

mysql> desc Start;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| field1 | varchar(50) | YES  |     | NULL    |       |
| field2 | varchar(50) | YES  |     | NULL    |       |
| field3 | varchar(50) | YES  |     | NULL    |       |
| field4 | varchar(50) | YES  |     | NULL    |       |
| field5 | varchar(50) | YES  |     | NULL    |       |
| field6 | varchar(50) | YES  |     | NULL    |       |
| field7 | varchar(50) | YES  |     | NULL    |       |
| field8 | varchar(50) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

mysql> desc Stop;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| field1  | varchar(50) | YES  |     | NULL    |       |
| field2  | varchar(50) | YES  |     | NULL    |       |
| field3  | varchar(50) | YES  |     | NULL    |       |
| field4  | varchar(50) | YES  |     | NULL    |       |
| field5  | varchar(50) | YES  |     | NULL    |       |
| field6  | varchar(50) | YES  |     | NULL    |       |
| field7  | varchar(50) | YES  |     | NULL    |       |
| field8  | varchar(50) | YES  |     | NULL    |       |
| field9  | varchar(50) | YES  |     | NULL    |       |
| field10 | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

PERL ERROR
----------------------------------------------------------------------------
DBD::mysql::st execute failed: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near '[
          'START',
          'intent',
          'jobs.',
          'testing,,' at line 1 at ./dbi_test.pl line 35, <GEN0> line
1.
DBD::mysql::st execute failed: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near '[
          'START',
          'tent',
          'job',
          'can..this,be,' at line 1 at ./dbi_test.pl line 35, <GEN0>
line 2.
DBD::mysql::st execute failed: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near '[
          'STOP',
          '',
          'tent',
          '',
          '',
' at line 1 at ./dbi_test.pl line 40, <GEN0> line 3.
callroute:/var/home/collect/scripts >



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

Date: 23 Feb 2007 10:58:17 -0800
From: usenet@DavidFilmer.com
Subject: Re: Text::CSV and Mysql
Message-Id: <1172257097.278958.112760@p10g2000cwp.googlegroups.com>

On Feb 23, 10:14 am, azzi.geo...@gmail.com wrote:
> Thanks, and await for your tested resolution.

Oh, not only are we supposed to fix the problems, but we're supposed
to test it as well.  Would you like a mocha latte while we're working
on it?

Tell ya what - how 'bout I just point out a couple of obvious problems
and let you have another swing at it.


> my @data = Dumper([$csv->fields()]);

You want to know where those [] are coming from?  Well, that's
where...

> my $sql_start = "INSERT INTO Start VALUES (@data);";

No trailing semicolon on DBI statements.  This ain't a SQL shell.

> else ($line =~ /^STOP/) {

????  Did you mean elsif (...

> my $sql_stop = "INSERT INTO Stop VALUES (@data);";

Why don't you print that variable out for debugging purposes?  You
will immediately see the problem caused by trying to load Dumper()
output straight into the database.  This is your biggest problem.


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)



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

Date: 23 Feb 2007 06:44:42 -0800
From: "kent.westmoreland@gmail.com" <kent.westmoreland@gmail.com>
Subject: Using Substr and Regular expressions.
Message-Id: <1172241882.248257.182610@v33g2000cwv.googlegroups.com>

Hello,

I am attempting to take a string similar to the following as input:

response_93_johndoe_1171994965031.xml

and break it up into the following output:

93johndoe.txt

The format of the input will always be of the type
"response_<somenumber>_<somename>_<longstringofnumbers>.xml"

I would like this to be dynamic, such that I can run a perl script
from the command line, and it will take the above file name as input,
and give me the second name as output.

Any Ideas?

My Ultimate goal is to be able to create a log file based on the name
of the input file and store it as a variable to be used later.

Thanks,
Kent



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

Date: 23 Feb 2007 07:19:51 -0800
From: "Manish" <marora@gmail.com>
Subject: Re: Using Substr and Regular expressions.
Message-Id: <1172243991.015368.98160@k78g2000cwa.googlegroups.com>

On Feb 23, 9:44 am, "kent.westmorel...@gmail.com"
<kent.westmorel...@gmail.com> wrote:
> Hello,
>
> I am attempting to take a string similar to the following as input:
>
> response_93_johndoe_1171994965031.xml
>
> and break it up into the following output:
>
> 93johndoe.txt
>
> The format of the input will always be of the type
> "response_<somenumber>_<somename>_<longstringofnumbers>.xml"
>
> I would like this to be dynamic, such that I can run a perl script
> from the command line, and it will take the above file name as input,
> and give me the second name as output.
>
> Any Ideas?
>
> My Ultimate goal is to be able to create a log file based on the name
> of the input file and store it as a variable to be used later.
>
> Thanks,
> Kent

#!/usr/local/bin/perl -w

use strict;
my $pat = qr/response_(\d+)_(\w+)_.*.xml/;
my $d = "response_93_johndoe_1171994965031.xml";
my ($num, $name) = ($d =~ m"$pat");
print "$num$name.txt\n";

Should do the trick.



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

Date: Fri, 23 Feb 2007 09:21:33 -0600
From: "Dan Mercer" <damercer@comcast.net>
Subject: Re: Using Substr and Regular expressions.
Message-Id: <WpidnUqGHNjjm0LYnZ2dnUVZ_hWdnZ2d@comcast.com>


<kent.westmoreland@gmail.com> wrote in message news:1172241882.248257.182610@v33g2000cwv.googlegroups.com...
: Hello,
:
: I am attempting to take a string similar to the following as input:
:
: response_93_johndoe_1171994965031.xml
:
: and break it up into the following output:
:
: 93johndoe.txt
:
: The format of the input will always be of the type
: "response_<somenumber>_<somename>_<longstringofnumbers>.xml"
:
: I would like this to be dynamic, such that I can run a perl script
: from the command line, and it will take the above file name as input,
: and give me the second name as output.
:
: Any Ideas?
:
: My Ultimate goal is to be able to create a log file based on the name
: of the input file and store it as a variable to be used later.

My guess is that if you have to ask this question,  you're not ready
to program in perl yet.  A number of solutions are available:

    use strict;
    use warnings;
    my $num;
    my $name;
    ($num,$name) = (split /_/,$ARGV[0])[1,2];

is just one of many possibilities.

Dan Mercer

:
: Thanks,
: Kent
:




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

Date: 23 Feb 2007 07:25:26 -0800
From: "Xicheng Jia" <xicheng@gmail.com>
Subject: Re: Using Substr and Regular expressions.
Message-Id: <1172244326.343471.48350@8g2000cwh.googlegroups.com>

On Feb 23, 6:44 am, "kent.westmorel...@gmail.com"
<kent.westmorel...@gmail.com> wrote:
> Hello,
>
> I am attempting to take a string similar to the following as input:
>
> response_93_johndoe_1171994965031.xml
>
> and break it up into the following output:
>
> 93johndoe.txt
>
> The format of the input will always be of the type
> "response_<somenumber>_<somename>_<longstringofnumbers>.xml"
>
> I would like this to be dynamic, such that I can run a perl script
> from the command line, and it will take the above file name as input,
> and give me the second name as output.
>
> Any Ideas?
>
> My Ultimate goal is to be able to create a log file based on the name
> of the input file and store it as a variable to be used later.
>
> Thanks,
> Kent

For your input, you can do it on the command line.

echo response_93_johndoe_1171994965031.xml |
   perl -F_ -alne 'print "$F[1]$F[2].txt"'

Regards,
Xicheng



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

Date: Fri, 23 Feb 2007 16:29:05 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Using Substr and Regular expressions.
Message-Id: <ern51m.1bg.1@news.isolution.nl>

kent.westmoreland@gmail.com schreef:

> I am attempting to take a string similar to the following as input:
>     response_93_johndoe_1171994965031.xml
> and break it up into the following output:
>     93johndoe.txt

perl -wle'
  my $input  = q{response_93_johndoe_1171994965031.xml};
  my $output = join(q{}, (split '_', $input)[1..2]) . q{.txt};
  print $output;
'

perl -wle'
  my $input = q{response_93_johndoe_1171994965031.xml};
  print qq{$1$2.txt} if $input =~ /_([0-9]+)_([a-z0-9]+)/;
'

(know that [a-z] can match more than 26 different characters) 

-- 
Affijn, Ruud

"Gewoon is een tijger."


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

Date: 23 Feb 2007 09:54:00 -0800
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Using Substr and Regular expressions.
Message-Id: <1172253240.638321.90750@a75g2000cwd.googlegroups.com>

On Feb 23, 3:19 pm, "Manish" <mar...@gmail.com> wrote:
> On Feb 23, 9:44 am, "kent.westmorel...@gmail.com"
>
>
>
> <kent.westmorel...@gmail.com> wrote:
> > Hello,
>
> > I am attempting to take a string similar to the following as input:
>
> > response_93_johndoe_1171994965031.xml
>
> > and break it up into the following output:
>
> > 93johndoe.txt
>
> > The format of the input will always be of the type
> > "response_<somenumber>_<somename>_<longstringofnumbers>.xml"
>
> > I would like this to be dynamic, such that I can run a perl script
> > from the command line, and it will take the above file name as input,
> > and give me the second name as output.
>
> > Any Ideas?
>
> > My Ultimate goal is to be able to create a log file based on the name
> > of the input file and store it as a variable to be used later.
>
> > Thanks,
> > Kent
>
> #!/usr/local/bin/perl -w
>
> use strict;

Unless compatability with old Perl is an issue "use warnings" is
preferable to the -w switch

> my $pat = qr/response_(\d+)_(\w+)_.*.xml/;

I think it would be better anchored.

my $pat = qr/^response_(\d+)_(\w+)_.*\.xml$/;

> my ($num, $name) = ($d =~ m"$pat");

Although the m operator _allows_ you to use non-standard regex
delimiters,  IMHO, it aids clairy not to do so for no reason:

my ($num, $name) = $d =~ /$pat/;

Actually you can say...

my ($num, $name) = $d =~ $pat;

 ...but IMHO that's less clear.



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

Date: 23 Feb 2007 10:51:22 -0800
From: "Manish" <marora@gmail.com>
Subject: Re: Using Substr and Regular expressions.
Message-Id: <1172256682.497739.287630@8g2000cwh.googlegroups.com>

On Feb 23, 12:54 pm, "Brian McCauley" <nobul...@gmail.com> wrote:
> On Feb 23, 3:19 pm, "Manish" <mar...@gmail.com> wrote:
>
>
>
>
>
> > On Feb 23, 9:44 am, "kent.westmorel...@gmail.com"
>
> > <kent.westmorel...@gmail.com> wrote:
> > > Hello,
>
> > > I am attempting to take a string similar to the following as input:
>
> > > response_93_johndoe_1171994965031.xml
>
> > > and break it up into the following output:
>
> > > 93johndoe.txt
>
> > > The format of the input will always be of the type
> > > "response_<somenumber>_<somename>_<longstringofnumbers>.xml"
>
> > > I would like this to be dynamic, such that I can run a perl script
> > > from the command line, and it will take the above file name as input,
> > > and give me the second name as output.
>
> > > Any Ideas?
>
> > > My Ultimate goal is to be able to create a log file based on the name
> > > of the input file and store it as a variable to be used later.
>
> > > Thanks,
> > > Kent
>
> > #!/usr/local/bin/perl -w
>
> > use strict;
>
> Unless compatability with old Perl is an issue "use warnings" is
> preferable to the -w switch
>

sure

> > my $pat = qr/response_(\d+)_(\w+)_.*.xml/;
>
> I think it would be better anchored.
>
> my $pat = qr/^response_(\d+)_(\w+)_.*\.xml$/;
>

sure

> > my ($num, $name) = ($d =~ m"$pat");
>
> Although the m operator _allows_ you to use non-standard regex
> delimiters,  IMHO, it aids clairy not to do so for no reason:
>

sorry, couldn't follow *use non-standard regex ...*, can you explain a
little more?

> my ($num, $name) = $d =~ /$pat/;
>
> Actually you can say...
>
> my ($num, $name) = $d =~ $pat;
>
> ...but IMHO that's less clear.- Hide quoted text -
>
> - Show quoted text -




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

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 V11 Issue 167
**************************************


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