0

Hello I am working with a perl script and i have the following variable:

   $string1="ZZ:DPHTEST1ZZ";

I would like to build two additional variables by getting a substring from $string1 using ":" as separator, I would like to add two prints in my script with the following outputs:

print $fragment1;
print $fragment2;  

to get the following output:

ZZ
DPHTEST1ZZ

I tried with:

$fragment1 =  substr $string1, index($string1, ':');
print $fragment1;

but I am getting:

:DPHTEST1ZZ

therefore I would like to appreciate to get this strings, by using the separator, i was researching a little and I just found methods of perl that use the position, thanks for the support,

neo33
  • 1,809
  • 5
  • 18
  • 41
  • 5
    Why not use `split`? `($fragment1, $fragment2) = split(/:/, $string1, 2);` – Mr. Llama Nov 21 '16 at 14:40
  • 1
    @Mr.Llama, thanks a lot for the suggestion I was not aware about that this method is available if you post the complete answer then I am going to accept it, my only condition is to split my original split in the two desired parts, – neo33 Nov 21 '16 at 14:43
  • The issue seems to be more that you have spent no time learning the Perl language. You don't seem to be stuck with a specific problem, and there is nothing here that a quick skim of Perl basics wouldn't solve. – Borodin Nov 21 '16 at 18:13

2 Answers2

5

The index builtin returns the first position of a string within a string. For your example, index($string1, ':') is 2. If you want your substr to start after the : just add one so you get three.

my $fragment2 =  substr $string1, index($string1, ':') + 1;

To get the stuff on the left of the :, you need to take substr from the beginning of the string to the first occurrence of :.

use strict;
use warnings;
use feature 'say';

my $string1 = "ZZ:DPHTEST1ZZ";

my $left = substr $string1, 0, index( $string1, ':' );
my $right = substr $string1, index( $string1, ':' ) + 1;

say $left;
say $right;

The above approach is useful in SQL1 or other languages that do not include split. However, Perl does include split, so my ($fragment1, $fragment2) = split /:/, $string1 as suggested by Mr. Llama is the more sensible option. It's way more readable.


1) I linked a MySQL documentation here. Note the absence of split and a comment somewhere at the bottom saying that there is no such function including explanations of how to do substr/index instead.

Community
  • 1
  • 1
simbabque
  • 53,749
  • 8
  • 73
  • 136
  • @ thanks a lot this is very useful, however how can I obtain the first part since I want the two parts in distinct variables, thanks for the support. – neo33 Nov 21 '16 at 14:45
  • @neo33 I updated the answer to include an explanation – simbabque Nov 21 '16 at 14:48
  • thanks a lot for the support, Perl is a little strange. – neo33 Nov 21 '16 at 14:58
  • 1
    @neo why do you think it's strange? There is nothing strange here. A lot of programming languages have these built-ins. – simbabque Nov 21 '16 at 14:59
  • I just have one big question, I still dont undesrtand why in so many perl scripts the people use this word "my" or another before to use a variable, could you please explain me this part? please – neo33 Nov 21 '16 at 15:00
  • @neo33 In short, because you should have `use strict` and `use warnings` in all your code. If you don't _declare_ your vars with `my`, your program will then not work. For a long explanation please see http://stackoverflow.com/q/8023959/1331451, https://perlmaven.com/always-use-strict-and-use-warnings and also http://www.perlmonks.org/?node_id=87956. Note the article in the last link was posted in 2001. – simbabque Nov 21 '16 at 15:05
  • @neo If you are learning Perl from a resource that doesn't inlcude `strict`, please stop and get a better book or tutorial. We have lots of good ones linked [in the tag wiki](http://stackoverflow.com/tags/perl/info). – simbabque Nov 21 '16 at 15:05
  • 1
    thanks a lot for the suggestions and useful information I will read all, thanks for the support, – neo33 Nov 21 '16 at 15:06
  • @neo In general Perl is very flexible and forgiving. But it doesn't have to be. That means you can do a lot of things in a very dirty way if you just need to get something done very quickly on the command line. It's great to write throw-away code. But if you want to write maintainable code for production purposes, you need to make it readable. That's why we have `strict` and `warnings`. We all include them in our code examples to make sure people use them even if they started learning from an old book. It's great you asked this. I'm happy it worked and you're going to learn something! :) – simbabque Nov 21 '16 at 15:08
  • I agree for production things I prefer to use bash and python however this time I need to extract information from an script that generates reports, it is very interesting perl but I am starting learning it, If I implement something in this language I will consider to use strict and warnings to avoid inconsistencies, Thanks a lot for the advises, – neo33 Nov 21 '16 at 15:14
2

You can try the following

my @splitted = $string1 =~ /([A-Z0-9]*):([A-Z0-9]*)/g;
print "@splitted";

Or if you want to be more specific with this you will match "ZZ:DPHTEST1ZZ" but you won't match "ZZX:DPHTEST1ZZ":

my @splitted = $string1 =~ /([A-Z0-9]{2}):([A-Z0-9]*)/g;
bob_saginowski
  • 1,429
  • 2
  • 20
  • 35