Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

beginers help.... 4

Status
Not open for further replies.

wh3e

Technical User
Jul 1, 2008
2
SE
Hey.. I got a simple question !?!?!?
If I got a document I want to read.. it looks like this.

-pornchan
bla bla.
bla bla bla.
----------
-mychan
bla and bla.
bla bla blue.
----------

When I execute the perl script with "perl lame.pl mychan" then I want to print
bla and bla.
bla bla blue.

Thats my problem..
If many ways.. i want it to be fast.. pretty big file..
And one more question.....
Is there any way to check if "word" is in a line?
//wh3e
 
What have you tried? Do you know any perl?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
wh3e said:
Thats my problem..
Not quite. Your real problem is that you want someone to do it for you, for free.

But I don't want to come over completely negative here. In answer to your second question, you can use a regular expression to check if a word exists in a line
Perl:
while (<>) {
   print if (/veeblefetzer/);
}

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
That part aint the whole program...
Thats the part I can't get to work..
I'm pretty none knowing when I gets to perl.
I can make it read the file.. But can't start and stop on diffrent lines... Thanx for the other info.
 
Perl:
use strict;
use warnings;

my $channel = shift;
my $start = qr{^-$channel$};

while (<>) {
   if (/$start/ .. /^-+$/) {
      print unless (/^-/);
   }
}

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
a Stevexff you are a poet!!


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Well done Stevexff. Question - would you be able to shave a clock cycle or two by removing the semicolon on the print statement? Save the parser from reading in that extra character on startup.
 
Mmm. While we are shaving clock cycles, let's do it right:
Perl:
print unless (substr($_, 0, 1) eq '-') [red]# ;[/red]
would probably be faster, over the course of several million iterations. Along with my own personal favourites, removing the strict and warning pragmas...[smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
FWIW - timed two loops over the suggested code above. After 1 million iterations, /^-/ averaged 0.65 secs and substr(xxx) averaged 0.33 seconds. (ok so it's a slow day today)
 
unpack is rumored to be faster than substr:

Code:
print unless unpack("A1",$_) eq '-';


Maybe someone can put that rumor to the test.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Here is the code I used:
Code:
use Time::HiRes qw(gettimeofday);
$max = 10000000 ;
while ($max != 0) {
   $t0 = gettimeofday ;
   for ($i=0;$i<$max;++$i) {
      print unless (/^#/) ;
   };
   $test1 = gettimeofday - $t0;
   $t0 = gettimeofday ;
   for ($i=0;$i<$max;++$i) {
      print unless (substr($_, 0, 1) eq '#') ;
   };
   $test2 = gettimeofday - $t0;
   $t0 = gettimeofday ;
   for ($i=0;$i<$max;++$i) {
      print unless unpack("A1",$_) eq '#' ;
   };
   $test3 = gettimeofday - $t0;
   print "Test 1 >$test1<  Test 2 >$test2<  Test 3 >$test3<\n";
   print "(prev $max) Next max (0 exit) : ";
   $max = <STDIN> ; chomp $max ;
};
Results off a basic 2.8GHz Intel chip running perl5.10.0
Test 1 >6.39087104797363< Test 2 >3.46887302398682< Test 3 >11.312922000885<
(prev 10000000) Next max (0 exit) : 0
 
So much for that rumour, then. Star for PinkeyNBrain, for the effort.

I suspect that the OP is losing the will to live at this point, but I've learned more from this thread than I ever suspected I would when it started...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top