Noob! Help required and would be really appreciated!

Discussion in 'Perl Misc' started by raodeepak, Mar 2, 2006.

  1. raodeepak

    raodeepak Guest


    I'm a noob to perl and programming in general ( Have done basic C
    Programming 4 years ago) but I need some help getting started.

    My pseudo code is this

    Firstly the given file has to be scanned row by row.

    On finding the keyword "New Order", it has to start building a hash.

    Let us say that the program encounters the first "New Order".

    It has to then build a hash and the value of the key is the value of
    the parameter - orderid

    So under this key called orderid we should store the following values
    for example from the text file input;

    cntrClasCod: P
    cntrExpMthDat: 06
    cntrExerPrc: 00043
    cntrVersNo: 0
    ordrQty: +000000000015
    buyCod: B
    ordrExePrc: +0000000000350
    ordrResCod: I
    opnClsCod: O
    acctTypCod: M
    acctTypNo: 1
    userOrdrNum: 123456tttter (THIS IS WHAT THIS ENTIRE GROUP OF DATA
    ordrRmngQty: +000000000000
    ordrExeQty: +000000000015
    ordrExePrc: +0000000000350
    ordrNo: 669382866
    ordrTrnTypId: 050

    FOR EXAMPLE $USERORDRNUM -->123456tttter

    This should be a hash so that for the given datastructure i can call
    this group of data, in this example "123456tttter" and get the
    appropriate values like $ordrExeQty and so on.

    This process should be carried on until it hits the next "New order"
    upon which it has to do the same thing again, only thing that will
    change is the name of the group of data to a different userordrnum and
    the corresponding values to the ones for this group. I could then print
    out this hash to a text file.

    I hope this helps. Maybe this looks confusing but just think of it as
    many instances of the keyword saying "human being". under human being
    there is age, weight, date of birth, name etc. I want to store the
    values and give this group of values the name of the human being. So
    when I say Mike, i should get all your data or whatever i want.

    I know this is a lot to ask for, I've read 60% of learning perl and i
    kinda know what i should do (pattern matching?) but am quite lost. Any
    help would be appreciated

    raodeepak, Mar 2, 2006
    1. Advertisements

  2. wrote in
    Have you read the posting guidelines for this group?

    It looks like you accepted to do a commercial project for which you were
    not qualified, and now you are trying to get someone else do work for you,
    so you can collect a paycheck.

    You are expected to post a short script showing what you have tried, and
    what is not working.

    I strongly recommend reading and following the posting guidelines before
    your next post.

    A. Sinan Unur, Mar 2, 2006
    1. Advertisements

  3. raodeepak

    Rick Scott Guest

    ( uttered:)
    Put this as the first line of your program:


    Seriously, people in this group are here to help, but they won't write
    your whole program for you. Have a go -- break your problem down into
    smaller, simpler parts, solve all of the pieces that you can, and see
    how far you can get.

    If you run into trouble and are truly stumped, then
    1) read and apply the posting guidelines:
    2) post the code you have written so far with your plea for help.

    Be assured that if you have Learning Perl in your hands, you have at
    your disposal all the tools you need to solve this problem.

    Rick Scott, Mar 2, 2006
  4. raodeepak

    DJ Stunks Guest

    you already *have* a text file...

    perhaps the following oneliner will suffice:

    [[email protected] tmp]$ cat < input.file > output.file

    DJ Stunks, Mar 2, 2006
  5. raodeepak

    Rick Scott Guest

    Hey now! Mind keeping that shell stuff out of our Perl newsgroup?


    perl -e 'system q{ cat < input.file > output.file }'

    Rick Scott, Mar 2, 2006
  6. Sorry, but when talking about shell stuff, please bear in mind that Windows
    doesn't have `cat` by default, (unless you installed of course!), so I'd use this:

    perl -pe '1' input.file > output.file

    Andreas Pürzer
    Andreas Puerzer, Mar 2, 2006

  7. if ( /^New Order/ )

    You might also benefit from:

    local $/ = "New Order\n";

    See perlvar.pod for a description of the $/ variable.

    I suggest you change your specification:

    The value after the colon should be entered into a corresponding
    hash key.

    For example $hash{userOrdrNum} = '123456tttter';

    Sounds to me like you need a hash-of-hashes (HoH) data structure.

    That may be difficult as a first programming task...
    Tad McClellan, Mar 2, 2006
  8. raodeepak

    raodeepak Guest

    To the others ahead, im sorry if I offended you, i didnt read the
    guidelines before posting. Im not out to collect a paycheck for work i
    didnt do. Im a mechanical engineering student, and im stuck.Tads post
    helped me to do the following, i thought ill start by filtering the
    text to all relevant data betwen two "New Orders" and saving it to a
    new text file.


    print "Output file name: ";
    chomp($outfilename = <STDIN>); #asks for a output filename

    open (example, "example.log") || die ("Could not open file. $!");
    #opening my log file

    open(OUT,">$outfilename") ||
    die "cannot create $outfilename: $!";

    while (<example>) { # read a line from file IN into $_
    if ( /New Order/ ) {
    print OUT $_; # print that line to file OUT

    This code prints out a text file like this

    [02/28/2006 08:09:00.818666808] New Order
    [02/28/2006 08:09:20.818146290] New Order
    [02/28/2006 08:10:18.376717401] New Order
    [02/28/2006 08:13:25.337482472] New Order
    [02/28/2006 08:18:08.971185035] New Order
    [02/28/2006 08:20:25.923936655] New Order
    [02/28/2006 08:23:46.173082714] New Order
    [02/28/2006 08:31:08.147866876] New Order
    [02/28/2006 08:32:54.214712274] New Order
    [02/28/2006 08:33:16.773162336] New Order
    [02/28/2006 08:34:32.234426181] New Order
    [02/28/2006 08:35:13.893788142] New Order
    [02/28/2006 08:35:31.421244208] New Order
    [02/28/2006 08:36:51.523023548] New Order
    [02/28/2006 08:39:22.164170155] New Order
    [02/28/2006 08:39:48.688776296] New Order
    [02/28/2006 08:43:48.427145817] New Order
    [02/28/2006 08:47:19.699004699] New Order
    [02/28/2006 08:47:19.702569033] New Order
    [02/28/2006 08:47:19.705909449] New Order
    [02/28/2006 08:47:43.090299010] New Order

    Can anyone help me on how to capture the fields between each "New
    Order"? like

    cntrClasCod: P
    cntrExpMthDat: 06
    cntrExerPrc: 00043
    cntrVersNo: 0
    ordrQty: +000000000015
    buyCod: B
    ordrExePrc: +0000000000350
    ordrResCod: I
    opnClsCod: O
    acctTypCod: M
    acctTypNo: 1
    userOrdrNum: 123456tttter
    ordrRmngQty: +000000000000
    ordrExeQty: +000000000015
    ordrExePrc: +0000000000350
    ordrNo: 669382866
    ordrTrnTypId: 050

    I wrote some code which prints out range, but stops at the first "New

    use strict;

    undef $/;
    open (FILE, "example.log") or die "Could not open sample.txt: $!";

    my $file = <FILE>;
    my ($required) =
    ($file =~ m/.*?(New Order.*?ordrTrnTypId).*/s);
    print "$required\n";
    close (FILE) or die "Could not close sample.txt: $!";

    Im unsure of how to combine the two programs to get the required
    output. Sorry again for my previous post.
    raodeepak, Mar 3, 2006
  9. trolled:
    You don't have to read the guidelines. The guidelines are the work
    of a small, but very anal clique, who speaks for only a tiny
    minority of posters in this forum. In any case, never apologize to
    Ronald Matthews, Mar 3, 2006

  10. That is perfectly OK, maybe you did not know about the guidelines.

    However, most of us are expecting that you would go find them
    after having been told about them.

    Did you do that?

    It doesn't look like it, since you haven't turned on warnings and
    strict, nor have you shown us your data.

    We have never seen a data line that matches /New Order/ for example.

    Build a small data file for testing, two or three records with
    two or three fields in each.

    Get your code working with that small data set, *then* modify
    it for your actual application.

    Neither are we likely to help someone collect a grade for
    homework they didn't do.

    That's OK.

    We are willing to _help_ you do your homework.

    We are not willing to do your homework for you.

    Show us what you've tried, and where you are stuck (like you did
    below, but did not do in your initial post).

    The Perl Frequently Asked Questions have an answer for that question:

    perldoc -q between

    How can I pull out lines between two patterns that are themselves on
    different lines?

    But I think the optimum solution to collecting a record's worth
    of data at a time would be to set the $/ special variable
    to an appropriate value.

    For testing purposes here in the newsgroup, please use
    the __DATA__ token to include the file contents within
    the _program_ file, as suggested in the Posting Guidelines.

    Your code will break when the next release of perl introduces
    a new function named example().

    You should choose UPPER CASE filehandles:

    open (EXAMPLE, "example.log") || die ...

    That is not what I had suggested you use.

    Why did you decide to leave out the anchor that I had in my code?

    That will make an output line when:

    $_ = "This in not a New Order, do not match me!\n";

    I assume that is not what you want, but I can't suggest what
    you really want, because we have never seen the data that
    you are trying to process with that code (ie. a line
    containing "New Order").

    [ snip a bunch more lines like that one ]

    Two or three records, rather than 20, ought to be enough.

    Two or three fields, rather than 20, ought to be enough.

    Why are the *two* entries for "userOrdrNum"?

    Is this really what your data looks like?

    That is not the "most appropriate" value for $/ (I think)


    Why did you include that in your pattern?

    That is, what do you think that it will do for you that
    would not be done if you left it out?

    Here is a short and complete program that you can run.

    Maybe it will help.

    use warnings;
    use strict;

    local $/ = "New Order\n";
    while ( <DATA> ) {
    chomp; # removes the New Order\n from the end of the record
    process_record( $_ );

    sub process_record {
    my($record) = @_;

    my %hash = split /[:\n]/, $record; # load fields into a hash

    # Dump the hash contents to see if we got what we wanted.
    # In the "real program", this sub should probably
    # return a reference to %hash rather than just do print()s
    foreach my $key ( sort keys %hash ) {
    print "hash key='$key' hash value='$hash{$key}'\n";
    print "END RECORD\n\n";

    cntrClasCod: A
    New Order
    cntrClasCod: B
    New Order
    cntrClasCod: C
    Tad McClellan, Mar 3, 2006
  11. No, not "most of us." You don't represent "most of us." You
    represent yourself, your mother, and about half a dozen of her cats.
    I guess that is none of your bloody business, isn't it?
    Bugger off, troll. Nobody has to do what an anal little turd like
    you suggests. You don't have a majority of posters behind you. You
    have a tiny clique of anals at best.

    Bugger off.

    cordially, as always,

    Tad McClellan, Mar 3, 2006
  12. That's true. But then why would you want to deliberately reduce your chances
    of someone actually reading your post to virtually nil? Yes, you do have the
    right to shoot yourself in the foot. But it doesn't exactly transpire
    A few years ago there were weekly posting statistics. Too bad that they have
    stopped. Then probably you would have noticed that this "small clique"
    probably accounts for more than 80% of postings.
    Darn, you just got me.

    Jürgen Exner, Mar 3, 2006
  13. raodeepak

    Uri Guttman Guest

    RM> trolled:
    RM> You don't have to read the guidelines. The guidelines are the work
    RM> of a small, but very anal clique, who speaks for only a tiny
    RM> minority of posters in this forum. In any case, never apologize to
    RM> trolls.

    and you obviously haven't read it. you quoted the entire post without
    editing it. but better you didn't even make one comment on his code or
    question. so helpful you non-anal cabal member you! i am sure you won
    over this newbie with your heartfelt comment and advice. notice how he
    responded to you and took your advice. he actually apologized for not
    reading the guidelines, he is posting real code that we can run and
    review. he is getting help from the cabal. so where do you fit in to
    this picture? nowhere it seems. so i say the cabal should take a vote
    and decide to have tony soprano make you sleep wid da fishes.

    Uri Guttman, Mar 3, 2006
  14. raodeepak

    Uri Guttman Guest

    TM> Bugger off, troll. Nobody has to do what an anal little turd like
    TM> you suggests. You don't have a majority of posters behind you. You
    TM> have a tiny clique of anals at best.

    TM> Bugger off.

    TM> cordially, as always,

    TM> rm

    the day you actually help someone with their perl code is the day you
    can make any comments on how this group is run. until then (and likely
    even beyond) you are a known troll and worse. this trival aliasing of
    tad's name (and you even sign it with your initials) is not going to do
    any good. so why waste your time trying? just leave and find some other
    place to annoy.

    Uri Guttman, Mar 3, 2006

  15. Because it doesn't ask questions about Perl that it wants answered.

    I've never seen a post from it here discussing Perl.

    The guidelines are a collaboration of an extensive discussion
    held right here in this newsgroup.

    *Anyone* could, and did, participate.

    We even incorporated some changes that were suggested by a different troll!

    That isn't the rule.

    The rule is:

    Please don't feed the troll!

    Me too.

    We really must do a better job at doing the Right Thing...
    Tad McClellan, Mar 3, 2006
  16. raodeepak

    J?rgen Exner Guest

    Nope. The only people who might avoid your posting are a very small
    clique of anals with questionable advice in any case.
    A few years ago? You managed to chase real posters off, eh? But
    they're back, in numbers, and the anals who posted the weekly
    statistics stopped doing it because their names no longer appeared
    amongst the leaders.

    cordially, as always,

    J?rgen Exner, Mar 3, 2006
  17. raodeepak

    Uri Guttman Guest

    Blah, blah, blah.
    Who is "we?" Where on earth did you get the idea that you speak for
    the small anal clique?
    Um, sorry to dissolve your illusions, but Tony Soprano is fiction.

    So is Santa Claus and the Easter Bunny.

    cordially, as always,

    Uri Guttman, Mar 3, 2006
  18. raodeepak

    Uri Guttman Guest

    This group is run? HAHAHAHAHAHAHA!!!!!!!!!!!!!!!!!!!!!!!!!

    Sorry, sweetie, but if this group was "run" right, you and the other
    small clique wouldn't see your messages posted. You would be "run"
    right out of the group.

    You _don't_ speak for the other anals.

    cordially, as always,

    Uri Guttman, Mar 3, 2006
  19. raodeepak

    Tad McLellan Guest

    An extensive discussion by a bunch of self-important anals who
    somehow think they have the right to dictate to others how to post.
    Real usenet pros would not participate in your Gestapo meeting.
    All your "changes" were suggested by trolls.

    You have absolutely _no_ right to tell others how to post.

    cordially, as always,

    Tad McLellan, Mar 3, 2006
  20. s/

    Is this the new fun on usenet for unpleasant people? Using the names of
    the people they're mindlessly ripping into?

    Is that supposed to be "really showing them!"?

    I don't get it. Oh well, I guess I'll just go back to Perl...

    David H. Adler, Mar 3, 2006
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.