[31716] in Perl-Users-Digest
Perl-Users Digest, Issue: 2979 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jun 8 16:14:28 2010
Date: Tue, 8 Jun 2010 13:14:17 -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 Tue, 8 Jun 2010 Volume: 11 Number: 2979
Today's topics:
How to read a given number of lines? <pengyu.ut@gmail.com>
Re: How to read a given number of lines? <ralph@happydays.com>
Re: How to read a given number of lines? <mvdwege@mail.com>
Re: How to read a given number of lines? <mvdwege@mail.com>
Re: How to read a given number of lines? <ralph@happydays.com>
Re: How to read a given number of lines? <ben@morrow.me.uk>
Re: How to read a given number of lines? <willem@turtle.stack.nl>
Re: How to read a given number of lines? <ralph@happydays.com>
Re: How to read a given number of lines? <ben@morrow.me.uk>
Re: Posting Guidelines for comp.lang.perl.misc ($Revisi <ralph@happydays.com>
Re: something stupid <uri@StemSystems.com>
something <robin1@cnsp.com>
Re: something <ralph@happydays.com>
stuff <robin1@cnsp.com>
Re: suggestions for printing out a few records of a len <derykus@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 8 Jun 2010 09:14:52 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: How to read a given number of lines?
Message-Id: <f2df9610-ce9b-4b8a-9916-b58a7bf05077@i28g2000yqa.googlegroups.com>
I want to give a given number of lines. Current, I have to write the
following code to read, for example, 3 lines. Is there a subroutine to
read a given number of lines in an array?
$line1=<IN>;
$line2=<IN>;
$line3=<IN>;
Regards,
Peng
------------------------------
Date: Tue, 08 Jun 2010 12:48:42 -0400
From: Ralph Malph <ralph@happydays.com>
Subject: Re: How to read a given number of lines?
Message-Id: <9ec36$4c0e746b$40779ac3$32678@news.eurofeeds.com>
On 6/8/2010 12:14 PM, Peng Yu wrote:
> I want to give a given number of lines. Current, I have to write the
> following code to read, for example, 3 lines. Is there a subroutine to
> read a given number of lines in an array?
>
> $line1=<IN>;
> $line2=<IN>;
> $line3=<IN>;
This will store the lines in the
cleverly named array @lines.
I've set $limit to 3 as in your short example
but obviously this works for any limit.
Always be sure to double check any user-inputed values!
--------------------------------
my @lines;
my $limit=3;
my $counter=0;
while($counter < $limit){
$lines[$counter] = <IN> ;
$counter++;
}
------------------------------
Date: Tue, 08 Jun 2010 19:12:47 +0200
From: Mart van de Wege <mvdwege@mail.com>
Subject: Re: How to read a given number of lines?
Message-Id: <86vd9tphww.fsf@gareth.avalon.lan>
Ralph Malph <ralph@happydays.com> writes:
> On 6/8/2010 12:14 PM, Peng Yu wrote:
>> I want to give a given number of lines. Current, I have to write the
>> following code to read, for example, 3 lines. Is there a subroutine to
>> read a given number of lines in an array?
>>
>> $line1=<IN>;
>> $line2=<IN>;
>> $line3=<IN>;
> This will store the lines in the
> cleverly named array @lines.
> I've set $limit to 3 as in your short example
> but obviously this works for any limit.
> Always be sure to double check any user-inputed values!
> --------------------------------
> my @lines;
> my $limit=3;
> my $counter=0;
> while($counter < $limit){
> $lines[$counter] = <IN> ;
> $counter++;
> }
or:
my @lines;
my $limit = 3;
for my $line(1..$limit) {
$lines[$line] = <IN>
}
But then again I have a personal dislike of counters and flags, so I
tend to look for ways to avoid using them.
--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
------------------------------
Date: Tue, 08 Jun 2010 19:14:43 +0200
From: Mart van de Wege <mvdwege@mail.com>
Subject: Re: How to read a given number of lines?
Message-Id: <86pr01phto.fsf@gareth.avalon.lan>
Mart van de Wege <mvdwege@mail.com> writes:
> Ralph Malph <ralph@happydays.com> writes:
>
>> On 6/8/2010 12:14 PM, Peng Yu wrote:
>>> I want to give a given number of lines. Current, I have to write the
>>> following code to read, for example, 3 lines. Is there a subroutine to
>>> read a given number of lines in an array?
>>>
>>> $line1=<IN>;
>>> $line2=<IN>;
>>> $line3=<IN>;
>> This will store the lines in the
>> cleverly named array @lines.
>> I've set $limit to 3 as in your short example
>> but obviously this works for any limit.
>> Always be sure to double check any user-inputed values!
>> --------------------------------
>> my @lines;
>> my $limit=3;
>> my $counter=0;
>> while($counter < $limit){
>> $lines[$counter] = <IN> ;
>> $counter++;
>> }
>
> or:
>
> my @lines;
> my $limit = 3;
> for my $line(1..$limit) {
> $lines[$line] = <IN>
> }
>
> But then again I have a personal dislike of counters and flags, so I
> tend to look for ways to avoid using them.
And that was an off-by-one error. Saw it as I hit 'post'.
So the correct code would of course be:
my @lines;
my $limit = 3;
for my $line(0..$limit-1) {
$lines[$line] = <IN>
}
--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
------------------------------
Date: Tue, 08 Jun 2010 13:21:01 -0400
From: Ralph Malph <ralph@happydays.com>
Subject: Re: How to read a given number of lines?
Message-Id: <93776$4c0e7bfe$40779ac3$28762@news.eurofeeds.com>
On 6/8/2010 1:14 PM, Mart van de Wege wrote:
> Mart van de Wege<mvdwege@mail.com> writes:
>
>> Ralph Malph<ralph@happydays.com> writes:
>>
>>> On 6/8/2010 12:14 PM, Peng Yu wrote:
>>>> I want to give a given number of lines. Current, I have to write the
>>>> following code to read, for example, 3 lines. Is there a subroutine to
>>>> read a given number of lines in an array?
>>>>
>>>> $line1=<IN>;
>>>> $line2=<IN>;
>>>> $line3=<IN>;
>>> This will store the lines in the
>>> cleverly named array @lines.
>>> I've set $limit to 3 as in your short example
>>> but obviously this works for any limit.
>>> Always be sure to double check any user-inputed values!
>>> --------------------------------
>>> my @lines;
>>> my $limit=3;
>>> my $counter=0;
>>> while($counter< $limit){
>>> $lines[$counter] =<IN> ;
>>> $counter++;
>>> }
>>
>> or:
>>
>> my @lines;
>> my $limit = 3;
>> for my $line(1..$limit) {
>> $lines[$line] =<IN>
>> }
>>
>> But then again I have a personal dislike of counters and flags, so I
>> tend to look for ways to avoid using them.
>
> And that was an off-by-one error. Saw it as I hit 'post'.
>
> So the correct code would of course be:
>
> my @lines;
> my $limit = 3;
> for my $line(0..$limit-1) {
> $lines[$line] =<IN>
> }
In some senses you original code was correct although
it didn't fill $lines[0].
As required, it read a certain number of lines into an array.
Nonetheless, I suggest you start disliking silly errors
more than you dislike counters and flags.
HTH
------------------------------
Date: Tue, 8 Jun 2010 18:31:11 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to read a given number of lines?
Message-Id: <vbb2e7-l042.ln1@osiris.mauzo.dyndns.org>
Quoth Mart van de Wege <mvdwege@mail.com>:
> >
> > my @lines;
> > my $limit = 3;
> > for my $line(1..$limit) {
> > $lines[$line] = <IN>
> > }
> >
> > But then again I have a personal dislike of counters and flags, so I
> > tend to look for ways to avoid using them.
>
> And that was an off-by-one error. Saw it as I hit 'post'.
>
> So the correct code would of course be:
>
> my @lines;
> my $limit = 3;
> for my $line(0..$limit-1) {
> $lines[$line] = <IN>
> }
Better would be
my @lines;
push @lines, <IN> for 1..3;
though I would probably rather have
my @lines = map <IN>, 1..3;
since I don't like variable declarations just hanging around not doing
anything useful.
Ben
------------------------------
Date: Tue, 8 Jun 2010 17:42:29 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: How to read a given number of lines?
Message-Id: <slrni0t085.9te.willem@turtle.stack.nl>
Ben Morrow wrote:
) Better would be
)
) my @lines;
) push @lines, <IN> for 1..3;
)
) though I would probably rather have
)
) my @lines = map <IN>, 1..3;
)
) since I don't like variable declarations just hanging around not doing
) anything useful.
BZZT!
Both of your examples will read the entire file into @lines.
Left as an exercise to figure out why this is and how to fix it.
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: Tue, 08 Jun 2010 13:56:45 -0400
From: Ralph Malph <ralph@happydays.com>
Subject: Re: How to read a given number of lines?
Message-Id: <1d359$4c0e845e$40779ac3$19974@news.eurofeeds.com>
On 6/8/2010 1:42 PM, Willem wrote:
> Ben Morrow wrote:
> ) Better would be
> )
> ) my @lines;
> ) push @lines,<IN> for 1..3;
> )
> ) though I would probably rather have
> )
> ) my @lines = map<IN>, 1..3;
> )
> ) since I don't like variable declarations just hanging around not doing
> ) anything useful.
>
> BZZT!
>
> Both of your examples will read the entire file into @lines.
> Left as an exercise to figure out why this is and how to fix it.
Q: "How to fix it".
A: They need to stop trying to be so fucking clever. Clever kills.
------------------------------
Date: Tue, 8 Jun 2010 19:51:37 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to read a given number of lines?
Message-Id: <p2g2e7-0k42.ln1@osiris.mauzo.dyndns.org>
Quoth Ralph Malph <ralph@happydays.com>:
> On 6/8/2010 1:42 PM, Willem wrote:
> > Ben Morrow wrote:
> > ) Better would be
> > )
> > ) my @lines;
> > ) push @lines,<IN> for 1..3;
> > )
> > ) though I would probably rather have
> > )
> > ) my @lines = map<IN>, 1..3;
> > )
> > ) since I don't like variable declarations just hanging around not doing
> > ) anything useful.
> >
> > BZZT!
> >
> > Both of your examples will read the entire file into @lines.
> > Left as an exercise to figure out why this is and how to fix it.
True. Good catch. (Or possibly stupid mistake on my part.)
> Q: "How to fix it".
> A: They need to stop trying to be so fucking clever. Clever kills.
'They' in this case being me? I wasn't trying to be clever, I was trying
to write the code in a simple and obvious way. Counting indices by hand
is much easier to get wrong than letting perl count them for you.
Ben
------------------------------
Date: Tue, 08 Jun 2010 11:49:18 -0400
From: Ralph Malph <ralph@happydays.com>
Subject: Re: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.9 $)
Message-Id: <254d3$4c0e667f$40779ac3$29911@news.eurofeeds.com>
How about you post this nonsense about once every decade
instead once every few days?
"Instructions for Authors" for the most prestigious academic journals
are shorter than your stupid screed! Do you really think
the average new poster to this group will
read your nonsense?
Many posters here are non-native english speakers/writers.
While they can passably use the language this silly thing you
are spamming the group with is so long and pointless
hardly any of these people will bother.
Surely you must have had fun writing it though!
Print out several hard-copies and shove this drivel back up your ass.
------------------------------
Date: Tue, 08 Jun 2010 15:32:27 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: something stupid
Message-Id: <8739wx1fsk.fsf@quad.sysarch.com>
subject says it all
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Tue, 8 Jun 2010 12:29:32 -0700 (PDT)
From: Robin <robin1@cnsp.com>
Subject: something
Message-Id: <dd27829a-2125-4771-8be6-5d47229c07e8@b35g2000yqi.googlegroups.com>
perl tutorial: how to design programs that help with poltical action:
write a program that is a spiritual walkthrough...
Be safe with it.
-Robin
------------------------------
Date: Tue, 08 Jun 2010 15:32:16 -0400
From: Ralph Malph <ralph@happydays.com>
Subject: Re: something
Message-Id: <739b2$4c0e9ac0$40779ac3$6387@news.eurofeeds.com>
If I had to guess the poster
wanted to save a note of a profound thought
obtained whilst very drunk or high.
When this is read in a sober state regret will set in.
LOL
Still though, in some ways it was good they thought to take notes.
Next time use paper!
------------------------------
Date: Tue, 8 Jun 2010 12:35:12 -0700 (PDT)
From: Robin <robin1@cnsp.com>
Subject: stuff
Message-Id: <8bda88a4-231d-48ec-8e54-2f2228236b9e@s9g2000yqd.googlegroups.com>
I just wanted to say a few things about this newsgroup. I think it is
a really good newsgroup. Even though, in the past there has been some
things in this newsgroup I didn't like I also presenterd things that
in the newsgroup people don't like. I am also hoping that people post
stuff about more advanced perl stuff ethically and learn more perl
ethically and people start learning some more advanced stuff
ethically. I wanted to thank and or hope for the best thingsfor this
newsgroups in the future.
-Robin
------------------------------
Date: Tue, 8 Jun 2010 05:21:48 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: suggestions for printing out a few records of a lengthy file
Message-Id: <e12fb5f2-6e54-41a6-9092-458e923d3e2b@s1g2000prf.googlegroups.com>
On Jun 7, 5:16=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> >>>>> "JG" =3D=3D J Gleixner <glex_no-s...@qwest-spam-no.invalid> writes:
>
> =A0 JG> ccc31807 wrote:
> =A0 >> On Jun 7, 5:32 pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> =A0 >>> use a read database or even a DBD for a csv (pipe separated is ok=
)
> =A0 >>> file.
> =A0 >>
> =A0 >> We get the input file dumped on us every other month or so, as an
> =A0 >> ASCII file, and use it just once to create the PDFs. We never do a=
ny
> =A0 >> update, delete, or insert queries, and only a few select queries, =
so
> =A0 >> putting it into a RDB just to print maybe two dozen documents out =
of
> =A0 >> thousands seems like a lot of effort for very little benefit.
> =A0 >>
> =A0 >>> or you could save a lot of space by reading in each row and only =
saving
> =A0 >>> that text line in a hash with the key (you extract only the key).=
then
> =A0 >>> you can locate the rows of interest, parse out the fields and do =
the
> =A0 >>> usual stuff.
> =A0 >>
> =A0 >> This is what I thought I was doing. However, it occurs to me that =
I
> =A0 >> can use a counter initially set to the size of @ARVG, decrement it=
for
> =A0 >> every match, and exit when the counter reaches zero.
>
> =A0 JG> No need for all that. You could create a hash of the keys passed =
in
> =A0 JG> via ARGV.
>
> =A0 JG> my %ids =3D map { $_ =3D> 1 } @ARGV;
>
> =A0 JG> Then test if the key is one you're interested in:
>
> =A0 JG> while(<IN>)
> =A0 JG> {
> =A0 JG> =A0 my ($key, $first, $middle, $last ...) =3D split /\|/;
> =A0 JG> =A0 next unless $ids{ $key };
> =A0 JG> =A0 ...
>
> same idea i had but you didn't add in deleting found keys so you can
> exit early.
>
> also no need to do a full split on the line unless you know it was in
> the hash. only split after you find a needed line. you can easily grab
> the key from the front of each line as it comes in.
>
A match with \G and /gc is an alternative
to avoid a split() of the whole line:
while (<IN>)
{
my ($key) =3D m{\G(\d+)\|}gc;
next unless defined $key and $ids{ $key };
my @rest =3D m{\|\G([^|]+)}gc;
...
}
A regex and split() may not be as
efficient but is much easier though:
while (<IN>)
{
my ($key) =3D /^(\d+)/;
next unless...
my( undef, @rest ) =3D split (/\|/, $_ );
...
}
--
Charles DeRykus
------------------------------
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 2979
***************************************