Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Code explanation needed (was: What does this mean)

by Anonymous Monk
on Jun 10, 2002 at 13:12 UTC ( [id://173108]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

my @matches = grep { $file[$_] =~ /^\Q$storeline|\E/ } 0..$#file; no_match($storeline), return unless @matches; duped_ids($storeline), return if @matches > 1;
can someone explain to me in words what each thing is doing there.

Edit kudra, 2002-06-10 Changed title

Replies are listed 'Best First'.
Re: What does this mean
by Sifmole (Chaplain) on Jun 10, 2002 at 13:29 UTC
    my @matches = grep { $file[$_] =~ /^\Q$storeline|\E/ } 0..$#file;
    Loops through an array "file" pulling out array indexes which start with the string "$storeline|" (whatever storeline is) and set @matches to the result. This could be written as,
    @matches = grep { /^\Q$storeline|\E/ } @file;
    Calls the subroutine no_match passing $storeline as the arguement. returns if @matches is empty.
    no_match($storeline), return unless @matches;
    Calls the subroutine duped_ids passing $storeline as the arguement. returns if the number of elements in @matches is greater than 1.
    duped_ids($storeline), return if @matches > 1;
    </CODE>
      Eh, no,
      my @matches = grep { $file[$_] =~ /^\Q$storeline|\E/ } 0..$#file;
      and
      my @matches = grep { /^\Q$storeline|\E/ } @file;
      are not equivalent. The former grabs the indices of where matches occur, while the latter grabs the matches themselves. Without knowing more about the rest of the program, I'm not going to say which version should be preferred.

      Abigail

        In Sifmole's defense, they are equivalent in the context of the code given. Since the values within @matches are never explicitly used, it doesn't matter precisely what is in there, just how many entries there are.

        This means, taking Sifmole's optimization one step further is possible:
        my $matches = grep { /^\Q$storeline|\E/ } @file; if ($matches > 1) { duped_ids($storeline); } elsif (!$matches) { no_match($storeline); } return;
        What I mean is that while technically they are not equivalent, in this particular sub-section of the program, they could be considered functionally equivalent.

        Admittedly, as you pointed out, there might be more code, but it's not posted here.
        So right you are. My mistake.
Re: I got dem co(smic)|(ma) blues again Momma..
by frankus (Priest) on Jun 10, 2002 at 13:23 UTC
    Line 1:
    Collect all the lines that match the regex from the array @file, it's a dumb way of doing it but hey..

    Line 2:
    Needs a $ at the start, stores the regex in a hash of non_matches unless there are matches in the @matches array.

    Line 3:
    Again needs a $ at the start. Stores the regex in a hash if there are duplicates.

    It could be written nicer.. ;)

    --

    Brother Frankus.

    ¤

      Line 2 and 3 start with subroutine calls (not hash elements), so they don't need a $ at the start.
      -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: Code explanation needed (was: What does this mean)
by Zaxo (Archbishop) on Jun 11, 2002 at 05:03 UTC

    I'd like to point out that this fragment is from my Re: Help. Attribution of it and reference to its parent would have saved a lot of speculation and justifiable wrong turns in this thread. I did point out that the &no_match and &duped_id subs were up for grabs and could be used for error reporting. I'd expect duplicate ids in the database to be a fatal error, and no_match() to warn of bad input.

    In my reply, the emphasis was on error handling, which had pretty much been ignored in AM's original code. Perhaps I should have just suggested one of the CSV modules.

    ++Abigail-II for clear analysis.

    After Compline,
    Zaxo

Re: Code explanation needed (was: What does this mean)
by Anonymous Monk on Jun 10, 2002 at 13:54 UTC
    I dont get what you mean by there subs, dont the ususally have to start with
    sub blah { }
      They are calls to subroutines. The code for the subroutines is not present in the code snippet you posted.
Deleting lines from an array
by Anonymous Monk on Jun 10, 2002 at 14:30 UTC
    Im trying to delete a $array[value] out of @array, but wehn I print to my text file it like
    --------- 1|cool.gif|aaa|yes|yes 3|biggrin.gif|:D|biggie|yes 5|tongue.gif|:p|tongue|yes ---------
    See the space, thats what it does when it does, I want that gone.
    sub set_del_yes { $storeline = $q->param('id'); open(FILE, "info/smileset.txt"); @file = <FILE>; close(FILE); chomp @file; my @matches = grep { $file[$_] =~ /^\Q$storeline|\E/ } 0..$#file; no_match($storeline), return unless @matches; duped_ids($storeline), return if @matches > 1; undef($file[$matches[0]]); delete($file[$matches[0]]); print "Smiley set has been deleted!<br><br> <a href=\"admin.cgi?pass=$pas&do=settings_set\">Smiley Set</a>"; open FLE, '>', 'info/smileset.txt' or die $!; print FLE $_, $/ for @file; close FLE or die $!; }

    Edit kudra, 2002-06-10 Fixed literal [ and friend

    Title edit by tye

      Change this:

      print FLE $_, $/ for @file;

      To this:

      for ( @file ) { print FLE "$_\n" if /\S/; # must have at least one non-whitespace }

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      That's because a delete $array [$indx] only changes the size of the array if you are deleting from the end. If you really want it gone, you would use splice, but splicing away lots of individual elements from a large array is costly (as each splice in general take time linear in the size of the array).

      You could change your print to:

      defined ($_) && print FLE $_, $/ for @file;
      unless you have undeleted, undefined elements in @file which you want to print. In that case, you could do something like:
      foreach (0 .. $#file) { print FLE $file [$_], $/ if exists $file [$_] }

      Abigail

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found