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

Perl Problem

Expand Messages
  • hiprateek007
    Hi, I have a task of sorting a file paragraph wise on the basis of an integer in the first line of those paragraphs. Like: the integer is 10 .... .... p1 the
    Message 1 of 11 , Jul 5, 2010
    • 0 Attachment
      Hi,
      I have a task of sorting a file paragraph wise on the basis of an integer in the first line of those paragraphs.

      Like:

      the integer is 10
      ....
      ....
      p1

      the integer is 20
      .....
      ...
      ....
      p2

      the output sd be :

      p2

      p1

      I managed to get this done by command:
      perl -00lne 'push @{ $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;}{print for map @{ $h{ $_ } }, sort { $b <=> $a } keys %h;'filename

      But now I also want to print the some of those integers. I guess we can use the $a and $b to do that but I dont know how exactly to do that since I am all new to perl.

      Thanks in advance

      Prateek Mathur
    • Shlomi Fish
      ... Don t use $a and $b because the same numbers are likely to repeat more than one time for each sort invocation. Instead use List::Util s sum() on keys(%h).
      Message 2 of 11 , Jul 6, 2010
      • 0 Attachment
        On Tuesday 06 Jul 2010 08:19:19 hiprateek007 wrote:
        > Hi,
        > I have a task of sorting a file paragraph wise on the basis of an integer
        > in the first line of those paragraphs.
        >
        > Like:
        >
        > the integer is 10
        > ....
        > ....
        > p1
        >
        > the integer is 20
        > .....
        > ...
        > ....
        > p2
        >
        > the output sd be :
        >
        > p2
        >
        > p1
        >
        > I managed to get this done by command:
        > perl -00lne 'push @{ $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;}{print for map
        > @{ $h{ $_ } }, sort { $b <=> $a } keys %h;'filename
        >
        > But now I also want to print the some of those integers. I guess we can use
        > the $a and $b to do that but I dont know how exactly to do that since I am
        > all new to perl.
        >

        Don't use $a and $b because the same numbers are likely to repeat more than
        one time for each sort invocation. Instead use List::Util's sum() on keys(%h).
        For more information see:

        http://perl-begin.org/

        I should note that if your command-line script ("one-liner") gets too
        complicated like that, it is usually a good idea to convert it to a full-
        fledged program in a file with "use strict;" and "use warnings;".

        Regards,

        Shlomi Fish

        > Thanks in advance
        >
        > Prateek Mathur

        --
        -----------------------------------------------------------------
        Shlomi Fish http://www.shlomifish.org/
        What Makes Software Apps High Quality - http://shlom.in/sw-quality

        God considered inflicting XSLT as the tenth plague of Egypt, but then
        decided against it because he thought it would be too evil.

        Please reply to list if it's a mailing list post - http://shlom.in/reply .
      • Prateek Mathur
        Hi Shlomi Fish, Can you tell me how to write this exactly as I am not aware of perl syntax. This is the generated code for the single line command: BEGIN { $/
        Message 3 of 11 , Jul 6, 2010
        • 0 Attachment
          Hi Shlomi Fish,

          Can you tell me how to write this exactly as I am not aware of perl syntax.
          This is the generated code for the single line command:

          BEGIN { $/ = ""; $\ = "\n\n"; }
          LINE: while (defined($_ = <ARGV>)) {
                      chomp $_;
                          push @{$h{$1};}, $_ if /of ([0-9]+) bytes/m;
                  }
                  {
                              foreach $_ (map @{$h{$_};}, (sort {$b <=> $a} keys %h))
                              {
                                              print $_;
                                             
                              }
                             
                  }

          How List::Utils sum needs to be added?

          Regards
          Prateek Mathur
          --- On Tue, 7/6/10, Shlomi Fish <shlomif@...> wrote:

          From: Shlomi Fish <shlomif@...>
          Subject: Re: [PBML] Perl Problem
          To: perl-beginner@yahoogroups.com
          Date: Tuesday, July 6, 2010, 1:38 PM

          On Tuesday 06 Jul 2010 08:19:19 hiprateek007 wrote:
          > Hi,
          >  I have a task of sorting a file paragraph wise on the basis of an integer
          > in the first line of those paragraphs.
          >
          > Like:
          >
          > the integer is 10
          > ....
          > ....
          > p1
          >
          > the integer is 20
          > .....
          > ...
          > ....
          > p2
          >
          > the output sd be :
          >
          > p2
          >
          > p1
          >
          > I managed to get this done by command:
          > perl -00lne 'push @{ $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;}{print for map
          > @{ $h{ $_ } }, sort { $b <=> $a } keys %h;'filename
          >
          > But now I also want to print the some of those integers. I guess we can use
          > the $a and $b to do that but I dont know how exactly to do that since I am
          > all new to perl.
          >

          Don't use $a and $b because the same numbers are likely to repeat more than
          one time for each sort invocation. Instead use List::Util's sum() on keys(%h).
          For more information see:

          http://perl-begin.org/

          I should note that if your command-line script ("one-liner") gets too
          complicated like that, it is usually a good idea to convert it to a full-
          fledged program in a file with "use strict;" and "use warnings;".

          Regards,

              Shlomi Fish

          > Thanks in advance
          >
          > Prateek Mathur

          --
          -----------------------------------------------------------------
          Shlomi Fish       http://www.shlomifish.org/
          What Makes Software Apps High Quality -  http://shlom.in/sw-quality

          God considered inflicting XSLT as the tenth plague of Egypt, but then
          decided against it because he thought it would be too evil.

          Please reply to list if it's a mailing list post - http://shlom.in/reply .


          ------------------------------------

          Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.htmlYahoo! Groups Links








          [Non-text portions of this message have been removed]
        • Shlomi Fish
          ... Well, you should be. This list is for Perl beginners who wish to learn Perl. See the resources on http://perl-begin.org/ for guidance how to learn Perl.
          Message 4 of 11 , Jul 6, 2010
          • 0 Attachment
            On Tuesday 06 Jul 2010 13:23:12 Prateek Mathur wrote:
            > Hi Shlomi Fish,
            >
            > Can you tell me how to write this exactly as I am not aware of perl syntax.

            Well, you should be. This list is for Perl beginners who wish to learn Perl.
            See the resources on http://perl-begin.org/ for guidance how to learn Perl.

            > This is the generated code for the single line command:
            >
            > BEGIN { $/ = ""; $\ = "\n\n"; }
            > LINE: while (defined($_ = <ARGV>)) {
            > chomp $_;
            > push @{$h{$1};}, $_ if /of ([0-9]+) bytes/m;
            > }
            > {
            > foreach $_ (map @{$h{$_};}, (sort {$b <=> $a} keys %h))
            > {
            > print $_;
            >
            > }
            >
            > }
            >
            > How List::Utils sum needs to be added?

            Reading from perldoc List::Util:

            [quote]
            sum LIST
            Returns the sum of all the elements in LIST. If LIST is empty then
            "undef" is returned.

            $foo = sum 1..10 # 55
            $foo = sum 3,9,12 # 24
            $foo = sum @bar, @baz # whatever

            This function could be implemented using "reduce" like this

            $foo = reduce { $a + $b } 1..10

            If your algorithm requires that "sum" produce an identity of 0,
            then make sure that you always pass 0 as the first argument to
            prevent "undef" being returned

            $foo = sum 0, @values;
            [/quote]

            Regards,

            Shlomi Fish

            >
            > Regards
            > Prateek Mathur

            --
            -----------------------------------------------------------------
            Shlomi Fish http://www.shlomifish.org/
            "The Human Hacking Field Guide" - http://shlom.in/hhfg

            God considered inflicting XSLT as the tenth plague of Egypt, but then
            decided against it because he thought it would be too evil.

            Please reply to list if it's a mailing list post - http://shlom.in/reply .
          • Sakura Kinomoto
            I read ur problem entirely,but i don t know what does this perl -00lne push @{ $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;}{print for map @{ $h{ $_ } }, sort { $b
            Message 5 of 11 , Jul 14, 2010
            • 0 Attachment
              I read ur problem entirely,but i don't know what does this perl -00lne 'push @{
              $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;}{print for map @{ $h{ $_ } }, sort { $b
              <=> $a } keys %h;'filename mean,why don't u try to write it as a
              script~?translate ur command-line code into a true perl code,and it can let u
              know where goes wrong^.^





              ________________________________
              From: hiprateek007 <hiprateek007@...>
              To: perl-beginner@yahoogroups.com
              Sent: Tue, July 6, 2010 1:19:19 PM
              Subject: [PBML] Perl Problem

               
              Hi,
              I have a task of sorting a file paragraph wise on the basis of an integer in the
              first line of those paragraphs.

              Like:

              the integer is 10
              ....
              ....
              p1

              the integer is 20
              .....
              ...
              ....
              p2

              the output sd be :

              p2

              p1

              I managed to get this done by command:
              perl -00lne 'push @{ $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;}{print for map @{
              $h{ $_ } }, sort { $b <=> $a } keys %h;'filename

              But now I also want to print the some of those integers. I guess we can use the
              $a and $b to do that but I dont know how exactly to do that since I am all new
              to perl.

              Thanks in advance

              Prateek Mathur







              [Non-text portions of this message have been removed]
            • Prateek Mathur
              Hi All,              Extending to the question I asked earlier I have one more task to do and I think perl can handle that easily. What I need to
              Message 6 of 11 , Jul 14, 2010
              • 0 Attachment
                Hi All,
                             Extending to the question I asked earlier I have one more task to do and I think perl can handle that easily.

                What I need to do is filer a file with multiple paragraphs based on certain criteria.
                1) Like I need to delete all those paragraphs which contain certain keyword
                         e.g delete all those paragraphs which contain words foo , bar , etc...(any one is sufficient to delete)
                2) Which contain two keywords one follwed by the other.
                     e.g all paragraphs that contain word foo followed by bar anywhere in the remaining text of the paragraph.
                3) Delete all single line paragraphs.

                Also I need to save all the contents which will be deleted in a separate file and all the filtered contents in another file.

                I was able to do this task of deleting using sed also but I am not sure how can I save the deleted content using sed.

                Please let me know in case something is not clear.

                Thanks in advance

                Prateek
                     

                --- On Wed, 7/14/10, Sakura Kinomoto <sxsakura@...> wrote:

                From: Sakura Kinomoto <sxsakura@...>
                Subject: Re: [PBML] Perl Problem
                To: perl-beginner@yahoogroups.com
                Date: Wednesday, July 14, 2010, 1:18 PM
















                 









                I read ur problem entirely,but i don't know what does this perl -00lne 'push @{

                $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;}{print for map @{ $h{ $_ } }, sort { $b

                <=> $a } keys %h;'filename mean,why don't u try to write it as a

                script~?translate ur command-line code into a true perl code,and it can let u

                know where goes wrong^.^



                ________________________________

                From: hiprateek007 <hiprateek007@...>

                To: perl-beginner@yahoogroups.com

                Sent: Tue, July 6, 2010 1:19:19 PM

                Subject: [PBML] Perl Problem



                 

                Hi,

                I have a task of sorting a file paragraph wise on the basis of an integer in the

                first line of those paragraphs.



                Like:



                the integer is 10

                ....

                ....

                p1



                the integer is 20

                .....

                ...

                ....

                p2



                the output sd be :



                p2



                p1



                I managed to get this done by command:

                perl -00lne 'push @{ $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;}{print for map @{

                $h{ $_ } }, sort { $b <=> $a } keys %h;'filename



                But now I also want to print the some of those integers. I guess we can use the

                $a and $b to do that but I dont know how exactly to do that since I am all new

                to perl.



                Thanks in advance



                Prateek Mathur



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






























                [Non-text portions of this message have been removed]
              • Jenda Krynicky
                To: perl-beginner@yahoogroups.com From: Sakura Kinomoto Date sent: Wed, 14 Jul 2010 00:48:42 -0700 (PDT)
                Message 7 of 11 , Jul 19, 2010
                • 0 Attachment
                  To: perl-beginner@yahoogroups.com
                  From: Sakura Kinomoto <sxsakura@...>
                  Date sent: Wed, 14 Jul 2010 00:48:42 -0700 (PDT)
                  Subject: Re: [PBML] Perl Problem
                  Send reply to: perl-beginner@yahoogroups.com

                  > I read ur problem entirely,but i don't know what does this perl -00lne 'push @{
                  > $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;}{print for map @{ $h{ $_ } }, sort { $b
                  > <=> $a } keys %h;'filename mean,why don't u try to write it as a
                  > script~?translate ur command-line code into a true perl code,and it can let u
                  > know where goes wrong^.^

                  I read your message entirely and right after I quit vomiting I sat
                  down to write a response. FOR GAWDS SAKE FORGET THAT STUPID KEWL
                  SPELLING.

                  There is no "ur" or "u" in English!

                  Jenda
                  P.S.: You do not have to respond to every single message sent to the
                  list, if you do not understand the question, why do you think your
                  reply will be of any use?
                  ===== 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
                • Rob Richardson
                  Ladies, gentlemen and others (of whom I hope we don t have too many), Please keep discussions civil. It is possible to suggest to others that their posts
                  Message 8 of 11 , Jul 19, 2010
                  • 0 Attachment
                    Ladies, gentlemen and others (of whom I hope we don't have too many),

                    Please keep discussions civil. It is possible to suggest to others
                    that their posts might be more easily understood if they are written
                    in correct English without insulting them.

                    Rob Richardson
                    Moderator, perl-beginner@yahoogroups.com
                  • Kenneth Wolcott
                    Hi; Here s my request: Please do not use phonetic shorthand English that is popular during cell phone texting if you want me to read and (hopefully) respond to
                    Message 9 of 11 , Jul 19, 2010
                    • 0 Attachment
                      Hi;

                      Here's my request:

                      Please do not use phonetic shorthand English that is popular during cell
                      phone texting if you want me to read and (hopefully) respond to your
                      posting.

                      Thanks,
                      Ken Wolcott

                      On Mon, Jul 19, 2010 at 14:52, Rob Richardson <CedricCicada@...>wrote:

                      >
                      >
                      > Ladies, gentlemen and others (of whom I hope we don't have too many),
                      >
                      > Please keep discussions civil. It is possible to suggest to others
                      > that their posts might be more easily understood if they are written
                      > in correct English without insulting them.
                      >
                      > Rob Richardson
                      > Moderator, perl-beginner@yahoogroups.com <perl-beginner%40yahoogroups.com>
                      >
                      >


                      [Non-text portions of this message have been removed]
                    • merlyn@stonehenge.com
                      ... Kenneth Please do not use phonetic shorthand English that is popular during cell Kenneth phone texting if you want me to read and (hopefully) respond
                      Message 10 of 11 , Jul 19, 2010
                      • 0 Attachment
                        >>>>> "Kenneth" == Kenneth Wolcott <kennethwolcott@...> writes:

                        Kenneth> Please do not use phonetic shorthand English that is popular during cell
                        Kenneth> phone texting if you want me to read and (hopefully) respond to your
                        Kenneth> posting.

                        Seconded.

                        As I say on IRC,

                        "if you're paying for IRC by the character, sucks to be u". :)

                        --
                        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                        Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                        See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
                      • andy_bach@wiwb.uscourts.gov
                        Another usage note Perl Problem isn t much of a subject line - er, what *else* would you be writing to the list with? ;- perl -00lne push @{ $h{ $1 } },
                        Message 11 of 11 , Jul 20, 2010
                        • 0 Attachment
                          Another usage note "Perl Problem" isn't much of a subject line - er, what
                          *else* would you be writing to the list with? ;->


                          perl -00lne '
                          push @{ $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;
                          }
                          {
                          print for map @{ $h{ $_ } }, sort { $b <=> $a } keys %h;' /tmp/h.dat

                          -00 is used to set perl to 'slurp' in paragraph mode (see perldoc perlrun
                          -0 (that's zero) option). -l is to add newlines to print stmts. -n is is
                          in auto-while loop and -e is to mark the script. Those unmatched curlies
                          are a trick to get an loop and END block - because -n takes your code and
                          wraps it in while loop
                          while (<>) {
                          <-e code>
                          }

                          adding a close then open curly, in the middle of your code make it:
                          while (<>) {
                          <-e code up to close curly>
                          }
                          {
                          <-e code after curlies>

                          push @{ $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;

                          finds the digits before the word "bytes", uses it as a hash key for %h,
                          which is then treated as an anonymous array and the whole line saved
                          therein <ahem> (that sentence is as convoluted as this code - sorry ;-).
                          The "m" flag isn't needed (?) as there's no "." in the RE, I believe. I
                          don't recall the data set but they may have meant/needed to use "\s+" for
                          the whitespace (if the data were wrapping on lines, say).

                          print for map @{ $h{ $_ } }, sort { $b <=> $a } keys %h;' /tmp/h.dat

                          after the data's been processed, we fetch back that # of bytes key, sort
                          them reverse numerically ("$b" before "$a") and print each line of the
                          anonymous array for that key.

                          #!/usr/bin/perl -00l
                          use strict;
                          use warnings;
                          my %data_lines;
                          while (<>) {
                          next unless /of (\d+) bytes/;
                          push @{ $data_lines{$1} }, $_;
                          # warn("adding $1 - $_");
                          }
                          foreach my $key ( sort { $b <=> $a } keys %data_lines ) {
                          print "Bytes: $key";
                          foreach my $line ( @{ $data_lines{$key} } ) {
                          # whatever you want, line by line now
                          print "\tdata: $line";
                          } # foreach line
                          } # foreach key


                          a

                          ----------------------
                          Andy Bach
                          Systems Mangler
                          Internet: andy_bach@...
                          Voice: (608) 261-5738;
                          Cell: (608) 658-1890

                          "Every time I see an adult on a bicycle, I no longer despair for the
                          future of
                          the human race."
                          H. G. Wells, 1904

                          [Non-text portions of this message have been removed]
                        Your message has been successfully submitted and would be delivered to recipients shortly.