Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: sorting inside specific

by mutated (Monk)
on Sep 10, 2004 at 13:25 UTC ( [id://390052]=note: print w/replies, xml ) Need Help??


in reply to sorting inside specific

This is nothing special but it should work...
open(IN, "<sort.in") || die "\nFailed to open infile $?\n"; open(OUT,">sort.out"); my @array; while(<IN>) { if (/<SEP>/) { print OUT join '',sort {$a <=> $b} @array; print OUT "<SEP>\n"; @array=(); } else { @array = (@array,$_); } } print OUT join '',sort {$a <=> $b} @array; close OUT; close IN;
UPDATE: Got rid of errors to occur when run with use strict and use warnings


daN.

Replies are listed 'Best First'.
Re^2: sorting inside specific
by Limbic~Region (Chancellor) on Sep 10, 2004 at 13:37 UTC
    mutated,
    but it should work

    Really? AFAICT it isn't even close. What exactly is sort numerically @array supposed to do? At what point are you storing anything in @array? Even after fixing your coding mistakes (strictures and warnings)++ - all I get for output is:

    <sep> <sep>

    Cheers - L~R

    Update: Even your updated code is broke. I preserved your original code in HTML comments. You would see the problem if you had warnings and strictures on
    Update 2: Good job! Not exactly the way I would have done it, but it works ;-)
      No worries, I should have tested before I posted in the first place..too early and no morning coffee..sigh..at any rate it works now :P..it's still not as eloquent as your solution but I'm learning...still trying rap my head around the sort function


      daN.
        mutated,
        at any rate it works now

        No it doesn't. I understand that you are learning and am really not trying to beat you up here but before saying something works you should verify the output against the expected results. I have only slightly modified your code, but I left in your case sensitivity mistake SEP vs sep.

        #!/usr/bin/perl use strict; use warnings; my @array; while( <DATA> ) { @array = (@array,$_); if (/<sep>/) { print join '',sort {$a <=> $b} @array; print "<sep>\n"; @array=(); } } print join '',sort {$a <=> $b} @array; __DATA__ <sep> 22 1 3 <sep> 4 2 44
        Here is the output:
        Argument "<sep>\n" isn't numeric in sort at foo.pl line 14, <DATA> lin +e 8. Argument "<sep>\n" isn't numeric in sort at foo.pl line 14, <DATA> lin +e 8. <sep> <sep> 1 2 3 4 22 44

        Cheers - L~R

Re^2: sorting inside specific
by jryan (Vicar) on Sep 10, 2004 at 19:37 UTC

    Take a look at push.

    @array = (@array,$_); # copies the entire array every time
    push @array, $_; # does not copy the entire array every time

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://390052]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found