Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

G'day lallison,

Welcome to the monastery.

This code does what you describe as being wanted:

$ perl -Mstrict -Mwarnings -e ' use autodie; use Tie::File; my $re = qr{^((\d+).+$)}s; my %data_for_part; open my $f1, "<", "pm_1045452_file1.txt"; while (<$f1>) { /$re/; $data_for_part{$2} = $1; } close $f1; tie my @file2, "Tie::File", "pm_1045452_file2.txt"; print $data_for_part{$_} for @file2; untie @file2; ' 3478749:AA:1D:AAA:DescriptionsY:C:2 3633731:AA:3E:AAA:DescriptionsZ:C:2

I made a minor change to "File1" to show different Descriptions:

$ cat pm_1045452_file1.txt 3478748:AA:1D:AAA:DescriptionsX:C:2 3478749:AA:1D:AAA:DescriptionsY:C:2 3633731:AA:3E:AAA:DescriptionsZ:C:2

"File2" data is as you show it:

$ cat pm_1045452_file2.txt 3478749 3633731

Notes:

  • You don't need to chomp any input nor add any newlines to the output.
  • There's no temporary arrays to process.
  • Tie::File comes standard with Perl: you won't need to install it.
"This file has over a million lines and is running very slow."

Given that you've been provided with a number of solutions, use Benchmark to determine which works best for you. (That module also comes standard with Perl.)

[Aside: The code you posted is difficult to read due to the <code> tag issue. You appear to have made an effort but were unsuccessful: see Writeup Formatting Tips for how, where and why to do it.]

-- Ken


In reply to Re: extract line by kcott
in thread extract line by lallison

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [atcroft]: james28909: What about October 5, 1582?
    [stevieb]: atcroft: "Make both hands into fists..."... is something my Ma taught me in our native lang, but I was to ignorant and young to pay attention. Thanks for that :)
    [atcroft]: stevieb: Sad to say that I only recently learned that particular trick, but I have since found it very useful.... :)
    [james28909]: ill be back with a solution eventually
    [stevieb]: it's a reminder to re-inforce it :P
    [atcroft]: james28909: That particular questions was a bit of trick, actually (depending on the country you are in). More interesting is, if you are trying to subtract from an epoch time, for instance, you might have to consider when/if DST occurs for a location,
    [atcroft]: because you may have to adjust the number of seconds you change from an epoch from 86400 (not to mention leap seconds)....
    [atcroft]: james28909: Although if your program is using a database, you might be able to "pass the buck" to the database and ask it to do the date change for you....

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (3)
    As of 2017-04-29 04:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      I'm a fool:











      Results (531 votes). Check out past polls.