$resFile = &getFileName($ARGV[0]);

$fileType = &getFileType($ARGV[0]);
$table = &getTableName($fileType);

could u please tell me as to what these commands are doing? also , if I convert them to shell script,will thsese work ?
there's a perl script that Im trying to convert to shell script.

resfile=getfilename $1
filetype=getfiletype $1
table = gettablename $filetype

looking for discussions,thanks

Recommended Answers

All 14 Replies

Those look like functions that someone wrote locally (I haven't done Perl in a while so I may be mistaken, but I do NOT remember functions with those names as part of standard Perl or its standard extension modules).

absolutely .. locally written functions .. just asking if the functions remain the same,can that syntax account for shell snippet ??

Of course you can write functions in shell scripts and call them with arguments. I think you should try finding a shell scripting tutorial. There is probably one (in pieces) at the site I linked you to in the other thread.

thanks and apologies for asking too much .. under some hammer :D

any idea what the below lines mean ?
$line1[$count] =~ s/^\"//;
$line1[$count] =~ s/\"$//;

It looks as though a line of text has previously been split (probably on whitespace or commas) and is now being looped over and those lines are removing leading and trailing whitespace. It seems as though it is reading a file of comma separated values.

Hello leghorn,

Good morning,
If I may comment on your Perl script.

$resFile = &getFileName($ARGV[0]);

$fileType = &getFileType($ARGV[0]);

You don't need the '&' in front of your subroutrine in this case. Though '&' is optional for perl subroutine or function as other programming language calls it.

What does those mean?
The subroutrine is taking the first element of the Command Line Interface paramenters, which in this case is the filename, probably do some stuff with file and then return the result to scalar variables "$resFile" and "$fileType".

    $table = &getTableName($fileType);

Afterwards, the fileType is known as then sent to another subroutine that could get the table Name or a kind of table that is generated and pass to the variable $table.

if I convert them to shell script,will thsese work ?

Definetly, you could change any problem source to another if you understand what those things were in the original language the problem is solved. But really I don't know why you want to change a Perl working script to a shell script.

    $line1[$count] =~ s/^\"//;
    $line1[$count] =~ s/\"$//;

I believe masijade put together a good idea of what might have been happening on these. However, I wonder why split a line then do the trouble of removing " from the beginning and the end of such. When simply that could be done on the line itself with or without the split function.

If the lines are CSV, then there are good module on CPAN that can make your day beautiful. Like Text::CSV_XS and others.

Hope have been able to help a bit.

Ah, yeah, just now noticed I said "leading and trailing whitespace". I obviously meant "leading and trailing quotes". Oops!

thanks ..

what is the code trying to do here ?

my @var = @{$_[0]};
$Length = @var

can I do the same thing in shell ?
If yes,how ?

lemme go this way ..

$line = <MYFILE>;

        @line1 = split(/,/ , $line);

    $length = @line1;
    $count = 0;
    while($count < $length)
    {
            $line1[$count] =~ s/^\"//;
            $line1[$count] =~ s/\"$//;
            $count++;
    }


    $line = <MYFILE>;
    @line2 = split(/,/ , $line);
    $length = @line2;
    $count = 0;
    while($count < $length)
    {
            $line2[$count] =~ s/^\"//;
            $line2[$count] =~ s/\"$//;
            $count++;
    }

I need to know what these lines are doing and convert them in shell script.
please break down what each of these things is doing,if possible.

Thanks

It is reading a csv, the first block counting the number of "fields" on the first line and the second counting the number of "fields" on the second line. I have NO IDEA why, though.

I will also say it is doing it INCORRECTLY. Looking at the fact that they are striping the quotes from the fields, I assume that it WANTS to read correctly formatted csv (i.e. "fields" separated by commas, with fields containing a comma being quoted, and quotes in a quoted field being escaped by another quote SQL style), BUT the split splits on the commas REGARDLESS of quotes, and so commas contained within quoted fields are not handled correctly, and neither are quotes within a quoted field.

Really there are better ways of doing these things, but because
what you wanted is the meaning of what these Perl means I can help:

$line = <MYFILE>;

means the variable $line get a line from the file been read. But it only a single line it's get because the "<>"
which is called readline internally in perl is called in a scalar context. Then...

@line1 = split(/,/ , $line);

Then the variable $line is called in an array context using a split function to assign the line "split"ed into an array variable @line1.

$length = @line1;

Variable $length get the length of the array @line1, because the array is called in the scalar context.

$count = 0;

A scalar variable $count is initialized to 0.

while($count < $length)

Using a while loop compare the variable length with the variable count if it greater, then

{

starting a while loop block ....

Then those ones we talked about before.... :)

}

Then end of while loop.

Seriously?? Start all over again??? No.. NO...
This is a wrong way to write a Perl Program/script.

You can do all of these in a single while loop, or you simply use
a module just like the one I mentioned before now.

The Perl script you presented is not properly done at all. You could post the data and we could help if you want.

my @var = @{$_[0]};

The above is an array reference, which is deference into an array variable @var.

$Length = @var;

Then scalar variable $length got the array length.

Hope this helps.

thank you so much fellas
big help
~cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.