Question

I have this script

open (DICT,'file 0 .csv'); 

my @dictio = <DICT>;
chomp @dictio;
print @dictio;

My file 0 .csv is like this :

AAAA , a
AAAT , b
AAAC , c

So using chomp I want to delete the new row character, but when I print it my array dissapear. And when I print my array without using chomp it prints like the initially file.

So what am I doing wrong with the command chomp?

Thanks

Was it helpful?

Solution

Try this

open (my $DICT, '<', 'file 0 .csv') or die "cannot open file "; 

my @dictio = <$DICT>;
chomp @dictio;
print @dictio;

OTHER TIPS

open (my $DICT, '<', 'file 0 .csv') or die "cannot open file ";
while ( my $line = <$DICT> ) {
    chomp $line;
    my @line = split( ',' , $line );
}
close($DICT);

Your current code reads in the entire file into an array. I think it's slurping when done this way. As a result, your chomp is not performing as you would expect. Chomp is usually used with a scalar variable, as i've used it above, to chop off the '\n' in each line.

The code i've written above reads your file line-by-line into an array i've chosen to call @line, which contains each field of the current line of your file. This allows you to process one line at a time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top