niallheavey wrote:
Hi all,
Hello,
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:
You should enable the warnings and strict pragmas in your code to let
perl help you find mistakes:
use warnings;
use strict;
$dat_file = 'sample.csv';
my $dat_file = 'sample.csv';
@data =('no', 'se', 'ip' ....... );
You should use a hash for that:
my %phone_types = (
no => 'Nokia',
se => 'Sony Ericsson',
ip => 'Iphone',
);
open (F, $dat_file), print "File Opened\n\n" || die ("Could not open
file");
Your die() will only work if print() fails. If open() fails you will
not be notified and your program will continue as if it had a valid
filehandle when it didn't.
open F, '<', $dat_file or die "Could not open '$dat_file' because $!";
print "File Opened\n\n";
#...@raw_data=<F>;
while ($line = <F>)
while ( my $line = <F> )
{
If you intend to use the last value in the line then you should also
chomp() the line to remove the newline:
chomp $line;
($time,$dat_type,$dat_from,$from_base,$dat_to,$to_base) = split
',', $line;
Regular expressions usually use // as the delimiters:
my ( $time, $dat_type, $dat_from, $from_base, $dat_to, $to_base ) =
split /,/, $line;
If you use the hash as I pointed out earlier then it should be simply:
if ( exists $phone_types{ $dat_type } ) {
print $phone_types{ $dat_type };
}
else {
warn "Phone type $dat_type does not exist in my database.\n";
}
:
:
:
:
:
}
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
end
I am having trouble getting the if loop to work with my code to output
There is no such thing as an "if loop". The code block of an if
statement is only ever executed zero or one time. A loop implies that
the code block can execute more than one time if the conditional allows.
the correct output.
Currently it is going into the 1st if loop and not coming back out of
it.
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/