steph84
asked on
Basic perl question.
Here's the situation (quite a common one) : I've got an ascii file filled with lines like aaa#bbb. I want to read this file line by line, filling an array with values got from the ascii file. I made it as usual with :
open(IN,"myfile");
while ($ligne=<IN>){some split and array stuff };
close(IN);
My question is very simple (I've a lot of points left!!)
I don't want to read the last line if this last line is blank (for example to carriage returns). What is the most elegant (more efficient, more perl-style) manner to do that.
open(IN,"myfile");
while ($ligne=<IN>){some split and array stuff };
close(IN);
My question is very simple (I've a lot of points left!!)
I don't want to read the last line if this last line is blank (for example to carriage returns). What is the most elegant (more efficient, more perl-style) manner to do that.
$_=$ligne; next if m/^\s*$/
alamo I like that last line :)
next if $ligne eq "\n"; <=== that's the way I would do it but
TIMTOWTDI :)
next if $ligne eq "\n"; <=== that's the way I would do it but
TIMTOWTDI :)
Do you only want to skip blanks when it's the last line?
(as it is, you're skipping "0" when it's the last line)
I'd also be interested in seeing your split and array stuff.
(as it is, you're skipping "0" when it's the last line)
I'd also be interested in seeing your split and array stuff.
Arn't we forgetting a key issue here? Is there a way to do /anything/ elegantly in Perl? ;)
Back to the question, why not make this conditional that you'll write to include lines not of the proper format? Ie, if a line doesn't have the format xxx#yyy or whatever, skip it. That'd include blank lines, and blank lines at the end of the file.
Something like
if $ligne=~ /^[a-zA-Z]{3}#[a-zA-Z]{3}$ / {
# process valid line
}
. or something like that. I don't have this stuff memorized, I'm just suggesting you might wanna cover the blanket case anyways if its something youll have to think about later ....
Back to the question, why not make this conditional that you'll write to include lines not of the proper format? Ie, if a line doesn't have the format xxx#yyy or whatever, skip it. That'd include blank lines, and blank lines at the end of the file.
Something like
if $ligne=~ /^[a-zA-Z]{3}#[a-zA-Z]{3}$
# process valid line
}
. or something like that. I don't have this stuff memorized, I'm just suggesting you might wanna cover the blanket case anyways if its something youll have to think about later ....
ASKER
for ozo and others, split and array stuff :
each line has this form : id#name#password.
I'm quite impressed by the number of comments or answers!!!
while ($ligne=<IN>)
{
@info = split(/#/,$ligne);
# array of array
@compte = (@compte,[@info]);
}
close(IN);
each line has this form : id#name#password.
I'm quite impressed by the number of comments or answers!!!
while ($ligne=<IN>)
{
@info = split(/#/,$ligne);
# array of array
@compte = (@compte,[@info]);
}
close(IN);
So you may vote now for your favorite one ;-)
while( <IN> ){
push @compte,[split/#/] if /#/;
}
@compte = map{ /#/?[split/#/]:() } <IN>;
#but these skip blanks evn if it's not the last line, or non-blank lines containing no '#'s
push @compte,[split/#/] if /#/;
}
@compte = map{ /#/?[split/#/]:() } <IN>;
#but these skip blanks evn if it's not the last line, or non-blank lines containing no '#'s
ASKER
sorry for others, but ozo's answer seems to be the best one....
Thanks anyway.
Thanks anyway.
By the way, you do realize the final element in each line's array has a trailing \n? This would cause problems in many implementations. A "chomp" can work wonders.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
next if $ligne eq "\n";
I would assume the first part if your processing would be "chop $ligne;", if so you could make the next line
next if $ligne eq '';
You might be able to detect a blank line slightly more efficiently as a byproduct of your processing the line, but it seems like it wouldn't be worth the effort.
But definitely don't consider the issue closed till ozo weighs in on this subject...