cancel
Showing results for
Did you mean:

## PERL script needed

SOLVED
Go to solution
Occasional Contributor

## PERL script needed

In a Data file with delimiter ‘|’
i need to know the unique store key with number of items for it.

for example .
121882|18281684|100|250416|2
121882|18281684|100|250416|30

\$3=store key ; \$5=items

Output:
unique store key 100 with 32 items
7 REPLIES
Honored Contributor

## Re: PERL script needed

\$_ = "121882|18281684|100|250416|2";
my (\$key, \$items) = (split /\s*\|\s*/)[2,4];

or inside the loop:

while (<>) {
chomp;
my (\$key, \$items) = (split /\s*\|\s*/)[2,4];
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Occasional Contributor

## Re: PERL script needed

am using this script

open (INP,"testcpn1.txt");
while(\$x=){
chomp(\$x);
my(\$key,\$items)=split(/\s*\|\s*/)[2,4];
@count[\$key]+=\$items;
}
close (INP);
foreach \$i (@count)
{
print \$i."|".\$count[\$i];
}

am getting out put as 0|0

please can you look at this and give me the proper way.

Honored Contributor
Solution

## Re: PERL script needed

my %count;
open INP, "while () {
chomp;
my (\$key, \$items) = (split /\s*\|\s*/)[2,4];
\$count{\$key} += \$items;
}
close INP;
foreach my \$i (sort keys %count) {
print "\$i|\$count{\$i}\n";
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Occasional Contributor

## Re: PERL script needed

its is working fine.
Thanks a lot.
please here are some of the other queries
1. i need to find out the count of number of unique stores in it.

2. i have a date field(fourth field (\$4)) in my file with format 2005-01-23 00:00:00. from this i want to extract the hourth value.how can i do it with perl.
in awk i have written it succesfully
BEGIN{
FS=OFS="|"
}
{
dat=split(\$4,aa," ") //
hour=split(aa[2],bb,":")
count[bb[1]]++
}
END{
for(i in count)
{
print "Hour", i,"th number of transactions happened = " count[i];
}
}

please can u write this script in perl.

i need to convert all of my awk excercise in to perl.
because awk behaves erratically for numbers beyond 4294967296 (2^32)

Great Thanks

Honored Contributor

## Re: PERL script needed

> its is working fine.

Good.

> Thanks a lot.

Thank can be expressed in assigning points

> please here are some of the other queries
> 1. i need to find out the count of number of unique stores in it.

print scalar keys %count;

> 2. i have a date field(fourth field (\$4)) in my file with format 2005-01-23 00:00:00.
> from this i want to extract the hourth value.how can i do it with perl.

my \$hour = (\$date_fld =~ m/\s+(\d+)/);

> in awk i have written it succesfully

awk is not my specialty. I consider awk a dead language, only useful for very easy tasks

> i need to convert all of my awk excercise in to perl.

# a2p <script.awk > script.pl

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Honored Contributor

## Re: PERL script needed

Sorry, two parenteses missing:

my (\$hour) = (\$date_fld =~ m/\s+(\d+)/);

FWIW, you could also consider using Date::Calc

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Occasional Contributor

## Re: PERL script needed

for unique stores script.the number of unique stores in itd isplays as 2. in the data file there is only one store 100.