Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Space delimted to CSV, Index and data extraction loop

by Not_a_Number (Parson)
on Jul 30, 2011 at 11:15 UTC ( #917610=note: print w/ replies, xml ) Need Help??


in reply to Space delimted to CSV, Index and data extraction loop

Here's one way of doing the actual parsing bit (ignoring headers, reading from __DATA__ and printing to the screen for simplicity):

use strict; use warnings; my $name_id = 0; my %seen_name; <DATA> for 1 .. 5; # Remove headers while ( my $line = <DATA> ) { next unless $line =~/\w/; if ( $line =~ /Name/ ) { $name_id = parse_name( $line ); } else { my @tmp = split ' ', $line; shift @tmp; my $date = shift @tmp; print join ',', $name_id, $name_id, @tmp, $date; print "\n"; } } print "\n________Names file________\n\n"; for my $key ( sort { $seen_name{$a} <=> $seen_name{$b} } keys %seen_na +me ) { my @names = split /,/, $key; my $val = $seen_name{$key}; print join ",$val,", @names, "\n"; } sub parse_name { my $line = shift; my $name = join ',', ( split ' ', $line )[1, 3]; if ( $seen_name{$name} ) { $name_id = $seen_name{$name}; } else { $name_id += 1; $seen_name{$name} = $name_id; } return $name_id; }


Comment on Re: Space delimted to CSV, Index and data extraction loop
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://917610]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-11-27 05:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (180 votes), past polls