Noob! Help required and would be really appreciated!

R

raodeepak

Hi,

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;

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

PLEASE NOTE THAT THE VALUE AFTER THE COLON SHOULD BE SCANNED AND
ENTERED INTO THE CORRESPONDING VARIABLE.
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

Thanks
Deepak
 
A

A. Sinan Unur

(e-mail address removed) wrote in @e56g2000cwe.googlegroups.com:
Hi,

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.

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.

Sinan
 
R

Rick Scott

([email protected] uttered:)
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.

Put this as the first line of your program:

#!/usr/bin/perl

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:
http://mail.augustmail.com/~tadmc/clpmisc.shtml
2) post the code you have written so far with your plea for help.

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

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
 
D

DJ Stunks

So under this key called orderid we should store the following values
for example from the text file input;
I could then print out this hash to a text file.

you already *have* a text file...

perhaps the following oneliner will suffice:

[jpeavy1@localhost tmp]$ cat < input.file > output.file

-jp
 
R

Rick Scott

(DJ Stunks said:
So under this key called orderid we should store the following
values for example from the text file input;
I could then print out this hash to a text file.

you already *have* a text file...

perhaps the following oneliner will suffice:

[jpeavy1@localhost tmp]$ cat < input.file > output.file

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

*ahem*

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




=)
Rick
 
A

Andreas Puerzer

Rick said:
(DJ Stunks said:
perhaps the following oneliner will suffice:

[jpeavy1@localhost tmp]$ cat < input.file > output.file


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

*ahem*

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

Sorry, but when talking about shell stuff, please bear in mind that Windows
doesn't have `cat` by default, (unless you installed
http://search.cpan.org/~cwest/ppt-0.14 of course!), so I'd use this:

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

Greetings,
Andreas Pürzer
 
T

Tad McClellan

My pseudo code is this

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


On finding the keyword "New Order",


if ( /^New Order/ )

You might also benefit from:

local $/ = "New Order\n";

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

it has to start building a hash.
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;

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

PLEASE NOTE THAT THE VALUE AFTER THE COLON SHOULD BE SCANNED AND
ENTERED INTO THE CORRESPONDING VARIABLE.


I suggest you change your specification:

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

FOR EXAMPLE $USERORDRNUM -->123456tttter


For example $hash{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.


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

That may be difficult as a first programming task...
 
R

raodeepak

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.

#!/usr/local/bin/perl

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
}
}
close(example);
close(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

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

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

#!/usr/bin/perl
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.
 
R

Ronald Matthews

(e-mail address removed) trolled:
To the others ahead, im sorry if I offended you, i didnt read the
guidelines before posting.

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
trolls.
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
}
}
close(example);
close(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
prodId:DDD
cntrClasCod: P
cntrExpYrDat:2006
cntrExpMthDat: 06
cntrExerPrc: 00043
cntrVersNo: 0
ordrQty: +000000000015
buyCod: B
ordrExePrc: +0000000000350
ordrResCod: I
opnClsCod: O
ordrExpDat:
acctTypCod: M
acctTypNo: 1
userOrdrNum: 123456tttter
ordrRmngQty: +000000000000
ordrExeQty: +000000000015
ordrExePrc: +0000000000350
ordrNo: 669382866
ordrTrnTypId: 050
userOrdrNum:123456tttter
I wrote some code which prints out range, but stops at the first "New
Order"
#!/usr/bin/perl
use strict;
undef $/;
open (FILE, "example.log") or die "Could not open sample.txt: $!";
 
T

Tad McClellan

To the others ahead, im sorry if I offended you, i didnt read the
guidelines before posting.


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.

Im not out to collect a paycheck for work i
didnt do. Im a mechanical engineering student,


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

and im stuck.


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).

Tads post
helped me to do the following,

Good.


i thought ill start by filtering the
text to all relevant data betwen two "New Orders" and saving it to a
new text file.


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.

#!/usr/local/bin/perl

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


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.

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


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 ...

if ( /New Order/ ) {


That is not what I had suggested you use.

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

print OUT $_; # print that line to file OUT


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").

This code prints out a text file like this

[02/28/2006 08:09:00.818666808] New Order

[ snip a bunch more lines like that one ]

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

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

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


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?

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

#!/usr/bin/perl
use strict;

undef $/;


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

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

my $file = <FILE>;
my ($required) =
($file =~ m/.*?(New Order.*?ordrTrnTypId).*/s);
^^^
^^^

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?

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.


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

Maybe it will help.

----------------------------------
#!/usr/bin/perl
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";
}

__DATA__
prodId:AAA
cntrClasCod: A
userOrdrNum:123456AAAAAA
New Order
prodId:BBB
cntrClasCod: B
userOrdrNum:123456BBBBBB
New Order
prodId:CCC
cntrClasCod: C
userOrdrNum:123456CCCCCC
 
T

Tad McClellan

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.

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.
Did you do that?

I guess that is none of your bloody business, isn't it?
It doesn't look like it, since you haven't turned on warnings and
strict, nor have you shown us your data.

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,

rm
 
J

Jürgen Exner

Ronald said:
(e-mail address removed) trolled:

You don't have to read the guidelines.

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
smartness.
The guidelines are the work
of a small, but very anal clique, who speaks for only a tiny
minority of posters in this forum.

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.
In any case, never apologize to
trolls.

Darn, you just got me.

jue
 
U

Uri Guttman

RM> (e-mail address removed) 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
 
U

Uri Guttman

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
 
T

Tad McClellan

Jürgen Exner said:
That's true. But then why would you want to deliberately reduce your chances
of someone actually reading your post to virtually nil?


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!

Darn, you just got me.


Me too.

We really must do a better job at doing the Right Thing...
 
J

J?rgen Exner

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 smartness.

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 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.

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.
Darn, you just got me.

Yep.

cordially, as always,

rm
 
U

Uri Guttman

Uri Guttman said:
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

Blah, blah, blah.
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

Who is "we?" Where on earth did you get the idea that you speak for
the small anal clique?
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.

Um, sorry to dissolve your illusions, but Tony Soprano is fiction.

So is Santa Claus and the Easter Bunny.

cordially, as always,

rm
 
U

Uri Guttman

Uri Guttman said:
the day you actually help someone with their perl code is the day you
can make any comments on how this group is run.

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,

rm
 
T

Tad McLellan

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

An extensive discussion by a bunch of self-important anals who
somehow think they have the right to dictate to others how to post.
*Anyone* could, and did, participate.

Real usenet pros would not participate in your Gestapo meeting.
We even incorporated some changes that were suggested by a
different troll!

All your "changes" were suggested by trolls.

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

cordially, as always,

rm
 
D

David H. Adler

Tad McClellan <[email protected]> trolled:

s/augustmail.com/RReett.org/

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...

dha
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,008
Messages
2,570,268
Members
46,867
Latest member
Lonny Petersen

Latest Threads

Top