[31329] in Perl-Users-Digest
Perl-Users Digest, Issue: 2574 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Aug 29 11:09:41 2009
Date: Sat, 29 Aug 2009 08:09:05 -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 Sat, 29 Aug 2009 Volume: 11 Number: 2574
Today's topics:
Re: Perl on Windows - use Perl as DLL <claus.kick@googlemail.com>
Re: Perl on Windows - use Perl as DLL <claus.kick@googlemail.com>
Re: Perl on Windows - use Perl as DLL <thepoet_nospam@arcor.de>
Re: Perl on Windows - use Perl as DLL <claus.kick@googlemail.com>
Re: perl script to generate server round-robin assignme <someone@example.com>
Re: perl script to generate server round-robin assignme <tadmc@seesig.invalid>
Re: perl script to generate server round-robin assignme <inetquestion@hotmail.com>
Re: strategy for parsing text file <cartercc@gmail.com>
Re: strategy for parsing text file <someone@example.com>
Re: strategy for parsing text file <tadmc@seesig.invalid>
Re: strategy for parsing text file <tadmc@seesig.invalid>
Re: strategy for parsing text file <dn.perl@gmail.com>
Re: strategy for parsing text file <cartercc@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 29 Aug 2009 04:13:50 -0700 (PDT)
From: "claus.kick@googlemail.com" <claus.kick@googlemail.com>
Subject: Re: Perl on Windows - use Perl as DLL
Message-Id: <5d2c6079-8405-4a19-93f0-2d93c2672b54@r36g2000vbn.googlegroups.com>
On 28 Aug., 16:46, Daniel Molina Wegener <d...@coder.cl> wrote:
> Look at the perlembed manual page.
>
> http://www.perl.com/doc/manual/html/pod/perlembed.html
Hello,
ok, thanks for that, but I think that is not quite what I am looking
for.
This:
http://docs.activestate.com/activeperl/5.8/Components/Windows/PerlEz.html
is I think what I need.
I had been wondering whether there were better ways than doing (sorry
for the Java)
Runtime.getRuntime().exec(new String[] {"perl.exe", "script.pl",
"arg1", "arg2"});
------------------------------
Date: Sat, 29 Aug 2009 05:36:28 -0700 (PDT)
From: "claus.kick@googlemail.com" <claus.kick@googlemail.com>
Subject: Re: Perl on Windows - use Perl as DLL
Message-Id: <f7f539ca-31e0-4b99-826b-1b447ac668cb@33g2000vbe.googlegroups.com>
On 29 Aug., 13:13, "claus.k...@googlemail.com"
<claus.k...@googlemail.com> wrote:
> On 28 Aug., 16:46, Daniel Molina Wegener <d...@coder.cl> wrote:
>
> > Look at the perlembed manual page.
>
> > http://www.perl.com/doc/manual/html/pod/perlembed.html
>
> Hello,
>
> ok, thanks for that, but I think that is not quite what I am looking
> for.
>
> This:
>
> http://docs.activestate.com/activeperl/5.8/Components/Windows/PerlEz....
>
> is I think what I need.
>
> I had been wondering whether there were better ways than doing (sorry
> for the Java)
>
> Runtime.getRuntime().exec(new String[] {"perl.exe", "script.pl",
> "arg1", "arg2"});
Ok progress:
I can load the library, create an interpreter, but calling PerlEzCall1
(...) fails with: 5 -> Function call caused an exception
I figure my error is not knowing what to pass as pFunction - pointer
name of the function to call.
What do I have to pass as function parameter?
------------------------------
Date: Sat, 29 Aug 2009 16:12:25 +0200
From: Christian Winter <thepoet_nospam@arcor.de>
Subject: Re: Perl on Windows - use Perl as DLL
Message-Id: <4a99371f$0$32672$9b4e6d93@newsspool2.arcor-online.net>
claus.kick@googlemail.com schrieb:
>> http://docs.activestate.com/activeperl/5.8/Components/Windows/PerlEz....
>>
>> is I think what I need.
>>
>> I had been wondering whether there were better ways than doing (sorry
>> for the Java)
>>
>> Runtime.getRuntime().exec(new String[] {"perl.exe", "script.pl",
>> "arg1", "arg2"});
>
> Ok progress:
>
> I can load the library, create an interpreter, but calling PerlEzCall1
> (...) fails with: 5 -> Function call caused an exception
> I figure my error is not knowing what to pass as pFunction - pointer
> name of the function to call.
>
> What do I have to pass as function parameter?
The name of the function. If you're just trying to run a script,
you don't have to invoke PerlEzCall at all, the script will be
executed when you call PerlEzCreate. What you won't have is
a capturing facility for STDOUT and STDERR, so in case you need
that, invoking a command line perl or linking the interpreter
directly as in perlembed might be more convenient in the long run.
PerlEzCall is used to invoke a specific function, see
http://www.perlmonks.org/?node_id=571371
for an example.
-Chris
------------------------------
Date: Sat, 29 Aug 2009 08:02:33 -0700 (PDT)
From: "claus.kick@googlemail.com" <claus.kick@googlemail.com>
Subject: Re: Perl on Windows - use Perl as DLL
Message-Id: <3dd3dfa9-5d2a-4f74-9a1a-80d1ed19b398@p23g2000vbl.googlegroups.com>
On 29 Aug., 16:12, Christian Winter <thepoet_nos...@arcor.de> wrote:
> claus.k...@googlemail.com schrieb:
>
>
>
> >>http://docs.activestate.com/activeperl/5.8/Components/Windows/PerlEz....
>
> >> is I think what I need.
>
> >> I had been wondering whether there were better ways than doing (sorry
> >> for the Java)
>
> >> Runtime.getRuntime().exec(new String[] {"perl.exe", "script.pl",
> >> "arg1", "arg2"});
>
> > Ok progress:
>
> > I can load the library, create an interpreter, but calling PerlEzCall1
> > (...) fails with: 5 -> Function call caused an exception
> > I figure my error is not knowing what to pass as pFunction - pointer
> > name of the function to call.
>
> > What do I have to pass as function parameter?
>
> The name of the function. If you're just trying to run a script,
> you don't have to invoke PerlEzCall at all, the script will be
> executed when you call PerlEzCreate. What you won't have is
> a capturing facility for STDOUT and STDERR, so in case you need
> that, invoking a command line perl or linking the interpreter
> directly as in perlembed might be more convenient in the long run.
> PerlEzCall is used to invoke a specific function, seehttp://www.perlmonks.org/?node_id=571371
> for an example.
I am sorry to ask in such a stupid way, but do you mean "sub routine"
with "function"?
Wouldnt it be possible to basically create a main() sub routine in a
script like this:
#!c:\perl\bin
my $name = $ARGV[0];
main();
sub main
{
print "hello world: ".$name."!\n";
}
and hence be able to have STDOUT and STDERR (thats probably what
lpBuffer is for)?
------------------------------
Date: Fri, 28 Aug 2009 19:14:10 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: perl script to generate server round-robin assignments
Message-Id: <L90mm.187578$FP2.12631@newsfe05.iad>
inetquestion wrote:
> #!/usr/bin/perl
use warnings;
use strict;
> ##################
> ### Main Begin ###
> ##################
>
> if ($#ARGV < 0) {
> &DoUsage;
if ( @ARGV < 1 ) {
DoUsage();
Or probably better as:
if ( @ARGV != 2 ) {
DoUsage();
> exit;
> }
>
> my ($hostname, $limit) = @ARGV; # Script inputs
>
> if ($hostname =~ /^([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)$/) { # Filter
Why use capturing parentheses? Why put the \d character class inside a
character class?
if ( $hostname =~ /^\d+\.\d+\.\d+\.\d+$/ ) {
> 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
Why does DoAsc2bin() add the whitespace if you are just going to remove it?
> from base2 string
> $decString = DoBin2dec($binaryNumber); # Convert base2
> string to base10
> $assignment = DoAssign(scalar(@B), $decString, $hostname, $limit); #
You should probably pass a reference to @B instead of using it globally.
> 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
^^^^
Why are you adding these spaces if you don't really want them?
> 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
> }
There is no need to reverse the contents of @B to get the same results:
sub DoAssign {
# Here $ListB contains a reference to @B
my ( $ListB, $num, $host, $lim ) = @_;
$lim ||= 100;
push @$ListB, splice @$ListB, 0, $num % @$ListB;
for my $i ( 0 .. $#$ListB ) {
if ( $ListB->[ $i ] eq $host ) {
#print "Match on $host\n"; # Debug statement
# Modify array order by moving the "match" to front
unshift @$ListB, splice @$ListB, $i, 1;
last;
}
}
return "@{ $ListB }[ 0 .. $lim - 1 ]";
}
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 21:57:07 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: perl script to generate server round-robin assignments
Message-Id: <slrnh9h5kt.i72.tadmc@tadmc30.sbcglobal.net>
inetquestion <inetquestion@hotmail.com> wrote:
[snip 100 lines of code with no question]
Mr. inetquestion,
Did you mean to ask a question?
What was the point of your post?
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Sat, 29 Aug 2009 06:42:58 -0700 (PDT)
From: inetquestion <inetquestion@hotmail.com>
Subject: Re: perl script to generate server round-robin assignments
Message-Id: <c877348e-92a2-4d58-9bdd-b7eb4757939e@q14g2000vbi.googlegroups.com>
On Aug 28, 10:14=A0pm, "John W. Krahn" <some...@example.com> wrote:
> inetquestionwrote:
> > #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> > ##################
> > ### Main Begin ###
> > ##################
>
> > if ($#ARGV < 0) {
> > =A0 =A0 &DoUsage;
>
> if ( @ARGV < 1 ) {
> =A0 =A0 =A0DoUsage();
>
> Or probably better as:
>
> if ( @ARGV !=3D 2 ) {
> =A0 =A0 =A0DoUsage();
>
> > =A0 =A0 exit;
> > }
>
> > my ($hostname, $limit) =3D @ARGV; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Script inputs
>
> > if ($hostname =3D~ /^([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)$/) { =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 # Filter
>
> Why use capturing parentheses? =A0Why put the \d character class inside a
> character class?
>
> if ( $hostname =3D~ /^\d+\.\d+\.\d+\.\d+$/ ) {
>
> > on IP address
> > =A0 =A0 =A0 $hostString=3D$hostname; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # Save IP
> > } else {
> > =A0 =A0 =A0($hostString) =3D split /\./,$hostname; =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 # Obtain first portion
> > of FQDN component
> > }
>
> > @B =3D qw(svr01 svr02 svr03 svr04 svr05 svr06); =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0# Array containing
> > assignable server
> > $binaryNumber =3D DoAsc2bin($hostString); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0# Convert string
> > into base2
> > $binaryNumber =3D~ s/\s+//g; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # Remove spaces
>
> Why does DoAsc2bin() add the whitespace if you are just going to remove i=
t?
>
> > from base2 string
> > $decString =3D DoBin2dec($binaryNumber); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 # Convert base2
> > string to base10
> > $assignment =3D DoAssign(scalar(@B), $decString, $hostname, $limit); #
>
> You should probably pass a reference to @B instead of using it globally.
>
>
>
> > 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. =A0The input to
> > the script is a single server hostname or IP from list-A. =A0This serve=
r
> > name will go through a conversion (ascII->Binary->Dec), then the order
> > of the assignments will be made. =A0As 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 { =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Convert ASCII string to b=
inary equivalent
> > =A0 =A0 my ($string) =3D @_; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # Input
> > =A0 =A0 my @bytes; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # Decla=
re byte array
> > =A0 =A0 for (split //, $string) { =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Run throuh for loo=
p per character
> > of string being split
> > =A0 =A0 =A0 push @bytes, unpack "B8", $_; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Store binary equivalent of
> > each character into @bytes
> > =A0 =A0 }
> > =A0 =A0 return wantarray ? @bytes : join " ", @bytes; =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0# Return @bytes or
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 ^^^^
> Why are you adding these spaces if you don't really want them?
>
>
>
> > a string of all content of @bytes
> > }
>
> > sub DoBin2dec {
> > =A0 =A0 return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));=
=A0#
> > Converts binary string to decimal equivalent
> > }
>
> > sub DoAssign {
> > =A0 =A0 my ($numListB, $num, $host, $lim) =3D @_; =A0 =A0 =A0 =A0# Func=
tion inputs
> > =A0 =A0 $lim ||=3D 100; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Set lim=3D100 unless =
otherwise specified
> > =A0 =A0 @B =3D reverse(@B); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Reverse array, then pop last
> > =A0 =A0 my $popNum =3D $num % $numListB; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 # Get modulus, then pop that
> > many elements from reversed array
> > =A0 =A0 while ( $popNum !=3D 0 ) { =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #
> > Iterate until all values have been popped off array
> > =A0 =A0 =A0 =A0 push(@popSave, pop(@B)); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 # Save popped elements
> > =A0 =A0 =A0 =A0 $popNum--; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # Decrement pop counter
> > =A0 =A0 }
> > =A0 =A0 @B =3D reverse(@B); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Reverse array
> > =A0 =A0 push(@B, @popSave); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0 =A0# Appennd popped elements to
> > reversed (original direction) array
>
> > =A0 =A0 for( $i =3D 0; $i < scalar(@B); $i++) { =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 # Loop
> > through all array elements, maintain counter
> > =A0 =A0if( $B[$i] eq $host ) { =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 # Look for match
> > =A0 =A0 =A0 =A0 =A0 =A0 #print "Match on $host\n"; =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 # Debug statement
> > =A0 =A0 =A0 =A0 =A0 =A0 @B =3D ($B[$i], @B[0..($i-1)], @B[($i+1)..scala=
r
> > (@B)]); =A0 =A0 =A0 # Modify array order by moving the "match" to front
>
> > last; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # Move on
> > =A0 =A0 =A0 =A0 }
> > =A0 =A0 }
> > =A0 =A0 return join " ", @B[0..($lim-1)]; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0# Return space
> > deleimited string
> > }
>
> There is no need to reverse the contents of @B to get the same results:
>
> sub DoAssign {
> =A0 =A0 =A0# Here $ListB contains a reference to @B
> =A0 =A0 =A0my ( $ListB, $num, $host, $lim ) =3D @_;
>
> =A0 =A0 =A0$lim ||=3D 100;
>
> =A0 =A0 =A0push @$ListB, splice @$ListB, 0, $num % @$ListB;
>
> =A0 =A0 =A0for my $i ( 0 .. $#$ListB ) {
> =A0 =A0 =A0 =A0 =A0if ( $ListB->[ $i ] eq $host ) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0#print "Match on $host\n"; =A0 =A0 =A0 =A0 =A0=
# Debug statement
> =A0 =A0 =A0 =A0 =A0 =A0 =A0# Modify array order by moving the "match" to =
front
> =A0 =A0 =A0 =A0 =A0 =A0 =A0unshift @$ListB, splice @$ListB, $i, 1;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0last;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0return "@{ $ListB }[ 0 .. $lim - 1 ]";
> =A0 =A0 =A0}
>
> John
> --
> Those people who think they know everything are a great
> annoyance to those of us who do. =A0 =A0 =A0 =A0-- Isaac Asimov
Thanks for the suggestion; that clears up the questions I had. As i
was writing that I thought there must be an easier way to do
that... :)
------------------------------
Date: Fri, 28 Aug 2009 18:45:37 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: strategy for parsing text file
Message-Id: <b52ac853-85d8-426a-a714-f65f5473a295@a13g2000yqc.googlegroups.com>
On Aug 28, 9:04=A0pm, "John W. Krahn" <some...@example.com> wrote:
John, sorry, but I haven't seen some of what you used. Do you mine
helping me out?
[] returns a reference to an anonymous array, right? How does it work
assigning it to an array type?
> my @data =3D [];
> while ( <FILE> ) {
> =A0 =A0 =A0chomp;
I understand the use of the conjunctive Boolean, but again, I don't
understand how pushing [] to the array works.
> =A0 =A0 =A0/^9[12]/ && push @data, [];
This pushes $_ to the end of the array, but how to you designate the
value of $_ in this case?
> =A0 =A0 =A0push @{ $data[ -1 ] }, qq/"$_"/;
> =A0 =A0 =A0if ( @data =3D=3D 2 || eof ) {
> =A0 =A0 =A0 =A0 =A0no warnings 'uninitialized';
Why '8'? The problem is that the values can be anywhere from three to
eight, and you don't know how many or which ones.
> =A0 =A0 =A0 =A0 =A0print join( ',', @{ shift @data }[ 0 .. 8 ] ), "\n";
> =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0}
When I looked at the data file, I saw this pseudocode:
read each line
if the line is the key:
save the value as a key
read the next three lines
write each value as the value of a hash element for the key
Two points -- (1) I didn't take the time to explore accessing the
lines of the file in an inner loop, although that occurred to me,
which is why Tad's example made the light bulb light up. (2) It seems
much more natural to use a hash rather than an array to hold the data
elements, and now I'm wondering if using an array to hold the records
is a better solution.
The output part of my script looks like this:
foreach my $k (keys %bands)
{
print OUTFILE qq("$k","$bands{$k}{name}","$bands{$k}{grade}","$bands
{$k}{branch}"\n);
}
To me, this looks a lot more intuitive and understandable than some of
the print statements above, which look convoluted (if not obfuscated)
to me.
CC.
------------------------------
Date: Fri, 28 Aug 2009 19:30:41 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: strategy for parsing text file
Message-Id: <fp0mm.187582$FP2.98504@newsfe05.iad>
ccc31807 wrote:
> On Aug 28, 9:04 pm, "John W. Krahn" <some...@example.com> wrote:
>
> John, sorry, but I haven't seen some of what you used. Do you mine
> helping me out?
Ok, I'll try. :-)
> [] returns a reference to an anonymous array, right? How does it work
> assigning it to an array type?
Just the same as assigning any scalar to an array. The first element of
the array now contains a reference to an array.
>> my @data = [];
>> while ( <FILE> ) {
>> chomp;
>
> I understand the use of the conjunctive Boolean, but again, I don't
> understand how pushing [] to the array works.
>> /^9[12]/ && push @data, [];
That adds a scalar value onto the end of the array. In this case the
scalar value is a reference to an array.
> This pushes $_ to the end of the array, but how to you designate the
> value of $_ in this case?
I don't know what you mean by "designate the value of $_"?
>> push @{ $data[ -1 ] }, qq/"$_"/;
>> if ( @data == 2 || eof ) {
>> no warnings 'uninitialized';
>
> Why '8'? The problem is that the values can be anywhere from three to
> eight, and you don't know how many or which ones.
>> print join( ',', @{ shift @data }[ 0 .. 8 ] ), "\n";
I assumed that you meant that each record *should* have 9 fields, but if
that is not what you want then just remove the '[ 0 .. 8 ]' part.
>> }
>> }
>
> When I looked at the data file, I saw this pseudocode:
> read each line
> if the line is the key:
> save the value as a key
> read the next three lines
> write each value as the value of a hash element for the key
>
> Two points -- (1) I didn't take the time to explore accessing the
> lines of the file in an inner loop, although that occurred to me,
> which is why Tad's example made the light bulb light up. (2) It seems
> much more natural to use a hash rather than an array to hold the data
> elements, and now I'm wondering if using an array to hold the records
> is a better solution.
TMTOWTDI ;-)
> The output part of my script looks like this:
> foreach my $k (keys %bands)
> {
> print OUTFILE qq("$k","$bands{$k}{name}","$bands{$k}{grade}","$bands
> {$k}{branch}"\n);
>
> }
>
> To me, this looks a lot more intuitive and understandable than some of
> the print statements above, which look convoluted (if not obfuscated)
> to me.
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 21:47:41 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: strategy for parsing text file
Message-Id: <slrnh9h537.i72.tadmc@tadmc30.sbcglobal.net>
ccc31807 <cartercc@gmail.com> wrote:
> open INFILE, '<', 'bands.txt';
You should always, yes *always*, check the return value from open():
open INFILE, '<', 'bands.txt' or die "could not open 'bands.txt' $!";
> if (/9[12]\d{3}/)
matches if $_ = 'twin towers 9112001'; ...
> $flag = 1;
That is a horrid choice of variable name.
Leave me wondering *what* is being flagged...
$field_number or $field_position or *something* that is meaningful
would be much better.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Fri, 28 Aug 2009 21:53:07 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: strategy for parsing text file
Message-Id: <slrnh9h5dd.i72.tadmc@tadmc30.sbcglobal.net>
ccc31807 <cartercc@gmail.com> wrote:
> $bands{$key}{name} = $_;
If you like a hash rather than an array:
----------------------
while ( <DATA> ) {
next unless /^9[12]\d\d\d$/; # 5 digits, starts with 91 or 92
my %record = (number => $_);
$record{bandname} = <DATA>;
$record{grade} = <DATA>;
$record{branch} = <DATA>;
chomp %record;
print Dumper \%record;
}
----------------------
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Sat, 29 Aug 2009 01:08:17 -0700 (PDT)
From: "dn.perl@gmail.com" <dn.perl@gmail.com>
Subject: Re: strategy for parsing text file
Message-Id: <bcb652f3-ddff-4fda-8c28-a4f2966d8f57@k13g2000prh.googlegroups.com>
On Aug 28, 3:50=A0pm, Steve C <smallp...@juno.com> wrote:
> RedGrittyBrick wrote:
>
> > #!perl
> > use strict;
> > use warnings;
>
> > my @f;
> > while (<DATA>) {
> > =A0 chomp;
> > =A0 if (/^9[12]\d{3}$/) {
> > =A0 =A0 print join (',', @f), "\n" if @f;
> > =A0 =A0 @f=3D();
> > =A0 }
> > =A0 push @f, $_;
> > }
>
> > __DATA__
>
> I think you are losing the last record.
>
That script has one more flaw. It publishes all the elements of @f,
whereas the OP wants only the first 4 elements.
------------------------------
Date: Sat, 29 Aug 2009 03:33:26 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: strategy for parsing text file
Message-Id: <b17789b9-c0e5-4337-aa0f-9dad3e2b3d76@d21g2000vbm.googlegroups.com>
On Aug 28, 10:53=A0pm, Tad J McClellan <ta...@seesig.invalid> wrote:
> while ( <DATA> ) {
> =A0 =A0 next unless /^9[12]\d\d\d$/; =A0# 5 digits, starts with 91 or 92
> =A0 =A0 my %record =3D (number =3D> $_);
> =A0 =A0 $record{bandname} =3D <DATA>;
> =A0 =A0 $record{grade} =3D <DATA>;
> =A0 =A0 $record{branch} =3D <DATA>;
> =A0 =A0 chomp %record;
> =A0 =A0 print Dumper \%record;}
Yes. This is almost identical to what I had after I saw your first
solution, except for a small variation in the hash variable. I chose a
hash because I anticipated a need to sort by branch and possible by
grade.
This was a throwaway script, that I ran exactly once, so while I agree
with checking the value of open() and using more meaningful names,
this was just the first cut and was all I needed.
Thanks for your help. I now know about using <> in inner loops.
CC.
------------------------------
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 2574
***************************************