Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] Please help with String Manipulation issue

Expand Messages
  • Denham Eva
    OK Randal, I am sorry, I have done that and am running it as you described. ie perl address.pl
    Message 1 of 14 , Feb 16, 2007
      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@...> wrote:
      > >>>>> "Denham" == Denham Eva <denhameva@...> 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@...> <URL:http://www.stonehenge.com/merlyn/>
      > Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
      > See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
      >
    • Alan
      On Friday 16 February 2007 12:26, Denham Eva wrote: [ . . ] ... It (the next) works here (I ran it. Output follows the code). I don t grasp what your prob
      Message 2 of 14 , Feb 16, 2007
        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.
      • dkeva
        Hello Randal, Alan. Thanks Guys for the help! I have worked out the issue. The data file supplied did not have true new line characters. So the data was never
        Message 3 of 14 , Feb 17, 2007
          Hello Randal, Alan.

          Thanks Guys for the help! I have worked out the issue.
          The data file supplied did not have true new line characters.
          So the data was never being paragraphed (slurped).
          Once I had converted the data file, Randal your script ran perfectly
          and did not "act funny".
          Thanks Alan for your tip on the @ARGV.

          Thanks.
          Denham
        • N Thamma
          Hi all: I ve few question regarding Hashes, hashes of hashes, my understanding of hashes is not quite clear. I got these error, sm1.list smoke.list sm0.list
          Message 4 of 14 , Feb 19, 2007
            Hi all:

            I've few question regarding Hashes, hashes of hashes, my understanding
            of hashes is not quite clear.

            I got these error, sm1.list smoke.list sm0.list 1/8 5/8 1/8 G

            I did:

            $HashList = ();
            foreach my $a1 (@list) {
            $HashList{$a1} = ReadFromList($a1) ; # <--- Hash of hash
            }

            # Function reads contents for file in this format and returns a hash;
            # Filename -o "Values ....."
            #
            sub ReadFromList {

            while <FILEFP> {
            $hash{key} = $val...
            }
            return %hash; <--- returning HASH
            }

            I got:

            perl didn't like it,
            syntax error scr.pl line 151, near "$HashList{" syntax error at scr.pl line 153, near "}" Execution of scr.pl aborted due to compilation errors.

            Question :

            1. Can I retun a hash ?, if not what's the alternative ?

            I tried to dump using Data::Dumper, it printed something like 1/8 4/7 etc,

            Thanks,Thama


            ---------------------------------
            Any questions? Get answers on any topic at Yahoo! Answers. Try it now.

            [Non-text portions of this message have been removed]
          • merlyn@stonehenge.com
            ... N Hi all: N I ve few question regarding Hashes, hashes of hashes, my understanding N of hashes is not quite clear. N I got these error,
            Message 5 of 14 , Feb 19, 2007
              >>>>> "N" == N Thamma <nt557@...> writes:

              N> Hi all:

              N> I've few question regarding Hashes, hashes of hashes, my understanding
              N> of hashes is not quite clear.

              N> I got these error, sm1.list smoke.list sm0.list 1/8 5/8 1/8 G

              N> I did:

              N> $HashList = ();
              N> foreach my $a1 (@list) {
              N> $HashList{$a1} = ReadFromList($a1) ; # <--- Hash of hash
              N> }

              N> # Function reads contents for file in this format and returns a hash;
              N> # Filename -o "Values ....."
              N> #
              N> sub ReadFromList {

              N> while <FILEFP> {
              N> $hash{key} = $val...
              N> }
              N> return %hash; <--- returning HASH

              That doesn't return a hash. You can't return a hash. You can only
              return a list. And you can't have a "hash of hashes", even though
              the term is tossed around casually like that. You can only have a hash
              of hashrefs. See "perldoc perlreftut" (at your commandline) for the
              basics of references, so you can do this and many other things with
              complex data structures.

              N> }

              N> I got:

              N> perl didn't like it,
              N> syntax error scr.pl line 151, near "$HashList{" syntax error at scr.pl line 153, near "}" Execution of scr.pl aborted due to compilation errors.

              N> Question :

              N> 1. Can I retun a hash ?, if not what's the alternative ?

              N> I tried to dump using Data::Dumper, it printed something like 1/8 4/7 etc,

              N> Thanks,Thama


              N> ---------------------------------
              N> Any questions? Get answers on any topic at Yahoo! Answers. Try it now.

              N> [Non-text portions of this message have been removed]



              N> Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
              N> Yahoo! Groups Links






              --
              Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
              <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
              Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
              See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
            • Bob Kardell
              I have tried using the paragraph mode in the past with some success, but this issue brought up a question I have had - is it possible to define $/ two
              Message 6 of 14 , Mar 4, 2007
                I have tried using the paragraph mode in the past with some success, but this issue brought up a question I have had - is it possible to define "$/" two different ways in the same script? In the past when I have tried to redefine $/ to something other than the default I have had to create separate scripts. I noticed that you put the following code in:


                {

                local $/ = "";

                @data = <>; # slurp all input in paragraph mode

                }

                is it possible to do something like:


                {

                local $/ = "";

                @data = <>; # slurp all input in paragraph mode

                }

                #do some code here

                {

                local $/ = "\n\n";

                @data = <>; # slurp all input in paragraph mode

                }

                #do something else here

                When I have tried this in the past, the two separate $/ have not worked nicely with one another and I have to use two separate scripts because of it. I am obviously missing something.

                Thanks,

                Bob

                ----- Original Message ----
                From: "merlyn@..." <merlyn@...>
                To: Denham Eva <denhameva@...>
                Cc: perl-beginner@yahoogroups.com
                Sent: Friday, February 16, 2007 2:07:45 PM
                Subject: Re: [PBML] Please help with String Manipulation issue













                >>>>> "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 Director\nThe Farmer\nPO Box 8051\nTownname\ n1000"



                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.stonehen ge.com/merlyn/>

                Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.

                See PerlTraining. Stonehenge. com for onsite and open-enrollment Perl training!












                <!--

                #ygrp-mlmsg {font-size:13px;font-family:arial,helvetica,clean,sans-serif;}
                #ygrp-mlmsg table {font-size:inherit;font:100%;}
                #ygrp-mlmsg select, input, textarea {font:99% arial,helvetica,clean,sans-serif;}
                #ygrp-mlmsg pre, code {font:115% monospace;}
                #ygrp-mlmsg * {line-height:1.22em;}
                #ygrp-text{
                font-family:Georgia;
                }
                #ygrp-text p{
                margin:0 0 1em 0;
                }
                #ygrp-tpmsgs{
                font-family:Arial;
                clear:both;
                }
                #ygrp-vitnav{
                padding-top:10px;
                font-family:Verdana;
                font-size:77%;
                margin:0;
                }
                #ygrp-vitnav a{
                padding:0 1px;
                }
                #ygrp-actbar{
                clear:both;
                margin:25px 0;
                white-space:nowrap;
                color:#666;
                text-align:right;
                }
                #ygrp-actbar .left{
                float:left;
                white-space:nowrap;
                }
                .bld{font-weight:bold;}
                #ygrp-grft{
                font-family:Verdana;
                font-size:77%;
                padding:15px 0;
                }
                #ygrp-ft{
                font-family:verdana;
                font-size:77%;
                border-top:1px solid #666;
                padding:5px 0;
                }
                #ygrp-mlmsg #logo{
                padding-bottom:10px;
                }

                #ygrp-vital{
                background-color:#e0ecee;
                margin-bottom:20px;
                padding:2px 0 8px 8px;
                }
                #ygrp-vital #vithd{
                font-size:77%;
                font-family:Verdana;
                font-weight:bold;
                color:#333;
                text-transform:uppercase;
                }
                #ygrp-vital ul{
                padding:0;
                margin:2px 0;
                }
                #ygrp-vital ul li{
                list-style-type:none;
                clear:both;
                border:1px solid #e0ecee;
                }
                #ygrp-vital ul li .ct{
                font-weight:bold;
                color:#ff7900;
                float:right;
                width:2em;
                text-align:right;
                padding-right:.5em;
                }
                #ygrp-vital ul li .cat{
                font-weight:bold;
                }
                #ygrp-vital a {
                text-decoration:none;
                }

                #ygrp-vital a:hover{
                text-decoration:underline;
                }

                #ygrp-sponsor #hd{
                color:#999;
                font-size:77%;
                }
                #ygrp-sponsor #ov{
                padding:6px 13px;
                background-color:#e0ecee;
                margin-bottom:20px;
                }
                #ygrp-sponsor #ov ul{
                padding:0 0 0 8px;
                margin:0;
                }
                #ygrp-sponsor #ov li{
                list-style-type:square;
                padding:6px 0;
                font-size:77%;
                }
                #ygrp-sponsor #ov li a{
                text-decoration:none;
                font-size:130%;
                }
                #ygrp-sponsor #nc {
                background-color:#eee;
                margin-bottom:20px;
                padding:0 8px;
                }
                #ygrp-sponsor .ad{
                padding:8px 0;
                }
                #ygrp-sponsor .ad #hd1{
                font-family:Arial;
                font-weight:bold;
                color:#628c2a;
                font-size:100%;
                line-height:122%;
                }
                #ygrp-sponsor .ad a{
                text-decoration:none;
                }
                #ygrp-sponsor .ad a:hover{
                text-decoration:underline;
                }
                #ygrp-sponsor .ad p{
                margin:0;
                }
                o {font-size:0;}
                .MsoNormal {
                margin:0 0 0 0;
                }
                #ygrp-text tt{
                font-size:120%;
                }
                blockquote{margin:0 0 0 4px;}
                .replbq {margin:4;}
                -->







                [Non-text portions of this message have been removed]
              • Jenda Krynicky
                From: Bob Kardell ... You must be, this should work correctly. The local() will make sure the $/ gets reset to the default value at the
                Message 7 of 14 , Mar 6, 2007
                  From: Bob Kardell <bobkardell@...>
                  > I have tried using the paragraph mode in the past with some success,
                  > but this issue brought up a question I have had - is it possible to
                  > define "$/" two different ways in the same script? In the past when I
                  > have tried to redefine $/ to something other than the default I have
                  > had to create separate scripts. I noticed that you put the following
                  > code in:
                  >
                  >
                  > {
                  >
                  > local $/ = "";
                  >
                  > @data = <>; # slurp all input in paragraph mode
                  >
                  > }
                  >
                  > is it possible to do something like:
                  >
                  >
                  > {
                  > local $/ = "";
                  > @data = <>; # slurp all input in paragraph mode
                  > }
                  >
                  > #do some code here
                  >
                  > {
                  > local $/ = "\n\n";
                  > @data = <>; # slurp all input in paragraph mode
                  > }
                  >
                  > #do something else here
                  >
                  > When I have tried this in the past, the two separate $/ have not
                  > worked nicely with one another and I have to use two separate scripts
                  > because of it. I am obviously missing something.

                  You must be, this should work correctly. The local() will make sure
                  the $/ gets reset to the default value at the end of the block:

                  $a = "global value";
                  print "$a\n";
                  {
                  local $a = "local";
                  print "$a\n";
                  }
                  print "$a\n";
                  {
                  local $a = "other local";
                  print "$a\n";
                  }
                  print "$a\n";


                  Try it again and post the code if it doesn't work as expected.

                  Jenda
                  ===== Jenda@... === http://Jenda.Krynicky.cz =====
                  When it comes to wine, women and song, wizards are allowed
                  to get drunk and croon as much as they like.
                  -- Terry Pratchett in Sourcery
                • a_z0_9_blah
                  ... success, but this issue brought up a question I have had - is it possible to define $/ two different ways in the same script? In the past when I have
                  Message 8 of 14 , Mar 6, 2007
                    --- In perl-beginner@yahoogroups.com, Bob Kardell <bobkardell@...>
                    wrote:
                    >
                    > I have tried using the paragraph mode in the past with some
                    success, but this issue brought up a question I have had - is it
                    possible to define "$/" two different ways in the same script? In
                    the past when I have tried to redefine $/ to something other than
                    the default I have had to create separate scripts. I noticed that
                    you put the following code in:
                    >
                    >
                    > {
                    >
                    > local $/ = "";
                    >
                    > @data = <>; # slurp all input in paragraph mode
                    >
                    > }
                    >
                    > is it possible to do something like:
                    >
                    >
                    > {
                    >
                    > local $/ = "";
                    >
                    > @data = <>; # slurp all input in paragraph mode
                    >
                    > }
                    >
                    > #do some code here


                    see perldoc perlfunc for the function seek().


                    >
                    > {
                    >
                    > local $/ = "\n\n";
                    >
                    > @data = <>; # slurp all input in paragraph mode
                    >
                    > }
                    >
                    > #do something else here
                    >
                    > When I have tried this in the past, the two separate $/ have not
                    worked nicely with one another and I have to use two separate
                    scripts because of it. I am obviously missing something.
                    >
                    > Thanks,
                    >
                    > Bob
                  Your message has been successfully submitted and would be delivered to recipients shortly.