Perl

Moderators: Jonathan
Number of threads: 1236
Number of posts: 3605

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Alignment Posted by meshteb on 18 Feb 2004 at 3:47 AM
I got this problem with aligning my .txt file consisting of a list of word. The data is not align to the far-left and other words on the list are not on the same column or order.So i want to put them on the same column,at the far-left in .txt file(outpu)

Any suggestions?
Report
Re: Alignment Posted by mdw1982 on 18 Feb 2004 at 10:58 AM
This message was edited by mdw1982 at 2004-2-18 10:58:47

: I got this problem with aligning my .txt file consisting of a list of word. The data is not align to the far-left and other words on the list are not on the same column or order.So i want to put them on the same column,at the far-left in .txt file(outpu)
:
: Any suggestions?
:

Can you show an example of what the contents of the file looks like after the program writes to this file? It would also be helpful to know what the code doing the writing looks like.
--
Mark
"If I can't code I'm not going to be a happy camper!"



Report
Re: Alignment Posted by Jonathan on 18 Feb 2004 at 1:07 PM
: This message was edited by mdw1982 at 2004-2-18 10:58:47

: : I got this problem with aligning my .txt file consisting of a list of word. The data is not align to the far-left and other words on the list are not on the same column or order.So i want to put them on the same column,at the far-left in .txt file(outpu)
: :
: : Any suggestions?
: :
:
: Can you show an example of what the contents of the file looks like after the program writes to this file? It would also be helpful to know what the code doing the writing looks like.
: --
: Mark
: "If I can't code I'm not going to be a happy camper!"
:
It sounds very much like we have:-

   word
 something
         blah


And we want:-

word
something
blah


Read the entire file into an array, say called @words, then do this:-

s/^\s+// foreach (@words);

You can then write @words back to the file. Equivalent to the above code is:-

@words = map { /^\s*(.+)$/; $1 } @words;

I think the first one is more readable.

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: Alignment Posted by meshteb on 19 Feb 2004 at 1:21 AM
The code i've used here,output the words horizontally.I want each word on a new line.I've even tried to put "\n", but it does not work



#!/usr/local/bin/perl -w

my $words="C:/pediwordlist.txt";

my $out="c:/output.txt";

# open prompt file
open my $fh, "< $words" || die ("Unable to open prompt file $words");

open my $wfh, "> $out" || die ("Can't create output file!");

# Read every word into an array
my @words = <$fh>;

close $fh;
@words = map { /^\s*(.+)$/; $1 } @words;

print $wfh "@words";

close $wfh;





Report
Re: Alignment Posted by Jonathan on 19 Feb 2004 at 2:52 AM
: The code i've used here,output the words horizontally.I want each
: word on a new line.I've even tried to put "\n", but it does not work
:
Ooops. Shoulda been:-

@words = map { /^\s*(.+)$/s; $1 } @words;

Need the s modifier so . catches the \n and $ doesn't match it. I shoulda just left you with the simpler alternative, which does work, and not bothered with this map one...

: print $wfh "@words";
No "'s needed here.

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: Alignment Posted by mdw1982 on 19 Feb 2004 at 4:06 AM
This message was edited by mdw1982 at 2004-2-19 4:8:30

This message was edited by mdw1982 at 2004-2-19 4:7:15

: : The code i've used here,output the words horizontally.I want each
: : word on a new line.I've even tried to put "\n", but it does not work
: :
: Ooops. Shoulda been:-
:
: @words = map { /^\s*(.+)$/s; $1 } @words;
:
: Need the s modifier so . catches the \n and $ doesn't match it. I shoulda just left you with the simpler alternative, which does work, and not bothered with this map one...
:
: : print $wfh "@words";
: No "'s needed here.
:

What about this one?

# open and read the file into array
open ( FILE, "<your_textfile.txt" );
@words = <FILE>;
close(FILE);

# open output file
open ( OP, ">some_outputfile.txt" );

# output the files contents to another file
foreach my $line ( @words )
{
    chomp( $line );
    print OP "$line\n";
}

close(OP);
exit;


this reads easily and doesn't require the use of regex. granted its a wee bit more typing, but gets the job done nicely.
--
Mark
"If I can't code I'm not going to be a happy camper!"





Report
Re: Alignment Posted by Jonathan on 19 Feb 2004 at 5:48 AM
: This message was edited by mdw1982 at 2004-2-19 4:8:30

: This message was edited by mdw1982 at 2004-2-19 4:7:15

: : : The code i've used here,output the words horizontally.I want each
: : : word on a new line.I've even tried to put "\n", but it does not work
: : :
: : Ooops. Shoulda been:-
: :
: : @words = map { /^\s*(.+)$/s; $1 } @words;
: :
: : Need the s modifier so . catches the \n and $ doesn't match it. I shoulda just left you with the simpler alternative, which does work, and not bothered with this map one...
: :
: : : print $wfh "@words";
: : No "'s needed here.
: :
:
: What about this one?
:
:
: # open and read the file into array
: open ( FILE, "<your_textfile.txt" );
: @words = <FILE>;
: close(FILE);
: 
: # open output file
: open ( OP, ">some_outputfile.txt" );
: 
: # output the files contents to another file
: foreach my $line ( @words )
: {
:     chomp( $line );
:     print OP "$line\n";
: }
: 
: close(OP);
: exit;
: 

:
: this reads easily and doesn't require the use of regex. granted its a wee bit more typing, but gets the job done nicely.
:
Uhhhh...and where does it remove the blanks at the start of the line?

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: Alignment Posted by mdw1982 on 19 Feb 2004 at 6:05 AM
: : This message was edited by mdw1982 at 2004-2-19 4:8:30

: : This message was edited by mdw1982 at 2004-2-19 4:7:15

: : : : The code i've used here,output the words horizontally.I want each
: : : : word on a new line.I've even tried to put "\n", but it does not work
: : : :
: : : Ooops. Shoulda been:-
: : :
: : : @words = map { /^\s*(.+)$/s; $1 } @words;
: : :
: : : Need the s modifier so . catches the \n and $ doesn't match it. I shoulda just left you with the simpler alternative, which does work, and not bothered with this map one...
: : :
: : : : print $wfh "@words";
: : : No "'s needed here.
: : :
: :
: : What about this one?
: :

# open and read the file into array
open ( FILE, "<your_textfile.txt" );
@words = <FILE>;
close(FILE);

# open output file
open ( OP, ">some_outputfile.txt" );
 
# output the files contents to another file
foreach my $line ( @words )
{
    chomp( $line );
    # beginning spaces removal added later 
    # cause was sleepy when I posted this early in
    # the morning
    $line =~ s/^\d+//;
    print OP "$line\n";
}
close(OP);
exit;


: Uhhhh...and where does it remove the blanks at the start of the line?
:
: Jonathan

I was a little sleepy this morning when I posted that and I understand better now that a regex is indeed necessary to clean the beginning of the line if there are spaces there. Ooooooooooops, my bad.

I also realize that my code example is somewhat simpler, and probably less elegant, but then I'm a simple sort of guy.
--
Mark
"If I can't code I'm not going to be a happy camper!"

Report
Re: Alignment Posted by Jonathan on 19 Feb 2004 at 6:28 AM
: : : This message was edited by mdw1982 at 2004-2-19 4:8:30

: : : This message was edited by mdw1982 at 2004-2-19 4:7:15

: : : : : The code i've used here,output the words horizontally.I want each
: : : : : word on a new line.I've even tried to put "\n", but it does not work
: : : : :
: : : : Ooops. Shoulda been:-
: : : :
: : : : @words = map { /^\s*(.+)$/s; $1 } @words;
: : : :
: : : : Need the s modifier so . catches the \n and $ doesn't match it. I shoulda just left you with the simpler alternative, which does work, and not bothered with this map one...
: : : :
: : : : : print $wfh "@words";
: : : : No "'s needed here.
: : : :
: : :
: : : What about this one?
: : :
:
:
: # open and read the file into array
: open ( FILE, "<your_textfile.txt" );
: @words = <FILE>;
: close(FILE);
: 
: # open output file
: open ( OP, ">some_outputfile.txt" );
:  
: # output the files contents to another file
: foreach my $line ( @words )
: {
:     chomp( $line );
:     # beginning spaces removal added later 
:     # cause was sleepy when I posted this early in
:     # the morning
:     $line =~ s/^\d+//;
:     print OP "$line\n";
: }
: close(OP);
: exit;
: 

:
: : Uhhhh...and where does it remove the blanks at the start of the line?
: :
: : Jonathan
:
: I was a little sleepy this morning when I posted that and I understand better now that a regex is indeed necessary to clean the beginning of the line if there are spaces there. Ooooooooooops, my bad.
:
: I also realize that my code example is somewhat simpler, and probably less elegant, but then I'm a simple sort of guy.
:
Go get more coffee.

$line =~ s/^\d+//;

Should be:-

$line =~ s/^\s+//;

s for whitespace. d is for digits.

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: Alignment Posted by mdw1982 on 19 Feb 2004 at 6:43 AM
: : : : This message was edited by mdw1982 at 2004-2-19 4:8:30

: : : : This message was edited by mdw1982 at 2004-2-19 4:7:15

: : : : : : The code i've used here,output the words horizontally.I want each
: : : : : : word on a new line.I've even tried to put "\n", but it does not work
: : : : : :
: : : : : Ooops. Shoulda been:-
: : : : :
: : : : : @words = map { /^\s*(.+)$/s; $1 } @words;
: : : : :
: : : : : Need the s modifier so . catches the \n and $ doesn't match it. I shoulda just left you with the simpler alternative, which does work, and not bothered with this map one...
: : : : :
: : : : : : print $wfh "@words";
: : : : : No "'s needed here.
: : : : :
: : : :
: : : : What about this one?
: : : :
: :
: :
: : # open and read the file into array
: : open ( FILE, "<your_textfile.txt" );
: : @words = <FILE>;
: : close(FILE);
: : 
: : # open output file
: : open ( OP, ">some_outputfile.txt" );
: :  
: : # output the files contents to another file
: : foreach my $line ( @words )
: : {
: :     chomp( $line );
: :     # beginning spaces removal added later 
: :     # cause was sleepy when I posted this early in
: :     # the morning
: :     $line =~ s/^\d+//;
: :     print OP "$line\n";
: : }
: : close(OP);
: : exit;
: : 

: :
: : : Uhhhh...and where does it remove the blanks at the start of the line?
: : :
: : : Jonathan
: :
: : I was a little sleepy this morning when I posted that and I understand better now that a regex is indeed necessary to clean the beginning of the line if there are spaces there. Ooooooooooops, my bad.
: :
: : I also realize that my code example is somewhat simpler, and probably less elegant, but then I'm a simple sort of guy.
: :
: Go get more coffee.
:
: $line =~ s/^\d+//;
:
: Should be:-
:
: $line =~ s/^\s+//;
:
: s for whitespace. d is for digits.
:
: Jonathan

O God!!!! More Coffee and make it stronger! Geez! what a stupid mistake. No wonder I'm pulling my hair out with this program.
--
Mark
"If I can't code I'm not going to be a happy camper!"

Report
Re: Alignment Posted by Jonathan on 19 Feb 2004 at 9:47 AM
: O God!!!! More Coffee and make it stronger! Geez! what a stupid
: mistake. No wonder I'm pulling my hair out with this program.
:
Hehe, no worries. Personally I drink large quantities of green tea, amongst other sorts of tea. I've never really been into coffee, but love (black) tea so it provides my source of daily caffine.

Jonathan


###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.