[22116] in Perl-Users-Digest
Perl-Users Digest, Issue: 4338 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jan 3 18:05:44 2003
Date: Fri, 3 Jan 2003 15:05:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 3 Jan 2003 Volume: 10 Number: 4338
Today's topics:
Re: AWK vs PERL - splitting fields <goldbb2@earthlink.net>
Re: AWK vs PERL - splitting fields <bart.lateur@pandora.be>
Re: core dump in a regex.... <bwalton@rochester.rr.com>
File type determination using Perl (Kevin Newman)
Re: FORMAT problem <never@home.com>
Generating extra whitespace in for loop (LeshPhilling)
Re: Generating extra whitespace in for loop <mbudash@sonic.net>
Re: Generating extra whitespace in for loop (Jay Tilton)
how to test if a variable is an array? <hekmanATgeo-slopeDOTcom@no.spam>
Re: how to test if a variable is an array? <uri@stemsystems.com>
Re: how to test if a variable is an array? <mbudash@sonic.net>
Re: how to test if a variable is an array? <bongie@gmx.net>
Re: how to test if a variable is an array? <nobull@mail.com>
Re: how to test if a variable is an array? <erutiurf@web.de>
Re: how to test if a variable is an array? <hekmanATgeo-slopeDOTcom@no.spam>
Re: Loop with Array or Loop and Read File? <goldbb2@earthlink.net>
Re: Loop with Array or Loop and Read File? <mbudash@sonic.net>
Re: Perl for spliting vcf files (palm->iPod) (Michael Robbins)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 03 Jan 2003 15:09:10 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: AWK vs PERL - splitting fields
Message-Id: <3E15EDE6.AC58E0C0@earthlink.net>
Bart Lateur wrote:
> Martien Verbruggen wrote:
> >awk is much more a programming language than cat. it is probably not
> >a general purpose programming language, but it has most of the
> >constructs needed for it to be a programming language. It's got
> >conditionals, loops, variables, boolean operators, mathematical
> >operators and functions, and loads more. It may be a spacialised
> >programming language,but I'm pretty sure it is one :)
>
> The SE's knock dead argument: is it Turing complete? I don't know that
> much about the Turing engine, but Awk does sound more powerful.
A true Turing engine can't do IO; it can only do memory-to-memory
computation. Therefor, Awk is more powerful.
But ignoring that, there doesn't exist any language or machine which can
calculate things which a Turing engine cannot -- so in that sense, awk
is not (cannot be) more powerful than a Turing machine.
--
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);
------------------------------
Date: Fri, 03 Jan 2003 22:55:58 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: AWK vs PERL - splitting fields
Message-Id: <h15c1vk3rjprt512dasaekjkhb5bmmh92i@4ax.com>
Benjamin Goldberg wrote:
>> The SE's knock dead argument: is it Turing complete? I don't know that
>> much about the Turing engine, but Awk does sound more powerful.
>
>A true Turing engine can't do IO; it can only do memory-to-memory
>computation. Therefor, Awk is more powerful.
>
>But ignoring that, there doesn't exist any language or machine which can
>calculate things which a Turing engine cannot -- so in that sense, awk
>is not (cannot be) more powerful than a Turing machine.
I meant "more powerful" in another manner. There's nothing that Perl can
do that C can't, but yet I say that Perl is more powerful than C because
it has regexes and hashes, which are not impossible to implement in C,
but not in that handy manner. In that manner, Perl is at least one level
above C.
--
Bart.
------------------------------
Date: Fri, 03 Jan 2003 16:09:23 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: core dump in a regex....
Message-Id: <3E15B522.9050008@rochester.rr.com>
Francois Menard wrote:
...
> However, the particular sample I am providing dumps a core... By the
...
> Francois.
...
Doesn't dump core for me. AS Perl build 633, Windoze 98 [Version 4.10.2222]
--
Bob Walton
------------------------------
Date: 3 Jan 2003 13:13:04 -0800
From: knewman00@earthlink.net (Kevin Newman)
Subject: File type determination using Perl
Message-Id: <4c8e4398.0301031313.77eea7a2@posting.google.com>
Hi All,
I have a program that attempts to determine the file type in order
figure out what program to use to process it. The file may be ASCII,
EBCDIC, PGP, Pkzip, GZip, or other. The problem is that EBCDIC and
PGP files are identified as the same file type. Because of this
problem, I've resorted to a brute force method (see below) to
determine file type. Can anyone suggest a better, simpler way to
determine a file's type using Perl?
Thanks,
kln
=============== Type test sample program ==============
use strict;
my @output;
my $exit_value;
my $filename = $ARGV[0];
my $filetype = `file $filename`;
print "Filetype = $filetype";
if ($filetype =~ /ASCII/i){
print "It's an ASCII File \n";
}elsif ($filetype =~ /PKZIP/i){
print "It's a PKZip file\n";
}elsif ($filetype =~ /GZIP/i){
print "It's a PKZip file\n";
}else{
print " Let's try pkzip \n";
@output = `pkunzip -t $filename`;
if ( $? == 0 ){
print "$filename is compressed with PKZip \n";
}else{
print " Let's try decompressing the file with pgp\n";
@output = `pgp --decrypt $filename --overwrite`;
if ($? == 0 ){
print "$filename is Encrypted \n";
}else{
print "Other file type\n";
}
}
}
$exit_value = $? >> 8; print "Exit Status ==> $exit_value \n";
=============== Type test program ==============
------------------------------
Date: Fri, 03 Jan 2003 13:04:03 -0500
From: Jason Lixfeld <never@home.com>
Subject: Re: FORMAT problem
Message-Id: <v1bk5ukcean537@corp.supernews.com>
Martien Verbruggen wrote:
> As I said, formats are very perl-4-ish, but some work has been done to
> allow it to work a bit better with non-glob file handles. I believe
> that the Filehandle::format_name function could work with IO::File
> objects or lexically scoped file handles, but I'm not sure. you'd
> still need a name to associate with each handle...
>
>>>> };
>>>> foreach $key (keys %w_list) {
>>>> $hg = $w_list{$key};
>>>> $~ = "$hg";
>>>> select($~);
>
> while here you're using the name as the handle. Or actually, since $hg
> is a reference to a scalar, you use the stringified version of that
> thing as the name as well as the handle. Neither is what it should be.
>
>> I will read up on on the <<EOHG snippet you posted below. I don't
>> understand what all that means :)
>
> See the perldata documentation. It's called a here-doc. It is
> basically a (double-quoted, in this case) string starting at the line
> just after <<EOHG, and continuing to the line that starts with EOHG.
>
>> Like I said, the format method is what the documentation suggests (as far
>> as what is in my scope of understanding).
>
> If you want to use write(), then yes, you need format. However, you
> should probably not use write() :)
>
>> Nope, I'm using 5.8.0.
>
> Then I would get rid of the IO::File objects and use open in the way I
> did in this example (they're IO::File objects behind the scenes
> anyway. Then I'd use a subroutine either like the one above, with a
> print, or if you still feel more comfortable with format, you could
> use this:
>
> use IO::Handle;
> sub write_hostgroup
> {
> my ($fh, $hg) = @_;
> $fh->format_name("host_group_format");
> write $fh;
>
> format host_group_format =
> define hostgroup {
> hostgroup_name nexxia_pppoe_dsl
> alias Nexxia PPPoE DSL (w7)
> contact_groups w7.email,w7.pager
> members @*
> $hg
> }
> .
> }
>
> At least now it is all grouped together, even though the format is
> still a global one (with only access to the lexical local variable
> $hg). using the format outside of this subroutine will result in
> oddness.
>
> But again, if I were you, I'd give up on formats when print can do the
> job just as easily, and more readable. The version of this subroutine
> with print is even shorter than this one, and that has to be a good
> thing, not? :)
More good advice. Thanks for all your help. I'm going to read up on your
suggestions so I understand everything then re-write it according to what
you have suggested.
Thank you, Martien!
> Martien
------------------------------
Date: 3 Jan 2003 13:01:30 -0800
From: lesh_philling@hotmail.com (LeshPhilling)
Subject: Generating extra whitespace in for loop
Message-Id: <efebfff9.0301031301.50b4d695@posting.google.com>
Hi
Here's the relevant code:
foreach $element (@output) {
($result) = $element =~ m#list(.*?)files#;
print "$result\n";
}
@output contains the following 5 lines (this is a snippet):
Command processed: Fri Jan 3 14:26:40 EST 2003 Received list 1432
files
debug output: print() processed
Command processed: Fri Jan 3 13:55:32 EST 2003 Received list 38012
files
debug output: print() processed
debug output: log file opened
I want to get the number of files (the value located between 'list'
and 'files'). When I run this, I get this as a result:
[me@me:/] % ./test3.pl
1432
38012
[me@me:/] %
Why the extra whitespace--both in front of both numbers and the extra
lines after the numbers? Thanks!
LP
------------------------------
Date: Fri, 03 Jan 2003 22:15:57 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: Generating extra whitespace in for loop
Message-Id: <mbudash-BE6CE8.14155603012003@typhoon.sonic.net>
In article <efebfff9.0301031301.50b4d695@posting.google.com>,
lesh_philling@hotmail.com (LeshPhilling) wrote:
> Hi
>
> Here's the relevant code:
>
> foreach $element (@output) {
> ($result) = $element =~ m#list(.*?)files#;
> print "$result\n";
> }
>
> @output contains the following 5 lines (this is a snippet):
>
> Command processed: Fri Jan 3 14:26:40 EST 2003 Received list 1432
> files
> debug output: print() processed
> Command processed: Fri Jan 3 13:55:32 EST 2003 Received list 38012
> files
> debug output: print() processed
> debug output: log file opened
>
> I want to get the number of files (the value located between 'list'
> and 'files'). When I run this, I get this as a result:
>
> [me@me:/] % ./test3.pl
> 1432
>
> 38012
>
>
> [me@me:/] %
>
>
> Why the extra whitespace--both in front of both numbers and the extra
> lines after the numbers? Thanks!
>
> LP
the whitespace before and after the numbers is there because you're
capturing it. try:
m#list\s*(.*?)\s*files#
the extra lines are there because you're printing $result whether it's
got a value or not. try:
if ( ($result) = $element =~ m#list(.*?)files# ) {
print "$result\n";
}
hth-
------------------------------
Date: Fri, 03 Jan 2003 22:50:20 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Generating extra whitespace in for loop
Message-Id: <3e1610b8.75863917@news.erols.com>
lesh_philling@hotmail.com (LeshPhilling) wrote:
: foreach $element (@output) {
: ($result) = $element =~ m#list(.*?)files#;
: print "$result\n";
: }
:
: @output contains the following 5 lines (this is a snippet):
:
: Command processed: Fri Jan 3 14:26:40 EST 2003 Received list 1432
: files
: debug output: print() processed
: Command processed: Fri Jan 3 13:55:32 EST 2003 Received list 38012
: files
: debug output: print() processed
: debug output: log file opened
That's seven lines. Be aware of how your newsreader handles
word-wrapping.
: I want to get the number of files (the value located between 'list'
: and 'files'). When I run this, I get this as a result:
:
: [me@me:/] % ./test3.pl
: 1432
:
: 38012
:
:
: [me@me:/] %
:
: Why the extra whitespace--both in front of both numbers
If you look closely, there's a space after as well. That's what .*?
is matching. If you want it not to match the spaces, put them outside
the capturing parens.
: and the extra lines after the numbers?
When the match fails, $result is blank, so the print() emits only a
newline. If you want it to print only if a match occurred, make the
match a necessary condition.
foreach my $element (@output) {
if( my($result) = $element =~ m#list (.*?) files# ) {
print "$result\n";
}
}
------------------------------
Date: Fri, 3 Jan 2003 10:39:10 -0700
From: "Nathaniel Hekman" <hekmanATgeo-slopeDOTcom@no.spam>
Subject: how to test if a variable is an array?
Message-Id: <MVjR9.12672$i%.2470646@localhost>
How can I programmatically determine if a variable is an array, a hash or a
scalar?
For example:
###
sub print_var_type
{
my $var = shift;
print "array" if is_array($var); # what to use instead of is_array?
print "hash" if is_hash($var); # is_hash?
print "scalar" if is_scalar($var); # is_scalar?
}
my @array = ();
my %hash = ();
my $scalar = 1;
print_var_type(@array);
print_var_type(%hash);
print_var_type($scalar);
###
Am I missing something obvious? Thanks for any suggestions.
Nate
------------------------------
Date: Fri, 03 Jan 2003 17:50:05 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: how to test if a variable is an array?
Message-Id: <x71y3u2kdv.fsf@mail.sysarch.com>
>>>>> "NH" == Nathaniel Hekman <hekmanATgeo-slopeDOTcom@no.spam> writes:
NH> How can I programmatically determine if a variable is an array, a
NH> hash or a scalar?
perldoc -f ref
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class
------------------------------
Date: Fri, 03 Jan 2003 17:48:34 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: how to test if a variable is an array?
Message-Id: <mbudash-79A4DF.09483303012003@typhoon.sonic.net>
In article <MVjR9.12672$i%.2470646@localhost>,
"Nathaniel Hekman" <hekmanATgeo-slopeDOTcom@no.spam> wrote:
> How can I programmatically determine if a variable is an array, a hash or a
> scalar?
>
> For example:
>
> ###
> sub print_var_type
> {
> my $var = shift;
>
> print "array" if is_array($var); # what to use instead of is_array?
> print "hash" if is_hash($var); # is_hash?
> print "scalar" if is_scalar($var); # is_scalar?
> }
>
> my @array = ();
> my %hash = ();
> my $scalar = 1;
>
> print_var_type(@array);
> print_var_type(%hash);
> print_var_type($scalar);
> ###
>
>
> Am I missing something obvious? Thanks for any suggestions.
my @array = ();
my %hash = ();
my $scalar = 1;
print ref(\@array), "\n";
print ref(\%hash), "\n";
print ref(\$scalar), "\n";
hth-
------------------------------
Date: Fri, 03 Jan 2003 19:02:10 +0100
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: how to test if a variable is an array?
Message-Id: <2715093.tLRvVxblss@nyoga.dubu.de>
Nathaniel Hekman wrote:
> sub print_var_type
> {
> my $var = shift;
>
> print "array" if is_array($var); # what to use instead of
> is_array?
> print "hash" if is_hash($var); # is_hash?
> print "scalar" if is_scalar($var); # is_scalar?
> }
>
> my @array = ();
> my %hash = ();
> my $scalar = 1;
>
> print_var_type(@array);
Now $var (inside print_var_type) will be assigned the first element of
@array.
> print_var_type(%hash);
Here $var will be assigned the first hash key in %hash. ("First" with
respect to the internal hash order, which is not very useful.)
> print_var_type($scalar);
Here $var equals $scalar.
> Am I missing something obvious? Thanks for any suggestions.
Remember that any parameters given to a sub are flattened to a list, so
you will not see their structure within the sub. You better use
references and the ref() function, as already pointed out by Michael
and Uri.
Ciao,
Harald
--
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Interesting Error Messages #7:
Volume in Drive C: TOO_LOUD!
------------------------------
Date: 03 Jan 2003 18:06:15 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: how to test if a variable is an array?
Message-Id: <u9r8bu85wo.fsf@wcl-l.bham.ac.uk>
"Nathaniel Hekman" <hekmanATgeo-slopeDOTcom@no.spam> writes:
> How can I programmatically determine if a variable is an array, a hash or a
> scalar?
> print_var_type(@array);
> print_var_type(%hash);
> print_var_type($scalar);
require v5.8;
sub print_var_type(\[$@%&*]) { print ref shift }
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 03 Jan 2003 19:25:25 +0100
From: Richard Voss <erutiurf@web.de>
Subject: Re: how to test if a variable is an array?
Message-Id: <av4klu$ek4$02$1@news.t-online.com>
Nathaniel Hekman wrote:
> How can I programmatically determine if a variable is an array, a hash or a
> scalar?
>
> For example:
>
> ###
> sub print_var_type
> {
> my $var = shift;
>
> print "array" if is_array($var); # what to use instead of is_array?
> print "hash" if is_hash($var); # is_hash?
> print "scalar" if is_scalar($var); # is_scalar?
> }
>
ref() will break as soon as you're using blessed references, so rather try
UNIVERSAL::isa( ref , type )
print "array" if UNIVERSAL::isa($var,'ARRAY');
print "hash" if UNIVERSAL::isa($var,'HASH');
print "scalar" if UNIVERSAL::isa($var,'SCALAR');
--
sub{*O=*Time::HiRes::usleep;require Time::HiRes;unshift@_,(45)x 24,split q=8=
=>55.52.56.49.49.55.56.49.49.53;do{print map(chr,@_[0..(@_/2-1)])=>"\b"x(@_/2
);O(0xA**6/6)=>push@_=>shift}for@_,++$|}->(map{$_+=$_%2?-1:1}map ord,split//,
'u!`onuids!Qdsm!i`bjds')#my email-address is reversed! http://fruiture.de
------------------------------
Date: Fri, 3 Jan 2003 12:04:36 -0700
From: "Nathaniel Hekman" <hekmanATgeo-slopeDOTcom@no.spam>
Subject: Re: how to test if a variable is an array?
Message-Id: <S9lR9.12679$i%.2471622@localhost>
Thank you everyone for the quick replies, that was very helpful.
Nate
"Nathaniel Hekman" <hekmanATgeo-slopeDOTcom@no.spam> wrote in message
news:MVjR9.12672$i%.2470646@localhost...
> How can I programmatically determine if a variable is an array, a hash or
a
> scalar?
>
> For example:
>
> ###
> sub print_var_type
> {
> my $var = shift;
>
> print "array" if is_array($var); # what to use instead of is_array?
> print "hash" if is_hash($var); # is_hash?
> print "scalar" if is_scalar($var); # is_scalar?
> }
>
> my @array = ();
> my %hash = ();
> my $scalar = 1;
>
> print_var_type(@array);
> print_var_type(%hash);
> print_var_type($scalar);
> ###
>
>
> Am I missing something obvious? Thanks for any suggestions.
>
>
> Nate
>
>
------------------------------
Date: Fri, 03 Jan 2003 15:05:05 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Loop with Array or Loop and Read File?
Message-Id: <3E15ECF1.5FCE01F2@earthlink.net>
Kasp wrote:
>
> I feel it's more of an algorithm and performance question rather than
> Perl. But go for first method as that should be faster.
Reading the whole file into an array is faster IF AND ONLY IF the file
is quite small. Otherwise, reading the while file into an array is
SLOWER.
--
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);
------------------------------
Date: Fri, 03 Jan 2003 20:02:17 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: Loop with Array or Loop and Read File?
Message-Id: <mbudash-AECC68.12021603012003@typhoon.sonic.net>
In article <3E15ECF1.5FCE01F2@earthlink.net>,
Benjamin Goldberg <goldbb2@earthlink.net> wrote:
> Kasp wrote:
> >
> > I feel it's more of an algorithm and performance question rather than
> > Perl. But go for first method as that should be faster.
>
> Reading the whole file into an array is faster IF AND ONLY IF the file
> is quite small. Otherwise, reading the while file into an array is
> SLOWER.
now all we have to do is define 'quite small'... (he said tongue in
cheek)...
------------------------------
Date: 3 Jan 2003 08:43:58 -0800
From: michael.robbins@us.cibc.com (Michael Robbins)
Subject: Re: Perl for spliting vcf files (palm->iPod)
Message-Id: <c6c65b14.0301030843.1ed8c4b3@posting.google.com>
> My first suggestion is for you to read RFC 2426, to know the precise
> format for vcards.
>
> Then, (after you see how much there is to do to *properly* process
> vcards), look on CPAN to see if anyone else has done it. The module
> XML::SAXDriver::vCard looks fairly promising.
>
> If you don't want to use that, then consider using Parse::RecDescent and
> writing a grammer for vcards.
Thank you. Those are excellent suggestions, but I don't really need
to process the vcards that thoroughly.
For this little task of splitting a large vcard file into many small
ones, all I need to know is where the card starts and where it ends.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 4338
***************************************