Discussion:
Edit and Print Hashes
Phil
2014-10-06 15:11:02 UTC
Permalink
Hello There,

I have a text file that is built up as follows:

Naam;ISIN;Symbol;Market;Trading Currency
IDI;FR0000051393;IDIP;Euronext Paris;EUR
BETER BED;NL0000339703;BBED;Euronext Amsterdam;EUR
...
GENTICEL;FR0011790542;GTCL;Euronext Paris,Brussels;EUR

With the following code, I can read this text file into hashes:
--------------------------
use strict;
use warnings;

my %data;
my @names;


my $myFile = "myfile.csv";
open(FH, '<', $myFile) or error("Cannot open file ($!)");

while (<FH>){
chomp;
my @list=split(';');
for (my $i=0; $i<=$#list; $i++){
if ($.==1){
$names[$i]=$list[$i];
}
else {
push @{$data{$names[$i]}}, $list[$i];
}
}
}
close FH;
--------------------------
First Question:

I wish to create an additional column/list which will be a combination
of the "Symbol" and "Market".
The new column needs to contain the Symbol plus the one of the following cases:
- Euronext Amsterdam ==> append ".PA"
- Euronext Brussels ==> append ".BR"
- Euronext Paris ==> append ".PA"

Second Question:

How can I print all of the columns to a tab-delimited text file? The
following are the headers of the (new) text file:
Naam ISIN Symbol Ticker Market Trading Currency



Thanks for your tips/help.
Best regards

Phil
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Ken Slater
2014-10-07 00:15:56 UTC
Permalink
Post by Phil
Hello There,
Naam;ISIN;Symbol;Market;Trading Currency
IDI;FR0000051393;IDIP;Euronext Paris;EUR
BETER BED;NL0000339703;BBED;Euronext Amsterdam;EUR
...
GENTICEL;FR0011790542;GTCL;Euronext Paris,Brussels;EUR
--------------------------
use strict;
use warnings;
my %data;
my $myFile = "myfile.csv";
open(FH, '<', $myFile) or error("Cannot open file ($!)");
while (<FH>){
chomp;
for (my $i=0; $i<=$#list; $i++){
if ($.==1){
$names[$i]=$list[$i];
}
else {
}
}
}
close FH;
--------------------------
I wish to create an additional column/list which will be a combination of
the "Symbol" and "Market".
- Euronext Amsterdam ==> append ".PA"
- Euronext Brussels ==> append ".BR"
- Euronext Paris ==> append ".PA"
How can I print all of the columns to a tab-delimited text file? The
Naam ISIN Symbol Ticker Market Trading Currency
Thanks for your tips/help.
Best regards
Phil
Hi Phil,

In general, your code is fine. To nitpick, I would advise using lexical
file handles (
http://www.shlomifish.org/lecture/Perl/Newbies/lecture5/new-features/lexical-filehandles.html)
rather than FH.
Also, you usually do not have to use an indexed loop structure.

Below is an example that assumes you do not need to save the data, but
rather read it in and print it out. These examples do not do any error
checking.

use strict;
use warnings;

my %map = ( 'Euronext Amsterdam' => '.AM',
'Euronext Brussels' => '.BR',
'Euronext Paris' => '.PA',
);

# print output header line
print "Naam\tISIN\tSymbol\tTicker\tMarket\tTrading\tCurrency\n";

# Skip the header line
<DATA>;
while (<DATA>)
{
chomp;
my ($name, $isin, $symbol, $market, $currency) = split(';');
# If you wanted, do some error cheking to make sure none of the
# fields are undef
#
# Could check to insure $map{$market} is defined
my $ticker = "$symbol$map{$market}";
print "$name\t$isin\t$symbol\t$ticker\t$market\t$currency\n"
}

__DATA__
Naam;ISIN;Symbol;Market;Trading Currency
IDI;FR0000051393;IDIP;Euronext Paris;EUR
BETER BED;NL0000339703;BBED;Euronext Amsterdam;EUR
GENTICEL;FR0011790542;GTCL;Euronext Brussels;EUR

If you need to store the data, the following provides an example.
Note that it uses array slices to store the data. Of course there are many
ways to do this in Perl. Also not that I shift the name off the array of
fields rather than performing a check for the first element of the array.

use strict;
use warnings;

my %data;

my %map = ( 'Euronext Amsterdam' => '.AM',
'Euronext Brussels' => '.BR',
'Euronext Paris' => '.PA',
);

# print output header line
print "Naam\tISIN\tSymbol\tTicker\tMarket\tTrading\tCurrency\n";

# Skip the header line
<DATA>;
while (<DATA>)
{
chomp;
my @list=split(';');
my $name = shift @list;
# Using array slices
push @{$data{$name}}, @list[0..1];
push @{$data{$name}}, "$list[1]$map{$list[2]}";
push @{$data{$name}}, @list[2..$#list];
}

foreach my $name ( sort keys %data )
{
print join("\t", $name, @{$data{$name}}), "\n";
}

__DATA__
Naam;ISIN;Symbol;Market;Trading Currency
IDI;FR0000051393;IDIP;Euronext Paris;EUR
BETER BED;NL0000339703;BBED;Euronext Amsterdam;EUR
GENTICEL;FR0011790542;GTCL;Euronext Brussels;EUR


HTH, Ken

Loading...