[31328] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 2573 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Aug 28 21:09:43 2009

Date: Fri, 28 Aug 2009 18:09:09 -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, 28 Aug 2009     Volume: 11 Number: 2573

Today's topics:
        =?ISO-8859-1?Q?=22PAKISTANI_GIRLS=22_=22PAKISTANI_GIRLS <saimapk81@gmail.com>
    Re: differences between find2perl and find sln@netherlands.com
        Perl on Windows - use Perl as DLL <claus.kick@googlemail.com>
    Re: Perl on Windows - use Perl as DLL <dmw@coder.cl>
        perl script to generate server round-robin assignments <inetquestion@hotmail.com>
        strategy for parsing text file <cartercc@gmail.com>
    Re: strategy for parsing text file <tadmc@seesig.invalid>
    Re: strategy for parsing text file <cartercc@gmail.com>
    Re: strategy for parsing text file <RedGrittyBrick@SpamWeary.invalid>
    Re: strategy for parsing text file <smallpond@juno.com>
    Re: strategy for parsing text file sln@netherlands.com
    Re: strategy for parsing text file sln@netherlands.com
    Re: strategy for parsing text file <someone@example.com>
    Re: Test::Simple etc ... do you leave them in the finis <smallpond@juno.com>
    Re: windows one liner to output unix line feed <source@netcom.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 28 Aug 2009 17:13:34 -0700 (PDT)
From: saima81 <saimapk81@gmail.com>
Subject: =?ISO-8859-1?Q?=22PAKISTANI_GIRLS=22_=22PAKISTANI_GIRLS=22_=22CUTE_PAKISTANI_?= =?ISO-8859-1?Q?GIRLS=22_=22PAK_GIRLS=22_=22CUTE_PAK_GIRLS=22_on_www=2Eepakistanigirls=2Eb?= =?ISO-8859-1?Q?logspot=2Ec=ADom_=22PAKISTANI_GIRLS=22_=22PAKISTANI_GIRLS=22_=22CUTE_PAKI?= =?ISO-8859-1?Q?STANI_GIRLS=22_=22PAK_GIRLS=22_=22CUTE_PAK_GIRLS=22_on_www=2Eepakistanig?= =?ISO-8859-1?Q?irls=2Eblogspot=2Ec=AD?=
Message-Id: <800f3298-07df-4dd5-be2c-587ac3ec35fa@x6g2000prc.googlegroups.com>

"PAKISTANI GIRLS" "PAKISTANI GIRLS" "CUTE PAKISTANI GIRLS" "PAK GIRLS"
"CUTE PAK GIRLS" on www.epakistanigirls.blogspot.c=ADom "PAKISTANI
GIRLS" "PAKISTANI GIRLS" "CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK
GIRLS" on www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS"
"PAKISTANI GIRLS" "CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS"
on www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI
GIRLS" "CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom "PAKISTANI GIRLS" "PAKISTANI GIRLS"
"CUTE PAKISTANI GIRLS" "PAK GIRLS" "CUTE PAK GIRLS" on
www.epakistanigirls.blogspot.c=ADom


------------------------------

Date: Fri, 28 Aug 2009 13:28:00 -0700
From: sln@netherlands.com
Subject: Re: differences between find2perl and find
Message-Id: <ikdg95pvq7ijnvjriia491m91p7r5ilars@4ax.com>

On Thu, 27 Aug 2009 23:53:55 -0700 (PDT), GU <gerhard_ulrike@yahoo.de> wrote:

># Traverse desired filesystems
>File::Find::find({wanted => \&wanted}, **** list of files **** );
>exit;
>
>
>sub wanted {
>    my ($dev,$ino,$mode,$nlink,$uid,$gid);
>
>    ($File::Find::prune = 1) &&
>    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
>    -d _ &&
>    (int(-M _) < 30);
>}
>
>gm@h213:/tmp >

As far as find2perl:

-prune is going to stop it from enterring sub-direcoties in the found directory.
This is more of a mechanism to operate on a single directory that are found in
the direcoties list. So even if you want to examine the directories under
the current one, this prevents it. Therefore its totally useless in this context.

>find2perl * -prune -type d -mtime -30
           ^
This is not something like glob '*'.

>File::Find::find({wanted => \&wanted}, **** list of files **** );

find2perl dir1 dir2 dir3 file1 -prune -type d -mtime -30
does
File::Find::find({wanted => \&wanted}, 'dir1', 'dir2', 'dir3', 'file1'  );

and there doesen't appear to be any other way to to get a system generated
list. In fact because of this, find2perl is not that usefull in this context.
Its ok for generating a prototype wanted() which you can manipulate and execute
as a standalone script.

The documentation for find is pretty bizarr and not much explanation or examples.
Its all about magic and hocus-pocus.

From the find2perl docs (on the bottom): 
" ... or if you use the warnings pragma,
File::Find will report warnings for several weird situations..."

If you want to turn off warnings if piping the find2perl output to perl,
just add this:

find2perl * -prune -type d -mtime -30 | perl -M-warnings

Good luck. I'm not too suprised there were no responses.
-sln



------------------------------

Date: Fri, 28 Aug 2009 04:27:19 -0700 (PDT)
From: Snorik <claus.kick@googlemail.com>
Subject: Perl on Windows - use Perl as DLL
Message-Id: <6d4687f2-82cd-4311-8824-1fb95129f02c@c14g2000yqm.googlegroups.com>

Hello everyone,

is it possible to use the perl interpreter as DLL to pass arguments
(scriptname, arguments and so on) in the windows way of loadLibrary(),
sendMessage:lparam:wparam: closeLibrary()?

I am normally using perl on Solaris, or just as scripting language,
but now I would like to embed perl in some other language...


------------------------------

Date: Fri, 28 Aug 2009 10:46:16 -0400
From: Daniel Molina Wegener <dmw@coder.cl>
Subject: Re: Perl on Windows - use Perl as DLL
Message-Id: <1257090.MHlYuv61k6@coder.cl>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA512

Snorik <claus.kick@googlemail.com>
on Friday 28 August 2009 07:27
wrote in comp.lang.perl.misc:


> Hello everyone,
> 
> is it possible to use the perl interpreter as DLL to pass arguments
> (scriptname, arguments and so on) in the windows way of loadLibrary(),
> sendMessage:lparam:wparam: closeLibrary()?

  Look at the perlembed manual page.

  http://www.perl.com/doc/manual/html/pod/perlembed.html

> 
> I am normally using perl on Solaris, or just as scripting language,
> but now I would like to embed perl in some other language...

Best regards,
- -- 
 .O. | Daniel Molina Wegener   | FreeBSD & Linux
 ..O | dmw [at] coder [dot] cl | Open Standards
 OOO | http://coder.cl/        | FOSS Developer
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.4.9 (FreeBSD)

iQIcBAEBCgAGBQJKl+24AAoJEHxqfq6Y4O5NRM0P/3dzM6U7MYFwlKvgz9B35FfM
An+3M/tA1tMSSFAJ2ODGxkX8GjV6VByR38K9UOuM3ciIsaYOgBZpvNkp7v64ckDi
64hwB9PPbu2XivWUIoZcNCzEQhZqSj0w68RHttEUzFpTUEUZoEh46TD2wRvzKQ+a
aWEtaPtB4JVPyDcBJjcMqRDVYYQoVzGcBEBxBuzk7dvvY0fhYzDQ2xTJtP96KrTN
F0wB+YnAzmcAo+ffWZ2NZpxFVyjlnS9IkSQGDiw5nIFNF3JkXfAhRoVDyjdDiAg0
30H1YPoxD5aYZE5wSnvfLAZx0WELMc4FuA3+th/7F2CFpUIPgW9+3kFFcWQDaKlx
2r5LBZsv/H5Q8ldVeP0XMlyKNQ4b9EQ9FBua17bmk0lPH36rly3HgL6WJIqLMFe5
teJseK/IjzDszaAcoawll3pUvWZexZE0Oqu5HlzwB3R4e2FyUeHtOyPPfk4senZj
N6o/k71pMvS3toGdygH0LaqTYBX4gbcsTBzA+rcbDzbvp/w3XoELwCuhceUuyICc
6dK71uVYkmiu1DqCKV5GItATr9xO1HTZayDD4iG7NWcnr0iZEV2wFlXBuxABK0ry
xzGH9eIHuXQf8XBlbRMKfo6S+e8A6DWFUC42KxckeANAJKW/zzgOnR17fCB/fLKd
bITse4VzJ+LygfNt033I
=fNmN
-----END PGP SIGNATURE-----


------------------------------

Date: Fri, 28 Aug 2009 17:02:36 -0700 (PDT)
From: inetquestion <inetquestion@hotmail.com>
Subject: perl script to generate server round-robin assignments
Message-Id: <944db6f5-1cc1-49f1-905c-f737bd0f0e23@18g2000yqa.googlegroups.com>

#!/usr/bin/perl

##################
### Main Begin ###
##################

if ($#ARGV < 0) {
    &DoUsage;
    exit;
}

my ($hostname, $limit) = @ARGV;					# Script inputs

if ($hostname =~ /^([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)$/) { 		# Filter
on IP address
      $hostString=$hostname;						# Save IP
} else {
     ($hostString) = split /\./,$hostname;				# Obtain first portion
of FQDN component
}

@B = qw(svr01 svr02 svr03 svr04 svr05 svr06);			# Array containing
assignable server
$binaryNumber = DoAsc2bin($hostString);			       	# Convert string
into base2
$binaryNumber =~ s/\s+//g;      		       			        # Remove spaces
from base2 string
$decString = DoBin2dec($binaryNumber);	       			# Convert base2
string to base10
$assignment = DoAssign(scalar(@B), $decString, $hostname, $limit);	#
Call subroutine to get assignments

print "$hostname: $assignment\n";
exit;

########################
### Subroutine Begin ###
########################

sub DoUsage() {

print <<EOM;


Generates assignment values where servers in list-A need to
communicate with all or some of the servers in list-B.  The input to
the script is a single server hostname or IP from list-A.  This server
name will go through a conversion (ascII->Binary->Dec), then the order
of the assignments will be made.  As long as no two servers have the
same hostname, the assignment will be varied across the list to ensure
the servers in List-B are distributed evenly across those in List-A.

Usage: $0 <hostname|IP> <entry limit>

EOM
}

sub DoAsc2bin {							# Convert ASCII string to binary equivalent
    my ($string) = @_;							# Input
    my @bytes;								# Declare byte array
    for (split //, $string) {						# Run throuh for loop per character
of string being split
      push @bytes, unpack "B8", $_;				# Store binary equivalent of
each character into @bytes
    }
    return wantarray ? @bytes : join " ", @bytes;		# Return @bytes or
a string of all content of @bytes
}

sub DoBin2dec {
    return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));	#
Converts binary string to decimal equivalent
}

sub DoAssign {
    my ($numListB, $num, $host, $lim) = @_;	# Function inputs
    $lim ||= 100;							# Set lim=100 unless otherwise specified
    @B = reverse(@B);  			      		# Reverse array, then pop last
    my $popNum = $num % $numListB;  		# Get modulus, then pop that
many elements from reversed array
    while ( $popNum != 0 ) {                                 	#
Iterate until all values have been popped off array
        push(@popSave, pop(@B));  			# Save popped elements
        $popNum--;					     	# Decrement pop counter
    }
    @B = reverse(@B);	      				# Reverse array
    push(@B, @popSave);			      		# Appennd popped elements to
reversed (original direction) array

    for( $i = 0; $i < scalar(@B); $i++) {                 	# Loop
through all array elements, maintain counter
	if( $B[$i] eq $host ) {                           	# Look for match
            #print "Match on $host\n";			# Debug statement
            @B = ($B[$i], @B[0..($i-1)], @B[($i+1)..scalar
(@B)]);       # Modify array order by moving the "match" to front
 
last;                                                       # Move on
        }
    }
    return join " ", @B[0..($lim-1)];	       		# Return space
deleimited string
}


------------------------------

Date: Fri, 28 Aug 2009 14:25:53 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: strategy for parsing text file
Message-Id: <bd2049be-26ca-48a2-9e61-42e1b7506ceb@w41g2000yqb.googlegroups.com>

I've solved this problem, but I'm just curious as to how by betters
would approach this.

The file is a long file, so I have copied only the first seven records
below as an example. The file is from a table with nine fields, all of
which are named in the first nine lines. The key is a five digit
number beginning with either 91 or 92. For each record, sometimes all
fields are populated (like the first, 91709), but normally only the
first four are guaranteed to be populated while the remaining five may
or may not have values. Each datum occupies a line all to itself, and
the file does not contain record separators.

The requirement is to capture the first four fields and write to an
Excel readable file (CSV format).

My solution was pretty dirty and crude, but I'll share it later (and
take the hit for stupidity). My question is how others might approach
the problem. Below is the first seven records of the file and the
column header.

Thanks, CC.

-------------file below--------------------
Number
BandName
Grade
Branch
Instr
PipingInst
PInstDate
DrumInst
DrumInstDate
91709
87th Cleveland Pipe Band IV
PB4
Ohio Valley
y
Tyler Tagliafero, Great Lakes
01-Mar-09
Drew Donnelly, Great Lakes
01-Mar-09
91068
Adirondack Pipes & Drums
PB5
Northeast
n
91212
Alabama Pipes & Drums
PB4
Southern
n
91801
Albany Police P&D
PB5
Northeast
y
Dan Cole, Oran Mor
01-Mar-09
92033
American Celtic Pipe Band
PB5
Metro
n
91826
Anderson Pipe Band
PB5
Southwest
y
Victor Anderson, Westminster
01-Mar-09
Tim Vermillion, Westminster
01-Mar-09
91802
AOH Pipe & Drum Band
PB5
Northeast
n


------------------------------

Date: Fri, 28 Aug 2009 16:59:26 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: strategy for parsing text file
Message-Id: <slrnh9gk6q.g3f.tadmc@tadmc30.sbcglobal.net>

ccc31807 <cartercc@gmail.com> wrote:

> The key is a five digit
> number beginning with either 91 or 92. 


I will assume that you are absolutely certain that none of the other
field's values will match that specification...


> The requirement is to capture the first four fields


-------------------
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

while ( <DATA> ) {
    next unless /^9[12]\d\d\d$/;  # 5 digits, starts with 91 or 92
    my @record = $_;
    push @record, scalar(<DATA>) for 1..3;
    chomp @record;
    print Dumper \@record;
}
-------------------


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Fri, 28 Aug 2009 15:14:35 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: strategy for parsing text file
Message-Id: <4f15b82f-14cb-4a06-ba2d-e9f830031df7@c14g2000yqm.googlegroups.com>

On Aug 28, 5:59=A0pm, Tad J McClellan <ta...@seesig.invalid> wrote:
> I will assume that you are absolutely certain that none of the other
> field's values will match that specification...

Absolutely!

> -------------------
> #!/usr/bin/perl
> use warnings;
> use strict;
> use Data::Dumper;
>
> while ( <DATA> ) {
> =A0 =A0 next unless /^9[12]\d\d\d$/; =A0# 5 digits, starts with 91 or 92
> =A0 =A0 my @record =3D $_;
> =A0 =A0 push @record, scalar(<DATA>) for 1..3;
> =A0 =A0 chomp @record;
> =A0 =A0 print Dumper \@record;}

I see ... you can access the file within the while loop by using the
<> in an inner loop. I maybe should have thought of that, but I had to
produce it quickly and didn't want to experiment.

Thanks, and here is the guts of my solution. Pretty crude, but it
worked.

open INFILE, '<', 'bands.txt';
while (<INFILE>)
{
	next unless /\w/;
	print; #debugging
	chomp;
	if (/9[12]\d{3}/)
	{
		$count++;
		$key =3D $_;
		$flag =3D 1;
	}
	elsif ($flag =3D=3D 1)
	{
		$bands{$key}{name} =3D $_;
		$flag =3D 2;
	}
	elsif ($flag =3D=3D2)
	{
		$bands{$key}{grade} =3D $_;
		$flag =3D 3;
	}
	elsif ($flag =3D=3D 3)
	{
		$bands{$key}{branch} =3D $_;
		$flag =3D 0;
	}

}

#print the %bands hash to a .csv file


------------------------------

Date: Fri, 28 Aug 2009 23:28:45 +0100
From: RedGrittyBrick <RedGrittyBrick@SpamWeary.invalid>
Subject: Re: strategy for parsing text file
Message-Id: <6sKdnYiDy_aCxwXXnZ2dnUVZ8lFi4p2d@bt.com>

ccc31807 wrote:
> I've solved this problem, but I'm just curious as to how by betters
> would approach this.
> 
> The file is a long file, so I have copied only the first seven records
> below as an example. The file is from a table with nine fields, all of
> which are named in the first nine lines. The key is a five digit
> number beginning with either 91 or 92. For each record, sometimes all
> fields are populated (like the first, 91709), but normally only the
> first four are guaranteed to be populated while the remaining five may
> or may not have values. Each datum occupies a line all to itself, and
> the file does not contain record separators.
> 
> The requirement is to capture the first four fields and write to an
> Excel readable file (CSV format).
> 
> My solution was pretty dirty and crude, but I'll share it later (and
> take the hit for stupidity). My question is how others might approach
> the problem. Below is the first seven records of the file and the
> column header.
> 

#!perl
use strict;
use warnings;

my @f;
while (<DATA>) {
   chomp;
   if (/^9[12]\d{3}$/) {
     print join (',', @f), "\n" if @f;
     @f=();
   }
   push @f, $_;
}

__DATA__



-- 
RGB


------------------------------

Date: Fri, 28 Aug 2009 18:50:54 -0400
From: Steve C <smallpond@juno.com>
Subject: Re: strategy for parsing text file
Message-Id: <h79n11$q2f$1@news.eternal-september.org>

RedGrittyBrick wrote:
> ccc31807 wrote:
>> I've solved this problem, but I'm just curious as to how by betters
>> would approach this.
>>
>> The file is a long file, so I have copied only the first seven records
>> below as an example. The file is from a table with nine fields, all of
>> which are named in the first nine lines. The key is a five digit
>> number beginning with either 91 or 92. For each record, sometimes all
>> fields are populated (like the first, 91709), but normally only the
>> first four are guaranteed to be populated while the remaining five may
>> or may not have values. Each datum occupies a line all to itself, and
>> the file does not contain record separators.
>>
>> The requirement is to capture the first four fields and write to an
>> Excel readable file (CSV format).
>>
>> My solution was pretty dirty and crude, but I'll share it later (and
>> take the hit for stupidity). My question is how others might approach
>> the problem. Below is the first seven records of the file and the
>> column header.
>>
> 
> #!perl
> use strict;
> use warnings;
> 
> my @f;
> while (<DATA>) {
>   chomp;
>   if (/^9[12]\d{3}$/) {
>     print join (',', @f), "\n" if @f;
>     @f=();
>   }
>   push @f, $_;
> }
> 
> __DATA__
> 
> 
> 
I think you are losing the last record.


------------------------------

Date: Fri, 28 Aug 2009 16:36:02 -0700
From: sln@netherlands.com
Subject: Re: strategy for parsing text file
Message-Id: <16qg959p5drhsvbl64atl8psjfu8rspt1h@4ax.com>

On Fri, 28 Aug 2009 14:25:53 -0700 (PDT), ccc31807 <cartercc@gmail.com> wrote:

>I've solved this problem, but I'm just curious as to how by betters
>would approach this.
>
>The file is a long file, so I have copied only the first seven records
>below as an example. The file is from a table with nine fields, all of
>which are named in the first nine lines. The key is a five digit
>number beginning with either 91 or 92. For each record, sometimes all
>fields are populated (like the first, 91709), but normally only the
>first four are guaranteed to be populated while the remaining five may
>or may not have values. Each datum occupies a line all to itself, and
>the file does not contain record separators.
>
>The requirement is to capture the first four fields and write to an
>Excel readable file (CSV format).
>
>My solution was pretty dirty and crude, but I'll share it later (and
>take the hit for stupidity). My question is how others might approach
>the problem. Below is the first seven records of the file and the
>column header.
>
>Thanks, CC.
>

This is knarly too. Its just the inner while if you
can slurp the whole file, but somehow I don't think
you want that.

-sln

Output:

"Number","BandName","Grade","Branch","Instr","PipingInst","PInstDate","DrumInst","DrumInstDate"
"91709","87th Cleveland Pipe Band IV","PB4","Ohio Valley"
"91068","Adirondack Pipes & Drums","PB5","Northeast"
"91212","Alabama Pipes & Drums","PB4","Southern"
"91801","Albany Police P&D","PB5","Northeast"
"92033","American Celtic Pipe Band","PB5","Metro"
"91826","Anderson Pipe Band","PB5","Southwest"
"91802","AOH Pipe & Drum Band","PB5","Northeast"

==========

use strict;
use warnings;

my ($header,$line,$data) = (1);

while ($line=<DATA>)
{
	$line = '' if $line =~ /^\s*$/;
	my $end = eof(DATA);
	$data .= $line if $end;

	if ($end || $line =~ /^9[12]\d{3}/)
	{
		# process header
		if ($header) {
			$header = 0;
			my $cnt = 1;
			$data =~ /((?:^.*\n){9})/mg;
			print "\"$_\"".($cnt++ < 9 ? ',':"\n") for (split /\n/, $1);
		}
		# process record
		else {
			while ($data =~ /(^9[12]\d{3}\n(?:^(?!9[12]\d{3}).*\n){4,8})/mg)
			{
				my $cnt = 1;
				print "\"$_\"".($cnt++ < 4 ? ',':"\n") for (split /\n/, $1)[0..3];
			}
		}
		$data = $line;
		next;
	}
	$data .= $line;
}



------------------------------

Date: Fri, 28 Aug 2009 17:12:52 -0700
From: sln@netherlands.com
Subject: Re: strategy for parsing text file
Message-Id: <t7sg95h8bku1r4f7cpqgunvv6o1olf5g16@4ax.com>


>use strict;
>use warnings;
>
>my ($header,$line,$data) = (1);
>
>while ($line=<DATA>)
>{
>	$line = '' if $line =~ /^\s*$/;
>	my $end = eof(DATA);
>	$data .= $line if $end;
>
>	if ($end || $line =~ /^9[12]\d{3}/)
>	{
		# process header
		if ($header) {
			$header = 0;
			my $cnt = 1;
			if ($data =~ /((?:^.*\n){9})/mg) {
				print "\"$_\"".($cnt++ < 9 ? ',':"\n") for (split /\n/, $1);
			}
		}
		# process record
		else {
			my $cnt = 1;
			if ($data =~ /(^9[12]\d{3}\n(?:^.*\n){4,8})/mg) {
				print "\"$_\"".($cnt++ < 4 ? ',':"\n") for (split /\n/, $1)[0..3];
			}
		}
>		$data = $line;
>		next;
>	}
>	$data .= $line;
>}

Sorry, the short version: process record 'while' before was for if the file is slurped
and used a negative look ahead. Still works for single record but is not needed.

-sln


------------------------------

Date: Fri, 28 Aug 2009 18:04:22 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: strategy for parsing text file
Message-Id: <j8%lm.274359$E61.61685@newsfe09.iad>

ccc31807 wrote:
> I've solved this problem, but I'm just curious as to how by betters
> would approach this.
> 
> The file is a long file, so I have copied only the first seven records
> below as an example. The file is from a table with nine fields, all of
> which are named in the first nine lines. The key is a five digit
> number beginning with either 91 or 92. For each record, sometimes all
> fields are populated (like the first, 91709), but normally only the
> first four are guaranteed to be populated while the remaining five may
> or may not have values. Each datum occupies a line all to itself, and
> the file does not contain record separators.
> 
> The requirement is to capture the first four fields and write to an
> Excel readable file (CSV format).
> 
> My solution was pretty dirty and crude, but I'll share it later (and
> take the hit for stupidity). My question is how others might approach
> the problem. Below is the first seven records of the file and the
> column header.
> 
> Thanks, CC.
> 
> -------------file below--------------------
> Number
> BandName
> Grade
> Branch
> Instr
> PipingInst
> PInstDate
> DrumInst
> DrumInstDate
> 91709
> 87th Cleveland Pipe Band IV
> PB4
> Ohio Valley
> y
> Tyler Tagliafero, Great Lakes
> 01-Mar-09
> Drew Donnelly, Great Lakes
> 01-Mar-09
> 91068
> Adirondack Pipes & Drums
> PB5
> Northeast
> n
> 91212
> Alabama Pipes & Drums
> PB4
> Southern
> n
> 91801
> Albany Police P&D
> PB5
> Northeast
> y
> Dan Cole, Oran Mor
> 01-Mar-09
> 92033
> American Celtic Pipe Band
> PB5
> Metro
> n
> 91826
> Anderson Pipe Band
> PB5
> Southwest
> y
> Victor Anderson, Westminster
> 01-Mar-09
> Tim Vermillion, Westminster
> 01-Mar-09
> 91802
> AOH Pipe & Drum Band
> PB5
> Northeast
> n


my @data = [];
while ( <FILE> ) {
     chomp;
     /^9[12]/ && push @data, [];
     push @{ $data[ -1 ] }, qq/"$_"/;
     if ( @data == 2 || eof ) {
         no warnings 'uninitialized';
         print join( ',', @{ shift @data }[ 0 .. 8 ] ), "\n";
         }
     }




John
-- 
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov


------------------------------

Date: Fri, 28 Aug 2009 13:37:30 -0400
From: Steve C <smallpond@juno.com>
Subject: Re: Test::Simple etc ... do you leave them in the finished program?
Message-Id: <h794l8$424$1@news.eternal-september.org>

Henry Law wrote:
> J. Gleixner wrote:
>> Henry Law wrote:
>>> The thing I can't find is the strategy for using them.
> 
>> The tests would 'use YourModule', instantiate things, call methods, make 
> 
> Thank you (and also thanks to Steve C); I understand now.  I had the 
> wrong idea altogether, and was thinking of "ok" as a kind of "assert" 
> statement which was included inline in the code.
> 
> One last point of clarification.  Am I right then in thinking that I can 
> use Test::whatever for _modules_ that I write, but not for the mainline 
> code which uses them?  If so that looks like something of a limitation.
> 

You can use Test for many things, they don't even have to be perl.  This
is from an installer test script:

$rc = `/bin/uname -s`;
chomp $rc;
is ($rc, 'Linux', 'os');


------------------------------

Date: Fri, 28 Aug 2009 10:00:57 -0700
From: David Harmon <source@netcom.com>
Subject: Re: windows one liner to output unix line feed
Message-Id: <_4ydndfbEpQdkAXXnZ2dnUVZ_gSdnZ2d@earthlink.com>

On Thu, 27 Aug 2009 11:58:06 -0700 (PDT) in comp.lang.perl.misc, boman
<shambo_p@yahoo.com> wrote,
>Chris's suggestion worked:
>
>perl -pi.orig -e "binmode(ARGVOUT); s|foo|bar|g" myfile.txt

I knew there had to be a binmode in it, but I might never have found
ARGVOUT.  It's not in a lot of the usual lists of prefab file handles.


------------------------------

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 V11 Issue 2573
***************************************


home help back first fref pref prev next nref lref last post