CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
How to extract sub strings?


New User

Aug 9, 2017, 12:53 PM

Post #1 of 3 (2579 views)
How to extract sub strings? Can't Post


Ref : Ubuntu 16LTS, perl 5.22

I am new to perl and want to learn how to extract sub strings in a scrip. I want to use the metadata in a picture.jpg to create a caption. I want to extract the date stamp of 2017:06:12 from the variable. What am I missing? Thanks.

datum from metadata jpg picture

Region Name : Lily, Angel, Travis
Create Date : 2017:06:12 07:13:32


#use strict;
use warnings;
my $picnames="";
my $createdtg="";
my $dtg="";
$picnames=system "exiftool pic-0.jpg |grep 'Region Name'";
print $picnames
$createdtg=`exiftool pic-0.jpg | grep 'Create Date'`;
print $createdtg
$dtg=split(' ', $createdtg);
print $dtg[4]

results are

Name "main::dtg" used only once: possible typo at ./ line 15.
Region Name : Lily, Angel, Travis
print() on unopened filehandle 0 at ./ line 9.
print() on unopened filehandle Create Date : 2017:06:12 07:13:32
Use of uninitialized value in print at ./ line 15.


What am I missing?
Why is a CLI and bash script so different from a perl script?



Aug 9, 2017, 2:01 PM

Post #2 of 3 (2569 views)
Re: [BoJo] How to extract sub strings? [In reply to] Can't Post

Hello BoJo:

ALWAYS use strict. In this case, it would have found one of your errors.

Every statement must end in a semicolon. This is not exactly true, but its not worth worrying about the exceptions because it never hurts.

The function 'system' is not the same as backticks. You need the backticks in both places.

The first argument of split is a regex, not a string.

The variable that you call $dtg is an array, not a scalar. It must be declared @dtg.

I expect that your script will work with these changes.

Although it 'works', it is a poor practice to declare all your variables at the start of the script.

It is very bad practice to use system or backticks when a perl solution is available. In your case, you should use the perl module Image::ExifTool rather than the shell utility.

Update: Added corrected code (untested).

use strict;
use warnings;
my $picnames= `exiftool pic-0.jpg |grep 'Region Name'`;
print $picnames;
my $createdtg=`exiftool pic-0.jpg | grep 'Create Date'`;
print $createdtg;
my @dtg=split(/ /, $createdtg);
print $dtg[4] ;

Good Luck,

(This post was edited by BillKSmith on Aug 9, 2017, 2:32 PM)

New User

Aug 9, 2017, 4:57 PM

Post #3 of 3 (2557 views)
Re: [BillKSmith] How to extract sub strings? [In reply to] Can't Post

Thanks for the reply Bill.

And most thanks for educating a new user. That is exactly what I needed to get over a stumble.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives