some stupid questions about string search & replace in perl

W

walala

Dear all,

I have been learning Perl for several days by reading online tutorials...
now when I really need it to some serious work, I found I still have a bunch
of questions:

1. How to make a string all to "UPPER CASE"?

2. Suppose there is a "(" (bracket character) in the string $str, and I want
to remove it, how can I do that?

3. Suppose there are two lines in my text file:

M1834 S_4 47 52 VDD! PCH L=239.99999143598E-9 W=3.99999998990097E-6 \
AD=2.04320002757108E-12 AS=1.58200004745507E-12 PD=5.36000015927129E-6 \
PS=829.999976303952E-9 NRD=+2.50000001E-01 NRS=+2.50000001E-01 M=1.0

I want to change the "\ ^n" ( The "\" plus an "LF" at the end of each line)
to "^n +" (first do an "LF", then add a "+" to the front of each line)

The result is:

M1834 S_4 47 52 VDD! PCH L=239.99999143598E-9 W=3.99999998990097E-6
+AD=2.04320002757108E-12 AS=1.58200004745507E-12 PD=5.36000015927129E-6
+PS=829.999976303952E-9 NRD=+2.50000001E-01 NRS=+2.50000001E-01 M=1.0

How can I do that?

Thanks a lot,

-Walala
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

I have been learning Perl for several days by reading online tutorials...
now when I really need it to some serious work, I found I still have a bunch
of questions:

A more local source of answers is the standard perl documentation. Use
the 'perldoc' program that comes with perl.
1. How to make a string all to "UPPER CASE"?

The uc() function. See 'perldoc -f uc'.
2. Suppose there is a "(" (bracket character) in the string $str, and I want
to remove it, how can I do that?

With a regular expression (substitution) like s/\(//.
3. Suppose there are two lines in my text file:

M1834 S_4 47 52 VDD! PCH L=239.99999143598E-9 W=3.99999998990097E-6 \
AD=2.04320002757108E-12 AS=1.58200004745507E-12 PD=5.36000015927129E-6 \
PS=829.999976303952E-9 NRD=+2.50000001E-01 NRS=+2.50000001E-01 M=1.0

I want to change the "\ ^n" ( The "\" plus an "LF" at the end of each line)
to "^n +" (first do an "LF", then add a "+" to the front of each line)

Are those really line-feeds? Or are they newlines? You might want the
substitution s/\\\n/\n+/g, but I can't be sure.
 
M

Mina Naguib

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Dear all,

I have been learning Perl for several days by reading online tutorials...
now when I really need it to some serious work, I found I still have a bunch
of questions:

1. How to make a string all to "UPPER CASE"?

$str = uc($str);

See perldoc -f uc
2. Suppose there is a "(" (bracket character) in the string $str, and I want
to remove it, how can I do that?

$data =~ s/\(//;

See perldoc perlop
3. Suppose there are two lines in my text file:

M1834 S_4 47 52 VDD! PCH L=239.99999143598E-9 W=3.99999998990097E-6 \
AD=2.04320002757108E-12 AS=1.58200004745507E-12 PD=5.36000015927129E-6 \
PS=829.999976303952E-9 NRD=+2.50000001E-01 NRS=+2.50000001E-01 M=1.0

I want to change the "\ ^n" ( The "\" plus an "LF" at the end of each line)
to "^n +" (first do an "LF", then add a "+" to the front of each line)

$data =~ s/\\\n/\n+/g;

See perldoc perlop

Also it might benefit you to buy and learn from a good Perl book by O'Reilly.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/bfJweS99pGMif6wRAkgIAJ0SsXYBSTINKK11ipNeAfdo/sOkzgCeILKs
39ke2/fXlp5j4hwzvydQFeA=
=48d3
-----END PGP SIGNATURE-----
 
W

walala

Dear Mina,

sorry, for that problem 3, I guess what I want is to remove the "\" at the
end of each line and add a "+" to the beginning of the next line(not the
front of that line, but next line)...

Can you statement still do the work?

Thanks a lot,

-Walala
 
W

walala

Dear Jeff,

sorry, for that problem 3, I guess what I want is to remove the "\" at the
end of each line and add a "+" to the beginning of the next line(not the
front of that line, but next line)...

Can you statement still do the work?

Thanks a lot,

-Walala
 
J

John W. Krahn

walala said:
I have been learning Perl for several days by reading online tutorials...
now when I really need it to some serious work, I found I still have a bunch
of questions:

1. How to make a string all to "UPPER CASE"?

$string = uc $string;

perldoc -f uc

2. Suppose there is a "(" (bracket character) in the string $str, and I want
to remove it, how can I do that?

$string =~ s/\(//; # remove first '(' character

$string =~ tr/(//d; # remove all '(' characters

perldoc perlre
perldoc perlop

3. Suppose there are two lines in my text file:

M1834 S_4 47 52 VDD! PCH L=239.99999143598E-9 W=3.99999998990097E-6 \
AD=2.04320002757108E-12 AS=1.58200004745507E-12 PD=5.36000015927129E-6 \
PS=829.999976303952E-9 NRD=+2.50000001E-01 NRS=+2.50000001E-01 M=1.0

I want to change the "\ ^n" ( The "\" plus an "LF" at the end of each line)
to "^n +" (first do an "LF", then add a "+" to the front of each line)

The result is:

M1834 S_4 47 52 VDD! PCH L=239.99999143598E-9 W=3.99999998990097E-6
+AD=2.04320002757108E-12 AS=1.58200004745507E-12 PD=5.36000015927129E-6
+PS=829.999976303952E-9 NRD=+2.50000001E-01 NRS=+2.50000001E-01 M=1.0

How can I do that?

while ( <> ) {
s/\s*\\\s+\z/\n +/;
}



John
 
M

Mina Naguib

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

walala wrote:

[Re-located. Please do NOT top-post. Your writing should go BELOW the part you're writing about]
Dear Mina,

sorry, for that problem 3, I guess what I want is to remove the "\" at the
end of each line and add a "+" to the beginning of the next line(not the
front of that line, but next line)...

Can you statement still do the work?

Assuming that the whole chunk of data is in $data, yes.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/biZOeS99pGMif6wRAnXsAKDxp1QdG+hJlQ19MaPYod4gQVY/ywCgtTY6
rYprI0ASxozaeC9KKIRqDi8=
=pqFl
-----END PGP SIGNATURE-----
 
W

walala

Mina,

I guess our program should only read line-by-line... The reason is that I am
going to use this program run on a big input file which is 15MByte in
size... can I take in the whole chunk of data all at once?

Thank you,

-Walala

Mina Naguib said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

walala wrote:

[Re-located. Please do NOT top-post. Your writing should go BELOW the part you're writing about]
Dear Mina,

sorry, for that problem 3, I guess what I want is to remove the "\" at the
end of each line and add a "+" to the beginning of the next line(not the
front of that line, but next line)...

Can you statement still do the work?

Assuming that the whole chunk of data is in $data, yes.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/biZOeS99pGMif6wRAnXsAKDxp1QdG+hJlQ19MaPYod4gQVY/ywCgtTY6
rYprI0ASxozaeC9KKIRqDi8=
=pqFl
-----END PGP SIGNATURE-----
 
W

walala

Daer John,

sorry, for that problem 3, I guess what I want is to remove the "\" at the
end of each line and add a "+" to the beginning of the next line(not the
front of that line, but next line)...

Can you statement still do the work?

Thanks a lot,

-Walala
 
T

Tad McClellan

[ snip yet more top-posting ]

Mina Naguib said:
walala wrote:

[Re-located. Please do NOT top-post. Your writing should go BELOW the
part you're writing about]


Do you know what "top post" means?

If not, ask.

If so, then please honor the request to stop doing it. Soon.
 
T

Tad McClellan

walala said:
going to use this program run on a big input file which is 15MByte in


15Mb is not "big" in most contemporary circles...

size... can I take in the whole chunk of data all at once?


That depends on how much memory you have on your machine, there
is no limit builtin to perl.



[ snip rude TOFU ]
 
U

Uri Guttman

T> tp gestapo
T> "> Tad McClellan SGML consulting
T> tp gestapo

<plonk>

godwin's law is in effect

uri
 
J

John W. Krahn

walala said:
sorry, for that problem 3, I guess what I want is to remove the "\" at the
end of each line and add a "+" to the beginning of the next line(not the
front of that line, but next line)...

Can you statement still do the work?

Did you try it?


John
 
A

Anno Siegel

walala said:
Daer John,

sorry, for that problem 3, I guess what I want is to remove the "\" at the
end of each line and add a "+" to the beginning of the next line(not the
front of that line, but next line)...

Can you statement still do the work?

Thanks a lot,

-Walala

This is the third answer to your original query that you answer with that
very same canned reply, requesting that the author adapt their solution
to different requirements. Where is *your* effort to adapt the suggestions?

This is not how a Usenet dialog works. And don't top-post.

Anno

[TOFU snipped]
 
A

Anno Siegel

walala said:
Daer John,

sorry, for that problem 3, I guess what I want is to remove the "\" at the
end of each line and add a "+" to the beginning of the next line(not the
front of that line, but next line)...

Can you statement still do the work?

Thanks a lot,

-Walala

This is the third answer to your original query that you have answered with
that very same canned reply, requesting that the author adapt their solution
to different requirements. Where is *your* effort to adapt the suggestions?

This is not how a Usenet dialog works. And don't top-post.

Anno

[TOFU snipped]
 
B

Barry Kimelman

[This followup was posted to comp.lang.perl.misc]

Dear all,

I have been learning Perl for several days by reading online tutorials...
now when I really need it to some serious work, I found I still have a bunch
of questions:

1. How to make a string all to "UPPER CASE"? $string = uc $string;

2. Suppose there is a "(" (bracket character) in the string $str, and I want
to remove it, how can I do that?
$string =~ s/\(//g; # remove all "(" from $string
 
M

Mina Naguib

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

walala wrote:

[Your writing relocated _AGAIN_ because it was posted on top. Please see
http://www.caliburn.nl/topposting.html for what that means]
Mina,

I guess our program should only read line-by-line... The reason is that I am
going to use this program run on a big input file which is 15MByte in
size... can I take in the whole chunk of data all at once?

Unless you're working on embedded systems (in which case Perl might not be your best option) then
15MBytes of text isn't that big.

Besides, even if you do read it in line-by-line, since it's a text file you can't do direct
modifications to it, which means you'll need a temporary holder (memory or a temp file) to hold your
results before overwriting the original.

So I would suggest you read the whole file in memory, try the regex I gave you, the write it back to
the file.

And to answer your question, yes, there are several ways to read the whole file in memory. A good
Perl book would show you a few.

Here are some:


while (<FH>) {
$data .= $_;
}

# or

$data = join("", <FH>);

# or

{
local $/ = "";
$data = <FH>;
}

# and a few others.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/bwT/eS99pGMif6wRAn/5AJ40EaaEO9s6Ey6XU0WAVuQ/jfb1eQCdEmBk
fEnYqKjepNpga3OflqEYU5c=
=xuRT
-----END PGP SIGNATURE-----
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top