Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

CSV file info into xml tags

by CSharma (Sexton)
on Jul 28, 2015 at 08:03 UTC ( [id://1136559]=perlquestion: print w/replies, xml ) Need Help??

CSharma has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everybody, I've tab delimited data as below:
Retid State Rate 25 NJ 7.000 26 NJ 7.000 40 IL 9.750 40 IN 7.000 40 MI 6.000 40 WI 5.500 44 CT 6.000 50 NJ 7.000 87 VA 4.500

I want all unique Retid, comma seperated State & State corresponding to that Retid. Later I would need to add this data into different xml file by comparing Retid (Which is already in other file). I think using hashes would be OK as I don't want to read file everytime (else script will run longer), but not sure how to do this! Could anyone please suggest?

I've tried something like below:
open(IN, $taxFile) || die "$taxFile couldn't be opened $@\n"; while(<IN>) { chomp; @line = split("\t"); $sid = $line[0]; $sidHash{$sid}{state} = $line[1]; $sidHash{$sid}{rate} = $line[2]; }
Output file would be as below, where I'll add these 2 tags i.e. state & rate. Rest tags were read & printed from different file.
<RetId="40"> <name>xyz</name> <url>http://www.xyz.net</url> <type>CPC</type> <logoUrl/> <ntParentAcct>5403020</ntParentAcct> <feedCount>1</feedCount> <feedNumbers>1</feedNumbers> <state>IN, MI, WI</state> <rate>7.000, 6.000, 5.500</rate> </RetId>

Replies are listed 'Best First'.
Re: CSV file info into xml tags
by poj (Abbot) on Jul 28, 2015 at 08:39 UTC
    #!perl use strict; use warnings; use Data::Dump 'pp'; my %sidHash=(); open IN, '<', $taxFile or die "$taxFile couldn't be opened :$!\n"; while(<IN>){ chomp; next if /^Retid/; #skip header my ($sid,$state,$rate) = split /\s+/; push @{$sidHash{$sid}{state}}, $state; push @{$sidHash{$sid}{rate}} , $rate; } pp \%sidHash;
    poj
      Thanks POJ! To access its values, isn't this the way?
      foreach $t (sort keys %sidHash){ print "$t : State " . join(", ", @{$t->{'state'}}) . "rate: " + . join(", ", @{$t->{'rate'}} . "\n"; }

        What about using this:

        foreach my $t ( sort keys %sidHash ) { if ( scalar @{ $sidHash{$t}{state} } > 1 ) { print map { sprintf "%s: State %s rate: %s\n" => $t, $sidHash{$t}{state}[$_], $sidHash{$t}{rate}[$_] } 0 .. $#{ $sidHash{$t}{state} }; } else { print "$t: State ", join( ", " => @{ $sidHash{$t}{state} } ), " rate: ", join( ", " => @{ $sidHash{$t}{rate} } ), $/; }

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
        Have got it, accessed this way!
        foreach my $t (sort keys %midHash){ print $t . "\t" . join(", ", @{$midHash{$t}{state}}) . "\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1136559]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found