[23443] in Perl-Users-Digest
Perl-Users Digest, Issue: 5658 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 14 14:06:04 2003
Date: Tue, 14 Oct 2003 11:05:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 14 Oct 2003 Volume: 10 Number: 5658
Today's topics:
Appending to columns (Chandramohan Neelakantan)
Re: Appending to columns <some@one.com>
check if a string ends with particular letters <jjliu@earthlink.net>
Re: check if a string ends with particular letters <HelgiBriem_1@hotmail.com>
Re: check if a string ends with particular letters <abigail@abigail.nl>
Re: check if a string ends with particular letters <syscjm@gwu.edu>
Re: check if a string ends with particular letters <some@one.com>
drawing a tree (Alythh)
Re: drawing a tree <jwillmore@remove.adelphia.net>
Re: How does one move down a line in a file? <REMOVEsdnCAPS@comcast.net>
Re: How does one move down a line in a file? <abigail@abigail.nl>
Re: How does one move down a line in a file? (Tad McClellan)
Re: How does one move down a line in a file? (Anno Siegel)
Re: How does one move down a line in a file? (Anno Siegel)
Re: How does one move down a line in a file? <jwillmore@remove.adelphia.net>
Re: How does one move down a line in a file? (Anno Siegel)
Re: How does one move down a line in a file? <xx087@freenet.carleton.ca>
Re: How to update entries in a file <no@spam.here>
Re: How to update entries in a file <no@spam.here>
Re: How to update entries in a file (Randal L. Schwartz)
Re: How to update entries in a file (Roy Johnson)
Re: How to update entries in a file <jwillmore@remove.adelphia.net>
Re: How to update entries in a file (Roy Johnson)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 14 Oct 2003 08:39:18 -0700
From: knchandramohan@yahoo.com (Chandramohan Neelakantan)
Subject: Appending to columns
Message-Id: <69d11688.0310140739.7b1ecee0@posting.google.com>
hello
The following is the code Ive been using to parse hardware desing
rules from a ASCII file based on ideas from Gunnar Hjalmarsson and
Matija Papec.
******************************************************************
# A sample rules page is in $contents
$contents = "10.01 Width of n+buried layer
3.6
1, R1
10.02 Enclosure of oxide isolation (L35)
0.3 all devices,
1, R7
except power npn
CEBEC
10.03 Distance to scribe line (inside DIE AREA)
20.0
1, R2
Note: N+ buried layer is NOT allowed in scribe line
areas.";
# Begin Parsing
while ( $contents =~
/(\d+(?:\.\d+)+)
\s+
([\w\-\s]+[\w\-]\s{1,3}(.*?)) # Description
\s+
(\d+.*?\s{3}) # Dimensions
\s{1,3}
(.*?) # Devices
\s{6,}
(\d,\s[A-Z]\d) # Fig
/gix)
{
print $1,"\n"; # Rule No
print $2,"\n"; # Description
print $4,"\n"; # Dimension
print $5,"\n"; # Apllicable Devices
print $6,"\n"; # Figure
$contents = $';
$contents =~ /(.*?)(?=\d+(?:\.\d+)+|$)/sg;
$contents = $';
print "******************************************************************\n";
}
******************************************************************
Each rule has
1. A Rule Number
2. A Rule description ( Additional note to this column *may* appear in
subsequent lines)
3. Dimension info
4. Applicable devices ( may be blank or words for this column could
appear in subsequent lines
5. Figure
There are many instances where it is difficult to associate text to
its appropriate column.
For example,
******************************************************************
10.02 Enclosure of oxide isolation (L35)
0.3 all devices,
1, R7
except power npn
CEBEC
******************************************************************
is a rule where the Applicable devices column entry is 'all devices
except power npn CEBEC'. Since the entry is spanned over a few lines I
find it difficult to associate it with the appropriate column. Also ,
the entry for this column may be empty
Here is another example:
******************************************************************
10.03 Distance to scribe line (inside DIE AREA)
20.0
1, R2
Note: N+ buried layer is NOT allowed in scribe line
areas.
******************************************************************
The Note actually belongs to the Description column.
I need to find a way in which I can attach each of the text
appearing in subsequent lines of a rule to its subsequent collumn.
PLease note that there may or may not be Notes and Applicable devices
column entry to each rule .
I think the code mentioned above is mostly right. I need help in
attching the text to its appropriate column.
I have uploaded a copy of a sample text file here
http://geocities.com/knchandramohan/1920.zip
Help appreciated !
MAny thanks
CM
------------------------------
Date: Tue, 14 Oct 2003 17:53:29 GMT
From: Anand <some@one.com>
Subject: Re: Appending to columns
Message-Id: <tKWib.1828$8x2.1038138@newssrv26.news.prodigy.com>
I don't know if this is what you want .. you can use 'format' for
customized output.
--Anand
Chandramohan Neelakantan wrote:
> hello
> The following is the code Ive been using to parse hardware desing
> rules from a ASCII file based on ideas from Gunnar Hjalmarsson and
> Matija Papec.
>
>
>
>
> ******************************************************************
>
>
> # A sample rules page is in $contents
>
> $contents = "10.01 Width of n+buried layer
> 3.6
> 1, R1
>
>
> 10.02 Enclosure of oxide isolation (L35)
> 0.3 all devices,
> 1, R7
>
> except power npn
>
> CEBEC
>
>
> 10.03 Distance to scribe line (inside DIE AREA)
> 20.0
> 1, R2
>
> Note: N+ buried layer is NOT allowed in scribe line
> areas.";
>
>
>
> # Begin Parsing
>
> while ( $contents =~
> /(\d+(?:\.\d+)+)
> \s+
> ([\w\-\s]+[\w\-]\s{1,3}(.*?)) # Description
> \s+
> (\d+.*?\s{3}) # Dimensions
> \s{1,3}
> (.*?) # Devices
> \s{6,}
> (\d,\s[A-Z]\d) # Fig
>
> /gix)
>
>
>
>
>
>
> {
>
>
> print $1,"\n"; # Rule No
> print $2,"\n"; # Description
> print $4,"\n"; # Dimension
> print $5,"\n"; # Apllicable Devices
> print $6,"\n"; # Figure
>
> $contents = $';
>
>
> $contents =~ /(.*?)(?=\d+(?:\.\d+)+|$)/sg;
> $contents = $';
>
>
>
>
>
>
> print "******************************************************************\n";
> }
>
> ******************************************************************
>
>
>
>
> Each rule has
>
> 1. A Rule Number
> 2. A Rule description ( Additional note to this column *may* appear in
> subsequent lines)
> 3. Dimension info
> 4. Applicable devices ( may be blank or words for this column could
> appear in subsequent lines
> 5. Figure
>
>
>
> There are many instances where it is difficult to associate text to
> its appropriate column.
>
> For example,
>
> ******************************************************************
> 10.02 Enclosure of oxide isolation (L35)
> 0.3 all devices,
> 1, R7
>
> except power npn
>
> CEBEC
> ******************************************************************
>
> is a rule where the Applicable devices column entry is 'all devices
> except power npn CEBEC'. Since the entry is spanned over a few lines I
> find it difficult to associate it with the appropriate column. Also ,
> the entry for this column may be empty
>
>
> Here is another example:
>
>
> ******************************************************************
>
> 10.03 Distance to scribe line (inside DIE AREA)
> 20.0
> 1, R2
>
> Note: N+ buried layer is NOT allowed in scribe line
> areas.
>
> ******************************************************************
>
> The Note actually belongs to the Description column.
>
>
> I need to find a way in which I can attach each of the text
> appearing in subsequent lines of a rule to its subsequent collumn.
> PLease note that there may or may not be Notes and Applicable devices
> column entry to each rule .
>
>
> I think the code mentioned above is mostly right. I need help in
> attching the text to its appropriate column.
>
> I have uploaded a copy of a sample text file here
> http://geocities.com/knchandramohan/1920.zip
>
>
> Help appreciated !
>
> MAny thanks
> CM
------------------------------
Date: Tue, 14 Oct 2003 15:47:16 GMT
From: "jjliu" <jjliu@earthlink.net>
Subject: check if a string ends with particular letters
Message-Id: <8UUib.55437$mQ2.44123@newsread1.news.atl.earthlink.net>
Could someone tell me how to check if a string ends with particular letters.
For example, i want to check if the following string is ending with gif:
"htt://www.mypage.com/myimage.gif"
Thanks a lot for your help.
------------------------------
Date: Tue, 14 Oct 2003 16:17:16 +0000
From: Helgi Briem <HelgiBriem_1@hotmail.com>
Subject: Re: check if a string ends with particular letters
Message-Id: <b68oovgqkdrvquqlcrucbgm3bhkmcotaou@4ax.com>
On Tue, 14 Oct 2003 15:47:16 GMT, "jjliu" <jjliu@earthlink.net> wrote:
>Could someone tell me how to check if a string ends with particular letters.
>For example, i want to check if the following string is ending with gif:
>
>"htt://www.mypage.com/myimage.gif"
$_ = "htt://www.mypage.com/myimage.gif";
if (/gif$/) { print "$_ ends with gif\n"; }
else { print "$_ doesn't end with gif\n"}
But shouldn't it really be .gif or .GIF?
if (/(\.gif)$/i) { print "$_ ends with$1\n"; }
else { print "$_ doesn't end with .gif\n"}
------------------------------
Date: 14 Oct 2003 16:11:44 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: check if a string ends with particular letters
Message-Id: <slrnboo820.lac.abigail@alexandra.abigail.nl>
jjliu (jjliu@earthlink.net) wrote on MMMDCXCVI September MCMXCIII in
<URL:news:8UUib.55437$mQ2.44123@newsread1.news.atl.earthlink.net>:
{} Could someone tell me how to check if a string ends with particular letters.
{} For example, i want to check if the following string is ending with gif:
{}
{} "htt://www.mypage.com/myimage.gif"
m {"htt://www.mypage.com/myimage.gif"\z};
Abigail
--
sub _ {$_ = shift and y/b-yB-Y/a-yB-Y/ xor !@ _?
exit print :
print and push @_ => shift and goto &{(caller (0)) [3]}}
split // => "KsvQtbuf fbsodpmu\ni flsI " xor & _
------------------------------
Date: Tue, 14 Oct 2003 12:25:57 -0400
From: Chris Mattern <syscjm@gwu.edu>
Subject: Re: check if a string ends with particular letters
Message-Id: <3F8C2395.9050000@gwu.edu>
jjliu wrote:
> Could someone tell me how to check if a string ends with particular letters.
> For example, i want to check if the following string is ending with gif:
>
> "htt://www.mypage.com/myimage.gif"
>
>
> Thanks a lot for your help.
>
>
if ($the_string =~ /gif$/) {
do_stuff ;
}
else {
do_other_stuff ;
}
Chris Mattern
------------------------------
Date: Tue, 14 Oct 2003 17:48:39 GMT
From: Anand <some@one.com>
Subject: Re: check if a string ends with particular letters
Message-Id: <XFWib.1827$8x2.1037027@newssrv26.news.prodigy.com>
use regex.
if ($string =~ /gif$/) {
print "You got it";
}
--Anand
jjliu wrote:
> Could someone tell me how to check if a string ends with particular letters.
> For example, i want to check if the following string is ending with gif:
>
> "htt://www.mypage.com/myimage.gif"
>
>
> Thanks a lot for your help.
>
>
------------------------------
Date: 14 Oct 2003 08:17:27 -0700
From: alythh@netscape.net (Alythh)
Subject: drawing a tree
Message-Id: <6a25ba72.0310140717.266b3f22@posting.google.com>
(... hoping this is the right NG!)
I have a lot of structured data - produced by a C program -
representing a LARGE tree.
The tree starts from an ancestor node, each node has 2 children, but 2
nodes at the same level can share a common children, i.e. a node can
have multiple parents.
(so I dont think it is a standard binary tree).
Do you have knowledge of any Perl module that can help me in drawing
this kind of structure? Possibly to some kind of vectorial graphic
output?
Thanks for your help
Alessandro Magni
------------------------------
Date: Tue, 14 Oct 2003 17:12:08 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: drawing a tree
Message-Id: <20031014131230.5b403593.jwillmore@remove.adelphia.net>
On 14 Oct 2003 08:17:27 -0700
alythh@netscape.net (Alythh) wrote:
> (... hoping this is the right NG!)
>
> I have a lot of structured data - produced by a C program -
> representing a LARGE tree.
> The tree starts from an ancestor node, each node has 2 children, but
> 2 nodes at the same level can share a common children, i.e. a node
> can have multiple parents.
> (so I dont think it is a standard binary tree).
>
> Do you have knowledge of any Perl module that can help me in drawing
> this kind of structure? Possibly to some kind of vectorial graphic
> output?
>
> Thanks for your help
>
> Alessandro Magni
GD module - maybe?
try http://search.cpan.org
HTH
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
Hire the morally handicapped.
------------------------------
Date: Tue, 14 Oct 2003 08:52:33 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: How does one move down a line in a file?
Message-Id: <Xns9414646A395C5sdn.comcast@206.127.4.25>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
Helgi Briem <HelgiBriem_1@hotmail.com> wrote in
news:arrnov0l6o3fa5fpnn2cq1eoithg2nb7gl@4ax.com:
> Don't do that. Use a hash instead:
>
> #!perl
> use warnings;
> use strict;
> my %line;
> while (<DATA>)
> {
> $line{$.}= $_;
> }
>
> # Have a look at the contents of %line
> for (sort { $a <=> $b } keys %line)
> {
> print "$_\t$line{$_}";
> }
You would do the above instead of
@line = <DATA>;
for ($i=0; $i<@line; $i++)
{
print "$i\t$line[$i]";
}
?
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBP4v/jmPeouIeTNHoEQLQ4wCeNNGVhWyUvgPgSo2U6yhiYTogMo4An35P
4SOlnDyTHapWOcgjYeO+5wdD
=Tpwh
-----END PGP SIGNATURE-----
------------------------------
Date: 14 Oct 2003 13:52:54 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: How does one move down a line in a file?
Message-Id: <slrnbonvtm.ciq.abigail@alexandra.abigail.nl>
Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMDCXCVI
September MCMXCIII in <URL:news:bmgq1q$foe$2@mamenchi.zrz.TU-Berlin.DE>:
"" Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
"" > John (no@spam.here) wrote on MMMDCXCVI September MCMXCIII in
"" > <URL:news:NpRib.150645$bo1.139307@news-server.bigpond.net.au>:
"" > ~~ I'd like to move down a file and assign each line to a different variable.
"" > ~~ How can I skip/move forward a line within the following while loop?
"" > ~~
"" > ~~ while (<FILE>) {
"" > ~~ first = $_;
"" > ~~ second = $_;
"" > ~~ third = $_;
"" > ~~ }
"" >
"" > It depend a bit on what you want to do with the last lines if the
"" > file doesn't have a multiple of three lines in the file, but I
"" > would do something like:
"" >
"" > { my $first = <FILE> err last;
"" > my $second = <FILE> err last;
"" > my $third = <FILE> err last;
"" >
"" > # Process lines here.
"" >
"" > redo;
"" > }
""
"" Ah... you probably understood the OPs intention better than I did
"" in another reply.
""
"" The "err" operator looks interesting. How is it specified?
The same as the perl6 operator. Avaiable as a patch against 5.8.1 on
CPAN: authors/id/H/HM/HMBRAND/dor-5.8.1.diff.
Abigail
--
:;$:=~s:
-:;another Perl Hacker
:;chop
$:;$:=~y
:;::d;print+Just.
$:;
------------------------------
Date: Tue, 14 Oct 2003 08:00:28 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: How does one move down a line in a file?
Message-Id: <slrnbonsrc.49j.tadmc@magna.augustmail.com>
John <no@spam.here> wrote:
[ snip full-quote. Please do not do that. ]
> open(FILE, $file);
You should always, yes *always*, check the return value from open():
open(FILE, $file) or die "could not open '$file' $!";
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 14 Oct 2003 16:28:45 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: How does one move down a line in a file?
Message-Id: <bmh87t$pg5$1@mamenchi.zrz.TU-Berlin.DE>
Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMDCXCVI
> September MCMXCIII in <URL:news:bmgq1q$foe$2@mamenchi.zrz.TU-Berlin.DE>:
> "" Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
[...]
> "" > would do something like:
> "" >
> "" > { my $first = <FILE> err last;
> "" > my $second = <FILE> err last;
> "" > my $third = <FILE> err last;
> "" >
> "" > # Process lines here.
> "" >
> "" > redo;
> "" > }
> ""
> "" Ah... you probably understood the OPs intention better than I did
> "" in another reply.
> ""
> "" The "err" operator looks interesting. How is it specified?
>
> The same as the perl6 operator. Avaiable as a patch against 5.8.1 on
> CPAN: authors/id/H/HM/HMBRAND/dor-5.8.1.diff.
Ah, very nice. Compiling...
Anno
--
$_ = 'Just a LISP hacker,';
$x = \ substr( $_, 6, 5);
$$x = 'nother Perl';
print
------------------------------
Date: 14 Oct 2003 16:32:42 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: How does one move down a line in a file?
Message-Id: <bmh8fa$pg5$2@mamenchi.zrz.TU-Berlin.DE>
Eric J. Roode <REMOVEsdnCAPS@comcast.net> wrote in comp.lang.perl.misc:
> -----BEGIN xxx SIGNED MESSAGE-----
> Hash: SHA1
>
> Helgi Briem <HelgiBriem_1@hotmail.com> wrote in
> news:arrnov0l6o3fa5fpnn2cq1eoithg2nb7gl@4ax.com:
>
> > Don't do that. Use a hash instead:
> >
> > #!perl
> > use warnings;
> > use strict;
> > my %line;
> > while (<DATA>)
> > {
> > $line{$.}= $_;
> > }
> >
> > # Have a look at the contents of %line
> > for (sort { $a <=> $b } keys %line)
> > {
> > print "$_\t$line{$_}";
> > }
>
> You would do the above instead of
>
> @line = <DATA>;
>
> for ($i=0; $i<@line; $i++)
> {
> print "$i\t$line[$i]";
> }
Why that instead of
print "$_\t$line[$_]" for 0 .. $#line;
:)
Anno
------------------------------
Date: Tue, 14 Oct 2003 17:18:17 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: How does one move down a line in a file?
Message-Id: <20031014131839.525984d0.jwillmore@remove.adelphia.net>
On Tue, 14 Oct 2003 11:50:05 GMT
"John" <no@spam.here> wrote:
> I'd like to move down a file and assign each line to a different
> variable. How can I skip/move forward a line within the following
> while loop?
>
> while (<FILE>) {
> first = $_;
> second = $_;
> third = $_;
> }
>
> # all below vars have different values
> print $first;
> print $second;
> print $third;
--untested--
while(<FILE>){
my $first = <FILE>;
my $second = <FILE>;
my $third = <FILE>;
last;
}
--untested--
This will get the first three lines - 'last' breaks the loop.
HTH
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
Eat drink and be merry, for tomorrow they may make it illegal.
------------------------------
Date: 14 Oct 2003 17:45:10 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: How does one move down a line in a file?
Message-Id: <bmhcn6$rof$1@mamenchi.zrz.TU-Berlin.DE>
James Willmore <jwillmore@remove.adelphia.net> wrote in comp.lang.perl.misc:
> On Tue, 14 Oct 2003 11:50:05 GMT
> "John" <no@spam.here> wrote:
> > I'd like to move down a file and assign each line to a different
> > variable. How can I skip/move forward a line within the following
> > while loop?
> >
> > while (<FILE>) {
> > first = $_;
> > second = $_;
> > third = $_;
> > }
> >
> > # all below vars have different values
> > print $first;
> > print $second;
> > print $third;
>
> --untested--
> while(<FILE>){
> my $first = <FILE>;
> my $second = <FILE>;
> my $third = <FILE>;
> last;
> }
> --untested--
Indeed...
It drops the first line and reads the next three. It will also read
past eof if there are fewer than four lines. That raises the question
why "while" is there at all.
See Abigail's solution for a correct one. (If only the first three
lines are wanted, omit "redo" from Abigail's code.)
Anno
------------------------------
Date: 14 Oct 2003 17:47:56 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: How does one move down a line in a file?
Message-Id: <slrnboodnf.fgj.xx087@smeagol.ncf.ca>
James Willmore <jwillmore@remove.adelphia.net> wrote:
> --untested--
> while(<FILE>){
> my $first = <FILE>;
> my $second = <FILE>;
> my $third = <FILE>;
> last;
> }
> --untested--
>
> This will get the first three lines - 'last' breaks the loop.
That will get the 2nd, 3rd and 4th lines into $first, $second and $third
respectively. Because they are my variables, they will be lost as soon
as you exit the while loop.
--
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca
------------------------------
Date: Tue, 14 Oct 2003 13:36:31 GMT
From: "John" <no@spam.here>
Subject: Re: How to update entries in a file
Message-Id: <zZSib.150816$bo1.96210@news-server.bigpond.net.au>
"Tad McClellan" <tadmc@augustmail.com> wrote in message
news:slrnbonrfm.46u.tadmc@magna.augustmail.com...
> John <no@spam.here> wrote:
>
> > I am unable to use any of the CPAN modules. :(
>
>
> Why not?
>
>
> --
> Tad McClellan SGML consulting
> tadmc@augustmail.com Perl programming
> Fort Worth, Texas
It is homework related so we need to do it the hard way.
BTW, I am pursuing the following option:
*******************************
...
sub fileupdate {
open DATA, ">>test.xml"; # append to file
foreach $line (@lines) {
print DATA $_; # print $_
}
print DATA @lines [0]; # print the first element in the array
again
close DATA;
}
...
...
while <$addr> { # this is the input
@lines = <$addr>; # I'm putting it into an array
fileupdate(); # function call
}
...
Problems:
print DATA $_; <<< this prints the first element all the time
print DATA @lines [0]; <<< this prints the second element instead of the
first one
What am I missing?
------------------------------
Date: Tue, 14 Oct 2003 13:40:48 GMT
From: "John" <no@spam.here>
Subject: Re: How to update entries in a file
Message-Id: <A1Tib.150823$bo1.34819@news-server.bigpond.net.au>
"Tad McClellan" <tadmc@augustmail.com> wrote in message
news:slrnbonro6.46u.tadmc@magna.augustmail.com...
> John <no@spam.here> wrote:
>
>
> > I recognise that I will probably need to read the old file into a
temporary
> > one, do my editing there and save the results to a new file. The new
file
> > will then be renamed to the old file.
>
>
> Perl can do all of that "administrative" work for you, see
> the -i switch (perlrun.pod) and $^I (perlvar.pod).
>
>
> > Is each line in the file entry meant
> > to become a hash element?
>
>
> Hashes are unordered, but the order of lines matters, so a hash
> is not the Right Tool, it will break your data...
>
>
> --
> Tad McClellan SGML consulting
> tadmc@augustmail.com Perl programming
> Fort Worth, Texas
Thanks Tad. See my other reply if you can.
------------------------------
Date: Tue, 14 Oct 2003 15:16:48 GMT
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: How to update entries in a file
Message-Id: <f81a025c1122a62b9ca435a0b819b031@news.teranews.com>
>>>>> "John" == John <no@spam.here> writes:
>> > I am unable to use any of the CPAN modules. :(
>> Why not?
John> It is homework related so we need to do it the hard way.
Does your instructor know you are cheating?
You should hope I'm never the hiring manager at any future job to
which you apply. I'll walk you out the door so fast you won't know
what hit you.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: 14 Oct 2003 09:48:31 -0700
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: How to update entries in a file
Message-Id: <3ee08638.0310140848.6ba64e30@posting.google.com>
"John" <no@spam.here> wrote in message news:<YdPib.150444$bo1.70104@news-server.bigpond.net.au>...
> Hi,
>
> I have a file entry in the following format:
> <Employee number="111222">
> <Department>Sales</Department>
> <Surname>Jones</Surname>
> <Name>Tom</Name>
> </Employee number>
>
>
> Then, I'd like to update another file with the above entry. What I need
> though is to first check whether this entry already exists in the file to be
> updated.
Does that mean an entry with the same employee number (regardless of
other information) exists? If so, your key will be (only) the employee
number. The other fields can be stored in an anonymous hash, or array,
if the tags are consistent.
So start with a hash:
my %entry;
Parsing is up to you.
When you want to know if an entry exists, it's just
if ($entry{$emp_no}) {...
Although if your only reason for checking is to see whether you need
to make a new entry, that's automatic. Just write all the new entries,
and they'll overwrite any older versions of themselves.
When you're ready to assign your tags, it might look like:
$entry{$emp_no} = [$dept, $surname, $name];
or
$entry{$emp_no} = { department => $dept,
surname => $surname,
name => $name
};
or even (if the tags are variable)
$entry{$emp_no} = { %record_hash };
where you've parsed a single record's tag names as keys and the
contents as values into %record_hash.
------------------------------
Date: Tue, 14 Oct 2003 17:10:36 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: How to update entries in a file
Message-Id: <20031014131058.5e91158a.jwillmore@remove.adelphia.net>
On Tue, 14 Oct 2003 13:36:31 GMT
"John" <no@spam.here> wrote:
>
> "Tad McClellan" <tadmc@augustmail.com> wrote in message
> news:slrnbonrfm.46u.tadmc@magna.augustmail.com...
> > John <no@spam.here> wrote:
> >
> > > I am unable to use any of the CPAN modules. :(
> >
> >
> > Why not?
<snip>
> It is homework related so we need to do it the hard way.
Define "the hard way"? IMHO, using modules is the smart way to code
:-)
<sniped code that is "homework">
And the ground rules your instructor laid out included getting help
off
a newsgroup? Asking for help is one thing. but having someone else
solve the issues you're having is something else. More importantly,
did you read the FAQ for this group? You may find an answer there
(like, how to debug your own code).
I'll give you some tips that will help you solve your own issues ...
1) put '-w' on your first line
2) 'use strict'
3) 'use diagnostics' - since you are new to Perl, this will further
aid you in solving your own issues
4) read the documentation - more specifically, learn how to use
'perldoc' type 'perldoc perldoc" for more info.
5) if your instructor(s) think Perl can _only_ be used through the
CGI, email me. I have run into _many_ persons who think that Perl can
_only_ be run through a web server. I have proof positive that it
_can_ run in other mediums as well (such as the command line - hint,
hint).
HTH
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
In Pocataligo, Georgia, it is a violation for a woman over 200
pounds and attired in shorts to pilot or ride in an airplane.
------------------------------
Date: 14 Oct 2003 10:24:52 -0700
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: How to update entries in a file
Message-Id: <3ee08638.0310140924.11bcd808@posting.google.com>
I think it's great that Perl is being used for homework. Please excuse
my cheeky comments below. Sometimes I think I'm funny. I will try to
include some helpful ones as well.
"John" <no@spam.here> wrote in message news:<zZSib.150816$bo1.96210@news-server.bigpond.net.au>...
> foreach $line (@lines) {
> print DATA $_; # print $_
That comment may be a little obvious. You can print the whole array
with
print DATA @lines;
> print DATA @lines [0]; # print the first element in the array
Technically, you're printing an array slice. To print an element, use
scalar notation. (Effectively, they're the same, here, but you should
get used to knowing which context you really want.)
> while <$addr> { # this is the input
> @lines = <$addr>; # I'm putting it into an array
> fileupdate(); # function call
This is Not Good. Your while loop is reading line-by-line, but your
array read slurps the entire rest of the file. Figure out which way
you want to read it. Do you want to call fileupdate() for every line?
Or once for the entire file?
Good luck,
Roy
---
print map {for($a=a;$_--;++$a){} $a.' 'x(length($a)==4)}
map {hex} qw(2e805 41e 572d3 45993 153c 1d9f);
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 5658
***************************************