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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



BoJo
New User

Aug 9, 2017, 12:53 PM

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

Howdy

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

===============

#!/usr/bin/perl
#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

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

=======================

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

thanks


BillKSmith
Veteran

Aug 9, 2017, 2:01 PM

Post #2 of 3 (1236 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).

Code
#!/usr/bin/perl  
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,
Bill

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


BoJo
New User

Aug 9, 2017, 4:57 PM

Post #3 of 3 (1224 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.
:)
B

 
 


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

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