On Mon, Oct 6, 2014 at 11:11 AM, Phil <gamm...@gmail.com> wrote:

> 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
>

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

Reply via email to