[24574] in Perl-Users-Digest
Perl-Users Digest, Issue: 6750 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jun 30 06:10:35 2004
Date: Wed, 30 Jun 2004 03:10:07 -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, 30 Jun 2004 Volume: 10 Number: 6750
Today's topics:
Re: repeated calculations everyday (Shalini Joshi)
Re: repeated calculations everyday (Shalini Joshi)
Re: repeated calculations everyday <josef.moellers@fujitsu-siemens.com>
Search more than one wor in a string <jochen.friedmann3@de.bosch.com>
Re: Search more than one wor in a string <bernard.el-haginDODGE_THIS@lido-tech.net>
Re: Why can't I get WWW::Mechanize->find_all_links to w <atp5470 at fsu.edu>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 29 Jun 2004 22:15:00 -0700
From: shalinij1@yahoo.com (Shalini Joshi)
Subject: Re: repeated calculations everyday
Message-Id: <283d6b7e.0406292115.1f25e0ae@posting.google.com>
Hey Ben!
Thanks again for the help. What I am getting totally confused here is
to what data structures to use given the problem. I need to get at the
information across days so I store a reference to the array
BASE[$mday-1]{$i} for $i (@cusips) . NOw i would like to get at all
the elements of the array for all $i for a particular value of $mday.
I can't seem to do that.
Also could you advise me on the choice of a data structure. Right now
I read in all the records,and store them as array of hashes and
compare these.
Do let me know if it makes any sense or should I re-state my problem a
bit more clearly..
Thanks again ...
Regards,
Shalini
Ben Morrow <usenet@morrow.me.uk> wrote in message news:<vev2r1-636.ln1@mauzo.dyndns.org>...
> Quoth shalinij1@yahoo.com (Shalini Joshi):
> > So here is what I have of the code uptil now.
> >
> > if ((localtime)[3] ==1)
> > {
> > print "Today is the first day of the month!\n";
> > print "We are going to read all Account Position Files!\n";
>
> Set $\="\n" rather than putting it on the end of every print.
>
> >
> > #For each client we read the Account Position File: thus all account
> > position files on the server.
> >
> > opendir DIR, '/home/sj2122/perlstuff' or die "$!";
> > my @files = grep {/[^(03)]+03\.[^\.]+\.txt$/} readdir(DIR);
> > closedir(DIR);
>
> Use lexical dirhandles.
> Use scopes to close them.
> Include the filename in the error message.
>
> my @files = grep /.../, do {
> my $dir = '/home/sj2122/perlstuff';
> opendir my $DIR, $dir or die "can't opendir $dir: $!";
> readdir $DIR;
> };
>
> > foreach $i (@files)
>
> Why aren't you using strictures?
>
> for my $i (@files) {
>
> > {
> > # Read every position file
> > print "$i\n";
> > open ACCOUNT_POSITION, "<$i" or die "Can't open $i $!";
>
> Use lexical filehandles.
> Use scopes to close them.
> Use 3-arg open.
>
> open my $ACCOUNT_POSITION, '<', $i or die "can't open $i: $!";
>
> > local $_ = <ACCOUNT_POSITION>;
> > if (/^APR/)
> > {
> > push @position_records,$_;
> > }
>
> Why are you manually going through one iteration of the loop below?
>
> > while (<ACCOUNT_POSITION>)
> > { #get all lines of the record
> > # stop if we encounter an APR line
>
> This doesn't stop: it records the line and carries on. If you want to
> stop you will need a 'last' inside the if.
>
> >
> > if (/^APR/)
> > {
> > push @position_records, $_;
> >
> > }
>
> I would have written this
>
> /^APR/ and push @position_records, $_;
>
> >
> > }
> > close ACCOUNT_POSITION;
>
> There is no need for an explicit close with lexical FHs.
>
> > }
> >
> > foreach $i (@position_records)
>
> ^^ my
>
> > {
> >
> > %parameters_owner= (
>
> ^^ my
>
> > dealer_number => substr($i,6,7),
> > CUSIP => substr($i,22,9),
> > cust_accnt_number => substr($i,38,20),
> > total_shares => substr($i,59,15),
> > );
> >
> > if ($parameters_owner{dealer_number}==7654321)
>
> I presume there is a good reason for this value being hard-coded? If
> nothing else I would put
>
> use constant SELECTED_DEALER => 7654321;
>
> at the top and use that instead.
>
> > {
> >
> > print "Yahoo! We need to add this CUSIP number to selected records
> > in Price File!\n";
> >
> >
> > push @arrayofparams, {%parameters_owner};
>
> If you'd properly created the %parameters_owner as a lexical the you
> could simply push a reference to it
>
> push @arrayofparams, \%parameters_owner;
>
> and save a copy.
>
> > push @cusip, $parameters_owner{CUSIP};
> >
> > push @customers, $parameters_owner{cust_accnt_number};
> >
> > }
>
> > }
> > my %hash = map {$_ => 1} @cusip;
> > @cusip_new = keys %hash; # to get unique CUSIP numbers
> >
> > my %hash2 = map {$_ => 1} @customers;
> > @customers_new = keys %hash2;
>
> I would put each if these in a block: there is no need for the temporary
> hashes to exist outside of those two statements:
>
> my @cusip_new = do {
> my %tmp = map { $_ => 1 } @cusip;
> keys %tmp;
> };
>
> > foreach $i (@cusip_new)
> > {
> >
> > my $sharetotal=0;
> > for $y (0 .. $#arrayofparams)
>
> for my $y (@arrayofparams) {
>
> > {
> > for $valuesonly (values %{ $arrayofparams[$y]} )
>
> for my $valuesonly (values %$y) {
>
> Are you sure this is correct? Do you not simply want to test if
> $y->{CUSIP} eq $i? (I am asking 'is there an occasion where
> dealer_number, cust_acct_number or total_shares will match a cusip, and
> do you want to catch those matches or not?')
>
> > {
> > if ($valuesonly eq $i)
> > {
> > print "$valuesonly\n";
> > print "$arrayofparams[$y]{total_shares}\n";
>
> ...$y->{total_shares}...
>
> > $sharetotal += $arrayofparams[$y]{total_shares};
> > }
> > }
> > }
> > print "Total shares for $i is $sharetotal\n";
> > $BASE[0]{$i}= $sharetotal; # For day 1, total for the various
> > CUSIPs
>
> Why not just build your total in $BASE[0]{$i} in the first place?
>
> > print "$BASE[0]{$i}\n";
> >
> > }
> > store(\@BASE, 'base_values'); # For later access.
> >
> > open RECORDS, "< AETNA1.txt " or die "Can't read file: $!";
> >
> > local $_ = <RECORDS>;
> >
> > while (<RECORDS>)
> > { #get all lines of the record
> >
> > # stop if we encounter an FPR line
> >
> > if (/^FPR/)
> > {
> > push @records, $_;
> > }
> >
> > }
> >
> > close RECORDS;
> >
> >
> >
> > my $date= substr(@records[0],22,8);
>
> ^ $
> Why don't you have warnings turned on?
>
> > my $cpday= &UnixDate($date,"%d");
>
> Don't call subs with & unless you know what it does and why you need it.
>
> > my $cpmonth=&UnixDate($date,"%m");
> > my $cpyear=&UnixDate($date,"%y");
> > my $cpdate=$cpmonth.$cpday.$cpyear;
> >
> > my $cpfilename= "AD".$cpyear.$cpmonth.$cpday."\.PRI";
>
> my $cpfilename = UnixDate $date, 'AD%y%m%d.PRI';
>
> > my $wanted_handle = IO::File->new($cpfilename, O_WRONLY|O_CREAT)
> > or die "Couldn't open $cpfilename for writing:$!\n";
>
> Use lexical FHs instead of IO::File; or certainly, be consistent.
> It is usual to use caps for filehandles.
> Don't put "\n" on the end of die messages.
>
> > foreach $i (@records)
> > {
> >
> > %parameters= (
> >
> > CUSIP_P => substr($i,6,9),
> > fund_name => substr($i,59,38),
> > processing_date => $cpdate,
>
> processing_data => UnixData $data, '%m%d%y';
>
> Except don't use m-d-y date formats, they're just too brain-damaged for
> words...
>
> > NAV_P => substr($i,30,9),
> > Fund_Type_P => "MU",
> > Public_Offering_Price => "000".substr($i,39,9),
> > );
> >
> >
> > foreach $selectedcusip (@cusip_new)
> > {
> > if ($selectedcusip eq $parameters{CUSIP_P})
> > {
> > push @arraypriceparams, {%parameters};
> >
> > }
>
> > }
> > }
> >
> >
> > for $x (0 .. $#arraypriceparams)
> >
> > {
> > print $wanted_handle
> > "$arraypriceparams[$x]{CUSIP_P},$arraypriceparams[$x]{Fund_Type_P},$arraypriceparams[$x]{processing_date},$arraypriceparams[$x]{Public_Offering_Price}\n";
> > }
> >
> >
> > }
> >
> > #If not the first day of the month, read DIRECT FINANCIAL ACTIVITY
> > FILE
> >
> > else
> > {
> > my $mday = (localtime)[3];
> > print "Today we are going to read the Direct Financial Acitivity
> > File and see the status of our CUSIPs!\n";
> >
> > # Retrieving all base values
> >
> > my $base_ref = retrieve('base_values');
> >
> > #my @BASE = @{$base_ref};
> > #foreach $i (@{$base_ref})
> > #{
> > # print "$i{AET001A70}";
> > #}
> > print "$$base_ref[0]{AET001A20}\n";
> >
> > The thing that has me stumped is how do access the BASE array that I
> > stored using the storable module. When i try and print out the
> > particular element, it works but doesnt work ..gives me a HASH{xxxx}
> > error...
>
> Please explain...your $base_ref is here a ref to an array of hashes, as
> you seem to have figured out; I would have written the deref above
>
> $base_ref->[0]{AET001A20}
>
> . If you want to loop, you will need @$base_ref:
>
> for my $i (@$base_ref) {
> print $i->{AET001A70};
> }
>
> > also as I mention right at the end I store it as BASE[0][$i]
> > where $i is an element of an array of unique 9 digit numbers. (not
> > necessarily sorted etc)...Is there any way I can get it to print based
> > on the length of the hard-linked array..as opposed to the index or
> > something???
>
> Please explain more clearly what you want... what do you mean by 'print
> based on'? Do you mean 'print a list sorted by'? And there is no such
> thing as a 'hard-linked array' in Perl: the only link between
> $BASE[0]{$i} and @cusp_ip is the value they hold in common.
>
> Ben
------------------------------
Date: 29 Jun 2004 23:03:31 -0700
From: shalinij1@yahoo.com (Shalini Joshi)
Subject: Re: repeated calculations everyday
Message-Id: <283d6b7e.0406292114.5d91c63f@posting.google.com>
Hey Ben!
Thanks again for the help. What I am getting totally confused here is
to what data structures to use given the problem. I need to get at the
information across days so I store a reference to the array
BASE[$mday-1]{$i} for $i (@cusips) . NOw i would like to get at all
the elements of the array for all $i for a particular value of $mday.
I can't seem to do that.
Also could you advise me on the choice of a data structure. Right now
I read in all the records,and store them as array of hashes and
compare these.
Do let me know if it makes any sense or should I re-state my problem a
bit more clearly..
Thanks again ...
Regards,
Shalini
Ben Morrow <usenet@morrow.me.uk> wrote in message news:<vev2r1-636.ln1@mauzo.dyndns.org>...
> Quoth shalinij1@yahoo.com (Shalini Joshi):
> > So here is what I have of the code uptil now.
> >
> > if ((localtime)[3] ==1)
> > {
> > print "Today is the first day of the month!\n";
> > print "We are going to read all Account Position Files!\n";
>
> Set $\="\n" rather than putting it on the end of every print.
>
> >
> > #For each client we read the Account Position File: thus all account
> > position files on the server.
> >
> > opendir DIR, '/home/sj2122/perlstuff' or die "$!";
> > my @files = grep {/[^(03)]+03\.[^\.]+\.txt$/} readdir(DIR);
> > closedir(DIR);
>
> Use lexical dirhandles.
> Use scopes to close them.
> Include the filename in the error message.
>
> my @files = grep /.../, do {
> my $dir = '/home/sj2122/perlstuff';
> opendir my $DIR, $dir or die "can't opendir $dir: $!";
> readdir $DIR;
> };
>
> > foreach $i (@files)
>
> Why aren't you using strictures?
>
> for my $i (@files) {
>
> > {
> > # Read every position file
> > print "$i\n";
> > open ACCOUNT_POSITION, "<$i" or die "Can't open $i $!";
>
> Use lexical filehandles.
> Use scopes to close them.
> Use 3-arg open.
>
> open my $ACCOUNT_POSITION, '<', $i or die "can't open $i: $!";
>
> > local $_ = <ACCOUNT_POSITION>;
> > if (/^APR/)
> > {
> > push @position_records,$_;
> > }
>
> Why are you manually going through one iteration of the loop below?
>
> > while (<ACCOUNT_POSITION>)
> > { #get all lines of the record
> > # stop if we encounter an APR line
>
> This doesn't stop: it records the line and carries on. If you want to
> stop you will need a 'last' inside the if.
>
> >
> > if (/^APR/)
> > {
> > push @position_records, $_;
> >
> > }
>
> I would have written this
>
> /^APR/ and push @position_records, $_;
>
> >
> > }
> > close ACCOUNT_POSITION;
>
> There is no need for an explicit close with lexical FHs.
>
> > }
> >
> > foreach $i (@position_records)
>
> ^^ my
>
> > {
> >
> > %parameters_owner= (
>
> ^^ my
>
> > dealer_number => substr($i,6,7),
> > CUSIP => substr($i,22,9),
> > cust_accnt_number => substr($i,38,20),
> > total_shares => substr($i,59,15),
> > );
> >
> > if ($parameters_owner{dealer_number}==7654321)
>
> I presume there is a good reason for this value being hard-coded? If
> nothing else I would put
>
> use constant SELECTED_DEALER => 7654321;
>
> at the top and use that instead.
>
> > {
> >
> > print "Yahoo! We need to add this CUSIP number to selected records
> > in Price File!\n";
> >
> >
> > push @arrayofparams, {%parameters_owner};
>
> If you'd properly created the %parameters_owner as a lexical the you
> could simply push a reference to it
>
> push @arrayofparams, \%parameters_owner;
>
> and save a copy.
>
> > push @cusip, $parameters_owner{CUSIP};
> >
> > push @customers, $parameters_owner{cust_accnt_number};
> >
> > }
>
> > }
> > my %hash = map {$_ => 1} @cusip;
> > @cusip_new = keys %hash; # to get unique CUSIP numbers
> >
> > my %hash2 = map {$_ => 1} @customers;
> > @customers_new = keys %hash2;
>
> I would put each if these in a block: there is no need for the temporary
> hashes to exist outside of those two statements:
>
> my @cusip_new = do {
> my %tmp = map { $_ => 1 } @cusip;
> keys %tmp;
> };
>
> > foreach $i (@cusip_new)
> > {
> >
> > my $sharetotal=0;
> > for $y (0 .. $#arrayofparams)
>
> for my $y (@arrayofparams) {
>
> > {
> > for $valuesonly (values %{ $arrayofparams[$y]} )
>
> for my $valuesonly (values %$y) {
>
> Are you sure this is correct? Do you not simply want to test if
> $y->{CUSIP} eq $i? (I am asking 'is there an occasion where
> dealer_number, cust_acct_number or total_shares will match a cusip, and
> do you want to catch those matches or not?')
>
> > {
> > if ($valuesonly eq $i)
> > {
> > print "$valuesonly\n";
> > print "$arrayofparams[$y]{total_shares}\n";
>
> ...$y->{total_shares}...
>
> > $sharetotal += $arrayofparams[$y]{total_shares};
> > }
> > }
> > }
> > print "Total shares for $i is $sharetotal\n";
> > $BASE[0]{$i}= $sharetotal; # For day 1, total for the various
> > CUSIPs
>
> Why not just build your total in $BASE[0]{$i} in the first place?
>
> > print "$BASE[0]{$i}\n";
> >
> > }
> > store(\@BASE, 'base_values'); # For later access.
> >
> > open RECORDS, "< AETNA1.txt " or die "Can't read file: $!";
> >
> > local $_ = <RECORDS>;
> >
> > while (<RECORDS>)
> > { #get all lines of the record
> >
> > # stop if we encounter an FPR line
> >
> > if (/^FPR/)
> > {
> > push @records, $_;
> > }
> >
> > }
> >
> > close RECORDS;
> >
> >
> >
> > my $date= substr(@records[0],22,8);
>
> ^ $
> Why don't you have warnings turned on?
>
> > my $cpday= &UnixDate($date,"%d");
>
> Don't call subs with & unless you know what it does and why you need it.
>
> > my $cpmonth=&UnixDate($date,"%m");
> > my $cpyear=&UnixDate($date,"%y");
> > my $cpdate=$cpmonth.$cpday.$cpyear;
> >
> > my $cpfilename= "AD".$cpyear.$cpmonth.$cpday."\.PRI";
>
> my $cpfilename = UnixDate $date, 'AD%y%m%d.PRI';
>
> > my $wanted_handle = IO::File->new($cpfilename, O_WRONLY|O_CREAT)
> > or die "Couldn't open $cpfilename for writing:$!\n";
>
> Use lexical FHs instead of IO::File; or certainly, be consistent.
> It is usual to use caps for filehandles.
> Don't put "\n" on the end of die messages.
>
> > foreach $i (@records)
> > {
> >
> > %parameters= (
> >
> > CUSIP_P => substr($i,6,9),
> > fund_name => substr($i,59,38),
> > processing_date => $cpdate,
>
> processing_data => UnixData $data, '%m%d%y';
>
> Except don't use m-d-y date formats, they're just too brain-damaged for
> words...
>
> > NAV_P => substr($i,30,9),
> > Fund_Type_P => "MU",
> > Public_Offering_Price => "000".substr($i,39,9),
> > );
> >
> >
> > foreach $selectedcusip (@cusip_new)
> > {
> > if ($selectedcusip eq $parameters{CUSIP_P})
> > {
> > push @arraypriceparams, {%parameters};
> >
> > }
>
> > }
> > }
> >
> >
> > for $x (0 .. $#arraypriceparams)
> >
> > {
> > print $wanted_handle
> > "$arraypriceparams[$x]{CUSIP_P},$arraypriceparams[$x]{Fund_Type_P},$arraypriceparams[$x]{processing_date},$arraypriceparams[$x]{Public_Offering_Price}\n";
> > }
> >
> >
> > }
> >
> > #If not the first day of the month, read DIRECT FINANCIAL ACTIVITY
> > FILE
> >
> > else
> > {
> > my $mday = (localtime)[3];
> > print "Today we are going to read the Direct Financial Acitivity
> > File and see the status of our CUSIPs!\n";
> >
> > # Retrieving all base values
> >
> > my $base_ref = retrieve('base_values');
> >
> > #my @BASE = @{$base_ref};
> > #foreach $i (@{$base_ref})
> > #{
> > # print "$i{AET001A70}";
> > #}
> > print "$$base_ref[0]{AET001A20}\n";
> >
> > The thing that has me stumped is how do access the BASE array that I
> > stored using the storable module. When i try and print out the
> > particular element, it works but doesnt work ..gives me a HASH{xxxx}
> > error...
>
> Please explain...your $base_ref is here a ref to an array of hashes, as
> you seem to have figured out; I would have written the deref above
>
> $base_ref->[0]{AET001A20}
>
> . If you want to loop, you will need @$base_ref:
>
> for my $i (@$base_ref) {
> print $i->{AET001A70};
> }
>
> > also as I mention right at the end I store it as BASE[0][$i]
> > where $i is an element of an array of unique 9 digit numbers. (not
> > necessarily sorted etc)...Is there any way I can get it to print based
> > on the length of the hard-linked array..as opposed to the index or
> > something???
>
> Please explain more clearly what you want... what do you mean by 'print
> based on'? Do you mean 'print a list sorted by'? And there is no such
> thing as a 'hard-linked array' in Perl: the only link between
> $BASE[0]{$i} and @cusp_ip is the value they hold in common.
>
> Ben
------------------------------
Date: Wed, 30 Jun 2004 08:43:39 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: repeated calculations everyday
Message-Id: <cbtnah$uta$1@nntp.fujitsu-siemens.com>
Shalini Joshi wrote:
> Hey Ben!
>=20
> Thanks again for the help. What I am getting totally confused here is
> to what data structures to use given the problem. I need to get at the
> information across days so I store a reference to the array
> BASE[$mday-1]{$i} for $i (@cusips) . NOw i would like to get at all
> the elements of the array for all $i for a particular value of $mday.
> I can't seem to do that.
The advantage with computers is that you can try, experiment.
What have you tried so far and where have you failed?
What error messagesa have you got and where do you not understand them?
What is BASE[$mday-1]{$i}? It's not Perl, AFAIK, $BASE[$mday-1]{$i}=20
would be, it would be a hash reference.
[ useless quoting deleted ]
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
------------------------------
Date: Wed, 30 Jun 2004 11:14:51 +0200
From: "Jochen Friedmann" <jochen.friedmann3@de.bosch.com>
Subject: Search more than one wor in a string
Message-Id: <cbu0ac$jte$1@ns1.fe.internet.bosch.com>
Hello,
my problem:
I am looking for two words in a string var. If there are both words in it I
want to bring out an error message.
if (($param =~ m/move/gi)&&($param =~ m/copy/gi)){print "\n --- To much
parameters /Copy or /Move";}
Jochen
------------------------------
Date: Wed, 30 Jun 2004 11:39:36 +0200
From: "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net>
Subject: Re: Search more than one wor in a string
Message-Id: <Xns951876F17A439elhber1lidotechnet@62.89.127.66>
"Jochen Friedmann" <jochen.friedmann3@de.bosch.com> wrote:
> Hello,
>
> my problem:
>
> I am looking for two words in a string var. If there are both
> words in it I want to bring out an error message.
>
> if (($param =~ m/move/gi)&&($param =~ m/copy/gi)){print "\n ---
> To much
> parameters /Copy or /Move";}
That'll work if you remove the /g option from m// (perldoc perlop -
read about /g. I don't think it does what you think).
A possibly better way:
if ($param =~ m/\bmove\b/i and $param =~ m/\bcopy\b/i) {
# ...
}
In this case you won't match "blamove" or "copycat", for example.
Whether that's useful to you or not depends on your expected values
of $param.
--
Cheers,
Bernard
------------------------------
Date: Tue, 29 Jun 2004 23:57:44 -0500
From: "Andrew Palmer" <atp5470 at fsu.edu>
Subject: Re: Why can't I get WWW::Mechanize->find_all_links to work?
Message-Id: <10e4hu8d65knm43@corp.supernews.com>
> The server returns invalid html.
For your particular task, you could fetch the page another way (e.g.
LWP::UserAgent) and then parse the page manually for something like /<a
href="(.*?casen=.*?)"/
------------------------------
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 V10 Issue 6750
***************************************