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

RE: two questions

Thread Previous | Thread Next
From:
Dan Muey
Date:
December 31, 2002 09:30
Subject:
RE: two questions
Message ID:
D6D77DB239A2004BB08A240E2E71460D08FA31@customer.infiniplex.net
I used "\t" assuming you were using a tab delimeted values, just fyi. 
You may need to do split(/	/, $line) if the \t doesn't work on your system.

-----Original Message-----
From: Dan Muey 
Sent: Tuesday, December 31, 2002 11:23 AM
To: Adriano Allora; beginners@perl.org
Subject: RE: two questions


Try :

@file = <INPUT>;

foreach $line(@file) {
	if($line) { 
# or regex above to make sure the line you're using id the proper format
		@tmp = ''; # undef or whatever
		@tmp = split(/\t/, $line);
		push(@new, $tmp[0]);
		push(@orig, $tmp[1]);
		@tmp = ''; # undef or whatever
	}
}

@file = ''; # or undef or whatever , incase you want to loop throiughseveral files

Are these vars you're using actually holding data?
Test this :
while (<INPUT>)
{
	m/	 /;
	$new[$.] = $`;
	$orig[$.] = $´;
# what do these output ?
Print $.;
Print $`;
Print $´;
# you may try
		push(@new, $`);
		push(@orig, $´);
# if those vars actually hold the right data
}

-----Original Message-----
From: Adriano Allora [mailto:adriano_allora@mac.com] 
Sent: Tuesday, December 31, 2002 11:10 AM
To: beginners@perl.org
Subject: Re: two questions



Lunedì, 30 Dic 2002, alle 23:38 Europe/Rome, Wiggins d'Anconia ha 
scritto:

>
>
> Adriano Allora wrote:
>> 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?
>
> Easier is subjective, I know Perl already and while Awk may be related 
> I wouldn't want to have to learn new syntax/language just to process 
> some files, so Perl is easier *to me*.  As far as speed, your friend 
> could very well be right, but to what extent. If it takes your program 
> .5 ms to parse a file with perl and .3 ms to parse it with AWK, then 
> yes it is faster, but it isn't likely in most situations that it 
> matters that much, sure in others it does.  Ask your friend how he 
> connects to a database in awk? Or about the OOP features of Awk.  Or 
> about the list of publicly available pre-written modules to handle all 
> kinds of tasks in Awk. Awk is great at one thing, which is why it is 
> fast at that thing, Perl is great at lots of stuff, but not as fast as 
> any one product in its own particular ability.  Why is an orange 
> better than an apple?

you're right. thanks.

>> 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?
>>
>
> I don't completely understand what your doing here. Can you give an 
> example of the input and desired output?

Ok, I spent this time to complicate - or semplify - my script (because 
I noted a carachteristic of the input).
Now, we're close to the new year's eve and I cannot stop to mind this 
script (this evening, I suppose, I will stop - if I wanna spent a good 
party).
I don't like who look for a complete and correct script here, but I 
cannot solve my problem: this is my input-type:

Dante	 Alighieri 
Cecco	 Angiolieri 
Brunetto	 Latini 
Eugenio	 Montale 
Giacomo	 Leopardi 
Niccolò	 Tommaseo 
Guido	 Gozzano 
(and so on...)

all I want in my output are two arrays: the first column of the input 
(all the first names) in the first array and the second column (all the 
surnames) in the second array.
I wrote the script that follows, but my script does not work for the 
@orig (more precisely, this script gives an empty array) and I don't 
undestand why.


#!/usr/bin/perl -w
use strict;

my $file_name = "cicci.txt";
my $new = "";
my $orig = "";
my @new = ();
my @orig = ();

open (INPUT, $file_name) or die "File not opnd cos $!";
while (<INPUT>)
{
	m/	 /;
	$new[$.] = $`;
	$orig[$.] = $´;
}
close (INPUT);

#these print lines are only to test the script
#I need the output in those two arrays
print @new;
print "\n";
print @orig;
print "\n";

____END____

someone sees where I mistake?
Thank and have a nice nite (in Italy we say: a good end and a good 
start)!

all'adr


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


-- 
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