CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How to sort variables declared on multiple lines?

 



bulrush
Novice

Jul 10, 2014, 3:52 AM

Post #1 of 8 (4294 views)
How to sort variables declared on multiple lines? Can't Post

  • Perl 5.8.8 on Linux RHEL 5.5.56
  • I use PSPad 4.5.8 (2482), which runs on Windows. I can see my linux files in the Windows file manager so I can use Windows editors.

    I declare my variables at the beginning of a subroutine like so:


    Code
    my($b, $a, $k); 
    my($stuff,$data,$loc,$zpos);


    Is there a way to sort these with PSPad fairly easily? I believe PSPad uses Javascript for the scripting language but I don't know JS.

    Is there a better way to declare them, and then sort them, without putting them one per line like this? PSPad can sort selected lines.


    Code
    my($a); 
    my($c);
    my($b);
    my($zpos);
    my($data);
    my(@arr);


    Thanks.
    -----
    * Redhat Linux RHEL 5.5.56
    * Perl 5.8.8


  • BillKSmith
    Veteran

    Jul 10, 2014, 6:03 AM

    Post #2 of 8 (4216 views)
    Re: [bulrush] How to sort variables declared on multiple lines? [In reply to] Can't Post

    I cannot offer any help because I never heard of the PSPad editor before (sounds interesting).

    I am curious why you want to do this. As I mentioned in one of your other threads, declaring all variables at the beginning defeats the main advantage of using strict.
    Good Luck,
    Bill


    Laurent_R
    Veteran / Moderator

    Jul 10, 2014, 10:45 AM

    Post #3 of 8 (4041 views)
    Re: [bulrush] How to sort variables declared on multiple lines? [In reply to] Can't Post

    I agree 100% with everything Bill said, especially with respect to declaring the variables within the smallest possible lexical scope, rather than all at the beginning of your script.

    And I don't really understand what you are trying to do. You want to sort your variables by variable name? or by variable content? If you want to do it in a text editor, I suppose it is by variable names. But why would you want to do that? It seems fairly useless. Maybe you should say what you are trying to do, rather than ask how to do it the way you think it can be done. We might find a better solution.


    bulrush
    Novice

    Jul 11, 2014, 4:37 AM

    Post #4 of 8 (3647 views)
    Re: [Laurent_R] How to sort variables declared on multiple lines? [In reply to] Can't Post


    Quote
    especially with respect to declaring the variables within the smallest possible lexical scope, rather than all at the beginning of your script.


    I don't declare all variables as global at the beginning of my script, I declare them at the beginning of subroutines. Just so all the declarations are in one place.

    IMO, declaring variables in minimal scope, like just where they are first used, complicates matters, makes it harder to find where the variable is declared, and makes the program less readable, which violates the Rules of Programming.
    -----
    * Redhat Linux RHEL 5.5.56
    * Perl 5.8.8


    BillKSmith
    Veteran

    Jul 11, 2014, 6:24 AM

    Post #5 of 8 (3576 views)
    Re: [bulrush] How to sort variables declared on multiple lines? [In reply to] Can't Post

    Perhaps unfortunately, "good programming practice" depends somewhat on the language we use. It is easier to find declarations specified your way. This applies to every language I have ever used, including Perl. However, taking full advantage of Perl's scoping rules confers much larger advantages. Accidental misuse of variables is detected at compile time. Problems with variables not being "reinitialized" are eliminated because we get a fresh copy of the variable. Note: Other Perl programmers have come to expect this behavior.

    In my experience, there is very little reason to 'find' a declaration in a Perl script. It contains far less information than the declarations in most other languages.
    Good Luck,
    Bill


    FishMonger
    Veteran / Moderator

    Jul 11, 2014, 6:35 AM

    Post #6 of 8 (3569 views)
    Re: [bulrush] How to sort variables declared on multiple lines? [In reply to] Can't Post


    Quote
    IMO, declaring variables in minimal scope, like just where they are first used, complicates matters, makes it harder to find where the variable is declared, and makes the program less readable, which violates the Rules of Programming.


    Nearly all experienced Perl programmers would disagree with that conclusion.

    Your question in this thread is about the usage of a specific text editor which apparently no-one else here has heard of and has nothing to do with Perl, so it's clearly an OT question.


    Quote
    I believe PSPad uses Javascript for the scripting language but I don't know JS.

    Is this a web based editor? If not, then why would you think it uses javascript?

    You say PSPad can sort lines, so what happens when you used that feature to sort the var declaration lines?

    If you're wanting a text editor to take your original 2 lines that declare those 7 vars and split them out into 7 individual declarations in sorted order, you're out of luck. No text editor can do that. You would need to use a parser that understands perl syntax, not a plain text editor.


    FishMonger
    Veteran / Moderator

    Jul 11, 2014, 7:43 AM

    Post #7 of 8 (3522 views)
    Re: [bulrush] How to sort variables declared on multiple lines? [In reply to] Can't Post


    Quote
    I declare my variables at the beginning of a subroutine like so:


    Code
    	 
    my($b, $a, $k);
    my($stuff,$data,$loc,$zpos);



    I'm not sure if you are aware of this, but $a and $b are special built-in global vars used by the sort function. It's not good practice to declare them as lexicals, especially if they are in the same scope as a call to the sort function that needs to use them.

    Here's an example why it's bad to do that.

    Code
    use strict; 
    use warnings;
    use Data::Dumper;

    my ($a, $b);
    my @array = qw( 5 2 9 3 8);
    my @sorted = sort{$a <=> $b} @array;

    print Dumper \@sorted;


    Outputs:

    Code
    "my $a" used in sort comparison at C:\test\bulrush.pl line 8. 
    "my $b" used in sort comparison at C:\test\bulrush.pl line 8.
    Use of uninitialized value $b in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $a in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $b in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $a in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $b in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $a in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $b in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $a in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $b in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $a in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $b in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $a in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $b in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $a in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $b in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    Use of uninitialized value $a in numeric comparison (<=>) at C:\test\bulrush.pl line 8.
    $VAR1 = [
    '5',
    '2',
    '9',
    '3',
    '8'
    ];

    Code
    
    
    
          
        


    Laurent_R
    Veteran / Moderator

    Jul 11, 2014, 11:02 AM

    Post #8 of 8 (3393 views)
    Re: [bulrush] How to sort variables declared on multiple lines? [In reply to] Can't Post


    In Reply To
    IMO, declaring variables in minimal scope, like just where they are first used, complicates matters, makes it harder to find where the variable is declared, and makes the program less readable, which violates the Rules of Programming.


    The main (actually almost unique) purpose of declaring variables with my in Perl is to define its lexical scope. Narrowing that scope as much as possible is good practice. As for the so-called Rules of Programming, this might be true for the rules of C programming, but it is certainly not a universal rule of programming.

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives