d5e5 109 Master Poster

I didn't mean to imply science doesn't take the question seriously, many branches do: psychology, neuroscience, biology, and physics are principle among them. But there is yet to be a scientific consensus of what is consciousness, what how/what we perceive to be ourselves, and whether or not we have free will. The observer part of quantum mechanics is still debated (my favourite quotes by leading quantum mechanical physicists are that no one understands quantum mechanics including themselves). There are new interesting hypotheses suggesting quantum fluctuations in molecules in the brain may be what we perceive as free-will. Many others involve emergent properties of sufficiently complex neural networks.

As a general rule the more hypotheses/theories there are about a phenomenon the less it is understood.

PS That looks like an interesting book, worth a read....

From what you've said so far, I think you'd like that book. I doubt that I'll end up 100% convinced that his theory is correct. Have you read anything by the late Robert Anton Wilson? He claimed all models have varies levels of usefulness, but he didn't claim 100% certainty for any model.

d5e5 109 Master Poster

So far as you assume.

I assumed God's omnipotence for the sake of argument, because people often have defined God as the all-powerful creator of everything. If he's not all-powerful then what's to stop some other entity from taking his place as numero uno? ... in which case we wouldn't know which god we were referring to when we use the name 'God'.:)

d5e5 109 Master Poster

> You CAN know all this for sure. Ask God and he will reveal it to you.
Well, I asked my cat about it instead and he said I would never die so long as he needed feeding and that I would be rewarded eternally for this service.

I think he may be just making crap up to serve his own ends though.

If I may play Devil's advocate (if that's the right expression): your cat has an incentive to make up crap whereas God, being omnipotent and self-supporting and all, does not.:)

d5e5 109 Master Poster

> But I'm still reading, so there must be more to it than that.
Maybe not. Maybe it just starts repeating itself ;)

:DIt better not, or I'll make a mental note not to buy that book next time around.

d5e5 109 Master Poster

Remove the following line from inside your foreach loop and put it before the beginning of the loop starts. open OUTFILE, ">$file" or die "unable to open $file $!"; Opening a file for output multiple times results in overwriting the existing file each time so that only the last thing written will remain in the final version of the file.

Also in line 4, after you close your input file you re-open it for output and then don't print anything to it. Why?

d5e5 109 Master Poster

See the question here is what is "me" science has a very good explanation of what happens to a body after it dies and all bits of it. But science does not have a good understanding of what "me" is. If science can figure out what "me" is it should be easy to figure out what happens to it after death.

Religion thinks about the question a lot but is not equipped to answer/figure out the truth about anything.

Philosophy seems to break questions down so much that by the time they can logic out an answer there is no practical interpretation of their answer. eg. Trying to prove the existence of God by arguing there has to be a thing that causes its own existence.

Actually, physicists have taken quite seriously the question of the essential nature of an observer of a quantum event ("a good understanding of what 'me' is").

I've started reading a book about what happens after you die that bases it's conclusions on scientific evidence and theories. Of course that doesn't mean all scientists would endorse its conclusions but after reading about 10% I find it intriguing and rather persuasive. I've resisted the temptation to skip to the end of the book to find out what happens when I die, but it seems to be suggesting we relive our lives over again, sort of like Bill Murray in Groundhog Day. But I'm still reading, so there must be more to it than …

d5e5 109 Master Poster

After posting the above, I realised that the trick of writing and reading environment variables served no purpose. The child process can read values of lexical variables set in the parent just as well as it can read environment variables.

I like the following example better. It has five child processes sleeping for five seconds each. Doing five five second tasks sequentially would take 25 seconds altogether but by using fork, the total job takes just five seconds. I guess that's the point of using fork.

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

my @array = qw(test1 test2 test3 test4 test5);
my %children;
my $start = time();

foreach(@array){
    my $pid = fork();
    if ($pid) {
        # parent
        $children{$pid} = $_;#Parent stores children's process ids and one element
    } elsif ($pid == 0) {
        # child
        sleep(5);#Do something that takes 5 seconds
        exit(0);
    } else {
        die "couldn’t fork: $!\n";
    }
}
#wait for all children to finish
for my $pid (keys %children) {
    print "child $pid k: $children{$pid}\n";
    waitpid $pid, 0;
}

my $elapsed = time() - $start;
print "Time elapsed was $elapsed seconds.\n"
d5e5 109 Master Poster

Nobody who hasn't already died can know for sure what it's like, so I can't really fear death -- only my idea of what death might be like. Most of the time I don't fear my idea of death nearly so much as I fear some of the events or conditions that could cause death, most of which I find scary. Drowning in a vat of beer might be OK, but even that might have its downside.

I'd say I fear the pain, embarrassment and loss of control that often comes with fatal accidents or illnesses more than death itself. Fear won't stop you from dying, so don't let it stop you from living.

d5e5 109 Master Poster

I haven't used fork and don't know much about it, but doesn't a return value of 0 mean this is a child process? So why does your script print "parent\n"; when the $pid == 0?

How about using an environmental variable to pass each array element to a child, so each child prints one of the elements?

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

my @array = qw(test1 test2 test3 test4 test5);
my $x = 0;

foreach(@array){
    $ENV{X} = $x;
    $ENV{K} = $_;
    my $pid = fork();
    if ($pid) {
        # parent
        waitpid($pid,0);
    } elsif ($pid == 0) {
        # child
        print "child $ENV{X} k: $ENV{K}\n";
        exit(0);
    } else {
        die "couldn’t fork: $!\n";
    }
    $x++;
}
d5e5 109 Master Poster

Sorry. I've looked at your post again and can't understand what your script is trying to do. You show us the output results that you don't want but it's not clear to me what output layout you do want.

d5e5 109 Master Poster

Dear Experts,
Here i am written code for convert the text to CSV file conversion. i am facing a problem in while compare the two array to print the values row wise. below i have mentioned my code,input and output format.

input :
"OC192-11,OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:SESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:SEFSS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:CVL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:ESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:SESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:UASL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:FCL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:OPRN,58,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:PSCW,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:PSCP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:PSD,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:SESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:SEFSS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:CVL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:ESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:SESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:UASL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:FCL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:OPRN,59,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:PSCW,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"

output:

EQP_NAME, TID, AID, LOCN, DIRN, EQP_TYPE, AID_TYPE, TIMESTAMP, ESS, SESS, CVS, SEFSS, ESL, SESL, CVL, UASL, FCL, PSD, PSCW, PSCP, ESP, SESP, SASP, CVP, ALSP, UASP, FCP, ES, SES, UAS, LDS, SEFSP, CSSP, PSC, VLDTY, TMPER, CMP, CMPMX, LSDS, LUAS, INMACCONTROLFR, FRTOO, UTL, UTLMX, INFRAMES, OUTFRAMES, INFRAMESERR, INFRAMESDISC, OUTFRAMESDISC, FRTOOSHORTS, FRTOOLONGS, REC_RELIABILITY_IND, PART_MAP, OPR, OPT, OPRN
,,,,,,,,"OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SEFSS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:ESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:CVL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:UASL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:FCL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:PSD,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:PSCW,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:PSCP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"OC192:OPRN,58,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"
, , , , , , , ,"OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SEFSS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:ESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:CVL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:UASL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:FCL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1", , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,"OC192:OPRN,59,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"
, , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , ,,, , , , , , , , , , , , …

d5e5 109 Master Poster

You need to show us more than one line of code. Also "application/xml; charset=UTF-8" doesn't look like an error message to me. The utf8 module allows you to use accented characters as values or variable names in your script source code, but that's all it does, so maybe you need to use some other module as well. If you can give us a snippet of code and some data, or a URL, that allows us to recreate the error, then maybe we can help.

d5e5 109 Master Poster

No need to let not having a linux environment handicap you. Linux is free, and you don't have to uninstall Windows. I installed Ubuntu along with Windows as a dual-boot arrangement and now use Windows only occasionally. But you don't have to do that. You can run linux from a live-boot CD, from a thumb-drive, or lots of other ways that I haven't tried, but should be easy and free-of-charge. Try http://www.google.com/search?btnG=1&pws=0&q=try+linux+on+Windows

d5e5 109 Master Poster

The semicolon is optional for the last statement in a block of Perl statements. I think that explains your seeing a one-liner ending in a print command not terminated with a semicolon.

d5e5 109 Master Poster

Thanks a lot for the information. i meant replicas as my trajectory files which has coordinate files inside. im trying to upzip the files and chang them to pdb files.

I don't know bioinformatics so am not familiar with the terminology you use when referring to the different kinds of files.

What I think you want to do is start by making an array (a variable containing a list of trajectory file names) using the glob command like in my example script. Then you want to make a new directory for each of the trajectory files (a trajectory file is a zipped archive of coordinate files, right?) and then unzip the trajectory file into the new directory. (Make a new directory named 'rep2.tra3M.bz2' and unzip the rep2.tra3M.bz2 file so that all its coordinate files go into the rep2.tra3M.bz2 directory.)

After unzipping all the trajectory files into new directories you want to write a script that uses the glob command to build an array of the coordinate file names in each directory. Then you can loop through this array and perform a subroutine or whatever for each file name in the array to transform the data in the coordinate file into data that you want to write to a pdb file.

d5e5 109 Master Poster

i NEED A PERL SCRIPT TO CREATE NAME_DIRECTORY FOR ALL TRAJECTORIES.

i've trajectory files like this,

rep2.tra3M.bz2
rep2.tra4M.bz2
rep2.tra5M.bz2
rep2.tra6M.bz2
rep2.tra7M.bz2

i want a scipt to list all replicas,copy zipped replicas and unzip all replicas and create a file with list of replicas and call make_pdb.pl for all trajectories.

What do you mean by 'replica'? Assuming that replicas are files whose names start with 'rep' and you know in what directory they are located, you could list all the replicas like this:

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

my @replicas = glob('/home/david/data/rep*.*');
say "List of replicas:";
say join("\n", @replicas);
d5e5 109 Master Poster

I would start by writing a query that gives the information needed for the report. Print the results of the query. Call it a report.:)

If the report required must have a specific layout such as headers, details, subtotals, groups, footers, page numbers, etc. then you'll need to look for a free report generating application compatible with MySQL.

smantscheff commented: I love that "Call it a report"! +9
d5e5 109 Master Poster
#!/usr/bin/perl
use strict;
use warnings;

print "ESS\tSESS\tSEFSS\tCVL\n";

while(<DATA>){
    s/"//g;#Remove all double quotes
    my @fields = m/(.+?,)/g;
    print chr(34), @fields[0..2], chr(34), "\t";
}

__DATA__
"OC192-11,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"
"OC192-11,OC192:SESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"
"OC192-11,OC192:SEFSS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"
"OC192-11,OC192:CVL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"

Running the above prints the following to STDOUT

ESS	SESS	SEFSS	CVL
"OC192-11,OC192:ESS,0,"	"OC192-11,OC192:SESS,0,"	"OC192-11,OC192:SEFSS,0,"	"OC192-11,OC192:CVL,0,"
d5e5 109 Master Poster

Please attach data from your rep.tra1 file to your post so somebody can test your script.

In what way does the output of your script not meet your expectations?

d5e5 109 Master Poster

Also you can try running the following which uses only standard modules that you probably already have.

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

use constant (ONE_DAY => 86400);#Number of seconds in a day
use POSIX;

foreach(0 .. 11){
    my ($year, $dt_arref, $month_name) = get_saturdays_in_month(2011, $_);
    my $dtstr = strftime('%F', localtime($$dt_arref[0]));
    print "The first Saturday in $month_name of $year is $dtstr\n";
    $dtstr = strftime('%F', localtime($$dt_arref[1]));
    print "The second Saturday in $month_name of $year is $dtstr\n\n";
}

sub get_saturdays_in_month{
    my $yr =shift;
    my $mn = shift;
    my $da = 1;
    my $yrnbr = $yr - 1900;

    my $dt = mktime(0, 0, 0, $da, $mn, $yrnbr);
    my $datestr = ctime($dt);
    
    my $mthname = substr($datestr,4,3);
    my @saturdays;
    #
    while (@saturdays < 4){
        my $dayname = substr($datestr,0,3);
        push @saturdays, $dt if $dayname eq 'Sat';
        $dt += ONE_DAY;
        $datestr = ctime($dt);
    }
    return $yr, \@saturdays, $mthname;
}
d5e5 109 Master Poster

Another way, if you don't mind using a module:

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

use File::Basename;

my $string ="datafiles/source/main_data_files/content.csv";
my $filename = fileparse($string);

print "\nThe file name is $filename\n";
d5e5 109 Master Poster

You can avoid the "Perl v5.10.0 required--this is only v5.8.8" error by removing the use 5.010; statement (or changing it to use 5.008; ) and then changing the say statements

say "The first Saturday in $mth of $year is $$dt_arref[0]";
say "The second Saturday in $mth of $year is $$dt_arref[1]\n";

to print statements like the following:

print "The first Saturday in $mth of $year is $$dt_arref[0]\n";
print "The second Saturday in $mth of $year is $$dt_arref[1]\n\n";

You can probably eliminate the "Can't locate Time/Piece.pm..." error by installing the Time::Piece module from CPAN. I understand you are not allowed to upgrade to a newer version of Perl but you should still be able to install modules from CPAN.

d5e5 109 Master Poster

What happens when you type c:/xampp/htdocs/test.php?emp=$id into the URL area of your browser? I bet your browser can't read that as a valid URL. Your browser needs a valid URL that looks like http://HOSTNAME/test.php?emp=$id where you replace HOSTNAME with the real host name (try http://localhost/test.php?emp=$id ).

d5e5 109 Master Poster

You may prefer the following way of doing it if you don't have Date::Simple .

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

use Time::Piece;
use Time::Seconds;

foreach('01'..'12'){
    my ($year, $dt_arref, $month_name) = get_saturdays_in_month(2011, $_);
    print "The first Saturday in $month_name of $year is $$dt_arref[0]\n";
    print "The second Saturday in $month_name of $year is $$dt_arref[1]\n\n";
}

sub get_saturdays_in_month{
    my $yr =shift;
    my $mnbr = shift;
    my $date = Time::Piece->strptime("$yr-$mnbr-01",'%Y-%m-%d');
    my $mthname = $date->strftime('%B');
    my @saturdays;
    
    while ($date->strftime('%m') == $mnbr){
        push @saturdays, $date->strftime("%Y-%m-%d") if $date->strftime('%A') eq 'Saturday';
        $date += ONE_DAY;
    }
    return $yr, \@saturdays, $mthname;
}
d5e5 109 Master Poster

Hi...

Pearl is a very interesting language to learn. Through your link i got to know much more thing about this......

I have currently read about the first 10% of Modern Perl by chromatic which gives a somewhat more up-to-date view of Perl and seems refreshingly easy to read so far. You can download it for free also.

d5e5 109 Master Poster

The following contains a subroutine that returns an array of all the Saturday dates in a given month and year.

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

use Date::Simple;

my @month_names = qw(January February March
                    April May June July August
                    September October November December);

foreach('01'..'12'){
    my ($year, $dt_arref) = get_saturdays_in_month(2011, $_);
    my $mth = $month_names[$_ - 1];
    say "The first Saturday in $mth of $year is $$dt_arref[0]";
    say "The second Saturday in $mth of $year is $$dt_arref[1]\n";
}

sub get_saturdays_in_month{
    my $yr =shift;
    my $mnbr = shift;
    my $date = Date::Simple->new("$yr-$mnbr-01");
    my $first_day;
    my @saturdays;
    $first_day = $date->day_of_week();
    
    while ($date->month == $mnbr){
        push @saturdays, $date if $date->day_of_week == 6;
        $date = $date->next;
    }
    return $yr, \@saturdays;
}
d5e5 109 Master Poster

One way of finding the first Saturday of each month for a given year:

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

use Date::Simple;

my $year = 2011;
my @month_names = qw(January February March
                    April May June July August
                    September October November December);

foreach('01'..'12'){
    my $dt = first_saturday($_);
    my $mth = $month_names[$_ - 1];
    say "The first Saturday in $mth of $year is $dt";
}

sub first_saturday{
    my $mnbr = shift;
    my $date = Date::Simple->new("$year-$mnbr-01");
    my ($first_day, $first_sat);
    $first_day = $date->day_of_week();
    
    if ($first_day == 6){
        $first_sat = 0;
    }
    else{
        $first_sat = 6 - $first_day;
    }
    return $date + $first_sat;
}
d5e5 109 Master Poster

I bought and read Beginning Perl by Simon Cozens and have since downloaded it and reread much of it so that's the one I would recommend. You can download it for free now.

d5e5 109 Master Poster

I don't know but you could start by finding your httpd.conf file and having a look at this post in case the solution to that person's problem throws light on yours.

d5e5 109 Master Poster

hi guys i want to set password to mysql database please help me out

For which user do you want to set a password. If you haven't created any user accounts yet I guess you mean you want to set a password for the root user. What platform does your MySQL run on: Windows, Linux, something else? See http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html

d5e5 109 Master Poster

I suppose you could do it like this:

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

use CGI qw(:standard);

my $name_to_search = 'sessionid';

my ($n, $v) = find_cookie($name_to_search);

print header();

if ($n eq 'NOTFOUND'){
    print "Cookie named '$name_to_search' not found</br>";
}
else{
    print "Found cookie named '$n', Value is '$v'</br>";
}

sub find_cookie{
    my $name = shift;
    my $c = $ENV{HTTP_COOKIE};
    
    my @arr = split(/; /, $c);
    
    foreach (@arr){
        my @nv = split(/=/);
        next if $nv[0] ne $name;
        return @nv;
    }
    return ('NOTFOUND',"$name cookie not found")
}
d5e5 109 Master Poster

Hi, on the example it works however on the files extracted from SQL databases it doesn't. It only seems to work on text files I have manually created. Can you test to see if this is just my machine causing this problem please? I have attached an example extracted file to this message. I have also attached previous perl script.

Due to upload restrictions;
Please rename NewTesting.txt to NewTesting.pl
Please rename file.txt to file.csv

Thank you so much

The script doesn't split the data properly from the file you attached most recently. However when I look at the file you previously attached and the most recent file the delimiter characters don't appear the same. My text editor assumes they are both encoded as Latin-1 and they look like this:

Previous file.csv:

messageidâ°Nameâ°Time / Dateâ°Sent/Receivedâ°Readâ°Messageâ°Data Terminator
1â°Joâ°1â°Monâ°3â°1â°Message 1 here,°.
2â°Joâ°2â°Monâ°2â°1â°Message 2 hereâ°.
3â°Jimâ°1â°Wedâ°2â°1â°Message 3 hereâ°.
4â°Janeâ°2â°Wedâ°3â°1â°Message 4 here,°.

Current file.csv:

NumberAB FirstTime / DateSent/ReceivedReadMessageData Terminator
1JoMon32message 1.
2JoMon21message 2.
3JimWed32message 3.
4JaneWed21message 4.

See? The first one seems to have extra characters in the delimiter.

To get it to work for me with the most recent file I had to change the delimiter character used to split and join and change my guess for the encoding back to LATIN-1, in the following:

#!/usr/bin/perl
use strict;
use warnings;
use 5.008;
use utf8;
use Encode qw(encode decode);
my $in_filename = 'file.csv';
my $out_filename = 'newfile.csv';

#http://www.unicodemap.org/search.asp?search=%E2%80%9A%C3%84%E2%88%9E
#my $csv_delim = chr(0x201A) . chr(0x00C4) …
d5e5 109 Master Poster
mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2011-05-01 |
+------------+
1 row in set (0.00 sec)

mysql> select MONTH(CURDATE());
+------------------+
| MONTH(CURDATE()) |
+------------------+
|                5 |
+------------------+
1 row in set (0.00 sec)
d5e5 109 Master Poster

localtime and gmtime are not modules, these are built-in perl functions, same as time.

Yes. I used only the time function rather than localtime in my example because I don't see how localtime helps in calculating the difference between two times. The time function returns the time as number of seconds since epoch (some fixed date and time assumed by the platform on which perl runs) so subtracting time at the start of the job from time at the end gives the duration of the job in seconds.

If @amithlaxman wants to convert the number of seconds into hours and minutes, he can divide the seconds by the number of seconds in an hour, then divide the remainder by the number of seconds in a minute.

@amithlaxman hasn't told us who prohibited the use of modules. A manager who tells his Perl programmers not to use modules must have a reason, and I can't think of a good one.

d5e5 109 Master Poster

When I Googled "per mille sign" encoding I saw this document which says

There is a "per mille
# sign" variant of the Mac OS Gaelic encoding that still
# maps 0xE4 to U+2030; this can be used for older fonts.
# Note: U+204A is new with Unicode 3.0; for earlier Unicode
# versions, Mac OS Gaelic was unified with AMPERSAND.

which seems too arcane for me to understand except it made me think "Let's try the 'MacGaelic' encoding." Unfortunately, that doesn't work for me because I don't have a 'MacGaelic' encoding on my computer. However, I did try 'MacRoman' in the following script which seems to work OK under Linux (and under Windows Vista also).

#!/usr/bin/perl
use strict;
use warnings;
use 5.008;
use utf8;
use Encode qw(encode decode);
my $in_filename = 'file.csv';
my $out_filename = 'newfile.csv';

#http://www.unicodemap.org/search.asp?search=%E2%80%9A%C3%84%E2%88%9E
my $csv_delim = chr(0x201A) . chr(0x00C4) . chr(0x221E);

open my $in_fh, '<', $in_filename or die $!;
open(my $out_fh,'>', $out_filename) or die $!;

while(my $line = <$in_fh>){
    chomp($line);
    next unless($line =~ m/\S+/);
    
    my $line   = decode('MacRoman', $line);
    my @array = split($csv_delim,$line);

    if ($array[4] eq '2'){
        $array[4] = "Read";
    }
                    
    $line = join($csv_delim,@array);
    $line = encode('MacRoman', $line);
    print $out_fh $line ."\n";
}
close $in_fh;
close $out_fh;

On Linux, running the above script puts the following in newfile.csv:

messageid‰Name‰Time / Date‰Sent/Received‰Read‰Message‰Data Terminator
1‚Ä∞Jo‚Ä∞1‚Ä∞Mon‚Ä∞3‚Ä∞1‚Ä∞Message 1 here,Ä∞.
2‰Jo‰2‰Mon‰Read‰1‰Message 2 here‰.
3‰Jim‰1‰Wed‰Read‰1‰Message 3 here‰.
4‚Ä∞Jane‚Ä∞2‚Ä∞Wed‚Ä∞3‚Ä∞1‚Ä∞Message 4 here,Ä∞.
d5e5 109 Master Poster

I'm stumped. It works for me with ActivePerl on Windows Vista. I can't test it on Windows XP because I don't have XP. When you run the following test script, to see if the split populated your array correctly, what does it output?

#!/usr/bin/perl
use strict;
use warnings;
use 5.008;
use utf8;
use Encode qw(encode decode);
use Data::Dumper;

my $in_filename = 'file.csv';

#When viewed under Windows Vista in Komodo Edit 6.1 delimiter looks like ‰
#Looked up ‰ at http://www.unicodemap.org/search.asp
my $csv_delim = chr(0x03B2) . chr(0x20AC) . chr(0x00B0);

open my $in_fh, '<', $in_filename or die $!;

my $line;
$line = <$in_fh>;#Read and skip the first couple of records
$line = <$in_fh>;#Read and skip the first couple of records
$line = <$in_fh>;#Read the message 2 record
$line   = decode('CP1253', $line);
my @array = split($csv_delim,$line);

print Dumper(\@array); #Let's see what's in the array. Did the split work?

close $in_fh;

When I run it on Linux or Windows Vista it prints the following:

$VAR1 = [
          '2',
          'Jo',
          '2',
          'Mon',
          '2',
          '1',
          'Message 2 here',
          '.
'
        ];

If it prints something different for you on Windows XP then the split is not separating the data record into separate elements on Windows XP.

d5e5 109 Master Poster

Ah I can see an issue here -- change the line

if ($array[2] eq '2'){
        $array[2] = "Read";
    }

to

if ($array[4] eq '2'){
        $array[4] = "Read";
    }

The result were looking for is:

messageid‰Name‰Time / Date‰Sent/Received‰Read‰Message‰Data Terminator
1‰Jo‰1‰Mon‰3‰1‰Message 1 here,€°.
2‰Jo‰Read‰Mon‰Read‰1‰Message 2 here‰.      --- 'Read' is displayed rather than 2
3‰Jim‰1‰Wed‰Read‰1‰Message 3 here‰.         --- 'Read' is displayed rather than 2
4‰Jane‰Read‰Wed‰3‰1‰Message 4 here,€°.

The idea of this script is to substitute numbers in certain array positions (i.e. array 4 if number 2 is there, change it to 'Read') than can then be read into something else

I'm sorry I have no idea how i've made this mistake and put array 2 rather than array 4, sorry if i've wasted anyones time, i really appreciate your help

That's OK. So would seem that once you make that change in the array index (4 instead of 2) the script should run correctly on both platforms, right? If so, please mark this thread solved.

d5e5 109 Master Poster
#!/usr/bin/perl
use strict;
use warnings;
use 5.008;

my $start = time;

#some piece of code....
sleep(10); #For example, do nothing for 10 seconds;

my $end = time;

my $diff = $end - $start;

print "\n diff --- $diff seconds\n";

You could start with the above, but the modules will handle special cases that I have ignored such as changing from Standard Time to Daylight Savings Time before the job finishes, etc. That is one reason why it is better to use modules.

d5e5 109 Master Poster

What version of Windows do you use? I ran the following script using your file.csv on a Windows Vista SP2 platform and did not get any errors. Komodo Edit text editor for Windows says the file encoding is CP1253 so I modified the script based on that assumption.

#!/usr/bin/perl
use strict;
use warnings;
use 5.008;
use utf8;
use Encode qw(encode decode);
my $in_filename = 'C:/Users/David/Programming/Perl/file.csv';
my $out_filename = 'C:/Users/David/Programming/Perl/newfile.csv';

#When viewed under Windows Vista in Komodo Edit 6.1 delimiter looks like ‰
#Looked up ‰ at http://www.unicodemap.org/search.asp
my $csv_delim = chr(0x03B2) . chr(0x20AC) . chr(0x00B0);

open my $in_fh, '<', $in_filename or die $!;
open(my $out_fh,'>', $out_filename) or die $!;

while(my $line = <$in_fh>){
    chomp($line);
    next unless($line =~ m/\S+/);
    
    my $line   = decode('CP1253', $line);
    my @array = split($csv_delim,$line);

    if ($array[2] eq '2'){
        $array[2] = "Read";
    }
                    
    $line = join($csv_delim,@array);
    $line = encode('CP1253', $line);
    print $out_fh $line ."\n";
}
close $in_fh;
close $out_fh;

After running the script in Windows Vista, newfile.csv contains the following:

messageid‰Name‰Time / Date‰Sent/Received‰Read‰Message‰Data Terminator
1‰Jo‰1‰Mon‰3‰1‰Message 1 here,€°.
2‰Jo‰Read‰Mon‰2‰1‰Message 2 here‰.
3‰Jim‰1‰Wed‰2‰1‰Message 3 here‰.
4‰Jane‰Read‰Wed‰3‰1‰Message 4 here,€°.
d5e5 109 Master Poster

Why can't you use modules? Yes, even you can use CPAN

d5e5 109 Master Poster

Without knowing what encoding the database used when exporting the data to a file there's not much you can do except guess. Here's a test script in which I guess that the encoding might be UTF8:

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

use Encode qw(encode decode);
my $line   = decode('UTF8', readline DATA);

#Looked up the following at http://www.unicodemap.org/search.asp
#Character Information	Unicode Range	Unicode Binary	UCS-2 Binary
#"â"	LATIN SMALL LETTER A WITH CIRCUMFLEX	Latin-1 Supplement	0x00E2	0xE200
#""		Latin-1 Supplement	0x0080	0x8000
#"°"	DEGREE SIGN	Latin-1 Supplement	0x00B0	0xB000

my $csv_delim = chr(0x00E2) . chr(0x0080) . chr(0x00B0);

binmode STDOUT, ':encoding(UTF8)';
print "Let's try splitting with $csv_delim:\n";

my @array = split($csv_delim,$line);

my $index = 0;
foreach(@array){
    print "Array element $index = $array[$index]\n";
    $index++;
}

__DATA__
messageidâ°Nameâ°Time / Dateâ°Sent/Receivedâ°Readâ°Messageâ°Data Terminator
1â°Joâ°1â°Monâ°3â°1â°Message 1 here,°.
2â°Joâ°2â°Monâ°2â°1â°Message 2 hereâ°.
3â°Jimâ°1â°Wedâ°2â°1â°Message 3 hereâ°.
4â°Janeâ°2â°Wedâ°3â°1â°Message 4 here,°.

When I run it under Linux, and also on http://codepad.org/YkfPTjPb (try it there and see), I get the following output:

Let's try splitting with â°:
Array element 0 = messageid
Array element 1 = Name
Array element 2 = Time / Date
Array element 3 = Sent/Received
Array element 4 = Read
Array element 5 = Message
Array element 6 = Data Terminator
d5e5 109 Master Poster

Mike, your example works fine but I think the loop obscures your point that slurping means reading a file into a variable in one go... so no need for a loop.

#while(<DATA>){
#    $fulltext=$_;
#}  #The above loop works but to slurp all you need is 
$fulltext=<DATA>;
d5e5 109 Master Poster

what is this variable:

$/

"The input record separator, newline by default. This influences Perl's idea of what a 'line' is." from http://perldoc.perl.org/perlvar.html

d5e5 109 Master Poster

what if thats not the only cookie?

If the name of the cookie you created is 'sessionid' you can read the value of the cookie into a variable as follows:

use CGI qw(:standard -debug);
use CGI::Carp qw(fatalsToBrowser);
#Create a new CGI object
my $cgi = new CGI;
my $value=$cgi->cookie('sessionid');

If no cookie named 'sessionid' exists, your $value variable will have no value assigned to it.

d5e5 109 Master Poster

I guess the error messages indicate that utf8 is not the correct encoding. Please try the following script. It doesn't give me any errors on my Linux platform:

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

my $in_filename = 'file.csv';
my $out_filename = 'newfile.csv';
my $csv_delim = chr(0x00e2) . chr(0x0080) . chr(0x00b0);

#Open files with auto Latin-1/ISO-8859-1 decoding/encoding on I/O
open my $in_fh, '<:encoding(ISO-8859-1)', $in_filename or die $!;
open(my $out_fh,'>:encoding(ISO-8859-1)', $out_filename) or die $!;

while(my $line = <$in_fh>){
    
    chomp($line);
    next unless($line =~ m/\S+/);
    my @array = split($csv_delim,$line);

    if ($array[2] eq '2'){
        
        $array[2] = "Read";
    }
                    
    $line = join($csv_delim,@array);
    
    print $out_fh $line ."\n";
}
close $in_fh;
close $out_fh;
d5e5 109 Master Poster

Hi, Thank you for your reply, sorry for the late reply back.

Yep, to create the CSV file I just logged into an SQL database and outputted the table using Ä∞ as the separator and .Ä∞ as the data terminator. How would I specify the input and output encoding in perl? I haven't specified any encoding on the mac side but im guess it could be defaulting and using some encoding scheme. I've also tried extracting the data from the SQL database on windows but I still run into the same problem when using the perl script.

The snippet you've put above of your readout is consistent with mine so your text editor is reading it correctly.

You can specify the encoding when opening the files (see script below), but we still don't know what the encoding is. You logged into an SQL database and exported the contents of a table so the SQL database encoded the resulting file in whatever the default encoding was for that SQL database. Although our text editors tell us the encoding is Latin-1/ISO-8859-1 that can't be correct because the infinity symbol that you use, , does not belong to the Latin-1 character set.

The following script assumes the correct encoding is utf-8. Running it results in some non-fatal error messages saying some of the line terminator values are not valid, but since we don't know the actual encoding it's the best I can do.

#!/usr/bin/perl
use strict;
use warnings; …
d5e5 109 Master Poster

Can you tell us what encoding was used to create file.csv? My text editor guesses that it is encoded in Latin-1/ISO-8859-1 but that is just a guess. My text editor (Komodo Edit) displays file.csv as follows:

messageidâ°Nameâ°Time / Dateâ°Sent/Receivedâ°Readâ°Messageâ°Data Terminator
1â°Joâ°1â°Monâ°3â°1â°Message 1 here,°.
2â°Joâ°2â°Monâ°2â°1â°Message 2 hereâ°.
3â°Jimâ°1â°Wedâ°2â°1â°Message 3 hereâ°.
4â°Janeâ°2â°Wedâ°3â°1â°Message 4 here,°.

Your script tries to split on characters that look like "‰" so your split doesn't work as expected. My guess is that what you are doing wrong is not specifying the encoding for the input and output files. Perl has to guess what encoding to use and it seems to guess differently on a mac than it does on Windows or Linux.

d5e5 109 Master Poster

Hi,

I am attempting to read in a CSV file, modify certain arrays and output it into a new file. It works fine on a mac, however on Windows it does not do anything. Any ideas what im doing wrong? Code posted below;

open(FILE,"file.csv") or die $!;
open(TARGET,"> newfile.csv") or die $!;

while($line = <FILE>){
	chomp($line);
	next unless($line =~ m/\S+/);
	@array = split("‰",$line);
	
	
     if ($array[3] == 2)
	{
		
		$array[3] = "Read";
	}
			
	$line = join("‰",@array);
	
	print TARGET $line ."\n";
}
close FILE;
close TARGET;

Thank you for your help,
Tango

When you say "on Windows it does not do anything" do you mean that it creates the output file newfile.csv and writes the contents of file.csv into newfile.csv so that the output file contains the same data as the input file? That is what your script did when I ran it on my computer, which has Ubuntu (Linux), not Windows. Also, I noticed that when I added use strict; use warnings to the start of the script (always recommended) it gave me the following non-fatal errors:

Use of uninitialized value $array[3] in numeric eq (==) at /home/david/Programming/Perl/temp.pl line 14, <FILE> line 1.
Use of uninitialized value $array[3] in numeric eq (==) at /home/david/Programming/Perl/temp.pl line 14, <FILE> line 2.
Use of uninitialized value $array[3] in numeric eq (==) at /home/david/Programming/Perl/temp.pl line 14, <FILE> line 3.
Use of uninitialized value $array[3] in numeric eq (==) at /home/david/Programming/Perl/temp.pl line 14, <FILE> line 4.
Use of uninitialized value $array[3] in numeric eq (==) …
d5e5 109 Master Poster

Hi,

I am attempting to read in a CSV file, modify certain arrays and output it into a new file. It works fine on a mac, however on Windows it does not do anything. Any ideas what im doing wrong? Code posted below;

open(FILE,"file.csv") or die $!;
open(TARGET,"> newfile.csv") or die $!;

while($line = <FILE>){
	chomp($line);
	next unless($line =~ m/\S+/);
	@array = split("‰",$line);
	
	
     if ($array[3] == 2)
	{
		
		$array[3] = "Read";
	}
			
	$line = join("‰",@array);
	
	print TARGET $line ."\n";
}
close FILE;
close TARGET;

Thank you for your help,
Tango

Why not attach your file.csv to your post? (Look for the "Manage Attachments" button and attach your file.) Then someone who has Windows can try your script and may find the problem.

d5e5 109 Master Poster

I don't know about threads and my perl is not built for threads but I can illustrate the problem by the following:

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

#When you follow the sub name with arguments in parentheses
#the sub runs first and returns a scalar value,
#which the backslash makes into a scalar reference.
say ref(\&example(1, 2, 3));#See? We have a SCALAR reference, not a CODE ref

say ref(\&example);#Now we have a CODE reference.

sub example{
    my ($arg1, $arg2, $arg3) = @_; #Do something with arguments, if any
    return 'example sub executed successfully';
}