Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Noob! Help required and would be really appreciated!

Reply
Thread Tools

Noob! Help required and would be really appreciated!

 
 
raodeepak@gmail.com
Guest
Posts: n/a
 
      03-02-2006
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;

prodIdDD
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

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-02-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:1141272444.478004.119970
@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

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
 
 
 
Rick Scott
Guest
Posts: n/a
 
      03-02-2006
((E-Mail Removed) 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
--
key CF8F8A75 / print C5C1 F87D 5056 D2C0 D5CE D58F 970F 04D1 CF8F 8A75
PROJECTILE, n. The final arbiter in international disputes.
:Ambrose Bierce, "The Devil's Dictionary" (1911)
 
Reply With Quote
 
DJ Stunks
Guest
Posts: n/a
 
      03-02-2006

(E-Mail Removed) wrote:

> 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

 
Reply With Quote
 
Rick Scott
Guest
Posts: n/a
 
      03-02-2006
(DJ Stunks <(E-Mail Removed)> uttered
> (E-Mail Removed) wrote:
>
> > 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
--
key CF8F8A75 / print C5C1 F87D 5056 D2C0 D5CE D58F 970F 04D1 CF8F 8A75
The Perl motto is "There's more than one way to do it."
Divining how many more is left as an exercise to the reader.
erl.pod
 
Reply With Quote
 
Andreas Puerzer
Guest
Posts: n/a
 
      03-02-2006
Rick Scott schrieb:
> (DJ Stunks <(E-Mail Removed)> uttered
>>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

--
Have Fun,
and if you can't have fun,
have someone else's fun.
The Beautiful South
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-02-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

> My pseudo code is this
>
> Firstly the given file has to be scanned row by row.



while ( <GIVEN_FILE> )


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


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
raodeepak@gmail.com
Guest
Posts: n/a
 
      03-03-2006
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

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

 
Reply With Quote
 
Ronald Matthews
Guest
Posts: n/a
 
      03-03-2006
(E-Mail 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.


> #!/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


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


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-03-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

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


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
an oddball scary kind of thing you would think would never happen richard Computer Support 4 01-31-2010 06:34 PM
I really need help with this so if anyone can help me out that wouldbe really great of you. adityagaddam90@gmail.com Java 30 02-10-2008 11:29 PM
OT : But help really really needed re: Domain Name selling, hosting etc. problem nc HTML 1 02-03-2005 07:24 PM
What I would LOVE to see on dvd really soon... Adrian DVD Video 0 08-30-2004 09:50 PM
No, really: What would I miss (of value) Frank ess Digital Photography 5 05-10-2004 11:21 PM



Advertisments