[EMAIL PROTECTED] wrote:
> On Oct 6, 11:26 am, [EMAIL PROTECTED] (Jeff Pang) wrote:
>> [EMAIL PROTECTED] wrote:
>>>
>>> I have large file in following format:
>>> ID | Time | IP | Code
>>>
>>> Now I want to write script that will cluster data by IP addr. and
>>> count total number of IDs for corresponding IP.
>>
>> first I hope this is not a homework question.
>> you may try something like:
>>
>> my $hash = {};
>> while(<DATA>) {
>> my ($id,$ip) = (split)[0,2];
>> $hash->{$ip}->{$id} ++;
>>
>> }
>>
>> use Data::Dumper;
>> print Dumper $hash;
>
> Thanks. No it is not homework. I trying to analyze some data.
>
> I am trying read file line by line and then use split('|',-------)
>
> But it is not getting split by delimeter " | " . Below is example
> code. But out is not as expected bcoz it is not getting split
> properly. I am not sure what I am doing wrong.
>
> open(INFO, $file);
> @lines = <INFO>;
>
> @line = split('|', $lines[0]);
>
> print $line[0];
That's not the best way to do things, but the reason you are not getting the
results you expect is that the first parameter to split() is a regular
expression. The pipe character "|" is special in that context and needs to be
escaped.
Try something like the program below instead. In particular always precede your
programs with
use strict;
use warnings;
and be sure to check the success of file opnes.
HTH,
Rob
use strict;
use warnings;
my $file = 'filename';
open my $info, '<', $file or die $!;
while (<$info>) {
my @line = split /\|/;
print $line[0], "\n";
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/