develooper Front page | perl.beginners | Postings from December 2002

RE: two questions

Thread Previous | Thread Next
From:
Hanson, Rob
Date:
December 30, 2002 14:53
Subject:
RE: two questions
Message ID:
E693DE68445C064FA112BAFE0A5EFD25975663@exchange.nj.aptegrity.com
> 1 - Perl vs. AWK
> (my friend also forwarded me a mail of AWK
> mailing list in which someone who did a
> benchmark demonstrated the speed of awk...)

Did he benchmark the time it took to write it? :)

I'm not an authority on Awk, but it seems to me that Awk has one purpose,
and it isn't good at much else.  Perl can at least handle most programming
tasks, and excels at many of them.

Just because Awk shaves off a few seconds on a job doesn't make it better.
If that were the case we would all code directly in C.

Besides, writing Perl is fun once you get the hang of it (at least for me it
is).  I think the best part is that there are few (if any) limitations to
what it can do.  Imagine writing a compiler in Awk :)

> 2 - an unexplicable difference

I'm not exactly sure what you are doing, but this might suit you better...

##### START CODE #####
#!/usr/bin/perl -w

use strict; # shows you your mistakes

my $file_name = "cicci.txt";
my @new = (); # initialize the array

# always good to die on an error
open (INPUT, $file_name) or die $!;

while (my $in = <INPUT>) {
  # grabs all chars before a space, stored in $1
  if ($in =~ /^(\S+)/) {
    $new[$.] = $1;
  }
}

close (INPUT);
print @new, "\n";
##### END CODE #####

Rob


-----Original Message-----
From: Adriano Allora [mailto:adriano_allora@mac.com]
Sent: Monday, December 30, 2002 4:11 PM
To: beginners@perl.org
Subject: two questions


hi to all,
I'd like to know two things:

1 - Perl vs. AWK
I'm learning Perl to use it in text processing. Recently I start to 
argue with a friend of mine about the best language to process texts 
(clear them, or markup them, tokenize them or parse them), he says awk 
is better - quicker than perl, for example, and easier -.
I want to learn perl also for cging, but I'm curious about its skills 
with texts (my friend also forwarded me a mail of AWK mailing list in 
which someone who did a benchmark demonstrated the speed of awk...)
Someone want to tell me somethong about it?


2 - an unexplicable difference
I wrote two scripts in order to extract parts of text from a file and 
put these parts in two arrays.
The first one works very well, but the second one doesn't. Because of 
the scripts are identical in all but for the second regexp (but the 
regexp is not erroneous), I cannot undestand this difference.
Some advice?

THIS ONE WORKS:
#!/usr/bin/perl -w
my $file_name = "cicci.txt";
my $num = 0;
my $new = "";
open (INPUT, $file_name);
while (<INPUT>)
{
s/\s.+/\n/g;
foreach ($.)
	{
	m/\n/;
	$new[$.] = $`;
	}
}
close (INPUT);
print @new;
print "\n";

THIS ONE DOESN'T WORK:
#!/usr/bin/perl -w
my $file_name = "cicci.txt";
my $num = 0;
my $new = "";
open (INPUT, $file_name);
while (<INPUT>)
{
foreach (s/(\S|\S\S|\S\S\S)(	 )//g)
# typing here "print;" I saw that the regexp works;
	{
	print;
	m/\n/;
	$new[$.] = $`;
	}
}
close (INPUT);
print @new;
print "\n";

all'adr


-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About