Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Alignment

meshtebmeshteb Posts: 47Member
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?

Comments

  • mdw1982mdw1982 Posts: 124Member
    [b][red]This message was edited by mdw1982 at 2004-2-18 10:58:47[/red][/b][hr]
    : 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!"



  • JonathanJonathan Posts: 2,914Member
    : [b][red]This message was edited by mdw1982 at 2004-2-18 10:58:47[/red][/b][hr]
    : : 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:-

    [code] word
    something
    blah
    [/code]

    And we want:-

    [code]word
    something
    blah
    [/code]

    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.");

  • meshtebmeshteb Posts: 47Member
    The code i've used here,output the words horizontally.I want each word on a new line.I've even tried to put "
    ", 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;





  • JonathanJonathan Posts: 2,914Member
    : The code i've used here,output the words horizontally.I want each
    : word on a new line.I've even tried to put "
    ", but it does not work
    :
    Ooops. Shoulda been:-

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

    Need the s modifier so . catches the
    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.");

  • mdw1982mdw1982 Posts: 124Member
    [b][red]This message was edited by mdw1982 at 2004-2-19 4:8:30[/red][/b][hr]
    [b][red]This message was edited by mdw1982 at 2004-2-19 4:7:15[/red][/b][hr]
    : : The code i've used here,output the words horizontally.I want each
    : : word on a new line.I've even tried to put "
    ", but it does not work
    : :
    : Ooops. Shoulda been:-
    :
    : @words = map { /^s*(.+)$/s; $1 } @words;
    :
    : Need the s modifier so . catches the
    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?

    [code]
    # open and read the file into array
    open ( 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
    ";
    }

    close(OP);
    exit;
    [/code]

    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!"





  • JonathanJonathan Posts: 2,914Member
    : [b][red]This message was edited by mdw1982 at 2004-2-19 4:8:30[/red][/b][hr]
    : [b][red]This message was edited by mdw1982 at 2004-2-19 4:7:15[/red][/b][hr]
    : : : The code i've used here,output the words horizontally.I want each
    : : : word on a new line.I've even tried to put "
    ", but it does not work
    : : :
    : : Ooops. Shoulda been:-
    : :
    : : @words = map { /^s*(.+)$/s; $1 } @words;
    : :
    : : Need the s modifier so . catches the
    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?
    :
    : [code]
    : # open and read the file into array
    : open ( 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
    ";
    : }
    :
    : close(OP);
    : exit;
    : [/code]
    :
    : 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.");

  • mdw1982mdw1982 Posts: 124Member
    : : [b][red]This message was edited by mdw1982 at 2004-2-19 4:8:30[/red][/b][hr]
    : : [b][red]This message was edited by mdw1982 at 2004-2-19 4:7:15[/red][/b][hr]
    : : : : The code i've used here,output the words horizontally.I want each
    : : : : word on a new line.I've even tried to put "
    ", but it does not work
    : : : :
    : : : Ooops. Shoulda been:-
    : : :
    : : : @words = map { /^s*(.+)$/s; $1 } @words;
    : : :
    : : : Need the s modifier so . catches the
    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?
    : :

    [code]
    # open and read the file into array
    open ( FILE, ";
    close(FILE);

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

    # output the files contents to another file
    foreach my $line ( @words )
    {
    chomp( $line );
    [green]# beginning spaces removal added later [/green]
    [green]# cause was sleepy when I posted this early in[/green]
    [green]# the morning[/green]
    [red]$line =~ s/^d+//;[/red]
    print OP "$line
    ";
    }
    close(OP);
    exit;
    [/code]

    : 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!"

  • JonathanJonathan Posts: 2,914Member
    : : : [b][red]This message was edited by mdw1982 at 2004-2-19 4:8:30[/red][/b][hr]
    : : : [b][red]This message was edited by mdw1982 at 2004-2-19 4:7:15[/red][/b][hr]
    : : : : : The code i've used here,output the words horizontally.I want each
    : : : : : word on a new line.I've even tried to put "
    ", but it does not work
    : : : : :
    : : : : Ooops. Shoulda been:-
    : : : :
    : : : : @words = map { /^s*(.+)$/s; $1 } @words;
    : : : :
    : : : : Need the s modifier so . catches the
    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?
    : : :
    :
    : [code]
    : # open and read the file into array
    : open ( FILE, ";
    : close(FILE);
    :
    : # open output file
    : open ( OP, ">some_outputfile.txt" );
    :
    : # output the files contents to another file
    : foreach my $line ( @words )
    : {
    : chomp( $line );
    : [green]# beginning spaces removal added later [/green]
    : [green]# cause was sleepy when I posted this early in[/green]
    : [green]# the morning[/green]
    : [red]$line =~ s/^d+//;[/red]
    : print OP "$line
    ";
    : }
    : close(OP);
    : exit;
    : [/code]
    :
    : : 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.");

  • mdw1982mdw1982 Posts: 124Member
    : : : : [b][red]This message was edited by mdw1982 at 2004-2-19 4:8:30[/red][/b][hr]
    : : : : [b][red]This message was edited by mdw1982 at 2004-2-19 4:7:15[/red][/b][hr]
    : : : : : : The code i've used here,output the words horizontally.I want each
    : : : : : : word on a new line.I've even tried to put "
    ", but it does not work
    : : : : : :
    : : : : : Ooops. Shoulda been:-
    : : : : :
    : : : : : @words = map { /^s*(.+)$/s; $1 } @words;
    : : : : :
    : : : : : Need the s modifier so . catches the
    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?
    : : : :
    : :
    : : [code]
    : : # open and read the file into array
    : : open ( FILE, ";
    : : close(FILE);
    : :
    : : # open output file
    : : open ( OP, ">some_outputfile.txt" );
    : :
    : : # output the files contents to another file
    : : foreach my $line ( @words )
    : : {
    : : chomp( $line );
    : : [green]# beginning spaces removal added later [/green]
    : : [green]# cause was sleepy when I posted this early in[/green]
    : : [green]# the morning[/green]
    : : [red]$line =~ s/^d+//;[/red]
    : : print OP "$line
    ";
    : : }
    : : close(OP);
    : : exit;
    : : [/code]
    : :
    : : : 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!"

  • JonathanJonathan Posts: 2,914Member
    : 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.");

Sign In or Register to comment.