Extracting a piece from a string 
Author Message
 Extracting a piece from a string

I will be calling a script in my CGI-BIN from any of a number of similar
directories...

users/john/files/x.htm
and
users/andrew/files/x.htm
etc.

As you can see, the only things which will differ from all calling locations
is the client's name.

I'm trying the work out how to manipulate the string which contains the
calling location - $ENV{'HTTP_REFERER'} - so that all I'm left with is the
name of the client (john, andrew, and so on). By doing this, I can recreate
the client's file location and thus access those files in the resulting
script (eg. I can call up the client's logo into the script output, thus
making it customised).

Any help appreciated...

James



Thu, 12 Aug 2004 12:04:09 GMT  
 Extracting a piece from a string

Quote:
>I will be calling a script in my CGI-BIN from any of a number of similar
>directories...

>users/john/files/x.htm
>and
>users/andrew/files/x.htm
>etc.

Pattern matching is the way to go here.  If you know that this will always
appear as

   users/{CLIENT-NAME}/files

then just do this:

my $x = 'users/john/files/x.htm';
$x =~ m{users/([^/]+)/files};
my $client = $1 or warn "$x did not match the pattern!";
print $client, "\n" if $client;

--
~~~~~~~~~~~~~~~~|It is by caffeine alone I set my mind in motion,
Jeffery Boes    |It is by the beans of Java that thoughts acquire speed,

UIN 3394914     |It is by caffeine alone I set my mind in motion.



Thu, 12 Aug 2004 13:30:27 GMT  
 Extracting a piece from a string

Quote:

>users/john/files/x.htm
>and
>users/andrew/files/x.htm
>etc.

>As you can see, the only things which will differ from all calling locations
>is the client's name.

>I'm trying the work out how to manipulate the string which contains the
>calling location - $ENV{'HTTP_REFERER'} - so that all I'm left with is the
>name of the client (john, andrew, and so on).

-----------------------
#!/usr/bin/perl -w
use strict;

foreach ( qw( users/john/files/x.htm users/andrew/files/x.htm)) {
   my $name = (split m#/#)[1];     # List Slice
   print "$name  ($_)\n";

Quote:
}

-----------------------

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Thu, 12 Aug 2004 16:25:49 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Q: How to read a file piece by piece

2. extract string from another string

3. regular expression to extract a string between 2 other strings

4. Extract string from a string

5. Extracting strings from postscript in a single regexp?

6. Extracting strings with Perl.

7. extracting error strings from C source

8. Extracting Specific Chars from a Whole Large String

9. How to extract the toplevel-domain of a domainame from a string with regexp

10. Extract a substring in middle of a string.

11. How do you extract bits from a string?

12. How can I extract C'string

 

 
Powered by phpBB® Forum Software