[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to declare Perl format for dynamically changing in variable length

Posted on 2008-11-05
2
Medium Priority
?
791 Views
Last Modified: 2008-11-11
Hi Experts,

Currently we are writing Perl script where we need to use Perl format for displaying output in particular format. We don't want to use sprintf as it is errorprone and want to use only Perl format. We are facing below problem:

Suppose I want to define my Perl format then I am using below code:

my ( $len1, $len2, $len3, $len4 ) = (0,0,0,0);
my ( $var1, $var2, $var3, $var4 ) = ( );
#We have to declare above variables before declaring format
my $srcheader = "format SRCHEADER = \n" .
            '@' . '>' x $len1 . " " . '@' . '<' x $len2 . ": " . '@' . '<' x $len3 . '@' . '<' x $len4 . "\n" .
            '$var1,                  $var2,                      $var3,              $var4' . "\n" .
            ".\n";

Where we $len1 to $len4 to be dynamically change in program so that when we execute below code, we should get desire output:

      $len1 = 45 if ( $ALLSTATS );
      $len2 = length($config);
      $len3 = length($SRC_COUNT{$config});
      my $srcinfo = " ";
      $srcinfo = "(Source : $SYNC_SRC)" unless ( $OLD_LOGS );
      $len4 = length($srcinfo);
      print "( $len1, $len2, $len3, $len4 )\n";
      $var1 = "Configuration";
      $var2 = $config;
      $var3 = $SRC_COUNT{$config};
      $var4 = $srcinfo;
      
                     eval $srcheader;
                     die $@ if $@;
                      select(STDOUT);
                      $~ = $srcheader;
                      write();

Here problem is : though we have updated $len1 to $len4 values it is taking the first defined values (0,0,0,0) but it is working fine for $var1 to $var4. Could any one tell what is the problem.

Thanks.
0
Comment
Question by:deshaw
2 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 22887365
sub srcheader{
print "format SRCHEADER = \n".
            '@' . '>' x $len1 . " " . '@' . '<' x $len2 . ": " . '@' . '<' x $len3 . '@' . '<' x $len4 . \
"\n" .
            '$var1,                  $var2,                      $var3,              $var4' . "\n" .
            ".\n";
eval "format SRCHEADER = \n".
            '@' . '>' x $len1 . " " . '@' . '<' x $len2 . ": " . '@' . '<' x $len3 . '@' . '<' x $len4 . \
"\n" .
            '$var1,                  $var2,                      $var3,              $var4' . "\n" .
            ".\n";
}
srcheader();
select(STDOUT);
$~ = "SRCHEADER";
write();
0
 
LVL 1

Accepted Solution

by:
deshaw earned 0 total points
ID: 22893474
Thanks for the reply but that wont work for me  as I have to call SRCHEADER many times and both $len and $var variables are dynamic. I came out with below solution:

        $len1 = 26;
        $len1 = 45 if ( $ALLSTATS );
        $len2 = length($config);
        $len3 = length($SRC_COUNT{$config});
        my $srcinfo = " ";
        $srcinfo = "(Source : $SYNC_SRC)" unless ( $OLD_LOGS );
        $len4 = length($srcinfo);
        my $SRCHEADER = "SRCHEADER" . $i++;
        my $srcheader = "format $SRCHEADER = \n\n" .
            '@' . '>' x $len1 . " " . '@' . '<' x $len2 . ": " . '@' . '<' x $len3 . '@' . '<' x $len4 . "\n" .
            '"Configuration",         $config,                   $SRC_COUNT{$config}, $srcinfo' . "\n\n" .
            ".\n";
        eval $srcheader;
        My_Die( __LINE__, "ERROR:", $@ ) if $@;
        mywrite_stdout_log($SRCHEADER, @FHS);

I have used $i variable that will be increamented and appended to format name so that there wont be any problems.

Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question