Perl help please :(

Bes

Bes

Soldato
Joined
18 Oct 2002
Posts
7,318
Location
Melbourne
Hi

Trying to write a perl script that does this:

-Reads in a list of values from a txt file and splits into an array (filenames)

-Does a find on a directory structure and sorts the results into last modified order (It then processes each filename in a subroutine)

-Looks at the contents of the array... (foreach?) If the filename from the previous step is NOT in the array, it does processing. If it IS there, it does nothing and the sub exits (i.e. next find result is evaluated).

Now steps one and two are fine, but step 3 I cannot get working... THis is because if I use a simple foreach, I am going to end up processing the same data many times (for example- if the entry is at the 5th position in the array, I am going to process the file 4 times before I realise it's there).

I don't want to use a hash because the order in which the values are in the array is significant, and I also need to trim the array before writing it back to the file (i.e. so I don't end up with zillions of useless entries after a few weeks).

Can anyone help?

Thanks
 
Ah waddaya know- 10 minutes later I found the answer:

PHP:
if (grep /^$rel1$/, @test)
{
     print "Result = $rel1\n";
}

I had assumed grep was the unix command grep, hence would not be portable, but have realised it is actually a perl function, so should be fine for my purposes :)
 
Ah waddaya know- 10 minutes later I found the answer:

PHP:
if (grep /^$rel1$/, @test)
{
     print "Result = $rel1\n";
}

I had assumed grep was the unix command grep, hence would not be portable, but have realised it is actually a perl function, so should be fine for my purposes :)

Yep, you can use grep in this form too

PHP:
if(grep {/^$rel1$/} @test){
 print $re1, "\n";
}

Note
PHP:
 print $rel1, "\n";
is faster than
PHP:
 print "$rel1\n";
 
Hi,

Why is it faster to break the $rel1 out of the string? Is this always the case in perl?

Thanks
 
Hi,

Why is it faster to break the $rel1 out of the string? Is this always the case in perl?

Thanks

Hi Bes,

The honestly answer is I don't know :p it's actually a tip from the book "Mastering Algorithms in Perl" (O'Rielly) I guess Perl (at some level) actually needs to do some kind of regular expression on the string contents itself so
PHP:
print "This is a string to print : \t $my_string \n";

So Perl at some stage needs to check that things inbetween quotation marks contain strings or arrays or whatever you stick in them. So it'd look like this if it was a perl subroutine
PHP:
sub my_print_function{
$string_to_be_printed = $_[0];
if($string_to_be_print =~ /\$/){
 # do something here to actually print something 
}
}

Lower level languages don't allow you (as far as I'm aware) to do this neat trick of putting variables inbetween quote marks. So in Java it'd be
[JAVA]
System.out.println("This is a string to print : \t "+my_string);
[/JAVA]

Things like this don't really slow things down that much

Anyhow from the book itself:
Efficiency Tip: Printing. Why do we use print $_, "\n" instead of the simpler print
"$_\n" or even print $_. "\n"? Speed. "$_\n" is about 1.5% slower than $_. "\n" (even
though the latter is what they both compile into) and 21% slower than $_, "\n".
 
Back
Top Bottom