KevinADC 192 Practically a Posting Shark

A bare-bones code snippet to remove duplicate lines from a file. There are a number of ways to accomplish this task but this is a fast and dependable method using perls inplace editor and a simple hash to get the job done.

This probably should not be used for really big files, but files with a few thousand lines or even a few tens of thousands of lines should be OK. The bigger the file, the longer it may take to run.

d5e5 commented: I was looking for an example of using inplace editing without having to put the filename on the command line and there it was: local anonymous block and local @ARGV. Thanks. +1
KevinADC 192 Practically a Posting Shark

Given the date in mm-dd-yyyy (or M-D-YYYY) format (or any combination of that format) you can find out what day of the week that was on or is on or will be on, within the limitations of the system the script resides on.

This snippet uses only core functions and modules so nothing needs to be installed but it is worth mentioning that there are numerous date modules on CPAN that offer a wide range of functions for handling date calculations.

http://search.cpan.org/search?query=date&mode=all

This snippet assumes the date is in mm-dd-yyyy format but can easily be adapted to handle any combination of those date parameters. Can also handle years in three digit and two digit format but see the Time::Local and localtime() documentation for limitations.

leo002000 commented: 3 +3
KevinADC 192 Practically a Posting Shark

The perl documentation would have explained all of your errors. But I'll short-cut it for you.

First, $a and $b are variables used by perl for sorting and should not be used by you except for that purpose. Since they are global package variables you don't have to declare them with "my".

Quoted from the sort functions documentation:

In the interests of efficiency the normal calling code for subroutines is bypassed, with the following effects: the subroutine may not be a recursive subroutine, and the two elements to be compared are passed into the subroutine not via @_ but as the package global variables $a and $b (see example below). They are passed by reference, so don't modify $a and $b . And don't try to declare them as lexicals either.

So $a and $b need to be changed and all your variable need to be declared within scope using "my".

#!/usr/bin/perl
use warnings; 
use strict;

my $x = 6;
my $y = 9;
my $c = 7;

print $x, "\n";
print $y, "\n";
print $c, "\n";

I changed "." to "," in the print lines because using the dot (concatenation) is slow. 'print' is a list operator so it expects a list of things to print, so using a comma to create a list of thing to print is very fast as it doesn't force perl to build any intermitent strings first.

Quoted from the print operators documentation:

Prints a string …

VernonDozier commented: Helpful. +15
katharnakh commented: Brilliant explanation to know something unknown :) +4
KevinADC 192 Practically a Posting Shark

If you really want to do it manually you need a recursive function, something like this:

use warnings;
use strict;

#push @INC, "C:/Perl/Modules";

print "What is current download directory? ";
my $dir = <STDIN>;
chomp $dir;

list($dir);

sub list {
   my ($dir) = @_;
   print "$dir\n";
   opendir(my $DH, $dir) or do{print "Can't open $dir: $!\n"; return};
   my @list = grep {$_ ne '.' and $_ ne '..'} readdir $DH;
   foreach my $file (@list){
      print "\t$file\n";
      list("$dir/$file") if (-d "$dir/$file");
   }
}
musturd commented: Helped a lot! +2
KevinADC 192 Practically a Posting Shark
$decVal = <STDIN>;
chomp($decVal);#<--- you need this
KevinADC 192 Practically a Posting Shark

You need to print the header first, not second:

print $cgi->start_html(), $cgi->header();


should be:

print $cgi->header(), $cgi->start_html();

Comatose commented: :) +12
sid78669 commented: Rightt on time! +1
KevinADC 192 Practically a Posting Shark

You can't get the data sent to redirect.pl parsed by showdata.pl the way you are trying.

You have to recieve the data in your redirect.pl script and save it to disk and have showdata.pl open the file and get the data from a file or send the data to showdata.pl the same way the html form does, using POST or GET methods. You can use a module like LWP::UserAgent to send data from one CGI script to another. But if both scripts run on the same server you probably want to look into CGI::Sessions or maybe there are newer modules by now.

Comatose commented: Great Answer! +12
KevinADC 192 Practically a Posting Shark

I just need the logic of doing it .The real data is very close to the data which i gave.

Use a regular expression and a counter.

Comatose commented: Exactly +10
KevinADC 192 Practically a Posting Shark
if($form_element{'email'} =~ m/^[a-zA-Z0-9_\-]+@[a-zA-Z0-9_\-]+([.][a-zA-Z0-9_\-]+)+[a-zA-Z]{2,4}$/i){
        $values = 0;
        $mail = "Valid!";
} else {
        $mail = "InValid!";
}
sid78669 commented: Excellently answered!! +1
KevinADC 192 Practically a Posting Shark

Seems that maybe headedtomexico is now madeittomexico

Comatose commented: LMAO! Too True. +10
KevinADC 192 Practically a Posting Shark

A students best cheat tool is a search engine or the search feature of a website or forum. You would have found a recent thread discussing the exact same assignment had you done just a little searching.

http://www.daniweb.com/forums/thread169597.html

Salem commented: I like it :) +27
KevinADC 192 Practically a Posting Shark

see devshed for a reply to your question

KevinADC 192 Practically a Posting Shark

You could try using a reference to a function.

sub some_function {
   print "foo";
}

my $some_function = \&some_function;

You can pass $some_function like any other scalar, but you need to use the arrow operator to run the function.

$some_function->()

Assuming you pass references to functions to your sub rotuine:

sub function_runner {
   my @exit_functions = @_;
   my $counter = 1;
   foreach my $function (@exit_functions) {
      print "Performing exit function $counter:\n";
      $counter++;
      $function->();
   }
   print "\nAll exit functions completed.\n";
}
klactose commented: His post was very helpful +1
KevinADC 192 Practically a Posting Shark

the code you posted is not perl, I am not sure after reading your post if you know that or not.

PoovenM commented: Thank you for the assistance :) +2
KevinADC 192 Practically a Posting Shark

The warning will have no affect on the program. If you don't want the warning issued you can elect not to use the warnings pragma or use the "no warnings" switch in a block of code. See the warnings pragma documentation for details.

http://perldoc.perl.org/warnings.html

KevinADC 192 Practically a Posting Shark

basic script with no formatting of output:

use warnings;
use strict;
use File::Find;

use File::Find;
    find( {wanted=> \&wanted, no_chdir => 1}, 'C:/Program Files/Player' );
    sub wanted { print $_,"\n"; }

See File::Find (a core module) for details.

raul15791 commented: good! +1
KevinADC 192 Practically a Posting Shark

What you want is an IDE, a good free one is Perl Express

http://www.perl-express.com/

I think its only for Windows operating systems. Install activeperl from www.activestate.com then use the IDE to run write and run your perl scripts.

I believe you can also use it as just an editor if you don't want to install perl.

Some free ebooks:

http://www.perl.org/books/library.html

Most still relevant if a little out of date in reagards to the newest version of perl: 5.10.

raul15791 commented: useful +1
KevinADC 192 Practically a Posting Shark

You declared perl is headed to the graveyard, if you ask me, python will probably get there sooner judging by it lower usage and it's been around as long as perl if not longer in one form or version or another.

But anyways, thats my last post in this thead. These discussions might be of some small personal interest but never change anyones mind about anything.

Parting shot:

Python 3000

There are plans for a future version, to be called Python 3.0 (the project is called "Python 3000" or "Py3K") that will break backwards compatibility with the 2.x series in order to repair perceived flaws in the language.

jephthah commented: interesting. i did not know that. +2
KevinADC 192 Practically a Posting Shark

I have no idea why you say perl is headed to the graveyard. Your premise is wrong so there is no use in discussing it. Perl may not be as popular as it was at one time but it is far fom a dead language.

I don't know what perl does better than python or vice-versa.

KevinADC 192 Practically a Posting Shark

Emotional attachment? Perl is a good language, that is why people continue to use it.

People will stop using it when it no longer serves a purpose. That will not be for a long time I suspect.

KevinADC 192 Practically a Posting Shark

There is no urgent help here. All questions have the same priority: none.

If you want help you need to post your code, describe in detail the problems you are having and post any error messages you are getting.

Then if someone wants to help you they will.

jephthah commented: "There is no urgent help here. All questions have the same priority: none." LOL +2
KevinADC 192 Practically a Posting Shark

Good. But I still think it would be better to just count the words while building the initial data set, it would be less work since perl would only need to parse the file once to get all the data, instead of parse the file, then the data again.

godevars commented: All posts have helped me understand Perl more. +1
KevinADC 192 Practically a Posting Shark

Get the latest edition of "Perl BookShelf References" (on CD). 5 or 6 books on the CD, including "Learning Perl" and "Advanced Perl Programming" (at least last ime I checked they were included).

You can download (or simply read) a few perl books for free from this site:

http://www.perl.org/books/library.html

They are getting a little old but are still relevant for the most part.

"Data Munging" by Dave Cross is also excellent.

ithelp commented: Thanks. +3
KevinADC 192 Practically a Posting Shark

On second thought, forget my post. I have no idea why I even bothered to post in this thread, it just seems like such a non-issue that I am surprised anyone would care. But everyone is different, so if the bad rep has you confused or upset, so be it.

I'm out of this thread. Have a good one Christina, and really, don't worry about it or even think about it.

christina>you commented: I know no one cares... but thanks for posting anyway. Have a good day. +18
KevinADC 192 Practically a Posting Shark

it will be in the http server error logs but most likely will not help much. But look in the apache folder, find the logs folder and find the error file.

EnderX commented: Thank you. +3
KevinADC 192 Practically a Posting Shark

Use a hash to compare the lines.

use strict;
use warnings;

my $f1 = 'E:\upload\new\2.txt';
my $f2 = 'E:\upload\new\a.txt';
my $outfile = 'E:\upload\new\1.txt';
my %results = ();

open FILE1, "$f1" or die "Could not open file: $! \n";
while(my $line = <FILE1>){
   $results{$line}=1;
}
close(FILE1);

open FILE2, "$f2" or die "Could not open file: $! \n";
while(my $line =<FILE2>) {
   $results{$line}++;
}
close(FILE2);


open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
foreach my $line (keys %results) {
   print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;

There are also some perl modules that will compare files, or you can use the operating system commands Salem mentions.

KevinADC 192 Practically a Posting Shark

in the code I posted, change this line:

print qq~( $long, $lat, time(), $omschrijving, $longneg, $latneg )~;

to:

$sth->execute( $long, $lat, time(), $omschrijving, $longneg, $latneg );
Sulley's Boo commented: ^,^ +3
KevinADC 192 Practically a Posting Shark

I was actively looking for a solution to the problem of how to manage encapsulating many different topics targeted to different audiences under one roof,

IMHO, you are looking for the solution to a problem that does not exist. And that really sounds just like the middle-management buzz-word-bull-crap I used to hear in countless meetings back in the day. In the corporate world, that might get you some points with a clueless boss, but I don't think you're in that world. And I hope you aren't.

John A commented: Somehow I think you're right. +11
KevinADC 192 Practically a Posting Shark

Understood. I realize my post may not be clear, but if you notice I never say anything about you wanting to copy them or to be like them.

But you are also one of the most attentive administrators (if not the most) of all the forums I participate on. It's your personal attention to the day to day, and even hour to hour, operation of this forum that I appreciate more than anything else that is non-content related.

I get the impression you drink a lot of coffe, you are always tinkering with something on the website. Or at least it seems that way.

Dani commented: Thank you very much. +10
KevinADC 192 Practically a Posting Shark

It's those word games that she plays!;) lol.. I'm no1 to talk about that though.

But seriously, I think something should be changed about the rep system.. It has a specific purpose, and people abuse it way too often. I think it would be beneficial for people's names to be posted when they rep. For one, it would stop people from abusing the system. If you neg. rep someone for a stupid reason, you will be neg. repped back. But on the same token, if the neg. rep is deserved, I don't think there will be a rep war. If some1 neg. repped me for a good reason, I would not be angry or neg. rep them back.

First, I think anyone that takes rep seriousy needs to take a break from their computer.

Daniweb seems to have a group of members that is here more for the entertainment value and less the webdev stuff. Those members will abuse, and I use that word loosely, the rep system, seeing it as just part of the fun and games. I call it the myspace crowd for lack of a better description.

WolfPack commented: nicely put +5
KevinADC 192 Practically a Posting Shark
#!/usr/bin/perl
use strict;
use warnings;
open(IN, '<',"dna.dat") or die "Can't read file\n $!";
my $first_line = <IN>;
my $dna = '';
while(my $line=<IN>){
   chomp $line;
   $dna .= $line;
}
close IN;
my $cdna = '';
for my $i (0 .. length($dna)-1){
   $_ = substr($dna, $i, 1);
   if (!/[TAGC]/) {die "Unkown base: '$_'\n";}
   $cdna .= ($_ eq 'A') ? 'T' : 
            ($_ eq 'T') ? 'A' :
            ($_ eq 'C') ? 'G' :
            ($_ eq 'G') ? 'C' : '';
}
my $rdna = reverse $cdna;
print "The DNA string is now reversed complemented:\n\n $first_line\n$rdna\n";
KevinADC 192 Practically a Posting Shark

Are you forgetting the "Content-type: text/html\n\n" line? If you do, the browser doesn't know that HTML is coming and displays the script source instead.

if you forget that line with perl generated output to a browser you get an internal server error. But the server error log should show an error about incomplete/malformed header. The exact error message depends on the http server being used.

~s.o.s~ commented: The only person to keep the Perl Forum alive ;) - ~s.o.s~ +9
KevinADC 192 Practically a Posting Shark

That's good; thanks for that, it works as I'd expect it to.

However, going back to my example...

If this:

my(@DREF_A) = @$REF_A;
my(@DREF_B) = @$REF_B;
push(@DREF_A,"hippos");

Is changed to this:

my(@DREF_A) = @$REF_A;
push(@DREF_A,"hippos");
my(@DREF_B) = @$REF_B;

Shouldn't that change the contents of @DREF_B? (The data should now be copied after the array is changed) It doesn't though, I've just checked it (same output as before)

No. @DREF_A and @DREF_B are copies of whatever reference you dereferenced at that point. They are entirely seperate from the original data at that point.

Also, the @DREF_B output doesn't match the @DREF_A output even if both @DREF_A and @DREF_B are sourced from the same pointer, as in:

my(@DREF_A) = @$REF_A;
push(@DREF_A,"hippos");
my(@DREF_B) = @$REF_A;

Same reason. @DREF_A has no connection to $REF_A anymore. @DREF_B will have the value of $REF_A. You did not make any changes to $REF_A, you made a copy of it and changed the value of the copy (@DREF_A)'

Although it does work as per your example:

push(@$REF_A,"hippos");
my(@DREF_A) = @$REF_A;
my(@DREF_B) = @$REF_B;

Seems strange though! Shouldn't @$REF_A evaluate to the same thing whether its a subroutine parameter or a variable assignment?

In the above you changed the value of the original data that $REF_A points to. Then you make two new copies which will both have the same value because $REF_A and $REF_B are pointers to the same data.

:)

MattEvans commented: Good comments & valuable insight; thanks! +1
KevinADC 192 Practically a Posting Shark

There are a few ways to get the date/time with perl:

http://perldoc.perl.org/functions/gmtime.html

http://perldoc.perl.org/functions/localtime.html

http://perldoc.perl.org/functions/time.html

or you can use backtiks to get the date from the system date function, something like:

my $date = `/bin/date +"%a, %b %d, %Y"`;
Mushy-pea commented: Good advice, just the job. +1
KevinADC 192 Practically a Posting Shark
$datos .= qq~<a href="$me?C=OFERTAS2&EMPRESA=$empresa_param&NREF=$nref" onMouseOver="linkFTecnica(nref2)">~;
sut commented: what can i say? superior solution ;) +1