Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

search date occurance from csv column

by anakin30 (Acolyte)
on Mar 19, 2014 at 02:47 UTC ( #1078887=perlquestion: print w/ replies, xml ) Need Help??
anakin30 has asked for the wisdom of the Perl Monks concerning the following question:

I'm still a beginner in Perl programming. hence i need some advice in here to do a perl script. i have a input file as given below, and i need to use the current date search each column to get the number of occurrence from each columns. The date1 or date2 format first 8 numbers are "YYYYMMDD". i have write a basic code referring some perl code from internet and i only managed to come out with below :(. need some help or advice here.

input file

Student ID;date1;date2

13645722;20110327203202.0Z;20140211204731.0Z

14947374;20110327203202.0Z;20140220135320.0Z

15962660;20110327203156.0Z;20131106113716.0Z

17480884;20110327212821.0Z;20131211143804.0Z

13054554;20110327203141.0Z;20131205162959.0Z

#!/usr/bin/perl use strict; use warnings; my $file = $ARGV[0] or die "key in your csv file name\n"; my $count = 0; open(my $data, '<', $file) or die "Could not open '$file' $!\n"; while (my $line = <$data>) { chomp $line; my @fields = split ";" , $line; my $rows = $fields[2]; $count++; } print "$count\n";

Thank you in advanced.

Comment on search date occurance from csv column
Download Code
Re: search date occurance from csv column
by wjw (Curate) on Mar 19, 2014 at 03:11 UTC
    Just to start, you probably want to split on ";" instead of a ","

    Then, run your program with the debugger to learn about what is happening.
    That should give you a good start toward both learning what Perl is doing as well as how to troubleshoot.
    perl -d your_program_name.pl

    Following compiles and gives you a start

    #!/usr/bin/perl use strict; use warnings; my $count = 0; while (my $line = <DATA>) { chomp $line; my @fields = split ";" , $line; my $rows = $fields[2]; $count++; } print "$count\n"; __DATA__ 13645722;20110327203202.0Z;20140211204731.0Z 14947374;20110327203202.0Z;20140220135320.0Z 15962660;20110327203156.0Z;20131106113716.0Z 17480884;20110327212821.0Z;20131211143804.0Z 13054554;20110327203141.0Z;20131205162959.0Z

    Hope that is helpful...
    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results.
Re: search date occurance from csv column
by graff (Chancellor) on Mar 19, 2014 at 03:17 UTC
    It's not clear what you want to get as a result from your script. When you say:

    i need to use the current date search each column to get the number of occurrence from each columns

    I'm afraid that doesn't make any sense in English. Perhaps you can give an example of data that should produce a particular output, and tell us what that particular output should be.

    I notice that sample data you gave uses semi-colons between the fields (;), but you are using a comma in your split call (,), so @fields would always have a single element containing the whole line (because there are no commas), and $fields[2] will always be undef.

    Anyway, assuming that you do the split the right way, what do you really want to do with the second date string of each line?

      My apology for making things not clear

      I give you an example on what i want to achieve, let say today's date is 20140319 (based on format YYYYMMDD) i want to use this date to search the column date1 and date2 to find how many occurrence are there from each column and provide the total occurrences.

      expected output is as following

      column date1 = xx occurence

      column date2 = xx occurence

      __DATA__

      input file

      Student ID;date1;date2

      13645722;20110327203202.0Z;20140211204731.0Z

      14947374;20110327203202.0Z;20140220135320.0Z

      15962660;20110327203156.0Z;20131106113716.0Z

      17480884;20110327212821.0Z;20131211143804.0Z

      13054554;20110327203141.0Z;20131205162959.0Z

Re: search date occurance from csv column
by kcott (Abbot) on Mar 19, 2014 at 05:08 UTC

    G'day anakin30,

    There seems to be a disconnect between your description, sample data and attempted code. Even your subsequent post (Re^2: search date occurance from csv column) does not make it clear what you want: if you're searching for '20140319', and that does not occur anywhere in the sample data you provide, surely you want "... 0 occurrences" (for both lines of output), not "... xx occurence" (as you've written for both).

    All of this leaves me unclear as to what you're having difficulties with. Here's some possible areas where you might need help:

    • Your code doesn't determine any search date (current date or otherwise).
    • You have no code that compares a search date with any date extracted from your input file.
    • You don't show any code that extracts the data in the 'date1' column.
    • You're counting every line in the input file but you say you want to count matches in the 'date1' and 'date2' columns.
    • Your print statement does not, in any way, resemble the "expected output" shown in your subsequent post (in this thread).

    Here's a short script to show techniques for extracting your data, keeping a count and displaying the results based on search criteria. This isn't intended to be a solution (as your requirements are unclear) but should provide some pointers for achieving your goal.

    #!/usr/bin/env perl -l use strict; use warnings; my @counts; while (<DATA>) { my @dates = map { substr $_, 0, 8 } (split /;/)[1, 2]; ++$counts[$_]{$dates[$_]} for (0, 1); } for (qw{20110327 20140211 20140220}) { print "Search date: $_"; print 'date1: ', $counts[0]{$_} || 0; print 'date2: ', $counts[1]{$_} || 0; } __DATA__ 13645722;20110327203202.0Z;20140211204731.0Z 14947374;20110327203202.0Z;20140220135320.0Z 15962660;20110327203156.0Z;20131106113716.0Z 17480884;20110327212821.0Z;20131211143804.0Z 13054554;20110327203141.0Z;20131205162959.0Z

    Output:

    Search date: 20110327 date1: 5 date2: 0 Search date: 20140211 date1: 0 date2: 1 Search date: 20140220 date1: 0 date2: 1

    -- Ken

Re: search date occurance from csv column
by Lennotoecom (Pilgrim) on Mar 19, 2014 at 18:26 UTC
    $d = '20110327'; s/\;$d/$c++/ge for <DATA>; print "$c\n"; __DATA__ 13645722;20110327203202.0Z;20140211204731.0Z 14947374;20110327203202.0Z;20140220135320.0Z 15962660;20110327203156.0Z;20131106113716.0Z 17480884;20110327212821.0Z;20131211143804.0Z 13054554;20110327203141.0Z;20110627162959.0Z 13645722;20110427203202.0Z;20140211204731.0Z 14947374;20110527203202.0Z;20140220135320.0Z 15962660;20110127203156.0Z;20131106113716.0Z 17480884;20110627212821.0Z;20131211143804.0Z 13054554;20110227203141.0Z;20131205162959.0Z

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1078887]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2014-12-20 05:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls