Home: Perl Programming Help: Beginner:
Hi any help please



peacengell
New User

Jan 20, 2013, 9:57 PM


Views: 2307
Hi any help please

hello
I need help with this script

#!/usr/bin/perl

my %years;

while (<STDIN>) {

chomp;

my $year = (split /\t/)[3];

$years{$year}++;
}

foreach (sort keys %years) {

print "In $_, $years{$_} Cds were released. \n";
}

I save this file ( Data_munging.pl)

when i run it like this ( perl -w Datamunging.pl <cd.txt

Here is the output:

[root@cent Data_muning_perl]# perl -w ./Data_muning1.pl <cd.txt
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 1.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 2.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 3.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 4.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 5.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 6.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 7.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 8.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 9.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 10.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 11.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 12.
Use of uninitialized value $year in hash element at ./Data_muning1.pl line 11, <STDIN> line 13.
In , 13 Cds were released.
[root@cent Data_muning_perl]#

this is cd.txt:

[root@cent Data_muning_perl]# cat cd.txt
Dave's CD Collection
16 Sep 1999

Artist Title Label Released
--------------------------------------------------------
Bragg, Billy Workers' Playtime Cooking Vinyl 1987
Bragg, Billy Mermaid Avenue EMI 1998
Black, Mary The Holy Ground Grapevine 1993
Black, Mary Circus Grapevine 1996
Bowie, David Hunky Dory RCA 1971
Bowie, David Earthling EMI 1987

6 Records
[root@cent Data_muning_perl]#

The real out put I'm looking for is :

1971 1
1987 1
1993 1
1996 1
1997 1
1998 1

any help why i'm getting this error please thanks:


7stud
Enthusiast

Jan 20, 2013, 10:59 PM


Views: 2305
Re: [peacengell] Hi any help please

Where is there a \t anywhere in cd.txt? perl is telling you that there is no 4th item after split()'ing, i.e. $year remains undef. If you do this:


Code
my @pieces = split/\t/; 

for (@pieces) {
say;
}

say '*' x 20;

...you should be able to quickly see what's wrong.


(This post was edited by 7stud on Jan 20, 2013, 11:18 PM)


peacengell
New User

Jan 20, 2013, 11:24 PM


Views: 2295
Re: [7stud] Hi any help please

hi 7stud thanks for your reply i try this

my @pieces =split /\t/;

for (@pieces) {
print "say";
}

print '*' x 20;
[root@cent Data_muning_perl]#
When i run it :

[root@cent Data_muning_perl]# perl -w test.pl <cd.txt
Use of uninitialized value $_ in split at test.pl line 1.
********************

where is the problem with the cd.txt

if you like i can send you the book and the cd.txt file
you might like to have a look..
thanks


Rahul6990
Novice

Jan 20, 2013, 11:37 PM


Views: 2290
Re: [peacengell] Hi any help please

HI peacengell ,

Can you post you entire code like you did in the first post. $_ will contain the current line. Eg:

while (<STDIN>) {

chomp;

my $year = (split /\t/)[3];

$years{$year}++;
}


$_ will contain the single line from cd.txt one by one.


peacengell
New User

Jan 20, 2013, 11:43 PM


Views: 2287
Re: [Rahul6990] Hi any help please

Ok guys i found the problem is that the cd.txt
dont have tab in it somhow it been converted to space

so i put the Tab and it's works thanks for your help ..

I very please you spend your time to help me i try my best to help others and give a help as a newbeee..


thanks a lot ..

next time i will do all these test before getting you guys in trouble and anyone do the same as me thanks have a good day and enjoy ..


7stud
Enthusiast

Jan 20, 2013, 11:52 PM


Views: 2285
Re: [peacengell] Hi any help please


Quote
When i run it :


You were supposed to insert that code in the appropriate place in your code:


Code
while (<STDIN>) {  
chomp;

########

my @pieces = split /\t/;

for (@pieces) {
say;
}

say '*' x 20;

########

#my $year = (split /\t/)[3];
#$years{$year}++;
}



(This post was edited by 7stud on Jan 20, 2013, 11:52 PM)


peacengell
New User

Jan 21, 2013, 1:03 AM


Views: 2280
Re: [7stud] Hi any help please

thanks alot for your help 7stud..

I found the problem in the Text file...

that there was no tabs in there

file came from a windows host and i'm put it on linux host

....