Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: New to Perl - locating max, subtracting initial, printing output, all across several files in differing directories

by blindluke (Hermit)
on Feb 04, 2015 at 17:49 UTC ( [id://1115546]=note: print w/replies, xml ) Need Help??


in reply to New to Perl - locating max, subtracting initial, printing output, all across several files in differing directories

Welcome to the Monastery.

"Learning Perl" is a great book to read cover to cover, doing the tasks, reading the chapters in order. Not the best reference to pick out sections from and taking out stuff specific to the task you have at hand. There are better sources of knowledge for this type of search, a great point to start would be the Perl FAQ. Read the FAQ, try to get the bits you need, play around, break stuff, and read "Learning Perl" at your own pace. Make sure that the edition you have is at least the fourth one, and if you have the most recent one (6th edition), take everything you read in Chapter 15 (the one about the smart match operator) with a grain of salt. Some things have changed.

As to your problem, here is a small example that could get you started.

#!/usr/bin/perl use warnings; use strict; my $maxvalue; my $value_at_3k; while (my $line = <DATA>) { my ($time, $value) = split ' ', $line; if ($value > $maxvalue) { $maxvalue = $value; } unless (defined $value_at_3k) { if ($time > 3000) { $value_at_3k = $value; } } } print "Maxvalue was $maxvalue \n"; print "Value at 3k was $value_at_3k \n"; __DATA__ 2999.98 280 2999.99 281 3000.01 285 3000.02 290 3000.03 320 3000.04 350 3000.05 325 3000.06 275 3000.07 270 3000.08 260 3000.09 235

Start by reading the FAQ, then try to change the code so it reads the data from a file (with the filename declared explicitly in the code). Then wrap the code in a subroutine that accepts the filename as a parameter. Then try iterating over a directory - first printing the filenames, then passing them as arguments to your sub. Then wrap it up. Good luck with the task ahead of you.

And it's good to learn. You are very right about this.

- Luke

  • Comment on Re: New to Perl - locating max, subtracting initial, printing output, all across several files in differing directories
  • Download Code

Replies are listed 'Best First'.
Re^2: New to Perl - locating max, subtracting initial, printing output, all across several files in differing directories
by jtrousde (Initiate) on Feb 04, 2015 at 19:09 UTC

    Thanks for the responses! I can sort of see what's going on with your sample code. I assume I'm doing to replace DATA with the directory. Maybe not. I'm trying to make sense of the FAQ at the moment.

    I suppose I actually need to modify my original intentions.

    The data I need to read exists in directories within a parent directory. For example, I have directories 1, 2, 3, ... , 10, all within a parent directory (call it /users/parent/).

    Within those folders is a file (file will always have the same name) with six columns with headers. I'm assuming the $value and $time are the headers of columns within my data file.

    Additionally, I no longer need the difference. What I'd like to do is just pull the maximum value of a specific column within a specific range of column 1 (3000 to 3640), as well as the value in column two corresponding to the time closest to 3000 in column 1. These will be exported as two different columns to another file: max value in column one, "initial" value in column 2.

    Ugh. Perl's notation is so different from what I've seen elsewhere. I feel like I'm drinking from a firehouse for a fairly simple problem.

    EDIT: Working on it, btw. But it will take some time. Thanks for getting me started :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1115546]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2025-12-07 16:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (86 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.