| 1 | package Finance::GeniusTrader::List; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 4 | # This file is distributed under the terms of the General Public License | ||
| 5 | # version 2 or (at your option) any later version. | ||
| 6 | |||
| 7 | # Enhanced pod and load files with comments | ||
| 8 | # $Id$ | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Finance::GeniusTrader::List - List of symbols (shares) | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | This package provide some simple functions to work with a list of symbols. | ||
| 19 | |||
| 20 | =head2 Example | ||
| 21 | |||
| 22 | Create an empty Finance::GeniusTrader::List object : | ||
| 23 | my $list = Finance::GeniusTrader::List->new(); | ||
| 24 | |||
| 25 | Load data from a list of symbol : | ||
| 26 | $list->load("/bourse/listes/us/nasdaq"); | ||
| 27 | |||
| 28 | Add a symbol in a list : | ||
| 29 | $list->add("GeniusTrader"); | ||
| 30 | |||
| 31 | Remove a symbol in a list : | ||
| 32 | $list->remove("GeniusTrader"); | ||
| 33 | |||
| 34 | Save list in a file : | ||
| 35 | $list->save("/bourse/listes/us/nasdaq"); | ||
| 36 | |||
| 37 | Find how many symbols are in a list : | ||
| 38 | $list->count(); | ||
| 39 | |||
| 40 | Get symbol number $i : | ||
| 41 | $list->get($i); | ||
| 42 | |||
| 43 | =head2 Functions | ||
| 44 | |||
| 45 | =over | ||
| 46 | |||
| 47 | =cut | ||
| 48 | sub new { | ||
| 49 | my $type = shift; | ||
| 50 | my $class = ref($type) || $type; | ||
| 51 | |||
| 52 | my $self = { 'symbol' => [], 'count' => 0 }; | ||
| 53 | return bless $self, $class; | ||
| 54 | } | ||
| 55 | |||
| 56 | =item C<< $list->get($i) >> | ||
| 57 | |||
| 58 | Get symbol number $i. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | sub get { | ||
| 62 | my ($self, $i) = @_; | ||
| 63 | return $self->{'symbol'}[$i]; | ||
| 64 | } | ||
| 65 | |||
| 66 | =item C<< $list->add("symbol") >> | ||
| 67 | |||
| 68 | Add a symbol in a list. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | sub add { | ||
| 72 | my ($self, $symbol) = @_; | ||
| 73 | push @{$self->{'symbol'}}, $symbol; | ||
| 74 | $self->{'count'} += 1; | ||
| 75 | } | ||
| 76 | |||
| 77 | =item C<< $list->remove($i) >> | ||
| 78 | |||
| 79 | Remove the symbol number $i. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | sub remove { | ||
| 83 | my ($self, $i) = @_; | ||
| 84 | |||
| 85 | for (my $n = $i; $n < $self->count() - 1; $n++) { | ||
| 86 | $self->{'symbol'}[$n] = $self->{'symbol'}[$n + 1]; | ||
| 87 | } | ||
| 88 | delete $self->{'symbol'}[$self->count() - 1]; | ||
| 89 | $self->{'count'} -= 1; | ||
| 90 | } | ||
| 91 | |||
| 92 | =item C<< $list->count() >> | ||
| 93 | |||
| 94 | Find how many symbols are in the list. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | sub count { | ||
| 98 | return shift->{'count'}; | ||
| 99 | } | ||
| 100 | |||
| 101 | =item C<< $list->load("list_of_symbol.txt") >> | ||
| 102 | |||
| 103 | Load data from a list of symbol. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | sub load { | ||
| 107 | my ($self, $file) = @_; | ||
| 108 | open(FILE, "<", $file) || die "$0: error: Can't open file $file: $!\n"; | ||
| 109 | $self->{'symbol'} = []; | ||
| 110 | while (defined($_=<FILE>)) | ||
| 111 | { | ||
| 112 | chomp; | ||
| 113 | # ignore blank and sorta blank lines | ||
| 114 | =pod | ||
| 115 | |||
| 116 | Supports comments in loaded files. Comments start | ||
| 117 | with a # and continue to end of line. | ||
| 118 | |||
| 119 | Blank lines and lines with only white space are ignored. | ||
| 120 | |||
| 121 | Lines may also have data and a trailing comment. A comment is | ||
| 122 | separated from the data by whitespace. | ||
| 123 | |||
| 124 | The data need can be indented too. | ||
| 125 | |||
| 126 | examples: | ||
| 127 | |||
| 128 | V considered as column 1 of the line | ||
| 129 | # a comment line | ||
| 130 | AAPL # added to file on 25apr07 | ||
| 131 | # MSFT # no longer a growth company | ||
| 132 | ADPI # this one is indented with comment | ||
| 133 | |||
| 134 | =cut | ||
| 135 | next if /^\s*$/; # remove lines with only whitespace or nothing | ||
| 136 | # comments | ||
| 137 | next if /^#|^\s*#/; | ||
| 138 | # remove spaces at start of line, comments at end of lines | ||
| 139 | s/^\s*([^#\s]*)\s*#+\s*.*$/$1/; | ||
| 140 | |||
| 141 | $self->add($_); | ||
| 142 | } | ||
| 143 | close FILE; | ||
| 144 | } | ||
| 145 | |||
| 146 | =item C<< $list->save("list_of_symbol.txt") >> | ||
| 147 | |||
| 148 | Save list in a file. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | sub save { | ||
| 152 | my ($self, $file) = @_; | ||
| 153 | open(FILE, ">", $file) || die "Can't write in $file: $!\n"; | ||
| 154 | foreach (@{$self->{'symbol'}}) | ||
| 155 | { | ||
| 156 | print FILE "$_\n"; | ||
| 157 | } | ||
| 158 | close FILE; | ||
| 159 | } | ||
| 160 | |||
| 161 | =pod | ||
| 162 | |||
| 163 | =back | ||
| 164 | |||
| 165 | =cut | ||
| 166 | 1; |