[32907] in Perl-Users-Digest
Perl-Users Digest, Issue: 4185 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Apr 4 09:10:04 2014
Date: Fri, 4 Apr 2014 06:09:04 -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 Fri, 4 Apr 2014 Volume: 11 Number: 4185
Today's topics:
here documents <rweikusat@mobileactivedefense.com>
Re: read and parse a single line file <jimsgibson@gmail.com>
Re: read and parse a single line file <gamo@telecable.es>
Re: read and parse a single line file <rweikusat@mobileactivedefense.com>
Re: read and parse a single line file <rweikusat@mobileactivedefense.com>
Re: read and parse a single line file <john@castleamber.com>
Re: read and parse a single line file <rweikusat@mobileactivedefense.com>
Re: read and parse a single line file <john@castleamber.com>
Re: read and parse a single line file <hjp-usenet3@hjp.at>
Re: read and parse a single line file <gravitalsun@hotmail.foo>
Re: read and parse a single line file <rweikusat@mobileactivedefense.com>
Re: read and parse a single line file <rweikusat@mobileactivedefense.com>
Re: read and parse a single line file <gravitalsun@hotmail.foo>
Re: read and parse a single line file <rweikusat@mobileactivedefense.com>
Re: read and parse a single line file <gravitalsun@hotmail.foo>
Re: read and parse a single line file <gravitalsun@hotmail.foo>
Re: read and parse a single line file <rweikusat@mobileactivedefense.com>
Re: read and parse a single line file <rweikusat@mobileactivedefense.com>
Re: read and parse a single line file <gravitalsun@hotmail.foo>
Re: read and parse a single line file <gravitalsun@hotmail.foo>
Re: read and parse a single line file <rweikusat@mobileactivedefense.com>
Re: read and parse a single line file <gravitalsun@hotmail.foo>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 03 Apr 2014 22:29:16 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: here documents
Message-Id: <87mwg2xftf.fsf@sable.mobileactivedefense.com>
I'm currently forced to use/ write some non-entirely-trivial Javascript
code for the first time in my life. This code is going to be mostly
static with a few (one or two) parameters interpolated into it. And I
need to use it in the context of more than one 'generated web
page'. Because of this, I'm using a dedicated CGI script to write this
code and presently, the relevant section looks like this (trivial early
example because I'm not really familiar with the language and need to
'find a way through it' first):
print("Content-Type: text/javascript\n\n");
printf(<<TT, $name);
function showName()
{
alert('%s')
setTimeout(showName, 5000)
}
setTimeout(showName, 5000)
TT
Something like this was posted as supposedly deterrent example here a
while ago. In contrast to that, I think this is really an example of a
here-document coming in extremely handy because I can just put the
alien code into the Perl-script without it tripping up my editor and
without using a strangely-formatted printf-statement.
------------------------------
Date: Tue, 01 Apr 2014 16:27:30 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: read and parse a single line file
Message-Id: <010420141627300263%jimsgibson@gmail.com>
In article <87a9c4n38g.fsf@sable.mobileactivedefense.com>, Rainer
Weikusat <rweikusat@mobileactivedefense.com> wrote:
> Right now, I'm dealing with (two) single line files whose single line
> contains data in the form of
>
> YYYYMMDD XXXX
>
> the first being a date and the second a counter. So far, I've been using
> a pretty conventional
>
> $rc = <$fh>;
> chomp($rc);
> ($date, $counter) = split(/\s+/, $rc);
>
> for getting the data out of the file. While working with this code in
> order to add some features to it, it came to me that
>
> ($date, $counter) = split for <$fh>
>
> works as well, as does
>
> ($date, $counter) = map { split } <$fh>;
>
> but I like the first one better. Comments or alternate suggestions?
>
<> in scalar context will read one line, whereas <> in list context
will read the entire file. Since your files only have one line, it
doesn't matter. But what if in the future a blank line gets added to
the end of the file. I would prefer that my code still worked, so I
would prefer a solution that keeps <> in scalar context and only reads
the first line.
What about this:
($date, $counter) = split(' ',<$fh>);
That has <> in scalar context and also takes advantage of the
skip-any-null-fields-at-the-beginning feature of split with a single
space first argument, which is the default for split with no arguments
as in your second and third solutions.
--
Jim Gibson
------------------------------
Date: Wed, 02 Apr 2014 12:50:29 +0200
From: gamo <gamo@telecable.es>
Subject: Re: read and parse a single line file
Message-Id: <lhgq1q$l16$1@speranza.aioe.org>
El 02/04/14 01:27, Jim Gibson escribi:
> In article <87a9c4n38g.fsf@sable.mobileactivedefense.com>, Rainer
> Weikusat <rweikusat@mobileactivedefense.com> wrote:
>
>> Right now, I'm dealing with (two) single line files whose single line
>> contains data in the form of
>>
>> YYYYMMDD XXXX
>>
>> the first being a date and the second a counter. So far, I've been using
>> a pretty conventional
>>
>> $rc = <$fh>;
>> chomp($rc);
>> ($date, $counter) = split(/\s+/, $rc);
This is a clear solution.
>>
>> for getting the data out of the file. While working with this code in
>> order to add some features to it, it came to me that
>>
>> ($date, $counter) = split for <$fh>
This is not clear, and does a for for one element (one string).
And where is the chomp?
--
http://www.telecable.es/personales/gamo/
------------------------------
Date: Wed, 02 Apr 2014 12:43:06 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: read and parse a single line file
Message-Id: <87ha6c7y9h.fsf@sable.mobileactivedefense.com>
gamo <gamo@telecable.es> writes:
> El 02/04/14 01:27, Jim Gibson escribi:
>> In article <87a9c4n38g.fsf@sable.mobileactivedefense.com>, Rainer
>> Weikusat <rweikusat@mobileactivedefense.com> wrote:
>>
>>> Right now, I'm dealing with (two) single line files whose single line
>>> contains data in the form of
>>>
>>> YYYYMMDD XXXX
>>>
>>> the first being a date and the second a counter. So far, I've been using
>>> a pretty conventional
>>>
>>> $rc = <$fh>;
>>> chomp($rc);
>>> ($date, $counter) = split(/\s+/, $rc);
>
> This is a clear solution.
It's a seriously verbose solution. In particular, I'd like to get rid of
the helper variable.
>>> for getting the data out of the file. While working with this code in
>>> order to add some features to it, it came to me that
>>>
>>> ($date, $counter) = split for <$fh>
>
> This is not clear, and does a for for one element (one string).
> And where is the chomp?
Can you provide a definition of 'clear' which is not "different from
what I'm used to"? The 'foreach' for aliases all elements of the list to
$_ in turn and then executes whatever the 'loop body' happens to be, in
this case, the statement annotated with the for statement
modifier. Using for in this way is actually a Perl-idiom because it is
one of the 'traditional' ways to emulate a switch-style multi-way
conditional, eg (untested)
for ($text) {
/supersonic/ && do {
.
.
last;
};
/subsonic/ && do {
.
.
last;
};
die("too slow");
}
The chomp isn't needed because \n counts as whitespace. This means
splitting the line in this way results in a trailing empty field falling
through the cracks because only the value of the first two fields are
assigned to something.
------------------------------
Date: Wed, 02 Apr 2014 12:49:32 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: read and parse a single line file
Message-Id: <87d2h07xyr.fsf@sable.mobileactivedefense.com>
Jim Gibson <jimsgibson@gmail.com> writes:
> In article <87a9c4n38g.fsf@sable.mobileactivedefense.com>, Rainer
> Weikusat <rweikusat@mobileactivedefense.com> wrote:
>
>> Right now, I'm dealing with (two) single line files whose single line
>> contains data in the form of
>>
>> YYYYMMDD XXXX
>>
>> the first being a date and the second a counter.
[...]
>> While working with this code in
>> order to add some features to it, it came to me that
>>
>> ($date, $counter) = split for <$fh>
>>
>> works
[...]
> <> in scalar context will read one line, whereas <> in list context
> will read the entire file. Since your files only have one line, it
> doesn't matter. But what if in the future a blank line gets added to
> the end of the file. I would prefer that my code still worked, so I
> would prefer a solution that keeps <> in scalar context and only reads
> the first line.
The first thing I noticed about that is that I now need to truncate the
file before updating it to prevent a trailing blank line from appearing
in case the counter wraps from a two-digit to a one-digit number when
the date changes ;-).
> What about this:
>
> ($date, $counter) = split(' ',<$fh>);
See also "cannot see the forest because of all the trees". All the
one-line variants have one common problem, though: They're
debugging-unfriendly because it is not easily possible to inspect the
data read from the file before processing it. Presently, I'm thinking
about either using a helper variable nevertheless or something like
for (<$fh>) {
($date, $counter) = split;
}
possibly with the additional requirement that the counter will become a
fixed-width field.
------------------------------
Date: Wed, 02 Apr 2014 12:12:34 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: read and parse a single line file
Message-Id: <87y4zniorx.fsf@castleamber.com>
Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
> for (<$fh>) {
> ($date, $counter) = split;
> }
When I see this code it gives me the impression (out of context) that
the author wants to have the 2 values on the last line. Which is
correct, since there is only one. If I would use this, I probably would
add:
# There is only one line; get the 2 values on this line.
I probably would write it like this:
chomp ( my $line = <$fh> );
my ( $date, $counter ) = split ' ', $line;
As for the fixed field, I probably would use
truncate( $fh ) or die "Can't truncate '$filename': $!";
--
John Bokma j3b
Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
Perl for books: http://johnbokma.com/perl/help-in-exchange-for-books.html
------------------------------
Date: Wed, 02 Apr 2014 21:29:02 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: read and parse a single line file
Message-Id: <87fvlvzd9t.fsf@sable.mobileactivedefense.com>
John Bokma <john@castleamber.com> writes:
> Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
>
>> for (<$fh>) {
>> ($date, $counter) = split;
>> }
>
> When I see this code it gives me the impression (out of context) that
> the author wants to have the 2 values on the last line. Which is
> correct, since there is only one. If I would use this, I probably would
> add:
>
> # There is only one line; get the 2 values on this line.
>
> I probably would write it like this:
>
> chomp ( my $line = <$fh> );
> my ( $date, $counter ) = split ' ', $line;
After flirting with
local $_ = <$fh>;
($date, $counter) = split;
I've meanwhile settled on
$rc = <$fh>;
($date, $counter) = split(' ', $rc);
as the 'least byzantine way to express what I want' which has at least a
'simplified split' (' ' instead of /\s+/) and does away with the
redundant chomp.
The third programming language I learnt (after Apple Basic and 65C02
machine language[*]) was Pascal which is strictly 'declare everything
before use' and forces declarations of similar things to occur in
blocks, eg, 'all constants, all types, all variables'. I've mostly kept
this as a habit and in particular, I start every subroutine with
declarations of all 'local' (as in 'my', not as in 'local') variables. I
consider declarations distributed all throughout the code extremely
messy, not only because the mixing of 'different things' (declarations
and statements) but also because this tends to hide the real complexity
of the subroutine in question: If all variables are declared at the top,
subroutines ripe for segmentation can be identified by this list
becoming 'lengthy and messy', ie, containing lots of variables and
'strange naming conventions' in order to avoid name clashes.
[*] As a friendly reminder, a home computer looks like this:
http://upload.wikimedia.org/wikipedia/commons/thumb/4/48/Apple_IIc_with_monitor.jpg/600px-Apple_IIc_with_monitor.jpg
and not like this
http://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Toes.jpg/800px-Toes.jpg
even if you have 64 of them (in German, C is pronunced like Zeh which
means toe).
------------------------------
Date: Wed, 02 Apr 2014 14:44:34 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: read and parse a single line file
Message-Id: <87ha6ba2bx.fsf@castleamber.com>
Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
> The third programming language I learnt (after Apple Basic and 65C02
> machine language[*]) was Pascal which is strictly 'declare everything
If you don't count COMAL, same here. At least that's what I recall. And
replace Apple with Sinclair and 65C02 with Z80 ;-)
> before use' and forces declarations of similar things to occur in
> blocks, eg, 'all constants, all types, all variables'. I've mostly kept
> this as a habit and in particular, I start every subroutine with
> declarations of all 'local' (as in 'my', not as in 'local') variables. I
> consider declarations distributed all throughout the code extremely
> messy, not only because the mixing of 'different things' (declarations
> and statements) but also because this tends to hide the real complexity
> of the subroutine in question: If all variables are declared at the top,
> subroutines ripe for segmentation can be identified by this list
> becoming 'lengthy and messy', ie, containing lots of variables and
> 'strange naming conventions' in order to avoid name clashes.
I split a sub if:
- it makes it more readable as in I can move lines of code to a
separate sub and replace this with a call that makes the code more
easy to read.
- it has too many lines (more than 60 or so) and it makes sense to
split it.
And I do prefer to put my close to first use (makes factoring out
easier). But that probably also has a lot to do with that I like early
returns, etc. And a bunch of mys followed by a .... or return (or return
if ... ) looks weird to me.
> even if you have 64 of them (in German, C is pronunced like Zeh which
> means toe).
Ah, didn't know that even though being Dutch and having had one year of
German at school, and having read quite some (well written) German
computer magazines back in the day.
--
John Bokma j3b
Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
Perl for books: http://johnbokma.com/perl/help-in-exchange-for-books.html
------------------------------
Date: Wed, 2 Apr 2014 22:55:35 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: read and parse a single line file
Message-Id: <slrnljoua7.hp5.hjp-usenet3@hrunkner.hjp.at>
On 2014-04-02 11:43, Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:
> gamo <gamo@telecable.es> writes:
>> El 02/04/14 01:27, Jim Gibson escribi:
>>> In article <87a9c4n38g.fsf@sable.mobileactivedefense.com>, Rainer
>>> Weikusat <rweikusat@mobileactivedefense.com> wrote:
>>>> Right now, I'm dealing with (two) single line files whose single line
>>>> contains data in the form of
>>>>
>>>> YYYYMMDD XXXX
>>>>
>>>> the first being a date and the second a counter. So far, I've been using
>>>> a pretty conventional
>>>>
>>>> $rc = <$fh>;
>>>> chomp($rc);
>>>> ($date, $counter) = split(/\s+/, $rc);
>>
>> This is a clear solution.
>
> It's a seriously verbose solution.
The chomp is unnecessary, as you already noticed.
> In particular, I'd like to get rid of the helper variable.
Why not:
($date, $counter) = split(/\s+/, <$fh>);
?
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: Thu, 03 Apr 2014 01:14:43 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: read and parse a single line file
Message-Id: <lhi24k$1st6$1@news.ntua.gr>
<$fh> =~/^(?<date>\w+)\s+(?<counter>\w+)/;
print "*$+{date}* *$+{counter}*\n";
or
read $fh, my $date, 8;
seek $fh, 1,1;
read $fh, my $count, 4;
------------------------------
Date: Thu, 03 Apr 2014 16:00:05 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: read and parse a single line file
Message-Id: <87ppkyzcei.fsf@sable.mobileactivedefense.com>
George Mpouras <gravitalsun@hotmail.foo> writes:
> <$fh> =~/^(?<date>\w+)\s+(?<counter>\w+)/;
> print "*$+{date}* *$+{counter}*\n";
Slightly modified variant:
($date, $counter) = <$fh> =~ /(\d+)\s+(\d+)/;
Another we didn't have so far:
($date, $counter) = unpack('A8xA', <$fh>);
> read $fh, my $date, 8;
> seek $fh, 1,1;
> read $fh, my $count, 4;
This won't work because the count isn't a fixed-width field. Using
read($fh, $date, 8)
$counter = <$fh> + 0;
would, though.
------------------------------
Date: Thu, 03 Apr 2014 16:07:06 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: read and parse a single line file
Message-Id: <87lhvmzc2t.fsf@sable.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
[...]
> Another we didn't have so far:
>
> ($date, $counter) = unpack('A8xA', <$fh>);
This doesn't work either, as it only uses the first character of the
counter.
($date, $counter) = unpack('A9A*', <$fh>);
------------------------------
Date: Thu, 03 Apr 2014 20:40:57 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: read and parse a single line file
Message-Id: <lhk6fc$20eb$1@news.ntua.gr>
Στις 3/4/2014 18:07, ο/η Rainer Weikusat έγραψε:
> ($date, $counter) = unpack('A9A*', <$fh>);
my @array = unpack "A9 A*", <$fh>;
------------------------------
Date: Thu, 03 Apr 2014 18:47:21 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: read and parse a single line file
Message-Id: <878urmz4nq.fsf@sable.mobileactivedefense.com>
George Mpouras <gravitalsun@hotmail.foo> writes:
> Στις 3/4/2014 18:07, ο/η Rainer Weikusat έγραψε:
>> ($date, $counter) = unpack('A9A*', <$fh>);
>
> my @array = unpack "A9 A*", <$fh>;
What is this now supposed to communicate?
------------------------------
Date: Thu, 03 Apr 2014 21:21:38 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: read and parse a single line file
Message-Id: <lhk8rl$2651$1@news.ntua.gr>
>> my @array = unpack "A9 A*", <$fh>;
>
> What is this now supposed to communicate?
>
#!/usr/bin/perl
use strict;
use warnings;
open my $fh, 'file.txt' or die;
@{$_}{qw/date x count/} = unpack "A8ZA*", <$fh>;
print "*$_->{date}*";
print "*$_->{count}*";
------------------------------
Date: Thu, 03 Apr 2014 21:28:44 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: read and parse a single line file
Message-Id: <lhk990$2725$1@news.ntua.gr>
Στις 3/4/2014 20:47, ο/η Rainer Weikusat έγραψε:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>> Στις 3/4/2014 18:07, ο/η Rainer Weikusat έγραψε:
>>> ($date, $counter) = unpack('A9A*', <$fh>);
>>
>> my @array = unpack "A9 A*", <$fh>;
>
> What is this now supposed to communicate?
>
# substr is considered faster than regexs
open my $fh, 'file.txt' or die;
$_ = <$fh>;
my $date = substr $_, 0, 8, '';
my $count = substr $_, 1;
print "*$date* *$count*\n";
------------------------------
Date: Thu, 03 Apr 2014 20:06:39 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: read and parse a single line file
Message-Id: <874n2az0zk.fsf@sable.mobileactivedefense.com>
George Mpouras <gravitalsun@hotmail.foo> writes:
> Στις 3/4/2014 20:47, ο/η Rainer Weikusat έγραψε:
>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>> Στις 3/4/2014 18:07, ο/η Rainer Weikusat έγραψε:
>>>> ($date, $counter) = unpack('A9A*', <$fh>);
>>>
>>> my @array = unpack "A9 A*", <$fh>;
>>
>> What is this now supposed to communicate?
>>
>
>
> # substr is considered faster than regexs
>
> open my $fh, 'file.txt' or die;
> $_ = <$fh>;
> my $date = substr $_, 0, 8, '';
> my $count = substr $_, 1;
>
>
> print "*$date* *$count*\n";
$date=substr($_,0,-length($count=substr($_,rindex($_,' ')+1,-1)))for<$fh>
?
------------------------------
Date: Thu, 03 Apr 2014 20:16:04 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: read and parse a single line file
Message-Id: <87zjk2xlzf.fsf@sable.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>> Στις 3/4/2014 20:47, ο/η Rainer Weikusat έγραψε:
>>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>>> Στις 3/4/2014 18:07, ο/η Rainer Weikusat έγραψε:
>>>>> ($date, $counter) = unpack('A9A*', <$fh>);
>>>>
>>>> my @array = unpack "A9 A*", <$fh>;
>>>
>>> What is this now supposed to communicate?
>>>
>>
>>
>> # substr is considered faster than regexs
>>
>> open my $fh, 'file.txt' or die;
>> $_ = <$fh>;
>> my $date = substr $_, 0, 8, '';
>> my $count = substr $_, 1;
>>
>>
>> print "*$date* *$count*\n";
>
> $date=substr($_,0,-length($count=substr($_,rindex($_,' ')+1,-1)))for<$fh>
ts, ts, ts ... hasty postings bad ...
$date=substr($_,0,-(length($count=substr($_,rindex($_,' ')+1,-1))+2))for<$fh>
------------------------------
Date: Thu, 03 Apr 2014 22:16:27 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: read and parse a single line file
Message-Id: <lhkc2e$2dll$1@news.ntua.gr>
>
> $date=substr($_,0,-length($count=substr($_,rindex($_,' ')+1,-1)))for<$fh>
>
>
nice, but something goes wrong.
for file content "YYYYMMDD 123"
I got
*YYYYMMDD 1* *12*
------------------------------
Date: Thu, 03 Apr 2014 22:21:29 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: read and parse a single line file
Message-Id: <lhkcbs$2eal$1@news.ntua.gr>
> $date=substr($_,0,-(length($count=substr($_,rindex($_,' ')+1,-1))+2))for<$fh>
>
the last character is missing
*YYYYMMDD* *12*
------------------------------
Date: Thu, 03 Apr 2014 20:37:29 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: read and parse a single line file
Message-Id: <87vbuqxkzq.fsf@sable.mobileactivedefense.com>
George Mpouras <gravitalsun@hotmail.foo> writes:
>> $date=substr($_,0,-(length($count=substr($_,rindex($_,' ')+1,-1))+2))for<$fh>
>>
>
> the last character is missing
>
> *YYYYMMDD* *12*
Nope. The last character in the file is a \n and that's not supposed to
be part of the $count value
------------
system('echo 20140403 100 >/tmp/x');
open($fh, '<', '/tmp/x');
$date=substr($_,0,-2-length($count=substr($_,rindex($_,' ')+1,-1)))for<$fh>;
printf("'%s', '%s'\n", $date, $count);
------------
------------------------------
Date: Thu, 03 Apr 2014 22:50:22 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: read and parse a single line file
Message-Id: <lhke21$2i4m$1@news.ntua.gr>
Στις 3/4/2014 22:37, ο/η Rainer Weikusat έγραψε:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>>> $date=substr($_,0,-(length($count=substr($_,rindex($_,' ')+1,-1))+2))for<$fh>
>>>
>>
>> the last character is missing
>>
>> *YYYYMMDD* *12*
>
> Nope. The last character in the file is a \n and that's not supposed to
> be part of the $count value
>
> ------------
> system('echo 20140403 100 >/tmp/x');
> open($fh, '<', '/tmp/x');
> $date=substr($_,0,-2-length($count=substr($_,rindex($_,' ')+1,-1)))for<$fh>;
> printf("'%s', '%s'\n", $date, $count);
> ------------
>
>
>
>
my file have not final \r or \n ... so a xor maybe is necessary
------------------------------
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 4185
***************************************