|
List Info
Thread: Please help with String Manipulation issue
|
|
| Please help with String Manipulation
issue |
  United States |
2007-02-16 03:18:17 |
Hello Everyone,
I am trying to sort a texted file supplied.
The content is as follows..
-----------------Begin File Example--------------------
The Executive Director
The Farmer
PO Box 8051
Townname
1000
The Director
Kitty Veterinary Services
Private Bag X2
Waterfall
2023
-------------------Example End------------------
The output I am trying to achive is.
-------------------Example Output--------------
The Executive Director;The Farmer;PO Box 8051;Townname;1000
The Director;Kitty Veterinary Services;Private Bag
X2;Waterfall;2023
-------------------Example Output End--------
I have tried just to get rid of the empty lines, but
withoutr success.
Here is my code:
---------------Code Begin--------------
#!/usr/bin/perl -w
use strict;
open (SITES, 'K:/Denham/DataFix/stakeholderlabels.txt') ||
die "Can
not Open alrtlogs.txt: $!";
open (ADDS, '>K:/Denham/Datafix/test.txt') || die
"Can not Open file: $!";
my addresses;
while (<SITES>) {
chomp;
# push addresses, $_ if /^w+/||/^s+w+/;
# print ADDS $_;
foreach($_){
if ($_ =~ /w.*/){
print ADDS $_};
}
}
close SITES;
close ADDS;
-----------------------------End Code-------------------
Please can anyone help how this should look.
I am really confused at present.
Regards
Denham.
Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://g
roups.yahoo.com/group/perl-beginner/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http
://groups.yahoo.com/group/perl-beginner/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:perl-beginner-digest@yahoogroups.com
mailto:perl-beginner-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
perl-beginner-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| Re: Please help with String
Manipulation issue |
  United States |
2007-02-16 08:07:45 |
>>>>> "Denham" == Denham Eva
<denhameva gmail.com> writes:
Denham> -----------------Begin File
Example--------------------
Denham> The Executive Director
Denham> The Farmer
Denham> PO Box 8051
Denham> Townname
Denham> 1000
Denham> The Director
Denham> Kitty Veterinary Services
Denham> Private Bag X2
Denham> Waterfall
Denham> 2023
Denham> -------------------Example End------------------
Denham> The output I am trying to achive is.
Denham> -------------------Example Output--------------
Denham> The Executive Director;The Farmer;PO Box
8051;Townname;1000
Denham> The Director;Kitty Veterinary Services;Private
Bag X2;Waterfall;2023
First, you can use "paragraph mode" to break up a
file that is separated
by blank lines. See the variable $/ in "perldoc
perlvar", and note that
it says:
Setting to "" will treat two or more consecutive
empty lines as a single
empty line.
So, we can do that:
my data;
{
local $/ = "";
data = <>; # slurp all input in paragraph mode
}
The first element of data is now:
"The Executive DirectornThe FarmernPO Box
8051nTownnamen1000"
We need to turn n into ; for every element of data,
which can be
done easily with:
foreach ( data) {
tr/n/;/;
}
And now we need to sort them. You didn't see *how* you
wanted to sort,
so the easiest is just to sort alphabetically on each of
these strings
(since the main name comes first):
data = sort data;
Finally, we need to print that, and we have to add newlines
back
because we've gotten rid of them:
foreach ( data) {
print "$_n";
}
That should be pretty close.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. -
+1 503 777 0095
<merlyn stonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy,
etc. etc.
See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!
Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://g
roups.yahoo.com/group/perl-beginner/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http
://groups.yahoo.com/group/perl-beginner/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:perl-beginner-digest@yahoogroups.com
mailto:perl-beginner-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
perl-beginner-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| Re: Please help with String
Manipulation issue |
  United States |
2007-02-16 13:05:35 |
Thanks Randal for the help.
I have however discovered another reason why I have been
struggling
with this data.
Because your suggestion works to a point and then seems to
hang with the
data = <>;
Now after alot of head scratching, I took a look at the text
file in a
hex editor, and discovered that a 0d hex value was present
i.e. a
carriage return.
Could this be causing the hanging effect?
I just can't seem to get passed this point.
Many thanks
Denham
On 2/16/07, Randal L. Schwartz <merlyn stonehenge.com> wrote:
> >>>>> "Denham" == Denham Eva
<denhameva gmail.com> writes:
>
> Denham> -----------------Begin File
Example--------------------
>
>
>
> Denham> The Executive Director
> Denham> The Farmer
> Denham> PO Box 8051
> Denham> Townname
> Denham> 1000
>
>
>
>
>
>
>
>
> Denham> The Director
> Denham> Kitty Veterinary Services
> Denham> Private Bag X2
> Denham> Waterfall
> Denham> 2023
>
>
>
> Denham> -------------------Example
End------------------
> Denham> The output I am trying to achive is.
> Denham> -------------------Example
Output--------------
> Denham> The Executive Director;The Farmer;PO Box
8051;Townname;1000
> Denham> The Director;Kitty Veterinary
Services;Private Bag X2;Waterfall;2023
>
>
> First, you can use "paragraph mode" to break
up a file that is separated
> by blank lines. See the variable $/ in "perldoc
perlvar", and note that
> it says:
>
> Setting to "" will treat two or more
consecutive empty lines as a single
> empty line.
>
> So, we can do that:
>
> my data;
> {
> local $/ = "";
> data = <>; # slurp all input in paragraph mode
> }
>
> The first element of data is now:
>
> "The Executive DirectornThe FarmernPO Box
8051nTownnamen1000"
>
> We need to turn n into ; for every element of data,
which can be
> done easily with:
>
> foreach ( data) {
> tr/n/;/;
> }
>
> And now we need to sort them. You didn't see *how* you
wanted to sort,
> so the easiest is just to sort alphabetically on each
of these strings
> (since the main name comes first):
>
> data = sort data;
>
> Finally, we need to print that, and we have to add
newlines back
> because we've gotten rid of them:
>
> foreach ( data) {
> print "$_n";
> }
>
> That should be pretty close.
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services,
Inc. - +1 503 777 0095
> <merlyn stonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing,
Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!
>
Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://g
roups.yahoo.com/group/perl-beginner/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http
://groups.yahoo.com/group/perl-beginner/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:perl-beginner-digest@yahoogroups.com
mailto:perl-beginner-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
perl-beginner-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| Re: Please help with String
Manipulation issue |
  United States |
2007-02-16 13:27:43 |
>>>>> "Denham" == Denham Eva
<denhameva gmail.com> writes:
Denham> I have however discovered another reason why I
have been struggling
Denham> with this data. Because your suggestion works to
a point and then
Denham> seems to hang with the data = <>;
For that to work, you either have to invoke your code like:
./myprogram myfile
or
./myprogram <myfile
Did you do that?
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. -
+1 503 777 0095
<merlyn stonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy,
etc. etc.
See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!
Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://g
roups.yahoo.com/group/perl-beginner/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http
://groups.yahoo.com/group/perl-beginner/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:perl-beginner-digest@yahoogroups.com
mailto:perl-beginner-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
perl-beginner-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| Re: Please help with String
Manipulation issue |
  United States |
2007-02-16 14:30:36 |
>>>>> "Denham" == Denham Eva
<denhameva gmail.com> writes:
Denham> Hello Randal,
Denham> OK execing this in this way does work, not my
method as follows
Denham>
------------------------------------------------------------
---
Denham> open SITES,
'J:/Denham/DataFix/stakeholderlabels.txt' || die "Can
not
Denham> Open alrtlogs.txt: $!";
Denham> open ADDS, '>J:/Denham/Datafix/test.txt' ||
die "Can not Open file: $!";
Denham> my data;
Denham> while (<SITES>) {
Denham> my data = $_;
Denham> {
Denham> local $/ = "n";
Denham> data = <>; # slurp all input in paragraph mode
Denham> }
Denham>
------------------------------------------------------------
-----------------
Denham> However even though your method is producing
output it, it is verbatim
Denham> to the original file.
Denham> This is why I am so particularly confused.
Stop mixing up the methods!
GET RID OF YOUR CODE
PUT MY CODE IN PLACE.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. -
+1 503 777 0095
<merlyn stonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy,
etc. etc.
See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!
Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://g
roups.yahoo.com/group/perl-beginner/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http
://groups.yahoo.com/group/perl-beginner/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:perl-beginner-digest@yahoogroups.com
mailto:perl-beginner-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
perl-beginner-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| Re: Please help with String
Manipulation issue |
  United States |
2007-02-16 14:26:11 |
Hello Randal,
OK execing this in this way does work, not my method as
follows
------------------------------------------------------------
---
open SITES, 'J:/Denham/DataFix/stakeholderlabels.txt' || die
"Can not
Open alrtlogs.txt: $!";
open ADDS, '>J:/Denham/Datafix/test.txt' || die "Can
not Open file: $!";
my data;
while (<SITES>) {
my data = $_;
{
local $/ = "n";
data = <>; # slurp all input in paragraph mode
}
------------------------------------------------------------
-----------------
However even though your method is producing output it, it
is verbatim
to the original file.
This is why I am so particularly confused.
Thanks once again for the help.
Denham
On 2/16/07, Randal L. Schwartz <merlyn stonehenge.com> wrote:
> >>>>> "Denham" == Denham Eva
<denhameva gmail.com> writes:
>
> Denham> I have however discovered another reason why
I have been struggling
> Denham> with this data. Because your suggestion
works to a point and then
> Denham> seems to hang with the data =
<>;
>
> For that to work, you either have to invoke your code
like:
>
> ./myprogram myfile
>
> or
>
> ./myprogram <myfile
>
> Did you do that?
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services,
Inc. - +1 503 777 0095
> <merlyn stonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing,
Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!
>
Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://g
roups.yahoo.com/group/perl-beginner/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http
://groups.yahoo.com/group/perl-beginner/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:perl-beginner-digest@yahoogroups.com
mailto:perl-beginner-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
perl-beginner-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| Re: Please help with String
Manipulation issue |
  United States |
2007-02-16 14:45:26 |
OK Randal,
I am sorry, I have done that and am running it as you
described. ie
perl address.pl < address.txt
The output looks promising.
ie.
oriaBag X 13Aroductionceultureng & Extension
Servicesand Land Affairs
And that is it, nothing else.
However when I redirect the output ie
perl address.pl < address.txt >>test.txt
The output is appears to be exactly the same as the
original.
Or am I doing this wrong too, feel really stupid right now.
Thanks
Denham
On 2/16/07, Randal L. Schwartz <merlyn stonehenge.com> wrote:
> >>>>> "Denham" == Denham Eva
<denhameva gmail.com> writes:
>
> Denham> Hello Randal,
> Denham> OK execing this in this way does work, not
my method as follows
> Denham>
------------------------------------------------------------
---
> Denham> open SITES,
'J:/Denham/DataFix/stakeholderlabels.txt' || die "Can
not
> Denham> Open alrtlogs.txt: $!";
> Denham> open ADDS, '>J:/Denham/Datafix/test.txt'
|| die "Can not Open file: $!";
>
> Denham> my data;
>
> Denham> while (<SITES>) {
>
> Denham> my data = $_;
> Denham> {
> Denham> local $/ = "n";
> Denham> data = <>; # slurp all input in
paragraph mode
> Denham> }
> Denham>
------------------------------------------------------------
-----------------
> Denham> However even though your method is producing
output it, it is verbatim
> Denham> to the original file.
> Denham> This is why I am so particularly confused.
>
> Stop mixing up the methods!
>
> GET RID OF YOUR CODE
>
> PUT MY CODE IN PLACE.
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services,
Inc. - +1 503 777 0095
> <merlyn stonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing,
Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!
>
Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://g
roups.yahoo.com/group/perl-beginner/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http
://groups.yahoo.com/group/perl-beginner/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:perl-beginner-digest@yahoogroups.com
mailto:perl-beginner-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
perl-beginner-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| Re: Please help with String
Manipulation issue |
  United States |
2007-02-16 16:25:57 |
On Friday 16 February 2007 12:26, Denham Eva wrote:
[ . . ]
> OK execing this in this way does work, not my method as
follows
It (the next) works here (I ran it. Output follows the
code). I don't grasp
what your prob might be.
#!/usr/bin/perl
use warnings;
use strict;
my data;
{
local $/ = "";
data = <DATA>; # slurp all input in paragraph
mode
}
foreach ( data) {
tr/n/;/;
}
data
= sort data;
foreach ( data) {
print "$_n";
}
# the <DATA> filehandle
# data in paragraphs
__DATA__
The Executive Director
The Farmer
PO Box 8051
Townname
1000
The Director
Kitty Veterinary Services
Private Bag X2
Waterfall
2023
the code ends on previous blank line
That works here. Here's the output:
al p3srv:~$ parsdelim
The Director;Kitty Veterinary Services;Private Bag
X2;Waterfall;2023;
The Executive Director;The Farmer;PO Box
8051;Townname;1000;;
al p3srv:~$
----------------------------------------------------
next is untested. Not that you'd want to use it, though you
might
(And now, just playing with <>, <DATA>,
<SITES>) (and nothing more)
*OR* you should be able to
open SITES, 'J:/Denham/. . .
# then
data
= <SITES>; # slurp all input in paragraph mode
# -----------------------------
#!/usr/bin/perl
use warnings;
use strict;
# data input file
ARGV
= 'J:/Denham/DataFix/stakeholderlabels.txt';
open ADDS, '>J:/Denham/Datafix/test.txt' or die "Can
not Open file: $!";
my data;
{
local $/ = "";
data = <>; # slurp all input in paragraph mode
# above pulls from ARGV Perl's arg list
# which comes from the command line
}
foreach ( data) {
tr/n/;/;
}
data
= sort data;
foreach ( data) {
print ADDS "$_n"; # print to ADDS filehandle
}
# end
--
Alan.
Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://g
roups.yahoo.com/group/perl-beginner/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http
://groups.yahoo.com/group/perl-beginner/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:perl-beginner-digest@yahoogroups.com
mailto:perl-beginner-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
perl-beginner-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
[1-8]
|
|