[31718] in Perl-Users-Digest
Perl-Users Digest, Issue: 2981 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jun 9 14:09:29 2010
Date: Wed, 9 Jun 2010 11:09:12 -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 Wed, 9 Jun 2010 Volume: 11 Number: 2981
Today's topics:
Re: convert integer to string <uri@StemSystems.com>
Re: convert integer to string <bugbear@trim_papermule.co.uk_trim>
Re: convert integer to string <cartercc@gmail.com>
graphics <v.niekerk_@_hccnet.nl>
Re: graphics <m@rtij.nl.invlalid>
Re: graphics <spamtrap@shermpendley.com>
Re: graphics <jurgenex@hotmail.com>
Re: graphics <v.niekerk_@_hccnet.nl>
Re: graphics <willem@turtle.stack.nl>
Re: graphics <RedGrittyBrick@spamweary.invalid>
Re: graphics <john@castleamber.com>
Re: graphics <spamtrap@shermpendley.com>
Re: graphics <v.niekerk_@_hccnet.nl>
Re: How to get the class name from the reference to an <cdalten@gmail.com>
Re: How to read a given number of lines? <uri@StemSystems.com>
Re: How to read a given number of lines? <mvdwege@mail.com>
Re: How to read a given number of lines? <bankair@gmail.com>
Re: How to read a given number of lines? <uri@StemSystems.com>
Re: How to read a given number of lines? <bankair@gmail.com>
Re: How to read a given number of lines? <m@rtij.nl.invlalid>
Re: How to read a given number of lines? <justin.0911@purestblue.com>
Re: How to read a given number of lines? <pengyu.ut@gmail.com>
Re: How to read a given number of lines? <marc.girod@gmail.com>
Official Behavior of $str =~ $pattern <himanshu.garg@gmail.com>
Re: Official Behavior of $str =~ $pattern <peter@makholm.net>
Re: Why does perl allow so many different ways of doing <cartercc@gmail.com>
Re: Why does perl allow so many different ways of doing <john@castleamber.com>
Re: Why does perl allow so many different ways of doing <cwilbur@chromatico.net>
Why does perl allow so many different ways of doing ess <pengyu.ut@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 08 Jun 2010 21:26:21 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: convert integer to string
Message-Id: <87631tvvwi.fsf@quad.sysarch.com>
>>>>> "TM" == Tad McClellan <tadmc@seesig.invalid> writes:
>> my ($order, $first, $last, @years) = split /\|/;
>> __DATA__
>> 1|George|Washington|1788 1792
>> 2|John|Adams|1796
>> 3|Thomas|Jefferson|1800 1804
>> 4|James|Madison|1808 1812
>> 32|Franklin|Roosevelt|1932 1936 1940 1944
TM> @years always contains exactly one element, it is a non-arrayish array.
TM> $years would work as well, and would avoid looking like it wouldn't
TM> work...
gack, i didn't see that! no wonder it 'worked'. i was so caught up in
the wrong use of an array there i didn't notice it was only getting one
value which had the whole string with numbers. he never split that field
into a list of numbers. do'h!!
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: Wed, 09 Jun 2010 08:57:46 +0100
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: convert integer to string
Message-Id: <1s2dnRHZIqLn1JLRnZ2dnUVZ8gEAAAAA@brightview.co.uk>
Uri Guttman wrote:
>>>>>> "TM" == Tad McClellan <tadmc@seesig.invalid> writes:
>
> >> my ($order, $first, $last, @years) = split /\|/;
> >> __DATA__
> >> 1|George|Washington|1788 1792
> >> 2|John|Adams|1796
> >> 3|Thomas|Jefferson|1800 1804
> >> 4|James|Madison|1808 1812
> >> 32|Franklin|Roosevelt|1932 1936 1940 1944
>
>
> TM> @years always contains exactly one element, it is a non-arrayish array.
>
> TM> $years would work as well, and would avoid looking like it wouldn't
> TM> work...
>
> gack, i didn't see that! no wonder it 'worked'. i was so caught up in
> the wrong use of an array there i didn't notice it was only getting one
> value which had the whole string with numbers. he never split that field
> into a list of numbers. do'h!!
Chuckle. *MY* experience tells me that bugs are never where you're looking ;-)
BugBear
------------------------------
Date: Wed, 9 Jun 2010 07:39:33 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: convert integer to string
Message-Id: <cd39bb0c-9fa7-40dd-be1c-e6ab6f6c7687@k39g2000yqb.googlegroups.com>
On Jun 8, 9:08=A0pm, J rgen Exner <jurge...@hotmail.com> wrote:
> ccc31807 <carte...@gmail.com> wrote:
> >I get some of the data in CSV format. One of my sources switched from
> >an Access database to an Excel file. Turns out that Excel strips out
> >the leading zeros if it thinks that the datum is an integer.
>
> Which I would argue is the correct behaviour for a numerical data field.
> If you don't want a canonical numerical form, then declare the data
> field to be text. Problem solved.
I get these kinds of files as user input. My supposition is that prior
to this experience, the user was using Access, and configured the ID
field as text (even though it consists entirely of digits), so that
when exported as CSV it kept all seven digits, which it would have
done as a text field, a string.
Users don't normally bother to set the data type of Excel columns
unless they are currency, dates, or specific numeric fields, so Excel
treats a column with numeric characters as numeric, which is entirely
reasonable. When you save the Excel file as CSV, it only saves the
significant digits, not leading zeros. Again, this is entirely
reasonable.
My problem was that I wasn't aware of the switch (might have been told
but wasn't really aware of it) and ASSUMED that the numeric IDs were
all present including the leading zeros. When I figured out that the
errors were associated with the records that had ID consisting of
leading zeros, I ASSUMED that it was a software problem, a bug I had
introduced, a programming error.
As bugbear notes, it was indeed a programming error, but related to
validation of data, not conversion of data types. When I converted the
numeric fields to strings, I got the same error, which ultimately lead
me to examine the data file.
As to use of the @courses variable, I'll change that to $courses. I've
already explained why that happened, and I honestly don't feel too bad
about that, as that's the kind of error we all make when we write in
different languages at the same time.
CC
------------------------------
Date: 09 Jun 2010 04:40:45 GMT
From: Huub <v.niekerk_@_hccnet.nl>
Subject: graphics
Message-Id: <4c0f1b4d$0$4496$e4fe514c@dreader19.news.xs4all.nl>
Hi,
I'm using Perl to print from database, which works great. Now I'd like to
print a .JPG picture with it. However, searching CPAN I find a LOT of
graphics modules. Any recommendation which one to use for this?
Thanks.
------------------------------
Date: Wed, 9 Jun 2010 09:41:31 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: graphics
Message-Id: <b6t3e7-p39.ln1@news.rtij.nl>
On Wed, 09 Jun 2010 04:40:45 +0000, Huub wrote:
> Hi,
>
> I'm using Perl to print from database, which works great. Now I'd like
> to print a .JPG picture with it. However, searching CPAN I find a LOT of
> graphics modules. Any recommendation which one to use for this?
I'm looking into something similar right now. After searching CPAN, I
looked better at CD::Graph and Chart and found both very similar, and
lacking for my purpose. They are good though.
I then looked at driving gnuplot from perl, and it will probably serve
your needs. It's incredible what you can do with gnuplot.
It still didn't need my needs (for every x-pixel, draw min, max and avg)
so I'm currently deriving from GD::axestype to do it myself.
HTH,
M4
------------------------------
Date: Wed, 09 Jun 2010 08:50:09 -0400
From: Sherm Pendley <spamtrap@shermpendley.com>
Subject: Re: graphics
Message-Id: <m2zkz4z7y6.fsf@shermpendley.com>
Huub <v.niekerk_@_hccnet.nl> writes:
> I'm using Perl to print from database, which works great.
Good to know.
> Now I'd like to
> print a .JPG picture with it. However, searching CPAN I find a LOT of
> graphics modules. Any recommendation which one to use for this?
For *what*, exactly? The modules on CPAN are for *creating* graphics.
Is that what you want to do, or do you simply want to print an image?
Also, print to where? If this is a question about web programming, it
helps to say so up front - people use Perl for a wide variety of tasks.
sherm--
--
Sherm Pendley <www.shermpendley.com>
<www.camelbones.org>
Cocoa Developer
------------------------------
Date: Wed, 09 Jun 2010 07:34:10 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: graphics
Message-Id: <d99v06pv8dcdqqk2rkgq5uk2j715kpqv92@4ax.com>
Huub <v.niekerk_@_hccnet.nl> wrote:
>I'm using Perl to print from database, which works great. Now I'd like to
>print a .JPG picture with it.
I am puzzled.
"print" like in print to a sheet of paper? Normally you would use
whatever means your OS provides for that and normally yes, you could
initiate that via a Perl program, too, just like you would initiate any
other external command from a Perl program.
Or "print" like the print() function in Perl? Well, that is mostly for
text although of course you can use it for binary data, too.
Or what "print" are you talking about?
jue
------------------------------
Date: 09 Jun 2010 15:22:34 GMT
From: Huub <v.niekerk_@_hccnet.nl>
Subject: Re: graphics
Message-Id: <4c0fb1ba$0$4528$e4fe514c@dreader26.news.xs4all.nl>
On Wed, 09 Jun 2010 07:34:10 -0700, Jürgen Exner wrote:
> Huub <v.niekerk_@_hccnet.nl> wrote:
>>I'm using Perl to print from database, which works great. Now I'd like
>>to print a .JPG picture with it.
>
> I am puzzled.
>
> "print" like in print to a sheet of paper? Normally you would use
> whatever means your OS provides for that and normally yes, you could
> initiate that via a Perl program, too, just like you would initiate any
> other external command from a Perl program.
>
> Or "print" like the print() function in Perl? Well, that is mostly for
> text although of course you can use it for binary data, too.
>
> Or what "print" are you talking about?
>
> jue
Ok, looks like I haven't been clear enough.
I am talking about printing to paper and the intention is to print an
existing jpg picture on the paper after which the data from the database
is printed.
Reading the other reactions, I think it would be easier to first print
that image using Gimp or so, and then re-use the paper using Perl to
print the data. Though it would take at least twice as much time.
Any suggestion welcome.
------------------------------
Date: Wed, 9 Jun 2010 15:51:07 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: graphics
Message-Id: <slrni0ve3b.nta.willem@turtle.stack.nl>
Huub wrote:
) Ok, looks like I haven't been clear enough.
)
) I am talking about printing to paper and the intention is to print an
) existing jpg picture on the paper after which the data from the database
) is printed.
)
) Reading the other reactions, I think it would be easier to first print
) that image using Gimp or so, and then re-use the paper using Perl to
) print the data. Though it would take at least twice as much time.
What method are you using to print the data to the printer ?
Are you using some module ? Is it pure plain text ? Postscript ?
There's probably some Perl module out there that can print combined
text and jpg images to a printer.
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: Wed, 09 Jun 2010 18:04:31 +0100
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: graphics
Message-Id: <4c0fc9a0$0$28000$db0fefd9@news.zen.co.uk>
On 09/06/2010 05:40, Huub wrote:
>
> I'm using Perl to print from database, which works great.
So how are you doing that? You might be
* Using a Win32 API to make GDI calls etc
* Using Perl print() statements to write a text file
then exec() a 'lp' command
* A gazillion other possibilities.
In order to provide a relevant answer, potential helpers might benefit
from a little more context.
> Now I'd like to
> print a .JPG picture with it. However, searching CPAN I find a LOT of
> graphics modules. Any recommendation which one to use for this?
I'd have the Perl program write a Postscript file containing image data
and text, then use OS-dependant commands to spool that to a Postscript
printer or via a Postscript aware print subsystem like CUPS or
Ghostscript. Your mileage is likely to vary. CPAN is your friend.
--
RGB
------------------------------
Date: Wed, 09 Jun 2010 12:08:10 -0500
From: John Bokma <john@castleamber.com>
Subject: Re: graphics
Message-Id: <8739wwp211.fsf@castleamber.com>
Huub <v.niekerk_@_hccnet.nl> writes:
> On Wed, 09 Jun 2010 07:34:10 -0700, Jürgen Exner wrote:
>
>> Huub <v.niekerk_@_hccnet.nl> wrote:
>>>I'm using Perl to print from database, which works great. Now I'd like
>>>to print a .JPG picture with it.
>>
>> I am puzzled.
>>
>> "print" like in print to a sheet of paper? Normally you would use
>> whatever means your OS provides for that and normally yes, you could
>> initiate that via a Perl program, too, just like you would initiate any
>> other external command from a Perl program.
>>
>> Or "print" like the print() function in Perl? Well, that is mostly for
>> text although of course you can use it for binary data, too.
>>
>> Or what "print" are you talking about?
>>
>> jue
>
> Ok, looks like I haven't been clear enough.
>
> I am talking about printing to paper and the intention is to print an
> existing jpg picture on the paper after which the data from the database
> is printed.
>
> Reading the other reactions, I think it would be easier to first print
> that image using Gimp or so, and then re-use the paper using Perl to
> print the data. Though it would take at least twice as much time.
>
>
> Any suggestion welcome.
Install Java, Apache FOP, and Saxon-B XSLT
Generate XML via your Perl program
Convert this XML via saxonb-xslt to fo
Convert fo to pdf (or pick any other of the formats supported) using
Apache FOP.
It sounds like a lot of trouble, but IMO it's extremely flexible, see:
http://johnbokma.com/mexit/2010/02/11/
It's most likely easier than generating your own PDF. Another option is
to just generate a HTML file and print that one via a browser (or
convert HTML to PDF and print that). But I am quite sure that XSL-FO
gives you more control over the output than HTML.
--
John Bokma j3b
Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
------------------------------
Date: Wed, 09 Jun 2010 13:35:25 -0400
From: Sherm Pendley <spamtrap@shermpendley.com>
Subject: Re: graphics
Message-Id: <m27hm8ds82.fsf@shermpendley.com>
Huub <v.niekerk_@_hccnet.nl> writes:
> I am talking about printing to paper and the intention is to print an
> existing jpg picture on the paper after which the data from the database
> is printed.
I would generate an HTML document from the database, using Template
Toolkit or Mason, and referring to the JPEG pic in an <img ...> element.
> Reading the other reactions, I think it would be easier to first print
> that image using Gimp or so, and then re-use the paper using Perl to
> print the data.
Please tell me you're joking...
sherm--
--
Sherm Pendley <www.shermpendley.com>
<www.camelbones.org>
Cocoa Developer
------------------------------
Date: 09 Jun 2010 18:02:25 GMT
From: Huub <v.niekerk_@_hccnet.nl>
Subject: Re: graphics
Message-Id: <4c0fd731$0$4528$e4fe514c@dreader26.news.xs4all.nl>
On Wed, 09 Jun 2010 15:51:07 +0000, Willem wrote:
> Huub wrote:
> ) Ok, looks like I haven't been clear enough. )
> ) I am talking about printing to paper and the intention is to print an
> ) existing jpg picture on the paper after which the data from the
> database ) is printed.
> )
> ) Reading the other reactions, I think it would be easier to first print
> ) that image using Gimp or so, and then re-use the paper using Perl to )
> print the data. Though it would take at least twice as much time.
>
> What method are you using to print the data to the printer ? Are you
> using some module ? Is it pure plain text ? Postscript ?
>
> There's probably some Perl module out there that can print combined text
> and jpg images to a printer.
>
>
> SaSW, Willem
I use DBI to get the data from the database, after which it is printed as
pure plain text. So I'm not converting anything to XML, HTML, pdf or so.
This has worked for some years now, and no problem.
And my initial question was if there is a Perl module that enables me to
print both a jpg image and the data from the database in 1 single
printjob. But if I understood it correctly, the Graphics modules are
intended to create a graphical image, rather than to fetch one for e.g.
printing on paper.
------------------------------
Date: Wed, 9 Jun 2010 08:16:38 -0700 (PDT)
From: chad <cdalten@gmail.com>
Subject: Re: How to get the class name from the reference to an object?
Message-Id: <ebf03080-bb35-4fb5-8e20-8d058f584ccd@v12g2000prb.googlegroups.com>
On Jun 6, 3:35=A0pm, j...@toerring.de (Jens Thoms Toerring) wrote:
> Peng Yu <pengyu...@gmail.com> wrote:
> > Suppose I new an object. Is there a way to get the class name from the
> > reference to the object?
>
> The ref() function should return the name of the package of the
> object - I guess that's what you mean by "class name".
>
Is there more than one defintion to "class name"?
Chad
------------------------------
Date: Tue, 08 Jun 2010 21:29:54 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: How to read a given number of lines?
Message-Id: <871vchvvql.fsf@quad.sysarch.com>
>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
BM> Quoth Willem <willem@turtle.stack.nl>:
>> Ben Morrow wrote:
>> ) '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.
>>
>> You mean like: for (0..2) { $lines[$_] = <IN> }
BM> Mmm, I suppose. I think my problem with that is that I just dislike
BM> array indices other than [0] and [-1]: I think of arrays as 'frozen
BM> lists', so either you iterate over the whole thing or you attack it from
BM> the ends.
amusing viewpoint but what about random accesses, direct accesses (it
happens), splicing (both inserts and deletes), etc. true, i do what you
say most of the time but i still keep those other things in my
toolbox. :)
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: Wed, 09 Jun 2010 07:28:55 +0200
From: Mart van de Wege <mvdwege@mail.com>
Subject: Re: How to read a given number of lines?
Message-Id: <86hblcpyeg.fsf@gareth.avalon.lan>
Ralph Malph <ralph@happydays.com> writes:
>>> 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
You know, if you want your advice to be taken seriously, it would be
better not to behave like a complete twit.
Mart
--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
------------------------------
Date: Wed, 9 Jun 2010 01:12:44 -0700 (PDT)
From: bankair <bankair@gmail.com>
Subject: Re: How to read a given number of lines?
Message-Id: <761f85c3-4fc3-40c3-9dbd-a1f20cc4ff0a@g19g2000yqc.googlegroups.com>
On Jun 8, 6:14=A0pm, Peng Yu <pengyu...@gmail.com> 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=3D<IN>;
> $line2=3D<IN>;
> $line3=3D<IN>;
Hi,
May be the special var "$." (line NBR of the last read file handle)
could help :
my @lines;
while (<>) { push @lines,$_ if($.<4) }
Regards,
Alexandre
------------------------------
Date: Wed, 09 Jun 2010 04:17:33 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: How to read a given number of lines?
Message-Id: <8739wwr55u.fsf@quad.sysarch.com>
>>>>> "b" == bankair <bankair@gmail.com> writes:
b> On Jun 8, 6:14 pm, Peng Yu <pengyu...@gmail.com> 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>;
b> Hi,
b> May be the special var "$." (line NBR of the last read file handle)
b> could help :
b> my @lines;
b> while (<>) { push @lines,$_ if($.<4) }
and what happens to the lines after the third one? that will read to eof
which is likely not wanted.
better to invert the loop:
while ( $. < 4 ) {
push @lines, scalar <> ;
}
or for those lovers of one liners:
push @lines, scalar <> while $. < 4 ;
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: Wed, 9 Jun 2010 01:41:18 -0700 (PDT)
From: bankair <bankair@gmail.com>
Subject: Re: How to read a given number of lines?
Message-Id: <cf51656d-9ce9-4fd8-a7c8-4c2fbd2b5bf1@b35g2000yqi.googlegroups.com>
On Jun 9, 10:17=A0am, "Uri Guttman" <u...@StemSystems.com> wrote:
> >>>>> "b" =3D=3D bankair =A0<bank...@gmail.com> writes:
>
> =A0 b> On Jun 8, 6:14 pm, Peng Yu <pengyu...@gmail.com> wrote:
> =A0 >> I want to give a given number of lines. Current, I have to write t=
he
> =A0 >> following code to read, for example, 3 lines. Is there a subroutin=
e to
> =A0 >> read a given number of lines in an array?
> =A0 >>
> =A0 >> $line1=3D<IN>;
> =A0 >> $line2=3D<IN>;
> =A0 >> $line3=3D<IN>;
>
> =A0 b> Hi,
>
> =A0 b> May be the special var "$." (line NBR of the last read file handle=
)
> =A0 b> could help :
> =A0 b> my @lines;
> =A0 b> while (<>) { push @lines,$_ if($.<4) }
>
> and what happens to the lines after the third one? that will read to eof
> which is likely not wanted.
>
> better to invert the loop:
>
> while ( $. < 4 ) {
> =A0 =A0 =A0 =A0 push @lines, scalar <> ;
>
> }
>
> or for those lovers of one liners:
>
> =A0 =A0 =A0 =A0 push @lines, scalar <> while $. < 4 ;
Ah ! Your "scalar <>" (thank you) gave me another idea :
my @lines =3D grep {defined $_} map {scalar <>} (1..3);
------------------------------
Date: Wed, 9 Jun 2010 11:06:01 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: How to read a given number of lines?
Message-Id: <p424e7-p39.ln1@news.rtij.nl>
On Wed, 09 Jun 2010 04:17:33 -0400, Uri Guttman wrote:
>>>>>> "b" == bankair <bankair@gmail.com> writes:
>
> b> On Jun 8, 6:14Â pm, Peng Yu <pengyu...@gmail.com> 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>;
>
> b> Hi,
>
> b> May be the special var "$." (line NBR of the last read file handle)
> b> could help :
> b> my @lines;
> b> while (<>) { push @lines,$_ if($.<4) }
>
> and what happens to the lines after the third one? that will read to eof
> which is likely not wanted.
>
> better to invert the loop:
>
> while ( $. < 4 ) {
> push @lines, scalar <> ;
> }
>
> or for those lovers of one liners:
>
> push @lines, scalar <> while $. < 4 ;
Those have the potential of an infinite loop when the file has less than
3 lines. Besides, even when fixed, the 'push @lines, scalar <>' pushes
undef at eof, so I very much doubt this is what you want.
So I guess the best option (assuming you want to exit early at eof, not
push undefs):
while (<>) {
last if ($.>=4)
push @lines,$_;
}
Eventually followed by
push @lines, undef for ($. .. 3);
If you do want undefs for missing lines.
M4
------------------------------
Date: Wed, 09 Jun 2010 11:23:39 -0000
From: Justin C <justin.0911@purestblue.com>
Subject: Re: How to read a given number of lines?
Message-Id: <7c87.4c0f79bb.875b8@zem>
On 2010-06-08, Peng Yu <pengyu.ut@gmail.com> 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>;
>
> Regards,
> Peng
The variable $. holds the current line number for the last filehandle
accessed. It may be useful to you, see perldoc perlvar for details.
Justin.
--
Justin C, by the sea.
------------------------------
Date: Wed, 9 Jun 2010 08:48:43 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Re: How to read a given number of lines?
Message-Id: <56ba07b7-166d-4cec-9ffa-c75c47ca7574@u7g2000yqm.googlegroups.com>
On Jun 8, 3:58=A0pm, Marc Girod <marc.gi...@gmail.com> wrote:
> On Jun 8, 9:14=A0pm, Willem <wil...@turtle.stack.nl> wrote:
>
> > You mean like: for (0..2) { $lines[$_] =3D <IN> }
>
> I'am not smart enough to not try being clever.
> Besides (and for wrong reasons: I used to like Lisp), I like maps.
> So, I was thinking of the following minimal(?) fix(?):
>
> my @lines =3D map scalar <IN>, 1..3;
I don't understand why 'scalar' has to be used. Would you please let
me know where I should read in man page to understand file handle in
scalar and list context.
------------------------------
Date: Wed, 9 Jun 2010 10:17:24 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: How to read a given number of lines?
Message-Id: <fcfc1903-366a-41f4-a5e4-15a666e9c310@a30g2000yqn.googlegroups.com>
On Jun 9, 4:48=A0pm, Peng Yu <pengyu...@gmail.com> wrote:
> I don't understand why 'scalar' has to be used. Would you please let
> me know where I should read in man page to understand file handle in
> scalar and list context.
I/O Operators in perlop?
In scalar context, evaluating a filehandle in angle brackets
yields the
next line from that file (the newline, if any, included), or
"undef" at
end-of-file or on error.
...
If a <FILEHANDLE> is used in a context that is looking for a
list, a list
comprising all input lines is returned, one line per list
element.
and then, of course, scalar in perlfunc...
scalar EXPR
Forces EXPR to be interpreted in scalar context and
returns the
value of EXPR.
Marc
------------------------------
Date: Wed, 9 Jun 2010 02:41:32 -0700 (PDT)
From: "++imanshu" <himanshu.garg@gmail.com>
Subject: Official Behavior of $str =~ $pattern
Message-Id: <f2be0a17-4a59-40a5-93ac-568644e11514@h37g2000pra.googlegroups.com>
1 if ('abc' =~ 'ab')
2 {
3 print 'hello, world'
4 }
The snippet above prints 'hello, world' even though the RHS of =~
isn't inside // or m{}.
I have some legacy code which does a lot of this. Could you point me
to the official behaviour of such expressions in RHS, please.
Thank You,
Himanshu
------------------------------
Date: Wed, 09 Jun 2010 11:53:45 +0200
From: Peter Makholm <peter@makholm.net>
Subject: Re: Official Behavior of $str =~ $pattern
Message-Id: <87sk4wttue.fsf@vps1.hacking.dk>
"++imanshu" <himanshu.garg@gmail.com> writes:
> The snippet above prints 'hello, world' even though the RHS of =~
> isn't inside // or m{}.
>
> I have some legacy code which does a lot of this. Could you point me
> to the official behaviour of such expressions in RHS, please.
It is documented in 'perldoc perlop' look for the paragraph named
'Binding Operators':
If the right argument is an expression rather than a search pattern,
substitution, or transliteration, it is interpreted as a search
pattern at run time. Note that this means that its contents will be
interpolated twice, [...]
//Makholm
------------------------------
Date: Wed, 9 Jun 2010 10:18:34 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Why does perl allow so many different ways of doing essentially the same thing?
Message-Id: <c54188bc-e6e7-4654-b7ae-8f0642c542c9@20g2000vbi.googlegroups.com>
On Jun 9, 11:42=A0am, Peng Yu <pengyu...@gmail.com> wrote:
> Is there any study with concrete data demonstrates how this multiple-
> way-of-doing-the-same-thing philosophy actually can help programmer
> improve productivity?
Why ask, 'Why?'
Why does Python (supposedly) allow only one way to do something? Why
does C use explicit pointers? Why does Lisp use the same syntax for
both data and code? Why does Erlang forbid reassignable variables? Why
does Erlang not provide for shared objects but insist that you only
use message passing? Why does Java insists that you encapsulate
everything in a class, even going so far as to insist on a static
class if all you want to do is write a simple, stand alone script?
Why are different languages different? Why aren't all languages the
same?
Here's my answer, really a meta-answer. Have you ever noticed that
different tools are different? We have hammers for nails, screw
drivers for screws, wrenches for nuts and bolts, etc. Why? We have
Fortran for number crunching, JavaScript for browser apps, COBOL for
business logic, C for bit-twiddling, PL/SQL for databases, bash for
shells, and so on. Why?
Because different tools are optimized for different tasks. Yes, you
can use a hammer to drive and remove screws, and I've done that. You
can use a screw driver to drive and remove nails, and I've done that
as well. But generally, you are better served by using the right tool
for the job.
Erlang is optimized for telecom systems, systems that handle large
amounts of data, are very process intense, that have to be extremely
reliable, and that can have no downtime. Erlang doesn't have a string
data type. Imagine that -- a computer language without a string data
type!
Perl is optimized for particular kinds of jobs. I'm a database guy and
I manipulate data for a living. Turns out that Perl is pretty much
perfect for that, and I've tried several other languages for the same
thing, so I'm speaking from experience. Part of that optimization is
allowing different ways of doing stuff. If you munge data for a
living, you will know what I mean.
You don't have to use Perl. In fact, if your job isn't suited for
Perl, you SHOULDN'T use Perl, but something else. However, if you use
Perl for it's intended purpose, and have used other languages for
different kinds of jobs, you understand that TIMTOWTDI is part of
Perl's optimization, without which it wouldn't be Perl, but Python or
maybe Lisp or some other language.
CC.
------------------------------
Date: Wed, 09 Jun 2010 12:15:18 -0500
From: John Bokma <john@castleamber.com>
Subject: Re: Why does perl allow so many different ways of doing essentially the same thing?
Message-Id: <87y6eonn4p.fsf@castleamber.com>
Peng Yu <pengyu.ut@gmail.com> writes:
> I know some justification that people could choose whatever style they
> want if there are multiple ways of doing the same thing. But this
> justification is not convincing to me, considering that it may cause
> the code not readable and cause many maintenance issues.
Even if a language has a limited number of keywords and ways to do
things there are still a huge number of ways to implement an
algorithm. If in doubt, check out Python. Or better, repost the "how to
read the first 3 lines of a file" question in comp.lang.python, grab
some popcorn, sit back, and watch the drama unfold :-).
--
John Bokma j3b
Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
------------------------------
Date: Wed, 09 Jun 2010 13:18:11 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Why does perl allow so many different ways of doing essentially the same thing?
Message-Id: <86iq5s6s6k.fsf@mithril.chromatico.net>
>>>>> "PY" == Peng Yu <pengyu.ut@gmail.com> writes:
PY> But I doubt that having multiple ways of doing the same thing
PY> really give us any advantages over other languages.
The culture in which Perl has developed for the past couple of decades
embraces "TIMTOWTDI" - "there is more than one way to do it." See the
entry in the glossary of Programming Perl, 3rd ed., on page 1005.
Other languages' cultures prefer that there be one correct way to do
things; if that's what you prefer, then you'll probably be happier with
another language.
Charlton
--
Charlton Wilbur
cwilbur@chromatico.net
------------------------------
Date: Wed, 9 Jun 2010 08:42:04 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Why does perl allow so many different ways of doing essentially the same thing?
Message-Id: <11281c81-0c0d-4325-ad83-c6da9f11a956@b35g2000yqi.googlegroups.com>
The following thread shows many ways of reading a given number of
lines.
http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thread/478db847f15ce016/fe47580f819866cb?lnk=gst&q=pengyu.ut#fe47580f819866cb
But I doubt that having multiple ways of doing the same thing really
give us any advantages over other languages. Essentially this can be
encapsulate in a subroutine, which is easy for refactoring and code
transformation.
Although, there might be advantages in other scenario to have multiple
ways of doing the same thing. But I don't think it is the case for the
particular question.
I know some justification that people could choose whatever style they
want if there are multiple ways of doing the same thing. But this
justification is not convincing to me, considering that it may cause
the code not readable and cause many maintenance issues.
Is there any study with concrete data demonstrates how this multiple-
way-of-doing-the-same-thing philosophy actually can help programmer
improve productivity?
Regards,
Peng
------------------------------
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 2981
***************************************