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> | [reply] [d/l] [select] |
|
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 | [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
|
|
|
So right you are. My mistake.
| [reply] |
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.
¤ | [reply] |
|
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.
| [reply] [d/l] |
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
| [reply] |
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 {
}
| [reply] [d/l] |
|
They are calls to subroutines. The code for the subroutines is not present in the code snippet you posted.
| [reply] |
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 | [reply] [d/l] [select] |
|
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. | [reply] [d/l] [select] |
|
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 | [reply] [d/l] [select] |