Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print from CSV 1

Status
Not open for further replies.

cam24r

ISP
May 17, 2016
5
US
Hi,

I have the following input in a csv file(attached):

Size Color Shape
- |green |
- |red |-
- |- |Rectangle
- |- |Circle
big |- |-
small |- |-
....
...
..

and I want to get something like this:

big green rectangle
big green circle
big red rectangle
big red circle
small green rectangle
small green circle
small red rectangle
small red circle

Any idea where to start? Any help is greatly appreciated. Thanks.
 
It seems like the first line of data (the one with 'green') may have an error -- and there was no attachment. So taking what you entered as a sample, here you go:
Code:
use warnings;
use strict;
my %tiers;
my $fh = *DATA;

my $headers = <$fh>;	# Grab Headers

while (<$fh>) {
	next if /^\s*$/;	# Skip any Blank lines
	chomp;
	my @parts = split /\s*\|\s*/;
	
	for (my $i = 0; $i<= $#parts; $i++) {
		unless ($parts[$i] =~ /^\s*[-]?\s*$/) {
			push @{$tiers{$i}}, $parts[$i];
			last;
		}
	}
}

&print_tiers([], sort keys %tiers);

sub print_tiers {
	my $a_ref = shift;	# Array Reference with all the previous values
	my $key = shift;	# current key in %tiers
	if (scalar @_ == 0) {
		foreach my $val (@{$tiers{$key}}) {
			print join(" ", @{$a_ref}, $val), "\n";
		}
	} else {
		foreach my $val (@{$tiers{$key}}) {
			&print_tiers([@{$a_ref}, $val], @_);
		}
	}
}

__DATA__
Size Color Shape
- |green |
- |red |-
- |- |Rectangle
- |- |Circle
big |- |-	
small |- |-
 
Thank you rharsh, this did the job. I appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top