Solved

Basic perl question.

Posted on 1998-07-17
11
198 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:steph84
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 6

Expert Comment

by:alamo
ID: 1208478
If I understand, you want to skip your split and array stuff if the line is blank? If that's it, then try:

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...
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1208479
$_=$ligne; next if m/^\s*$/
0
 
LVL 1

Expert Comment

by:mikegrb
ID: 1208480
alamo I like that last line :)
next if $ligne eq "\n";  <=== that's the way I would do it but
TIMTOWTDI :)
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 84

Expert Comment

by:ozo
ID: 1208481
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.
0
 
LVL 1

Expert Comment

by:notanexpert
ID: 1208482
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 ....
0
 

Author Comment

by:steph84
ID: 1208483
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);
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1208484
So you may vote now for your favorite one ;-)
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208485
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
0
 

Author Comment

by:steph84
ID: 1208486
sorry for others, but ozo's answer seems to be the best one....
Thanks anyway.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208487
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.
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 1208488
while( <IN> ){
      chomp;
      push @compte,[split/#/] if /#/;
}

@compte = map{ /#/?(chomp,[split/#/]):() } <>;

0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question