Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Perl to run 2 files and print the third with loop

by Borodin (Sexton)
on Apr 01, 2018 at 16:43 UTC ( [id://1212105]=note: print w/replies, xml ) Need Help??


in reply to Re: Perl to run 2 files and print the third with loop
in thread Perl to run 2 files and print the third with loop

There's really no point in copying File B to a one-element hash, in fact it can be read line by line.
  • Comment on Re^2: Perl to run 2 files and print the third with loop

Replies are listed 'Best First'.
Re^3: Perl to run 2 files and print the third with loop
by jimpudar (Pilgrim) on Apr 01, 2018 at 23:51 UTC

    Yeah, I was thinking the same thing but I figured the smaller change would be easier to digest. However, it wouldn't be a one element hash, as I assume there will be more timestamps and we only have the beginning of the data.

    EBK, if your File B ends up being much larger (say a few 10s of GB) you would want to put the first loop inside the second one so that you can read it in line by line and not waste RAM by putting the data in the hash. However for such a small file like 800 lines I don't think it is worth the effort.

    Also, with this solution as someone else pointed out, if the totals are larger than the actual amount of lines of data, this will cause an undef $id and lots of warnings. I am assuming that the two files are consistent with each other.

    Jim

Re^3: Perl to run 2 files and print the third with loop
by jimpudar (Pilgrim) on Apr 02, 2018 at 06:35 UTC

    Was thinking about this a bit, and it seems based on the data we have been given by EBK, that the timestamps really don't matter at all. Perhaps if we could see the entirety of File B, this would be cleared up.

    That being said, here is a more simple (or perhaps more cryptic) solution which doesn't rely on storing all of File B in memory:

    #!/usr/bin/perl -wlaF, BEGIN { open FH, '<', 'FileB' } print "Loop tot-> $F[4]"; for ( 1 .. $F[4] ) { @G = split ',', <FH>; print join ',', $F[0], $G[0], $F[2]; }

    If you save this script as 'run.pl', you can call it like so:

    chmod 740 run.pl ./run.pl <FileA

    The -a option turns on autosplit mode and the -F, option sets the field delimiter to a comma.

    EBK, I'm curious to know whether this will work for you. Please let me know!

    Best,

    Jim

      Hi @jimpudar, I will do it tonight and I let you know the result.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (9)
As of 2024-04-18 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found