substr function in perl


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers substr function in perl
# 1  
Old 03-31-2009
Question substr function in perl

Hi friends,

I have written a perl code and it works fine but I am not sure tommorow it works or not, please help me.
problem : When diff is 1 then success other than its failure but tomorrow its 20090401 and the enddate is 20090331. thats why I write the code this type but it does not work and when date = 20090402, enddate = 20090331, it should failure.
Thanks in advance.

Here is one part of the my code..
Code:
#!/usr/bin/perl

use strict;
my $status = "";
my $dt = `date +%Y%m%d`;
#my $dt = "20090401";
my $enddate="20090331";
my $diff = $dt - $enddate;
#print $diff;
if ((substr($dt,7)=01 && (substr($enddate,7)=31)) || (substr($dt,7)=01 && (substr($enddate,7)=30)))
        {
           $status = "successful";
        }
        elsif ($diff = 01)
        {
            $status = "successful";
        }
        else
        {
            $status = "failure";
        }
print $status;


Last edited by otheus; 04-15-2009 at 09:04 AM.. Reason: code tags
# 2  
Old 04-15-2009
After $dt = `date ...` you should remove the trailing EOL:
Code:
my $dt = `date +%Y%m%d`;
chomp $dt;

Anyway, this $diff code fails at the end of the year or end of the month. You can use one of the vary nice packages like DateManip to solve this problem. Or you can do it manually like so:
Code:
use POSIX qw(strftime);
my $dt = time();
my $enddate = strftime("%s",0,0,0,31,3,2009 - 1900);
my $diff = ($dt - $enddate);
if ($diff < (3600*24)) { 
  print "Within a day\n";
} else  {
  print "More than a day\n";
}

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl substr or similar help

I have a large string containing about 17,500 characters and I would like to obtain the value for token. token only appears in the entire string once and is towards the end of the string at the 17,200 area but that could change. Using perl can someone assist me with obtaining the value which in... (10 Replies)
Discussion started by: azdps
10 Replies

2. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

3. Shell Programming and Scripting

perl file, one line code include "length, rindex, substr", slow

Hi Everyone, # cat a.txt a;b;c;64O a;b;c;d;ee;f # cat a.pl #!/usr/bin/perl use strict; use warnings; my $tmp3 = ",,a,,b,,c,,d,,e,,f,,"; open(my $FA, "a.txt") or die "$!"; while(<$FA>) { chomp; my @tmp=split(/\;/, $_); if ( ($tmp =~ m/^(64O)/i) || ($tmp... (3 Replies)
Discussion started by: jimmy_y
3 Replies

4. Shell Programming and Scripting

help required for 'expr substr' function

hi iam trying to extract a certain portion of the string whose value is stored below,but am getting syntax eror.The command is shown below for file in GMG_BASEL2*.txt do m1= cat reporting_date.txt year= expr substr $m1 1 2 echo $year done m1 has date 10/31/2009 but this vale... (6 Replies)
Discussion started by: jagadeeshn04
6 Replies

5. Shell Programming and Scripting

The function of substr clause in awk command

Hello all, Please help me in letting me know the function of *substr* function in awk... actually i am new with this function i can play with awk but for this function needs your help in making me understand the correct way of using it. I am writting a code please advice regarding this... (4 Replies)
Discussion started by: jojo123
4 Replies

6. Shell Programming and Scripting

use of substr function in awk

i want to get substring of second coloum of an file using awk substring function.please help me out (2 Replies)
Discussion started by: RahulJoshi
2 Replies

7. Shell Programming and Scripting

20090620231013 to date format i am using substr, any simple way in perl?

Hi Everyone, $tmp="20090620231013"; $tmp = substr($tmp,0,8)." ".substr($tmp,8,2).":".substr($tmp,10,2).":".substr($tmp,12,2); So my output is: 20090620 23:10:13. I only can think substr is easy, any perl can do this just one line very simple efficient one? :eek: Thanks (3 Replies)
Discussion started by: jimmy_y
3 Replies

8. UNIX for Dummies Questions & Answers

substr in perl

Let's assume that I have a file with contents delimited by pipe: "The mouse|ran up|the|clock" "May|had a|little|lamb" How would I use 'substr' to get the 3rd field. For example, "the" from the first line, and "little" from the second line? # Loop over a file and read $LINE { ... (2 Replies)
Discussion started by: ChicagoBlues
2 Replies

9. Shell Programming and Scripting

copy substr in existing string in Perl

Any clue to write something to a particular location in Perl? Suppose $line = ‘abc cde 1234” How to write ( example string "test") on location 4 without parsing the whole line. Output should be $line = ‘abctest 1234” this is not search and replace. just to add substring into... (3 Replies)
Discussion started by: jaivipin
3 Replies

10. Shell Programming and Scripting

Trouble using substr function with Bourne shell script

Hi, I'm a newbie to UNIX scripting and I'm having some trouble compiling my script. I'm using the Bourne Shell and cannot seem to use the substr function correctly. I'm trying to extract the last two digits of a year that's stored in a variable based off of a condition. I've searched the... (4 Replies)
Discussion started by: E2004
4 Replies
Login or Register to Ask a Question