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

Hi EBK,

You are quite close to the solution. Instead of looping over the array, try looping over the range from 1 to $tot.

Code looks like this:

use strict; use warnings; use Data::Dumper qw(Dumper); my $filea = "FileA"; my $fileb = "FileB"; open ( FA, '<', $filea) || die ( "File $filea Not Found!" ); open ( FB, '<', $fileb) || die ( "File $fileb Not Found!" ); my %ts; while ( <FB> ) { chomp; my($ids, $timestamp) = split ","; push @{ $ts{$timestamp} }, $ids; } while ( <FA> ) { chomp; my($life,$timestamp,$cls,$bool,$tot) = split ","; print STDERR "Loop tot-> $tot"; scalar <STDIN>; foreach ( 1 .. $tot ) { my $id = shift @{ $ts{$timestamp} }; print join(",",$life, $id, $cls )."\n"; } }

Using the shift operator ensures that no id is repeated.

Hope this helps!

Jim

Replies are listed 'Best First'.
Re^2: Perl to run 2 files and print the third with loop
by Borodin (Sexton) on Apr 01, 2018 at 16:43 UTC
    There's really no point in copying File B to a one-element hash, in fact it can be read line by line.

      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

      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.
Re^2: Perl to run 2 files and print the third with loop
by EBK (Sexton) on Apr 01, 2018 at 21:45 UTC
    It's worked. It is exactly I wanted. How does the shift operator work?

      shift or  perldoc -f shift from your command line. (Update: All Perl built-ins like shift are documented in perlfunc.)


      Give a man a fish:  <%-{-{-{-<

        G'day AnomalousMonk,

        The information you provided is quite correct and I have upvoted your post.

        However, for generally looking up functions, I'd recommend another online, perldoc page: "Perl functions A-Z" (which provides an index to Perl functions).

        The perlfunc page is huge: in most cases, you'll download many orders of magnitude more information than needed. Also, unless you're familiar with that page, navigation is not that easy. Having said that, perlfunc is sometimes exactly what you need.

        As an example of those pros and cons, consider the endpwent function. If you look under the "Alphabetical Listing of Perl Functions" section, you need to know that listing is not truly alphabetical and this order exists: getpwent ... setpwent ... endpwent. However, the information under the list of get* ... set* ... end* entries is probably what you'll want: compare with "endpwent", from the A-Z Index, which has no information at all except for the name of the function.

        On the other hand, using the A-Z Index will locate functions with just a mouse click or two. It does have the occasional limitation, as described above; however, in the main, it has the same information as perlfunc, and is faster and easier to use.

        The perlfunc page certainly has other information but, for simply looking up a function (such as shift) the documentation is identical to that found via the A-Z Index.

        — Ken