Hi all,

I'm very new to perl but I will try to explain where I am at the moment.
I have a lot of information in a .csv file. The 2nd columb has different values relating to different things. For example "no" = nokia, "se" = sony ericsson, "ip" = iphone and so on. So the 2nd columb will have a value like this no, se or ip. I am printing to screen what type of phone it is based on the information in the other columbs.

I have something like this implemented:

$dat_file = 'sample.csv';
@data =('no', 'se', 'ip' ....... );

open (F, $dat_file), print "File Opened\n\n" || die ("Could not open file");

  while ($line = <F>)
    ($time,$dat_type,$dat_from,$from_base,$dat_to,$to_base) = split ',', $line;


Within this while loop I am trying to print out all the information, I can do this in order to output no, se ip etc. However I am trying to set up an if loop as follows:
if dat_type = no
print nokia
elseif dat_type = se
print sony ericsson
else dat_type = ip
print iphone

I am having trouble getting the if loop to work with my code to output the correct output.
Currently it is going into the 1st if loop and not coming back out of it.

Anyone any ideas how I could sort this out?

Thanks very much.


6 Years
Discussion Span
Last Post by mitchems

I made up a simplified data file containing the following:


Notice the last record has a deliberately invalid item for testing. The following should print your data, after looking up the desired values for $dat_type that are defined in a hash. Using a hash means you don't need a complex if-else statement to test every possible $dat_type.

use strict;
use warnings;

# %dtypes hash will associate dat_type keys with values to print
my %dtypes = (no => 'nokia',
              se => 'sony ericsson',
              ip => 'iphone');

my $dat_file = 'sample.csv';

open (F, $dat_file) or die ("Could not open $dat_file: $!");

my $type2print;
printf "%-10s%-25s%-10s\n", ('Name', 'Type', 'Year');
while (my $line = <F>){
    chomp $line; #Remove trailing linefeed
    my ($name,$dat_type,$year) = split ',', $line;
    if (exists $dtypes{$dat_type}) {
        $type2print = $dtypes{$dat_type}
        $type2print = "Unknown Type '$dat_type'";

    printf "%-10s%-25s%-10s\n", ($name, $type2print, $year);

This prints the following output:

Name      Type                     Year      
Tom       nokia                    2009      
Dick      nokia                    2008      
Harry     sony ericsson            2010      
Jane      iphone                   2009      
Frank     Unknown Type 'te'        2007

Sure, use a keyed hash. See below.


my %ptypes=('no',"Noika",'se',"Sony Ericsson",'ip',"iPhone");

	my ($col1,$type,$rest)=split(/\,/);
	print "$ptypes{$type}\n";

BTW, there is also a way to store the elements of the csv file into an array of arrays. Check out the following...

CSV File

1,no,more 1,more 2,more 3,Moe
2,ip,more 1,more 2,more 3,Larry
3,se,more 1,more 2,more 3,Curly
4,no,more 1,more 2,more 3,Shemp
5,ip,more 1,more 2,more 3,Chunky Bacon

And code that does what you asked here AND gets elements from the stored arrays:


my %ptypes=('no',"Noika",'se',"Sony Ericsson",'ip',"iPhone");
my @holder;
print "Those phones\n";
	my (@data)=split(/\,/);
	print "$ptypes{$data[1]}\n";
	push (@holder,\@data);
close FILE;
#now you have the data saved in the holder with a ref to the array
print "\nThose stooges and their phones\n";
for (@holder){
	my $art;
	if($ptypes{$_->[1]}=~/^[aeiou]/i){ $art="an"; }else { $art="a"; } #grammar!
	print "$_->[5] has $art $ptypes{$_->[1]}\n";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.