[7537] in Perl-Users-Digest
Perl-Users Digest, Issue: 1165 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 13 03:37:15 1997
Date: Sun, 12 Oct 97 19:18:33 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 12 Oct 1997 Volume: 8 Number: 1165
Today's topics:
Re: Q: Dynamically creating hashes and arrays (Toutatis)
Re: Q: Dynamically creating hashes and arrays (Toutatis)
Re: Q: Dynamically creating hashes and arrays (Toutatis)
Re: Q: Dynamically creating hashes and arrays (Toutatis)
Re: Q: Dynamically creating hashes and arrays (Toutatis)
the oddest syntax error <oberon@nospam.erols.com>
Re: URL checking <usenet-tag@qz.little-neck.ny.us>
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 13 Oct 1997 00:46:10 GMT
From: toutatis@_SPAMTRAP_toutatis.net (Toutatis)
Subject: Re: Q: Dynamically creating hashes and arrays
Message-Id: <toutatis-ya023180001310970246050001@news.euro.net>
In article <1997101222304015886@du46-152.ppp.algonet.se>,
hansmbg@algonet.se (Hans Malmberg) wrote:
> Now, let's see if I can explain this...
> I'm trying ( I'll never leave that niveau..) to create
> a Perl script which opens a file with data and manipulates
> the data. The data file is built this way:
> ------------------------------------------
> name1
> date number number number
> ....... and more date + numbers
> date number number number
>
> name2
> date number number number
> ....... and more date + numbers
> date number number number
>
> name3
> date number number number
> ....... and more date + numbers
> date number number number
> -----------------------------------------
>
> in the real world it looks like this
> -----------------------------------------
> S-E-Bankens Fond1
> 97-01-09 932,84 55,3848 16,8429
> 97-03-27 -115,97 -6,3767 18,1865
> 97-04-07 1231,34 70,3961 17,4916
> 97-10-07 1140,05 52,4168 21,7497
>
> S-E-Bankens Teknofond
> 97-01-09 932,84 68,5388 13,6104
> 97-01-28 969,09 67,4929 14,3584
> 97-02-05 1231,34 83,5130 14,7443
> 97-03-05 1231,34 86,4238 14,2477
>
> S-E-Bankens Eur Sbolagsfond
> 97-01-09 932,84 70,0599 13,3149
> 97-03-05 1231,34 84,0896 14,6432
> 97-03-27 -123,64 -8,1237 15,2197
> 97-09-30 -145,37 -8,8902 16,3517
>
> S-E-Bankens Europafond
> 97-08-06 1140,05 95,0073 11,9996
> 97-09-05 1140,05 98,6680 11,5544
> -----------------------------------------
>
> I confess: I have a problem with hashes and lists,
> and I think I'm too old to ever fully understand the
> thing. But stubborn as I am I fight with trial and error.
>
> this is the code I'm currently stuck with...
> -----------------------------------------
> open(FONDER,"P5451") || die "Can't Open Fonder Data File: $!\n";
> @LINES = <FONDER>;
> close(FONDER);
>
> $SIZE = @LINES;
>
> foreach $lines (@LINES) {
>
> $_ = $lines;
> if (length($_) == 1) { # empty line, just a linefeed
> next;
> }
> $_ =~ s/\s*//; # strip leading spaces,
> ($datum, $inbelopp, $antal, $per_andel) = split(/\s+/,$_);
>
> $rec = {}; # create empty array
>
> if ( $datum =~ /S-E-B/ ) { # is it a date?
> chop; # no it is the name of the fund
> $rec->{$_} = $_; # store the name, named by itself
> print "Test 1 rad 103 $_\n"; # just checkin'
> push @MATRIS, $rec; # so the professionals say...
> next; # the name is the only element
> } # on this line
> else { # continue, it's a date
> $rec->{datum} = $datum; # store...
> $rec->{antal} = $antal; # and so on
> $rec->{per_andel} = $per_andel;
> }
>
> if ($inbelopp < 0) { # if negative then it cost's
> $rec->{kostnad} = $inbelopp; # fee is kostnad
> $avgifter += abs $inbelopp; # sum the fees
> }
> else {
> $rec->{inbelopp} = $inbelopp; # input
> }
>
> push @MATRIS, $rec; # Programming Perl sid 268
>
>
> } # end foreach
> -----------------------------------------
>
> It could be better, suggestions appreciated.
> Now, I want a hash for each separate name, e.g. I
> want to create a new hash for every new 'S-E-B...'-line.
> The name of the hash shall be the name of the fund.
> I have the book Perl Programming, but I have difficulties
> interpreting some of the syntax.
> If you can help I'm most grateful
> Regards,
> --
> /Hans --- LUND 13.10'30(E 55.43'24(N
I've read it too, even not too long ago...
My first advise to you is to keep reading-in data apart from
making reports out of it. I don't understand all swedish
terms, but it seems to me that you're trying to do these in one
go. For sure, calculating $afgivter (whatever that may be) will
not for long be the only thing you want to calculate, and then
you'll end up with code far too complex to maintain.
I tried to bake some code for you that should help you out
a little. It's only a small step away from turning it into a nice
module. I am almost sure there are still bugs in it, but -w
and 'strict' will help you a long way.
(Don't have a newsreader with built in compiler *yet*).
#!/usr/bin/perl -w
use strict;
#read the data:
my $data = read_data('your_filename_here');
#then play with it:
my $inbelop = get_total($data,'inbel_op');
my $antal = get_total($data,'antal');
my $per_andel = get_total($data,'per_andel');
sub read_data {
my $file = shift;
my (%hash,$fundname,@row);
open( F,"<$file") or die "problem opening $file for reading";
my @lines = <F>;
close (F);
while (@lines){
$_ = shift @lines;
chop;
next unless /\w/;
s/S-E-Bankens //;
s/ /_/;
while (@lines && ($lines[0] !~ /S-E-Bankens/)){
@row = split /\s+/, shift @lines;
$date = shift @row;
for qw(inbelopp antal per_andel){
$hash{$fundname}{$date}{$_} = shift @row;
}
}
}
return \%data;
}
sub get_total {
my ($data,$value) = @_;
my ($fund,$date);
my $total = 0;
foreach $fund(keys %$data){
foreach $date(keys %{$data->{$fund}}){
$total += $data->{$date}{$value};
}
}
return $total;
}
--
Roberto Bourgonjen,
mailto:roberto@toutatis.net
------------------------------
Date: 13 Oct 1997 00:49:37 GMT
From: toutatis@_SPAMTRAP_toutatis.net (Toutatis)
Subject: Re: Q: Dynamically creating hashes and arrays
Message-Id: <toutatis-ya023180001310970249360001@news.euro.net>
In article <1997101222304015886@du46-152.ppp.algonet.se>,
hansmbg@algonet.se (Hans Malmberg) wrote:
> Now, let's see if I can explain this...
> I'm trying ( I'll never leave that niveau..) to create
> a Perl script which opens a file with data and manipulates
> the data. The data file is built this way:
> ------------------------------------------
> name1
> date number number number
> ....... and more date + numbers
> date number number number
>
> name2
> date number number number
> ....... and more date + numbers
> date number number number
>
> name3
> date number number number
> ....... and more date + numbers
> date number number number
> -----------------------------------------
>
> in the real world it looks like this
> -----------------------------------------
> S-E-Bankens Fond1
> 97-01-09 932,84 55,3848 16,8429
> 97-03-27 -115,97 -6,3767 18,1865
> 97-04-07 1231,34 70,3961 17,4916
> 97-10-07 1140,05 52,4168 21,7497
>
> S-E-Bankens Teknofond
> 97-01-09 932,84 68,5388 13,6104
> 97-01-28 969,09 67,4929 14,3584
> 97-02-05 1231,34 83,5130 14,7443
> 97-03-05 1231,34 86,4238 14,2477
>
> S-E-Bankens Eur Sbolagsfond
> 97-01-09 932,84 70,0599 13,3149
> 97-03-05 1231,34 84,0896 14,6432
> 97-03-27 -123,64 -8,1237 15,2197
> 97-09-30 -145,37 -8,8902 16,3517
>
> S-E-Bankens Europafond
> 97-08-06 1140,05 95,0073 11,9996
> 97-09-05 1140,05 98,6680 11,5544
> -----------------------------------------
>
> I confess: I have a problem with hashes and lists,
> and I think I'm too old to ever fully understand the
> thing. But stubborn as I am I fight with trial and error.
>
> this is the code I'm currently stuck with...
> -----------------------------------------
> open(FONDER,"P5451") || die "Can't Open Fonder Data File: $!\n";
> @LINES = <FONDER>;
> close(FONDER);
>
> $SIZE = @LINES;
>
> foreach $lines (@LINES) {
>
> $_ = $lines;
> if (length($_) == 1) { # empty line, just a linefeed
> next;
> }
> $_ =~ s/\s*//; # strip leading spaces,
> ($datum, $inbelopp, $antal, $per_andel) = split(/\s+/,$_);
>
> $rec = {}; # create empty array
>
> if ( $datum =~ /S-E-B/ ) { # is it a date?
> chop; # no it is the name of the fund
> $rec->{$_} = $_; # store the name, named by itself
> print "Test 1 rad 103 $_\n"; # just checkin'
> push @MATRIS, $rec; # so the professionals say...
> next; # the name is the only element
> } # on this line
> else { # continue, it's a date
> $rec->{datum} = $datum; # store...
> $rec->{antal} = $antal; # and so on
> $rec->{per_andel} = $per_andel;
> }
>
> if ($inbelopp < 0) { # if negative then it cost's
> $rec->{kostnad} = $inbelopp; # fee is kostnad
> $avgifter += abs $inbelopp; # sum the fees
> }
> else {
> $rec->{inbelopp} = $inbelopp; # input
> }
>
> push @MATRIS, $rec; # Programming Perl sid 268
>
>
> } # end foreach
> -----------------------------------------
>
> It could be better, suggestions appreciated.
> Now, I want a hash for each separate name, e.g. I
> want to create a new hash for every new 'S-E-B...'-line.
> The name of the hash shall be the name of the fund.
> I have the book Perl Programming, but I have difficulties
> interpreting some of the syntax.
> If you can help I'm most grateful
> Regards,
> --
> /Hans --- LUND 13.10'30(E 55.43'24(N
I've read it too, even not too long ago...
My first advise to you is to keep reading-in data apart from
making reports out of it. I don't understand all swedish
terms, but it seems to me that you're trying to do these in one
go. For sure, calculating $afgivter (whatever that may be) will
not for long be the only thing you want to calculate, and then
you'll end up with code far too complex to maintain.
I tried to bake some code for you that should help you out
a little. It's only a small step away from turning it into a nice
module. I am almost sure there are still bugs in it, but -w
and 'strict' will help you a long way.
(Don't have a newsreader with built in compiler *yet*).
#!/usr/bin/perl -w
use strict;
#read the data:
my $data = read_data('your_filename_here');
#then play with it:
my $inbelop = get_total($data,'inbel_op');
my $antal = get_total($data,'antal');
my $per_andel = get_total($data,'per_andel');
sub read_data {
my $file = shift;
my (%hash,$fundname,@row);
open( F,"<$file") or die "problem opening $file for reading";
my @lines = <F>;
close (F);
while (@lines){
$_ = shift @lines;
chop;
next unless /\w/;
s/S-E-Bankens //;
s/ /_/;
while (@lines && ($lines[0] !~ /S-E-Bankens/)){
@row = split /\s+/, shift @lines;
$date = shift @row;
for qw(inbelopp antal per_andel){
$hash{$fundname}{$date}{$_} = shift @row;
}
}
}
return \%data;
}
sub get_total {
my ($data,$value) = @_;
my ($fund,$date);
my $total = 0;
foreach $fund(keys %$data){
foreach $date(keys %{$data->{$fund}}){
$total += $data->{$date}{$value};
}
}
return $total;
}
--
Toutatis
------------------------------
Date: 13 Oct 1997 01:12:04 GMT
From: toutatis@_SPAMTRAP_toutatis.net (Toutatis)
Subject: Re: Q: Dynamically creating hashes and arrays
Message-Id: <toutatis-ya023180001310970312040001@news.euro.net>
In article <1997101222304015886@du46-152.ppp.algonet.se>,
hansmbg@algonet.se (Hans Malmberg) wrote:
> Now, let's see if I can explain this...
> I'm trying ( I'll never leave that niveau..) to create
> a Perl script which opens a file with data and manipulates
> the data. The data file is built this way:
> ------------------------------------------
> name1
> date number number number
> ....... and more date + numbers
> date number number number
>
> name2
> date number number number
> ....... and more date + numbers
> date number number number
>
> name3
> date number number number
> ....... and more date + numbers
> date number number number
> -----------------------------------------
>
> in the real world it looks like this
> -----------------------------------------
> S-E-Bankens Fond1
> 97-01-09 932,84 55,3848 16,8429
> 97-03-27 -115,97 -6,3767 18,1865
> 97-04-07 1231,34 70,3961 17,4916
> 97-10-07 1140,05 52,4168 21,7497
>
> S-E-Bankens Teknofond
> 97-01-09 932,84 68,5388 13,6104
> 97-01-28 969,09 67,4929 14,3584
> 97-02-05 1231,34 83,5130 14,7443
> 97-03-05 1231,34 86,4238 14,2477
>
> S-E-Bankens Eur Sbolagsfond
> 97-01-09 932,84 70,0599 13,3149
> 97-03-05 1231,34 84,0896 14,6432
> 97-03-27 -123,64 -8,1237 15,2197
> 97-09-30 -145,37 -8,8902 16,3517
>
> S-E-Bankens Europafond
> 97-08-06 1140,05 95,0073 11,9996
> 97-09-05 1140,05 98,6680 11,5544
> -----------------------------------------
>
> I confess: I have a problem with hashes and lists,
> and I think I'm too old to ever fully understand the
> thing. But stubborn as I am I fight with trial and error.
>
> this is the code I'm currently stuck with...
> -----------------------------------------
> open(FONDER,"P5451") || die "Can't Open Fonder Data File: $!\n";
> @LINES = <FONDER>;
> close(FONDER);
>
> $SIZE = @LINES;
>
> foreach $lines (@LINES) {
>
> $_ = $lines;
> if (length($_) == 1) { # empty line, just a linefeed
> next;
> }
> $_ =~ s/\s*//; # strip leading spaces,
> ($datum, $inbelopp, $antal, $per_andel) = split(/\s+/,$_);
>
> $rec = {}; # create empty array
>
> if ( $datum =~ /S-E-B/ ) { # is it a date?
> chop; # no it is the name of the fund
> $rec->{$_} = $_; # store the name, named by itself
> print "Test 1 rad 103 $_\n"; # just checkin'
> push @MATRIS, $rec; # so the professionals say...
> next; # the name is the only element
> } # on this line
> else { # continue, it's a date
> $rec->{datum} = $datum; # store...
> $rec->{antal} = $antal; # and so on
> $rec->{per_andel} = $per_andel;
> }
>
> if ($inbelopp < 0) { # if negative then it cost's
> $rec->{kostnad} = $inbelopp; # fee is kostnad
> $avgifter += abs $inbelopp; # sum the fees
> }
> else {
> $rec->{inbelopp} = $inbelopp; # input
> }
>
> push @MATRIS, $rec; # Programming Perl sid 268
>
>
> } # end foreach
> -----------------------------------------
>
> It could be better, suggestions appreciated.
> Now, I want a hash for each separate name, e.g. I
> want to create a new hash for every new 'S-E-B...'-line.
> The name of the hash shall be the name of the fund.
> I have the book Perl Programming, but I have difficulties
> interpreting some of the syntax.
> If you can help I'm most grateful
> Regards,
> --
> /Hans --- LUND 13.10'30(E 55.43'24(N
I've read it too, even not too long ago...
My first advise to you is to keep reading-in data apart from
making reports out of it. I don't understand all swedish
terms, but it seems to me that you're trying to do these in one
go. For sure, calculating $afgivter (whatever that may be) will
not for long be the only thing you want to calculate, and then
you'll end up with code far too complex to maintain.
I tried to bake some code for you that should help you out
a little. It's only a small step away from turning it into a nice
module. I am almost sure there are still bugs in it, but -w
and 'strict' will help you a long way.
(Don't have a newsreader with built in compiler *yet*).
#!/usr/bin/perl -w
use strict;
#read the data:
my $data = read_data('your_filename_here');
#then play with it:
my $inbelop = get_total($data,'inbel_op');
my $antal = get_total($data,'antal');
my $per_andel = get_total($data,'per_andel');
sub read_data {
my $file = shift;
my (%data,$fundname,@row);
open( F,"<$file") or die "problem opening $file for reading";
my @lines = <F>;
chop @lines;
close (F);
while (@lines){
$_ = shift @lines;
next unless /\w/;
s/S-E-Bankens //;
s/ /_/g;
while (@lines && ($lines[0] !~ /S-E-Bankens/)){
@row = split /\s+/, shift @lines;
$date = shift @row;
for qw(inbelopp antal per_andel){
$data{$fundname}->{$date}{$_} = shift @row;
}
}
}
return \%data;
}
sub get_total {
my ($data,$value) = @_;
my ($fund,$date);
my $total = 0;
foreach $fund(keys %$data){
foreach $date(keys %{$data->{$fund}}){
$total += $data->{$date}{$value};
}
}
return $total;
}
--
Toutatis
------------------------------
Date: 13 Oct 1997 01:20:31 GMT
From: toutatis@_SPAMTRAP_toutatis.net (Toutatis)
Subject: Re: Q: Dynamically creating hashes and arrays
Message-Id: <toutatis-ya023180001310970320300001@news.euro.net>
In article <1997101222304015886@du46-152.ppp.algonet.se>,
hansmbg@algonet.se (Hans Malmberg) wrote:
> Now, let's see if I can explain this...
> I'm trying ( I'll never leave that niveau..) to create
> a Perl script which opens a file with data and manipulates
> the data. The data file is built this way:
> ------------------------------------------
> name1
> date number number number
> ....... and more date + numbers
> date number number number
>
> name2
> date number number number
> ....... and more date + numbers
> date number number number
>
> name3
> date number number number
> ....... and more date + numbers
> date number number number
> -----------------------------------------
>
> in the real world it looks like this
> -----------------------------------------
> S-E-Bankens Fond1
> 97-01-09 932,84 55,3848 16,8429
> 97-03-27 -115,97 -6,3767 18,1865
> 97-04-07 1231,34 70,3961 17,4916
> 97-10-07 1140,05 52,4168 21,7497
>
> S-E-Bankens Teknofond
> 97-01-09 932,84 68,5388 13,6104
> 97-01-28 969,09 67,4929 14,3584
> 97-02-05 1231,34 83,5130 14,7443
> 97-03-05 1231,34 86,4238 14,2477
>
> S-E-Bankens Eur Sbolagsfond
> 97-01-09 932,84 70,0599 13,3149
> 97-03-05 1231,34 84,0896 14,6432
> 97-03-27 -123,64 -8,1237 15,2197
> 97-09-30 -145,37 -8,8902 16,3517
>
> S-E-Bankens Europafond
> 97-08-06 1140,05 95,0073 11,9996
> 97-09-05 1140,05 98,6680 11,5544
> -----------------------------------------
>
> I confess: I have a problem with hashes and lists,
> and I think I'm too old to ever fully understand the
> thing. But stubborn as I am I fight with trial and error.
>
> this is the code I'm currently stuck with...
> -----------------------------------------
> open(FONDER,"P5451") || die "Can't Open Fonder Data File: $!\n";
> @LINES = <FONDER>;
> close(FONDER);
>
> $SIZE = @LINES;
>
> foreach $lines (@LINES) {
>
> $_ = $lines;
> if (length($_) == 1) { # empty line, just a linefeed
> next;
> }
> $_ =~ s/\s*//; # strip leading spaces,
> ($datum, $inbelopp, $antal, $per_andel) = split(/\s+/,$_);
>
> $rec = {}; # create empty array
>
> if ( $datum =~ /S-E-B/ ) { # is it a date?
> chop; # no it is the name of the fund
> $rec->{$_} = $_; # store the name, named by itself
> print "Test 1 rad 103 $_\n"; # just checkin'
> push @MATRIS, $rec; # so the professionals say...
> next; # the name is the only element
> } # on this line
> else { # continue, it's a date
> $rec->{datum} = $datum; # store...
> $rec->{antal} = $antal; # and so on
> $rec->{per_andel} = $per_andel;
> }
>
> if ($inbelopp < 0) { # if negative then it cost's
> $rec->{kostnad} = $inbelopp; # fee is kostnad
> $avgifter += abs $inbelopp; # sum the fees
> }
> else {
> $rec->{inbelopp} = $inbelopp; # input
> }
>
> push @MATRIS, $rec; # Programming Perl sid 268
>
>
> } # end foreach
> -----------------------------------------
>
> It could be better, suggestions appreciated.
> Now, I want a hash for each separate name, e.g. I
> want to create a new hash for every new 'S-E-B...'-line.
> The name of the hash shall be the name of the fund.
> I have the book Perl Programming, but I have difficulties
> interpreting some of the syntax.
> If you can help I'm most grateful
> Regards,
> --
> /Hans --- LUND 13.10'30(E 55.43'24(N
I've read it too, even not too long ago...
My first advise to you is to keep reading-in data apart from
making reports out of it. I don't understand all swedish
terms, but it seems to me that you're trying to do these in one
go. For sure, calculating $afgivter (whatever that may be) will
not for long be the only thing you want to calculate, and then
you'll end up with code far too complex to maintain.
I tried to bake some code for you that should help you out
a little. It's only a small step away from turning it into a nice
module. I am almost sure there are still bugs in it, but -w
and 'strict' will help you a long way.
(Don't have a newsreader with built in compiler *yet*).
#!/usr/bin/perl -w
use strict;
#read the data:
my $data = read_data('your_filename_here');
#then play with it:
my $inbelop = get_total($data,'inbel_op');
my $antal = get_total($data,'antal');
my $per_andel = get_total($data,'per_andel');
sub read_data {
my $file = shift;
my (%data,$fundname,@row);
open( F,"<$file") or die "problem opening $file for reading";
my @lines = <F>;
chop @lines;
close (F);
while (@lines){
$_ = shift @lines;
next unless s/S-E-Bankens //;
s/ /_/g;
while (@lines && ($lines[0] !~ /S-E-Bankens/)){
@row = split /\s+/, shift @lines;
$date = shift @row;
for qw(inbelopp antal per_andel){
$data{$fundname}->{$date}{$_} = shift @row;
}
}
}
return \%data;
}
sub get_total {
my ($data,$value) = @_;
my ($fund,$date);
my $total = 0;
foreach $fund(keys %$data){
foreach $date(keys %{$data->{$fund}}){
$total += $data->{$fund}{$date}{$value};
}
}
return $total;
}
--
Toutatis
--
Roberto Bourgonjen,
mailto:roberto@toutatis.net
------------------------------
Date: 13 Oct 1997 01:22:56 GMT
From: toutatis@_SPAMTRAP_toutatis.net (Toutatis)
Subject: Re: Q: Dynamically creating hashes and arrays
Message-Id: <toutatis-ya023180001310970322550001@news.euro.net>
In article <1997101222304015886@du46-152.ppp.algonet.se>,
hansmbg@algonet.se (Hans Malmberg) wrote:
> Now, let's see if I can explain this...
> I'm trying ( I'll never leave that niveau..) to create
> a Perl script which opens a file with data and manipulates
> the data. The data file is built this way:
> ------------------------------------------
> name1
> date number number number
> ....... and more date + numbers
> date number number number
>
> name2
> date number number number
> ....... and more date + numbers
> date number number number
>
> name3
> date number number number
> ....... and more date + numbers
> date number number number
> -----------------------------------------
>
> in the real world it looks like this
> -----------------------------------------
> S-E-Bankens Fond1
> 97-01-09 932,84 55,3848 16,8429
> 97-03-27 -115,97 -6,3767 18,1865
> 97-04-07 1231,34 70,3961 17,4916
> 97-10-07 1140,05 52,4168 21,7497
>
> S-E-Bankens Teknofond
> 97-01-09 932,84 68,5388 13,6104
> 97-01-28 969,09 67,4929 14,3584
> 97-02-05 1231,34 83,5130 14,7443
> 97-03-05 1231,34 86,4238 14,2477
>
> S-E-Bankens Eur Sbolagsfond
> 97-01-09 932,84 70,0599 13,3149
> 97-03-05 1231,34 84,0896 14,6432
> 97-03-27 -123,64 -8,1237 15,2197
> 97-09-30 -145,37 -8,8902 16,3517
>
> S-E-Bankens Europafond
> 97-08-06 1140,05 95,0073 11,9996
> 97-09-05 1140,05 98,6680 11,5544
> -----------------------------------------
>
> I confess: I have a problem with hashes and lists,
> and I think I'm too old to ever fully understand the
> thing. But stubborn as I am I fight with trial and error.
>
> this is the code I'm currently stuck with...
> -----------------------------------------
> open(FONDER,"P5451") || die "Can't Open Fonder Data File: $!\n";
> @LINES = <FONDER>;
> close(FONDER);
>
> $SIZE = @LINES;
>
> foreach $lines (@LINES) {
>
> $_ = $lines;
> if (length($_) == 1) { # empty line, just a linefeed
> next;
> }
> $_ =~ s/\s*//; # strip leading spaces,
> ($datum, $inbelopp, $antal, $per_andel) = split(/\s+/,$_);
>
> $rec = {}; # create empty array
>
> if ( $datum =~ /S-E-B/ ) { # is it a date?
> chop; # no it is the name of the fund
> $rec->{$_} = $_; # store the name, named by itself
> print "Test 1 rad 103 $_\n"; # just checkin'
> push @MATRIS, $rec; # so the professionals say...
> next; # the name is the only element
> } # on this line
> else { # continue, it's a date
> $rec->{datum} = $datum; # store...
> $rec->{antal} = $antal; # and so on
> $rec->{per_andel} = $per_andel;
> }
>
> if ($inbelopp < 0) { # if negative then it cost's
> $rec->{kostnad} = $inbelopp; # fee is kostnad
> $avgifter += abs $inbelopp; # sum the fees
> }
> else {
> $rec->{inbelopp} = $inbelopp; # input
> }
>
> push @MATRIS, $rec; # Programming Perl sid 268
>
>
> } # end foreach
> -----------------------------------------
>
> It could be better, suggestions appreciated.
> Now, I want a hash for each separate name, e.g. I
> want to create a new hash for every new 'S-E-B...'-line.
> The name of the hash shall be the name of the fund.
> I have the book Perl Programming, but I have difficulties
> interpreting some of the syntax.
> If you can help I'm most grateful
> Regards,
> --
> /Hans --- LUND 13.10'30(E 55.43'24(N
I've read it too, even not too long ago...
My first advise to you is to keep reading-in data apart from
making reports out of it. I don't understand all swedish
terms, but it seems to me that you're trying to do these in one
go. For sure, calculating $afgivter (whatever that may be) will
not for long be the only thing you want to calculate, and then
you'll end up with code far too complex to maintain.
I tried to bake some code for you that should help you out
a little. It's only a small step away from turning it into a nice
module. I am almost sure there are still bugs in it, but -w
and 'strict' will help you a long way.
(Don't have a newsreader with built in compiler *yet*).
#!/usr/bin/perl -w
use strict;
#read the data:
my $data = read_data('your_filename_here');
#then play with it:
my $inbelop = get_total($data,'inbel_op');
my $antal = get_total($data,'antal');
my $per_andel = get_total($data,'per_andel');
sub read_data {
my $file = shift;
my (%data,$fundname,@row);
open( F,"<$file") or die "problem opening $file for reading";
my @lines = <F>;
chop @lines;
close (F);
while (@lines){
$_ = shift @lines;
next unless s/S-E-Bankens //;
s/ /_/g;
while (@lines && ($lines[0] !~ /S-E-Bankens/)){
@row = split /\s+/, shift @lines;
$date = shift @row;
for qw(inbelopp antal per_andel){
$data{$fundname}->{$date}{$_} = shift @row;
}
}
}
return \%data;
}
sub get_total {
my ($data,$value) = @_;
my ($fund,$date);
my $total = 0;
foreach $fund(keys %$data){
foreach $date(keys %{$data->{$fund}}){
$total += $data->{$fund}{$date}{$value};
}
}
return $total;
}
--
Toutatis
------------------------------
Date: Sun, 12 Oct 1997 18:01:17 -0400
From: Kenneth Taborek <oberon@nospam.erols.com>
Subject: the oddest syntax error
Message-Id: <344148AD.42449036@nospam.erols.com>
I have encountered an error which I cannot come to terms with. The
program below has been stripped of all extraneous lines. But, prior to
paring it down, I had a small loop which printed the key-element pairs
of %rot13, and it worked fine. Then I noticed that @temp2 should fall
in range (a..m) instead of (a..n). When I made this change, I got the
following syntax error.
syntax error at ./thingy.pl line 12, near "if"
Execution of ./thingy.pl aborted due to compilation errors.
Here is my script:
#! /usr/bin/perl
$i = 0;
%rot13 = {};
@alpha = (a..z);
@temp1 = (n..z);
@temp2 = (a..n);
for ($i = 0; $i < 26; $i++) {
if ($i < 13) {
$rot13{$alpha[$i]} = $alpha[($i + 13)];
}
else {
$rot13{@alpha[$i]} = $alpha[($i - 13)];
}
}
Now, I am a complete beginner to perl, and may have made some basic
error, but I do not see how changing the range of elements in an array
could make the difference between working and breaking.
--oberon@erols.com
------------------------------
Date: 13 Oct 1997 01:33:37 GMT
From: Eli the Bearded <usenet-tag@qz.little-neck.ny.us>
Subject: Re: URL checking
Message-Id: <eli$9710122126@qz.little-neck.ny.us>
brian d foy <comdog@computerdog.com> wrote:
> "Sgt. Floyd Pepper" <thelenm@cs.hope.edu> wrote:
> > Hi, I'm wondering if there is a way to check whether a URL points to a
> > valid page.
> 1. The easy way: get your system admin to install LWP. tell him/her
...
> #/usr/bin/perl -wT
No need to rewrite the script. checkbot will do that and create a summary
of the results in a HTML page, suitable for use from cron, say, to check
things weekly.
checkbot should be on CPAN, it requires LWP.
Elijah
------
hasn't check links in nearly six months, now that he thinks of it
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.misc (and this Digest), send your
article to perl-users@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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V8 Issue 1165
**************************************