Search for the new guy

Discussion in 'Perl Misc' started by jld8996, Jan 14, 2004.

  1. jld8996

    jld8996 Guest

    Hi All,
    I just started learning Perl with Sams Teach Yourself Perl in 24
    hours. I have been able to accomplish part of what I wanted to do but
    need a little more help. I have 2 files, the first is a list of
    element numbers and the 2nd is a large text file (a .f06 file from
    NASTRAN for those who are familiar). I started writing a script shown
    below to open both files. What I need to do is step through the first
    file element by element and look up each element in the 2nd file.
    Once found I want to write the line where it is found and the next
    line to an output file. Then find the next element of file 1 and so
    on. Any help or guidance would be great. Thanks, Jeremy

    Here is my script so far:
    -----------------------
    #!/usr/bin/perl

    print "Enter the name of the element control file: \n";
    $control=<STDIN>;
    open(INPUT,$control) || die "$!";
    @input=<INPUT>;
    close(INPUT);

    print "Enter the name of the .f06 file: \n";
    $f06=<STDIN>;
    open(STRESS,$f06) || die "$!";
    @stresses=<STRESS>;
    close(STRESS);

    print "Enter the name of the output file: \n";
    $output=<STDIN>;
    open(OUTPUT,">$output") || die "$!";

    -------------------------------------------
    so I've only done the easy stuff. I read all of the .f06 file into
    the array "stresses". I'm not sure if this was necessary. The actual
    file is over 300,000 lines long so this might be slowing me down.

    Here's is a sample of file 1, the element file:

    -------------------------------------------
    119471
    119472
    119473
    119474

    --------------------------------------------

    And here is a piece of file 2, the .f06 file:

    --------------------------------------------

    1 MSC.NASTRAN JOB CREATED ON 19-SEP-03 AT 15:32:39
    W145
    0

    S T R E S S E S I N Q U A D R I L A T E R
    A
    ELEMENT FIBER STRESSES IN ELEMENT COORD SYSTEM
    ID. DISTANCE NORMAL-X NORMAL-Y SHEAR-XY
    0 119461 -6.250000E-02 -2.809423E+02 -2.226965E+03
    7.288095E+02
    6.250000E-02 -2.154822E+02 -2.395907E+03
    5.431436E+02
    0 119462 -6.250000E-02 9.256976E+02 -2.722196E+03
    1.075280E+03
    6.250000E-02 9.582150E+02 -3.030506E+03
    1.060233E+03
    0 119463 -6.250000E-02 -5.176458E+01 1.040512E+03
    -9.355118E+01
    6.250000E-02 -2.901073E+01 1.011089E+03
    -2.422135E+01
    0 119464 -6.250000E-02 -1.102965E+02 1.167524E+03
    5.211899E+01
    6.250000E-02 -1.033523E+02 1.100544E+03
    1.217817E+02
    0 119465 -6.250000E-02 8.531695E+02 3.763257E+02
    8.413060E+02
    6.250000E-02 7.949519E+02 8.895544E+01
    8.487442E+02
    0 119466 -6.250000E-02 2.496463E+02 1.557864E+03
    7.741991E+02
    6.250000E-02 1.736531E+02 1.307298E+03
    8.175496E+02
    0 119467 -6.250000E-02 -1.153684E+02 1.296065E+03
    3.091736E+02
    6.250000E-02 -1.198170E+02 1.147716E+03
    3.649098E+02
    0 119468 -6.250000E-02 1.097371E+03 3.642865E+03
    1.958549E+03
    6.250000E-02 1.037483E+03 3.431623E+03
    1.997865E+03
    0 119469 -6.250000E-02 -5.984709E+01 1.383306E+03
    1.489364E+02
    6.250000E-02 -5.029673E+01 1.210617E+03
    1.580537E+02
    0 119470 -6.250000E-02 1.908398E+02 1.890122E+03
    3.743426E+02
    6.250000E-02 1.284111E+02 1.673722E+03
    3.498486E+02
    0 119471 -1.900000E-01 5.097551E+02 1.409135E+02
    -2.752976E+01
    1.900000E-01 -1.391517E+03 -3.814824E+02
    -3.767153E+02
    0 119472 -1.900000E-01 -7.821536E+01 1.430543E+03
    -4.919512E+02
    1.900000E-01 3.173092E+02 1.755130E+03
    -6.183864E+02
    0 119473 -1.900000E-01 -4.190353E+02 -9.498234E+02
    -3.387274E+02
    1.900000E-01 1.031396E+02 2.249585E+02
    -2.015406E+02
    0 119474 -1.900000E-01 -6.918803E+01 -1.046147E+03
    -7.935986E+02
    1.900000E-01 7.496266E+02 8.220536E+02
    -1.732447E+02
    0 119475 -1.900000E-01 -9.534775E+02 -2.848530E+03
    -1.332869E+02
    1.900000E-01 1.081157E+03 3.724546E+03
    4.091841E+02
    0 119476 -1.900000E-01 2.104021E+01 -2.328312E+03
    -4.683439E+02
    1.900000E-01 -3.702834E+01 -3.805241E+02
    -1.132028E+02
    --------------------------------------------
    So I want to find "0 119471" and write that line plus the next to my
    output file. Then move onto 119472, etc.
    jld8996, Jan 14, 2004
    #1
    1. Advertising

  2. jld8996 wrote:
    > I just started learning Perl with Sams Teach Yourself Perl in 24
    > hours. I have been able to accomplish part of what I wanted to do
    > but need a little more help.


    To be honest, you haven't convinced me that you need any help at all.
    All you seem to need is some patience.

    For instance, you may want to study how to write a while loop
    (http://www.perldoc.com/perl5.8.0/pod/perlsyn.html), and the index()
    function (http://www.perldoc.com/perl5.8.0/pod/func/index.html) may
    come in handy.

    > What I need to do is step through the first file element by element
    > and look up each element in the 2nd file. Once found I want to
    > write the line where it is found and the next line to an output
    > file. Then find the next element of file 1 and so on.


    So, why don't you start working with a program that does that?

    > Here is my script so far:
    > -----------------------
    > #!/usr/bin/perl


    Before going on, please learn how to write programs with strictures
    and warnings enabled.

    use strict;
    use warnings;

    > I read all of the .f06 file into the array "stresses". I'm not
    > sure if this was necessary.


    I'm sure it wasn't.

    My suggestion is that you give it a _serious_ try. You are welcome
    back here if you would encounter any problem that you are not able to
    resolve by help of the Perl documentation.

    Good luck!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jan 14, 2004
    #2
    1. Advertising

  3. jld8996 <> wrote:


    > Subject: Search for the new guy



    Please put the subject of your article in the Subject of your article.


    > I just started learning Perl with Sams Teach Yourself Perl in 24
    > hours.



    That is a really bad Perl book you know. Sell it on E-bay and
    get a good one before you pick up bad habits...


    > Here is my script so far:
    > -----------------------
    > #!/usr/bin/perl



    It is insulting to be asked to do a machine's work.

    Please don't ask us to do that for you, ask the machine before
    asking people.

    You should put these at the top of every Perl program that you write:

    use strict;
    use warnings;

    You can see the docs for what those do with perldoc, eg:

    perldoc strict


    > $control=<STDIN>;
    > open(INPUT,$control) || die "$!";



    Is the filename supposed to have a newline on the end of it, or
    did you just forget to chomp() it after reading?

    Did the Sam's book teach you to use useless double quotes like that?

    perldoc -q vars

    What's wrong with always quoting "$vars"?



    > so I've only done the easy stuff.



    What part, specifically, are you having trouble with?

    Getting someone to help you with your code here is pretty easy.

    Getting someone to write your program for you will prove a
    bit more difficult...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 14, 2004
    #3
  4. On Wed, 14 Jan 2004, Tad McClellan wrote:

    > That is a really bad Perl book you know. Sell it on E-bay


    If it's that bad, surely hanging it in the smallest room would be a
    better gift to humanity...?
    Alan J. Flavell, Jan 15, 2004
    #4
  5. jld8996

    Bob Walton Guest

    jld8996 wrote:

    ....
    > I just started learning Perl with Sams Teach Yourself Perl in 24
    > hours. I have been able to accomplish part of what I wanted to do but



    Yuck. Try the book "Learning Perl", O'Reilly & Associates, Inc.


    > need a little more help. I have 2 files, the first is a list of
    > element numbers and the 2nd is a large text file (a .f06 file from
    > NASTRAN for those who are familiar). I started writing a script shown
    > below to open both files. What I need to do is step through the first
    > file element by element and look up each element in the 2nd file.
    > Once found I want to write the line where it is found and the next
    > line to an output file. Then find the next element of file 1 and so
    > on. Any help or guidance would be great. Thanks, Jeremy

    ....
    > so I've only done the easy stuff. I read all of the .f06 file into
    > the array "stresses". I'm not sure if this was necessary. The actual
    > file is over 300,000 lines long so this might be slowing me down.



    You will probably want to do a bit of preliminary parsing of the .f06
    file as you read it in. For example, skip to the section headings of
    sections that actually have stuff in them you want to retrieve with this
    program. It probably *is* a good idea to keep the data you want to
    search through in memory, as it looks like you will be doing the
    searches a large number of times. You should read through the .f06 file
    once, parsing it for element numbers, and, every time an element number
    is found, see if it appears in your "element file" (i.e., keep the
    elements identified in the element file in memory, rather than the stuff
    in the .f06 file). That would reduce the memory requirements and make
    everything faster. A Perl hash would be the perfect way to store the
    contents of your "element file" -- every time an element ID is located
    in the .f06 file, you look it up in the hash to see if it is one of the
    elements you want to output.

    You will also need to parse the .f06 file enough to be able to
    disinguish which number strings are element numbers and which are
    something else. In your sample, you could include the space characters
    before and after the element number in your match, which would prevent
    you from including lines with something like "2.119471E+02" when you're
    looking for element 119471. But that probably isn't sufficient to catch
    every string that could be confused with an element number. There are
    lots of potential output sections with Nastran (which I am familiar
    with), so you might have a lot of different parsing rules to do it
    right. Good luck.


    ....

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Jan 15, 2004
    #5
  6. Alan J. Flavell <> wrote:
    > On Wed, 14 Jan 2004, Tad McClellan wrote:
    >
    >> That is a really bad Perl book you know. Sell it on E-bay

    >
    > If it's that bad, surely hanging it in the smallest room would be a
    > better gift to humanity...?



    Too tough and inky to touch _my_ "humanity", so I wouldn't
    suggest it to others. :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 15, 2004
    #6
  7. jld8996

    jld8996 Guest

    Many thanks to Bob for his help. With his help I was able to complete
    my script. Many thanks to Gunnar and Tad for well...nothing. Thanks
    for taking time out of what must be your very busy day to reply to my
    message with your criticism.
    jld8996, Jan 19, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VG9t?=

    new guy needs some help

    =?Utf-8?B?VG9t?=, May 25, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    303
    =?Utf-8?B?RGVtZXRyaQ==?=
    May 25, 2006
  2. tom c
    Replies:
    2
    Views:
    521
    Rick Strahl
    Jul 9, 2006
  3. Replies:
    2
    Views:
    478
  4. Macon Joe Job Guy Joe Macon Job Guy

    Att: Macon Joe Job Guy Joe Macon Job Guy Macon

    Macon Joe Job Guy Joe Macon Job Guy, Oct 14, 2007, in forum: Java
    Replies:
    0
    Views:
    370
    Macon Joe Job Guy Joe Macon Job Guy
    Oct 14, 2007
  5. Replies:
    1
    Views:
    211
    Rectal Mania
    Aug 6, 2007
Loading...

Share This Page