Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Remove digits from a string and add timestamp to the result

by suneel.reddy (Novice)
on Jan 02, 2013 at 11:09 UTC ( #1011248=perlquestion: print w/ replies, xml ) Need Help??
suneel.reddy has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

This post is an extention of earlier post.

I have a file name like - 20130101Customer100.imp , now I need to truncate 20130101 from the file name.

based on the solutions offered from earlier post, I made it as,

$file =~ s/^\D*\d+// ;

which gave me 'Customer100' after removing the extention (.imp)

Okay...now the problem is - I need to insert some timestamp between 'Customer' and '100'.

It should look like Customer_<TimeStamp>_100. I can get the timestamp, but not sure how to fit it in that string.

Please help me.....!!!!

Thanks in advance.

Comment on Remove digits from a string and add timestamp to the result
Re: Remove digits from a string and add timestamp to the result
by sen (Hermit) on Jan 02, 2013 at 11:43 UTC

    Hi Sunil, If i understand your requirement correclty,

    try this, Customer_."$timestmp"._100

    Update:

    $a=Customer100; $b=20130201; $a=~ /(.*?)(\d+)/; $a= $1."_".$b."_".$2; print $a;
    Update1:
    $a=~ s/(.*?)(\d+)/$1\_$b\_$2/;
Re: Remove digits from a string and add timestamp to the result
by Ratazong (Prior) on Jan 02, 2013 at 11:45 UTC

    This depends on the exact format of the filename. But possible the following example will give you an idea:

    my $filename = "customer100"; my $timestamp= "02012013"; $filename =~ /(\w+)(\d+)/; # split filename into "custome +r" and "100" $filename = $1."_".$timestamp."_".$2;
    HTH! Rata

Re: Remove digits from a string and add timestamp to the result
by johngg (Abbot) on Jan 02, 2013 at 12:03 UTC

    What with your earlier post and this one it is not clear what your overall aim is. Could it be that you want to move the timestamp from the front to the middle? Perhaps you could explain more clearly what you are trying to achieve from both nodes.

    $ perl -Mstrict -Mwarnings -E ' > my $filename = q{20130101Customer100.imp}; > say $filename; > substr( $filename, 8, 0 ) = do { > my $ts = substr $filename, 0, 8, q{}; > qq{_${ts}_}; > }; > say $filename;' 20130101Customer100.imp Customer_20130101_100.imp $

    I hope this wild guess is useful.

    Cheers,

    JohnGG

Re: Remove digits from a string and add timestamp to the result
by ww (Bishop) on Jan 02, 2013 at 13:31 UTC
    I'm with johngg on this.

    WHY are you doing something that looks like makework? If you need to sort customers by alpha (the only plausible reason for your spec that I've dreamed up), you can do that with the datestamp in its initial position. This 4th line of this code is lifted almost verbatim from the doc for sort

    my @old = qw(20120301Customer100 20120402Alsocust200 20130102Nothercu3 +00); my (@new, $item); @new = sort { substr($a, 8) cmp substr($b, 8) } @old; for $item(@new) { say $item; }

    See perldoc -f sort and perldoc -f substr. But for best results (and as has been asked before) tell us what you really want to do and WHY you think you need to do that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011248]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-12-27 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls