Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How can I remove 0 from first line and empty space from the start of the following line and join them

by arturo (Vicar)
on Oct 16, 2002 at 02:28 UTC ( [id://205613]=note: print w/replies, xml ) Need Help??


in reply to How can I remove 0 from first line and empty space from the start of the following line and join them

Basic idea:

  1. read in two-line chunks (read one line, then another)
  2. Join the lines.
  3. Strip out the leading zero, and any excess whitespace (including newlines).

open FILE, "file.txt" or die "Can't open file.txt:$!\n"; while(my $firstline = <FILE>) { $secondline = <FILE> or die "Ran out of lines!\n"; # how to join the lines is left as an exercise for the # reader: hint ... Dot knows much. }

For the second part, you can use a regular expression. Here's a hint: /^a/ matches an a at the beginning of a string. Here's another hint: \s{7,} matches seven whitespace (space, tab, newline) characters. The general syntax for a search-and-replace (substitution) pattern match is $string =~ s/tick/arthur/;, and the /g modifier on the end of a regular expression tells the system to perform each match it finds (instead of just the first).

The rest is left as an exercise for the reader. Study materials: perlre, perlretut.

HTH

Update theorbtwo pointed out I hadn't closed my ordered list tag.

If not P, what? Q maybe?
"Sidney Morgenbesser"

  • Comment on Re: How can I remove 0 from first line and empty space from the start of the following line and join them
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://205613]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-18 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found