[32765] in Perl-Users-Digest
Perl-Users Digest, Issue: 4029 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Sep 8 18:09:41 2013
Date: Sun, 8 Sep 2013 15:09:05 -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 Sun, 8 Sep 2013 Volume: 11 Number: 4029
Today's topics:
Re: eval package <gravitalsun@hotmail.foo>
Re: exec and named pipe questions <ben@morrow.me.uk>
Re: exec and named pipe questions <ben@morrow.me.uk>
Re: exec and named pipe questions <derykus@gmail.com>
Re: exec and named pipe questions <dave@invalid.invalid>
Re: exec and named pipe questions <dave@invalid.invalid>
Re: exec and named pipe questions <dave@invalid.invalid>
Re: exec and named pipe questions <rweikusat@mobileactivedefense.com>
Re: exec and named pipe questions <ben@morrow.me.uk>
Re: exec and named pipe questions <dave@invalid.invalid>
Re: exec and named pipe questions <rweikusat@mobileactivedefense.com>
Re: exec and named pipe questions <ben@morrow.me.uk>
Re: File deduplication <news@lawshouse.org>
Re: korean character sets <cal@example.invalid>
Re: skipping blank array items <hjp-usenet3@hjp.at>
Re: skipping blank array items <cartercc@gmail.com>
Re: skipping blank array items <willem@turtle.stack.nl>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 07 Sep 2013 22:05:53 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: eval package
Message-Id: <l0ftel$t4g$1@news.ntua.gr>
Στις 7/9/2013 19:31, ο/η Dr.Ruud έγραψε:
> On 07/09/2013 17:58, George Mpouras wrote:
>> Στις 7/9/2013 01:18, ο/η George Mpouras έγραψε:
>
>>> I have a module /SomeDir/SomeCode.pm with a package name retrieved from
>>> its dynamic name, but when I use eval
>>>
>>> ( my $PackageName = __FILE__ ) =~s/(?i)^.*?([^\/]+?)\.pm$/$1/;
>>> eval "package $PackageName;";
>>>
>>> the calling script can not find its subroutines.
>>> ( the $PackageName has the correct value "SomeCode" )
>>> If i remove eval and just say
>>>
>>> package SomeCode;
>>>
>>> everything is ok; What happens here ?
>>
>>
>>
>> i do not think there is a solution to that. eval is pushing the
>> evaluated code one scope deeper than the current, so actually it is not
>> the same package any more.
>
> Just put the code in the same string. But what on earth are you trying
> to achieve? Maybe you are looking for AutoLoader?
>
its a complex file parsing and flow chart service , delivering results
to multiple destinations.
At some point may execute user defined functions from pluggable external
(Perl) modules.
------------------------------
Date: Sat, 7 Sep 2013 19:35:05 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: exec and named pipe questions
Message-Id: <p78sfa-g391.ln1@anubis.morrow.me.uk>
Quoth "Dave Saville" <dave@invalid.invalid>:
> I am writing a perl daemon on a Raspberry Pi.
>
> The perl script talks and listens to another process that it has
> started via fork/exec.
>
> Normally when one forks it is usual to close unneeded file handles -
> the first question then is should one close *all* the open handles if
> you are going to call exec anyway?
You should close any filehandles which you don't want open in the child
and which are not marked close-on-exec. Perl filehandles usually are,
except for STDIN, STDOUT and STDERR; this can be changed using $^F (a
bad idea) or with fcntl. See Fcntl and fcntl(2).
Obviously you also need to ensure any handles you *do* want the execed
process to inherit are *not* marked close-on-exec.
> Secondly, I was under the impression that it did not matter in which
> order named pipes are opened. The forked process is reading one named
> pipe and writing to a second. But more often than not my perl script
> hangs trying to open one.
>
> open my $_STDOUT, '<', 'fifo_stdout' or die "Can't open fifo_stdout
> $!";
>
> never returns.
It should return once something has opened the pipe for writing. Does
this not happen?
Ben
------------------------------
Date: Sat, 7 Sep 2013 19:36:06 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: exec and named pipe questions
Message-Id: <m98sfa-g391.ln1@anubis.morrow.me.uk>
Quoth "Dave Saville" <dave@invalid.invalid>:
> On Sat, 7 Sep 2013 15:29:13 UTC, Rainer Weikusat
> <rweikusat@mobileactivedefense.com> wrote:
>
> > Opening a FIFO for reading will block until 'something else' opens it
> > for writing.
>
> I had worked that out by now :-) - Google suggests cheating by opening
> +< which seems to work fine.
The only problem with this is that you will not get an EOF when the
other end closes the fifo, since you still have it open for writing.
Ben
------------------------------
Date: Sat, 07 Sep 2013 13:42:33 -0700
From: Charles DeRykus <derykus@gmail.com>
Subject: Re: exec and named pipe questions
Message-Id: <l0g33r$k6u$1@speranza.aioe.org>
On 9/7/2013 7:14 AM, Dave Saville wrote:
> ...
But more often than not my perl script
> hangs trying to open one.
>
> open my $_STDOUT, '<', 'fifo_stdout' or die "Can't open fifo_stdout
> $!";
>
> never returns.
>
Since the open for read blocks until it's created on the other end, you
may want to set the handle non-blocking in case the writer's open
failed. (You could add a wait/retry with a timeout to recover from
a delayed open)
use Fcntl;
sysopen($_STDOUT, "fifo_stdout", O_RDONLY|O_NONBLOCK) or die ...'
This also avoids the ambiguity of opening nonblocking via '+<' but then
not knowing if the writer closed the handle.
--
Charles DeRykus
------------------------------
Date: Sun, 8 Sep 2013 08:56:04 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: exec and named pipe questions
Message-Id: <fV45K0OBJxbE-pn2-DJTnckomxEg9@paddington.bear.den>
On Sat, 7 Sep 2013 18:36:06 UTC, Ben Morrow <ben@morrow.me.uk> wrote:
>
> Quoth "Dave Saville" <dave@invalid.invalid>:
> > On Sat, 7 Sep 2013 15:29:13 UTC, Rainer Weikusat
> > <rweikusat@mobileactivedefense.com> wrote:
> >
> > > Opening a FIFO for reading will block until 'something else' opens it
> > > for writing.
> >
> > I had worked that out by now :-) - Google suggests cheating by opening
> > +< which seems to work fine.
>
> The only problem with this is that you will not get an EOF when the
> other end closes the fifo, since you still have it open for writing.
Hi Ben
In this particular case that is not a problem.
--
Regards
Dave Saville
------------------------------
Date: Sun, 8 Sep 2013 09:02:30 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: exec and named pipe questions
Message-Id: <fV45K0OBJxbE-pn2-5bdIdpppZndi@paddington.bear.den>
On Sat, 7 Sep 2013 18:35:05 UTC, Ben Morrow <ben@morrow.me.uk> wrote:
>
> Quoth "Dave Saville" <dave@invalid.invalid>:
> > I am writing a perl daemon on a Raspberry Pi.
> >
> > The perl script talks and listens to another process that it has
> > started via fork/exec.
> >
> > Normally when one forks it is usual to close unneeded file handles -
> > the first question then is should one close *all* the open handles if
> > you are going to call exec anyway?
>
> You should close any filehandles which you don't want open in the child
> and which are not marked close-on-exec. Perl filehandles usually are,
> except for STDIN, STDOUT and STDERR; this can be changed using $^F (a
> bad idea) or with fcntl. See Fcntl and fcntl(2).
>
Ah, that makes sense. I have put specific closes in anyway. I was just
wondering about use counts if everything gets wiped by exec().
> Obviously you also need to ensure any handles you *do* want the execed
> process to inherit are *not* marked close-on-exec.
>
> > Secondly, I was under the impression that it did not matter in which
> > order named pipes are opened. The forked process is reading one named
> > pipe and writing to a second. But more often than not my perl script
> > hangs trying to open one.
> >
> > open my $_STDOUT, '<', 'fifo_stdout' or die "Can't open fifo_stdout
> > $!";
> >
> > never returns.
>
> It should return once something has opened the pipe for writing. Does
> this not happen?
Well it would if there were not another pipe in the other direction.
:-) Deadlock. One way around it was to put the open into another
thread - so it does not matter if that blocks and the other was the +<
trick. I will try Charles' O_NONBLOCK as well.
--
Regards
Dave Saville
------------------------------
Date: Sun, 8 Sep 2013 10:21:49 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: exec and named pipe questions
Message-Id: <fV45K0OBJxbE-pn2-hkT8m46vySVg@paddington.bear.den>
On Sat, 7 Sep 2013 20:42:33 UTC, Charles DeRykus <derykus@gmail.com>
wrote:
> On 9/7/2013 7:14 AM, Dave Saville wrote:
> > ...
> But more often than not my perl script
> > hangs trying to open one.
> >
> > open my $_STDOUT, '<', 'fifo_stdout' or die "Can't open fifo_stdout
> > $!";
> >
> > never returns.
> >
>
> Since the open for read blocks until it's created on the other end, you
> may want to set the handle non-blocking in case the writer's open
> failed. (You could add a wait/retry with a timeout to recover from
> a delayed open)
>
> use Fcntl;
> sysopen($_STDOUT, "fifo_stdout", O_RDONLY|O_NONBLOCK) or die ...'
>
> This also avoids the ambiguity of opening nonblocking via '+<' but then
> not knowing if the writer closed the handle.
>
Thanks Charles, works a treat - once I put a check for defined in the
read loop :-) Never used non blocking I/O before.
--
Regards
Dave Saville
------------------------------
Date: Sun, 08 Sep 2013 12:07:20 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: exec and named pipe questions
Message-Id: <87ppsjo9lj.fsf@sapphire.mobileactivedefense.com>
Charles DeRykus <derykus@gmail.com> writes:
[FIFO]
> Since the open for read blocks until it's created on the other end,
> you may want to set the handle non-blocking in case the writer's open
> failed. (You could add a wait/retry with a timeout to recover from
> a delayed open)
>
> use Fcntl;
> sysopen($_STDOUT, "fifo_stdout", O_RDONLY|O_NONBLOCK) or die ...'
>
> This also avoids the ambiguity of opening nonblocking via '+<' but then
> not knowing if the writer closed the handle.
Using a mode of +< is not 'a non-blocking open' but an open in
'read-write mode'. According to the Linux fifo(4) man page,
POSIX/UNIX(*) leaves the behaviour of that undefined. At least on
Linux, it will succeed on the grounds that the process opening the
FIFO in this way is both a reader and a writer. Provided a FIFO was
successfully opened for reading in blocking mode, an EOF condition
(read returning 0 bytes read) will occur once the last writer closed
the FIFO. Obviously, this can never be observed on an O_RDWR-mode
filecriptor referring to the FIFO.
------------------------------
Date: Sun, 8 Sep 2013 13:25:45 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: exec and named pipe questions
Message-Id: <9v6ufa-eho1.ln1@anubis.morrow.me.uk>
Quoth "Dave Saville" <dave@invalid.invalid>:
> On Sat, 7 Sep 2013 18:35:05 UTC, Ben Morrow <ben@morrow.me.uk> wrote:
> > Quoth "Dave Saville" <dave@invalid.invalid>:
> > >
> > > open my $_STDOUT, '<', 'fifo_stdout' or die "Can't open fifo_stdout
> > > $!";
> > >
> > > never returns.
> >
> > It should return once something has opened the pipe for writing. Does
> > this not happen?
>
> Well it would if there were not another pipe in the other direction.
> :-) Deadlock.
If you want two-way communication you might be better off with a
Unix-domain socket instead.
Ben
------------------------------
Date: Sun, 8 Sep 2013 13:22:19 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: exec and named pipe questions
Message-Id: <fV45K0OBJxbE-pn2-2wyokfERsCOd@paddington.bear.den>
On Sun, 8 Sep 2013 12:25:45 UTC, Ben Morrow <ben@morrow.me.uk> wrote:
>
> Quoth "Dave Saville" <dave@invalid.invalid>:
> > On Sat, 7 Sep 2013 18:35:05 UTC, Ben Morrow <ben@morrow.me.uk> wrote:
> > > Quoth "Dave Saville" <dave@invalid.invalid>:
> > > >
> > > > open my $_STDOUT, '<', 'fifo_stdout' or die "Can't open fifo_stdout
> > > > $!";
> > > >
> > > > never returns.
> > >
> > > It should return once something has opened the pipe for writing. Does
> > > this not happen?
> >
> > Well it would if there were not another pipe in the other direction.
> > :-) Deadlock.
>
> If you want two-way communication you might be better off with a
> Unix-domain socket instead.
>
Hi Ben
And how would one persuade another process to use the socket for its
STDOUT?
exec 'foo -input=input_fifo > output_fifo';
--
Regards
Dave Saville
------------------------------
Date: Sun, 08 Sep 2013 14:28:57 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: exec and named pipe questions
Message-Id: <87ob83e92e.fsf@sapphire.mobileactivedefense.com>
"Dave Saville" <dave@invalid.invalid> writes:
> On Sun, 8 Sep 2013 12:25:45 UTC, Ben Morrow <ben@morrow.me.uk> wrote:
>
>>
>> Quoth "Dave Saville" <dave@invalid.invalid>:
>> > On Sat, 7 Sep 2013 18:35:05 UTC, Ben Morrow <ben@morrow.me.uk> wrote:
>> > > Quoth "Dave Saville" <dave@invalid.invalid>:
>> > > >
>> > > > open my $_STDOUT, '<', 'fifo_stdout' or die "Can't open fifo_stdout
>> > > > $!";
>> > > >
>> > > > never returns.
>> > >
>> > > It should return once something has opened the pipe for writing. Does
>> > > this not happen?
>> >
>> > Well it would if there were not another pipe in the other direction.
>> > :-) Deadlock.
>>
>> If you want two-way communication you might be better off with a
>> Unix-domain socket instead.
>>
>
> Hi Ben
>
> And how would one persuade another process to use the socket for its
> STDOUT?
>
> exec 'foo -input=input_fifo > output_fifo';
By redirecting its standard output file descriptor (1, associated with
the STDOUT stream in perl) to the socket file descriptor:
open(STDOUT, '>&', $sockfh);
------------------------------
Date: Sun, 8 Sep 2013 22:29:55 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: exec and named pipe questions
Message-Id: <jr6vfa-9sq1.ln1@anubis.morrow.me.uk>
Quoth "Dave Saville" <dave@invalid.invalid>:
> On Sun, 8 Sep 2013 12:25:45 UTC, Ben Morrow <ben@morrow.me.uk> wrote:
> >
> > If you want two-way communication you might be better off with a
> > Unix-domain socket instead.
>
> And how would one persuade another process to use the socket for its
> STDOUT?
>
> exec 'foo -input=input_fifo > output_fifo';
Redirect STDOUT before calling exec (and omit the redirection,
obviously). Depending on what the program is, you can probably handle
the input by simply redirecting STDIN (to the same socket handle) and
omitting the -input argument; if the program insists on one,
-input=/dev/fd/0 might suffice.
If you specifically need foo's stdin to remain attached to the terminal
(unlikely...) you can redirect STDOUT to the socket handle and then pass
-input=/dev/fd/1: that is, you are explicitly telling the program to use
stdout as its input. Since the handle is rw this will work.
See also IPC::Open2, which uses anon pipes much as you are using fifos,
which avoids deadlock, and IPC::Run, which lets you do all sorts of
different redirections using a shell-like syntax.
Ben
------------------------------
Date: Sat, 07 Sep 2013 20:49:05 +0100
From: Henry Law <news@lawshouse.org>
Subject: Re: File deduplication
Message-Id: <xvidnfZFn9auHrbPnZ2dnUVZ8rudnZ2d@giganews.com>
On 03/09/13 20:58, John Bokma wrote:
> I
> suggest you use a kill file.
Quite so; but I still end up reading the follow-ups telling George where
to get off. Sigh ... at least they often provide some interest and
education.
--
Henry Law Manchester, England
------------------------------
Date: Sat, 07 Sep 2013 15:38:19 -0700
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: korean character sets
Message-Id: <L6ednejx3ZRAN7bPnZ2dnUVZ_vudnZ2d@supernews.com>
On 08/28/2013 07:17 AM, Charlton Wilbur wrote:
>>>>>> "C" == Cal Dershowitz <cal@example.invalid> writes:
>
> C> I think my machine or my brain wasn't working.
>
> Finally we are all on the same page.
>
> Charlton
>
With the exception of Ben, I wouldn't say I was bucking any help. I'll
freely admit that it was a bit of a crash landing, but I did get from
there to here by wondering why Ben made the mistake of thinking this had
anything to do with php. When does Ben ever make mistakes ...he
inherits them from me...aha....
I ended up writing out any elements of php. What follows is the
approximate functionality I was looking for. One can't really talk
about templating without some file structure of things that already
work. For any given workspace, I keep the templates in a subdirectory.
Now that I'm using Text::Template to make substitutions, it's come a
long way:
$ cd template_stuff/
$ cat hc_input2.txt
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="/css/{$css_file}"/>
<title>{$title}</title>
</head>
<body>
<div class="wrapper">
<div class = origin>
<h3>{$place}, {$date}</h3>
</div>
<h1>{$headline}</h1>
$ cat footer_center.txt
</div>
</div> <!-- end #wrapper -->
</body>
<div class="site_footer">
<p>
<a href="/">Home</a>
<a href="/pages/about/">About</a>
<a href="/pages/contact">Contact</a>
<a href="/pages/links">Links</a>
<a href="/pages/portfolio">Portfolio</a>
</p>
</div>
<div class="jigsaw">
<p>
<a href="http://jigsaw.w3.org/css-validator/check/referer">
<img style="border:0;width:88px;height:31px;"
src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
alt="Valid CSS!" />
</a>
</p>
</div>
</html>
$ cd ..
$ cat bc1.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use File::Basename;
use Cwd;
use Text::Template;
# diamond bracket in paragraph mode declared at top scope
$/ = "";http://merrillpjensen.com/pages/yardwork_3.html
## usage needs 1 arg from argv my_ftp
#identity and config
my $ident = 'ident.txt';
my ( $config, $domain );
$config = do($ident);
unless ($config) {
die("read error: $!") if $!;
die("parse error: $@") if $@;
}
$domain = $config->{ $ARGV[0] };
die("unknown domain: $ARGV[0]") unless $domain;
# get local files
my $path = "images1/";
my @files = <$path*>;
print " files are @files\n";
#dial up the server
my $ftp = Net::FTP->new( $domain->{domain}, Debug => 1, Passive => 1 )
or die "Can't connect: $@\n";
$ftp->login( $domain->{username}, $domain->{password} )
or die "Couldn't login\n";
$ftp->binary();
# go to /pages
$ftp->cwd("/pages") or die "Cannot change working directory ",
$ftp->message;
# show working directory
my $dir = cwd;
my $word = basename($dir);
# get files from remote root that end in php:
my @remote_files = $ftp->ls();
# make unique .html choice
my @matching = map /${word}_(\d+)\.html/, @remote_files;
print "matching is @matching\n";
push( @matching, 0 );
@matching = sort { $a <=> $b } @matching;
my $winner = pop @matching;
my $newnum1 = $winner + 1;
my $word2 = "${word}_$newnum1";
print " new word is $word2\n";
my $html_file = "${word2}.html";
print "html file is $html_file\n";
#make directory in images to correspond
$ftp->cdup();
$ftp->cwd("images/")
or die "Cannot change working directory ", $ftp->message;
$ftp->mkdir("${word2}/")
or warn "Cannot create directory ", $ftp->message;
$ftp->cdup();
# create unicode html file
open( my $fh, ">:utf8", $html_file )
or die("Can't open $html_file for writing: $!");
my $input = "template_stuff/hc_input2.txt";
# write in the header
open( my $gh, "<:utf8", $input ) or die("Can't open: $!");
my $template2 = Text::Template->new(SOURCE => $input)
or die "Couldn't construct template: $Text::Template::ERROR";
my %vars = (
title => $word,
headline => 'No war for us, thank you',
place => 'Oakland',
css_file => 'rad1.css',
);
my $now_string = localtime;
$vars{date} = $now_string;
my $result = $template2->fill_in(HASH => \%vars);
if (defined $result) { print $fh $result }
else { die "Couldn't fill in template: $Text::Template::ERROR" };
#create template outside loop
my $template = <<'TEMPLATE';
<img src="http://www.merrillpjensen.com/images/%s"/>
<p>%s</p>
<p>%s</p>
TEMPLATE
open my $CAPTIONS, "<:utf8", "eng_captions" or die "file not there\n";
open my $CAPTIONS2, "<:utf8", "trans_captions" or die "file not there\n";
$ftp->cwd("/images/${word2}/") or die "cwd failed $@\n";
# main control
for my $name (@files) {
print "name is $name\n";
my $remote_file = basename($name);
print "remote is $remote_file\n";
$ftp->put( $name, $remote_file ) or die "put failed $!\n";
# captions
my $caption = <$CAPTIONS>;
my $caption2 = <$CAPTIONS2>;
printf $fh $template, "${word2}/" . $remote_file, $caption, $caption2;
}
# write in the footer
my $footer = "template_stuff/footer_center.txt";
open( my $hh, "<:utf8", $footer )
or die("Can't open: $!");
while (<$hh>) {
print $fh $_;
}
close $fh;
$ftp->pwd();
$ftp->ls();
$ftp->cdup() or die "cdup failed $@\n";
$ftp->cdup() or die "cdup failed $@\n";
$ftp->cwd("/pages") or die "Cannot change working directory ",
$ftp->message;
$ftp->put($html_file) or die "put failed $@\n";
$
http://merrillpjensen.com/pages/yardwork_3.html
This is the functionality I hope to achieve in the original post.
Believe it or not, Charlton, your response helped me too, so I thank you
for it. Have a nice day.
--
Cal
------------------------------
Date: Sun, 8 Sep 2013 13:24:28 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: skipping blank array items
Message-Id: <slrnl2onjc.aag.hjp-usenet3@hrunkner.hjp.at>
On 2013-09-06 18:56, ccc31807 <cartercc@gmail.com> wrote:
> I have a csv file with 10K items. The header looks something like this:
> CustNo,Name,FirstPaidItem,FirstOrderedItem,FirstShippedItem,Items
>
> The first five fields are single valued. The last field (Items) has
> many items. Lines may look like this:
>
> 1,Joe,a,a,a,a,b,c,d,e,f,g,h,i
> 2,Jane,a,a,b,a,,,,,,a,b,c,d,e
> 3,Jim,b,a,a,a,b,c,d,e,f,g,h,i
> 4,Jill,b,b,b,b,,,,,c,,b,,e,f,g,h
>
> I parse the file like this, putting the first five values in scalars
> and the remainder in the @items array:
> foreach line
> my ($custno,$name,$paid,$ordered,$shipped,@items) = split on the commas
>
> The objective is to take the first item in the @items array and match
> it to one (or more) of $paid, $ordered, or $shipped. I just want to
> grab the first item in the @items array that has a value, IOW, skip
> all the blank items (as in line 2 above) that precede the first actual
> value.
>
> Is there a quick and dirty way to do this without having to do this>
If $custno, $name, $paid, $ordered, $shipped are guaranteed to be
non-empty, you could split on /,+/ to skip empty items, and if you need
only the first, use a scalar instead of an array.
hp
--
_ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | | Man feilt solange an seinen Text um, bis
| | | hjp@hjp.at | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpat. -- Ralph Babel
------------------------------
Date: Sun, 8 Sep 2013 14:18:26 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: skipping blank array items
Message-Id: <ce12386c-9ee1-4926-8ad2-06f7d594379e@googlegroups.com>
On Sunday, September 8, 2013 7:24:28 AM UTC-4, Peter J. Holzer wrote:
> If $custno, $name, $paid, $ordered, $shipped are guaranteed to be
> non-empty, you could split on /,+/ to skip empty items, and if you need
> only the first, use a scalar instead of an array.
Unfortunately,. only the first two items are guaranteed. This is the real w=
orld, and sometimes items are shipped without being ordered, sometimes item=
s are ordered without being shipped, and strange as it may be, items are pa=
id for without being ordered or shipped. The first thing I had to do was to=
validate the file, and each of the shipped, paid, and ordered columns had =
an error rate of around 20 percent. In this instance, it's troubling but no=
t a bid deal, as the real critical piece of information is the value of the=
first item in the @items array.
CC.
------------------------------
Date: Sun, 8 Sep 2013 21:55:40 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: skipping blank array items
Message-Id: <slrnl2psis.f07.willem@turtle.stack.nl>
ccc31807 wrote:
) I have a csv file with 10K items. The header looks something like this:
) CustNo,Name,FirstPaidItem,FirstOrderedItem,FirstShippedItem,Items
)
) The first five fields are single valued. The last field (Items) has many
) items. Lines may look like this:
)
) 1,Joe,a,a,a,a,b,c,d,e,f,g,h,i
) 2,Jane,a,a,b,a,,,,,,a,b,c,d,e
) 3,Jim,b,a,a,a,b,c,d,e,f,g,h,i
) 4,Jill,b,b,b,b,,,,,c,,b,,e,f,g,h
)
) I parse the file like this, putting the first five values in scalars and
) the remainder in the @items array:
) foreach line
) my ($custno,$name,$paid,$ordered,$shipped,@items) = split on the commas
)
) The objective is to take the first item in the @items array and match it
) to one (or more) of $paid, $ordered, or $shipped. I just want to grab the
) first item in the @items array that has a value, IOW, skip all the blank
) items (as in line 2 above) that precede the first actual value.
)
) Is there a quick and dirty way to do this without having to do this>
)
) my $item = '';
) foreach my $ele (@items)
) {
) $item = $ele if $ele =~ /\w/;
) last;
) }
I haven't seen this one yet (perhaps I missed it):
my ($custno,$name,$paid,$ordered,$shipped,@items) = split /,+/;
But I believe it's the simplest way to get what you want.
To explain: This splits on one-or-more-commas. (Split takes a regex.)
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 4029
***************************************