Parsing a string into an array

PERL

    Next

  • 1. manipulating X clipboard
    Hi, Does anyone know if there are any perl modules that will allow me to manipulate the X clipboard. I have searched cpan.org but didnt find anything. Thanks Paul
  • 2. LWP Logging or the such..
    Good Morning/Afternoon Everyone, I have been reading up on LWP::UserAgent and HTTP::Request of the LWP package. This is good stuff. But I am still confused about a couple of things. Here is ther senario. I have a file I want to download from my server and I can do that with LWP. But what I'm confused on is One: how do I save the file to a certain directory once I have started the GET process; insted of it dumping to stdout? The other part I am unclear on is this: if the site does not contain the file I am looking to GET, then I should receive the standard web response of 'file does not exist' or other responses like 'forbidden', etc. I am unclear how to 'log' these responses or redirect them to a file for later examination insted of dumping to stdout. Thus far I have a script that will do what I want it to do, but now I need to understand how to save my file(s) to disk insted of dumping to stdout, and logging the responses to a file for review. Let me know if I have made myself unclear. I look forward to hearing from you all soon with your input and ideas. Thank You, ~Martin
  • 3. How to rearrange an array by a hash
    Hi, How can I rearrange an array in a specific order based on the order of a hash? Something like this: my @a = qw(Mary John Dan); print join "\t", @a, "\n"; my %b = ( John => 0, Dan => 1, Mary => 2); print "$_ => $b{$_}\n" for (keys %b); print "$_-$b{$_}\t" foreach sort {$b{$a} <=> $b{$b}} keys %b; The final order for @a expect: John Dan Mary Thanks, Shiping
  • 4. getting in between dates in a hash
    Joseph Alotta wrote: >Greetings, > >I am having some conceptual problems doing what I think is a fairly >simple perl task. It is probably that my mind is just blank on >this. Could someone please recommend a strategy for this. > >I have a hash of data: > >%highest_level = ( 2001-10-14 => 152, 2002-01-15 => 163, 2003-03-13 => >210, 2004-08-07 => 307 ); > >And I am give some dates in between: > >@dates = ( 2001-12-30, 2002-03-19); > >I need to find what the highest level was on that date, matching >between known dates. This is the answer: > >%level = ( 2001-12-30 => 152, 2002-03-19 => 210); > > You mean like this? for (@dates) { $level{$_} = $highest_level{$_}; } HTH, Jan -- A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
  • 5. help on picking up key & values
    Comrades, I have a function which takes up arguments as shown below: $str = "./place_order -t " . "/thus/axioss/serviceID:\"" . $self->{serviceID} . "\" " . "/thus/axioss/supplierProduct:\"" . $self->{supplierProduct} . "\" " . "/thus/axioss/web/installationTelephoneNumber:\"" . $self->{installationTelephoneNumber} . "\n"; My problem is to get away with the hard coded values i.e /thus/axioss/serviceID:,/thus/axioss/supplierProduct:, and /thus/axioss/web/installationTelephoneNumber: I am fetching the above arguments from a file which contains these values as shown below. /thus/axioss/serviceID:123456 /thus/axioss/supplierProduct:BT IPStream 500 /thus/axioss/web/installationTelephoneNumber:020837111663 How can I do away with the hard coded keys? Thanks in advance.. ---------------------------------------------------------------------------- ---------------- regards, Ajitpal Singh,

Parsing a string into an array

Postby andrewmchorney » Sun, 18 Nov 2007 06:28:26 GMT

Hello

I am getting a strange error where I am trying to split a string into 
an array. The string is a line obtained from doing a directory of a 
disk. The error message is:

Reference to nonexistent group in regex; marked by <-- HERE in m/ 
Directory of C
:\Documents and Settings\Andrew\Application 
Data\ActiveState\KomodoIDE\4 <-- HER
E .1/ at D:\Perl Scripts\remove_duplicate_files.pl line 56.

The line in where it happens is the line of code with the split.

Thanks,
Andrew


#
# Find all the files
#
@file_list = `dir c: /S`;

#
# Build the list of directories and files
#
$temp_index = 0;

$file_index = 0;

$directory_index = 0;

$dir_list_size = scalar(@file_list);

print $dir_list_size;
print "\n:";

while ($temp_index < $dir_list_size)
{
    chomp @file_list[$temp_index];

    print @file_list[$temp_index];
    print "\n";

    if (@file_list[$temp_index] ne "")
    {
       #
       #
       @parse_line = split(@file_list[$temp_index]);

       # $line_size = scalar(@parse_line);
       $line_size = 0;

       if ($line_size > 0)
       {
          #print @parse_line[0];
          #print "\n";

          #
          # Determine if this is a directory and if so add it to the
          # directory array and increment the index
          #
          if (@parse_line[1] eq "Directory")
          {
             #$directory_list = @parse_line[1];
             #$directory_index =$directory_index + 1;
             #print "is a Directory\n";
          }
          else
          {
             ##$actual_files[file_index] = @parse_line[0];
             # $file_size[file_index] = @parse_line[4];
             #$file_deleted[file_index] = 0;

             #$file_index = $file_index + 1;
             #print "not a directory \n";
          }
       }
    }

    $temp_index = $temp_index + 1;
}
   


Re: Parsing a string into an array

Postby krahnj » Mon, 19 Nov 2007 07:52:31 GMT

n Friday 16 November 2007 13:28, AndrewMcHorney wrote:

Hello,


Why do you think it is strange? Have you read the documentation for
the split function and do you understand it?

perldoc -f split


In a regular expression pattern \1, \2, \3, \4, etc. refer back to
capturing parentheses in the pattern, respectively the first group, the
second group, the third group, the fourth group, etc. If you want to
match a literal '\' character in a regular expression you have to
escape it.

perldoc -f quotemeta


When developing your code you should include the warnings and strict
pragmas to let perl help you find mistakes.

use warnings;
use strict;


Have you thought of using a module like File::Find to do this instead?



chomp $file_list[$temp_index];


print $file_list[$temp_index];


if ($file_list[$temp_index] ne "")


perldoc -f split
split /PATTERN/,EXPR,LIMIT

split /PATTERN/,EXPR

split /PATTERN/

split Splits a string into a list of strings and returns
that list. By default, empty leading fields are
preserved, and empty trailing ones are deleted.


The first argument to split() is a regular expression pattern. If you
do not supply a regular expression pattern then split will convert
whatever you *did* supply *to* a regular expression pattern. So the
line above:

@parse_line = split(@file_list[$temp_index]);

is seen by perl as:

@parse_line = split( /$file_list[$temp_index]/, $_ );



#print $parse_line[0];


if ($parse_line[1] eq "Directory")


#$directory_list = $parse_line[1];


##$actual_files[file_index] = $parse_line[0];


# $file_size[file_index] = $parse_line[4];




John
--
use Perl;
program
fulfillment

Similar Threads:

1.Trying to dynamically create arrays, getting can't use string as ARRAY ref

Greets.

I have a 'config file' that contains a group name (alphanumeric) and
machine name/numbers separated by whitespace.  Each group is on it's
own line.  The file looks like this:

prod01 456 345 234
prod02 789 517 325
...etc, etc, etc...

What I am attempting to do is:
Put the file contents into an array
Pull the first entry off the array
Add the name of the array to a 'master array' list of the machine
groups
Then create another array, which would be referenced by the group
name, and contains the machines associated with the group.

Here is the code I have now that is giving me problems:

# parse the config file
foreach (@filelist){

        # for each line of the config file, place the entries
        # into a temp array
        my @temp=split(/ /);

        # pull the first entry off the array
        my $group=shift(@temp);

        # add the name of the array to the main rg array list
        push (@grouplist, $group);

        # push the rest of the entries into an array that has
        # the same name as the rg in question
        foreach (@temp){
                push (@$group, $_);
        }
}

The idea of the script as a whole is to take the config file and split
it into multiple arrays.  Once that is done, connect to each machine
and pull some files into another directory.

The script flow:
-Read the config file, put each line into an array - @temp = qw {prod1
456 345 234}

-Pull the first value from the temp array ($grp) and place it into the
master list - @master = qw {'prod1 prod2}

-Create a new array on the fly (it needs to be this way as new groups
may be created, or old ones removed) with the name of the pulled value
($grp) and place the machine names/numbers into that array - @prod1 =
qw {456 345 234} @prod02 = qw {789 517 325} etc...

-Loop through the master list, using each value as the pointer to the
individual array that contains the machines

-Connect to each machine in turn and pull the files

The script runs if I do not use strict (which I consider a Bad
Thing).  But when I run it with strict, I receive the following error:

Can't use string ("prod01") as an ARRAY ref while "strict refs" in use

Am I missing something really basic that is covered in the Llama (3rd
edition), and can anyone give me any hints?

2.comparing 2 arrays strings when the number/name of arrays is dynamic

Hello,

I was wondering if anyone knows how to get around the problem of a perl
array intersection with more than 1 array where the number of arrays,
and there names, can be variable... I have noticed that the code below
and also List::Compare pmm do not allow one to dynamically at run time
build a string for the arrays that need intersecting...  does anyone
know how to get around this ?   I tried adding a scalar variable
($string) which I build dynamically - the code below seems to want you
to hard code in the names and number of arrays, but again, mine vary
since I am in a loop of sorts, and I need to be able to dynamically
pass a variable number of arrays to be intersected.. any thoughts would
be appreciated..  Thanks, Jack

@Array1 = qw (A B C D G I H);
@Array2 = qw (A C E G H K);
@Array3 = qw (A B C G H L);
@Array4 = qw (A C F G H M);

foreach $element (@Array1, @Array2, @Array3, @Array4)
 { $Hash{$element}++; }

foreach $element (keys %Hash)
 {
  if ($Hash{$element} == 4)
   { push (@Intersection, $element); }
 }

print "@Intersection";

3.Two-dim array: Strange "can't use string as ARRAY ref"

I am using a two-dim hash with a string as first key, and an array as 2nd
key. But it doesn't work! Can anyone tell me what's wrong?

#!/usr/bin/perl -w
$hash{"test"}{["one", "two"]} = 42;
$hash{"test"}{["three", "four"]} = 23;
my $key = "test";
foreach(keys %{$hash{$key}}){
  print "Value for $_ is $hash{$key}{$_}\n"; # ARRAY(0x804c958)
  # print "@$_\n"; # Can't use string ("ARRAY(0x804c958)") as an ARRAY ref 
}

The output is:
Value for ARRAY(0x804c958) is 23

Why does it print only *one* value? Why can't I dereference 
the array ref $_?

Thanks!

4.Parse String

I have the following code in Perl

$buf = "<tr><td class=yfnc_tablehead1 width="48%">Change:</td><td
class=yfnc_tabledata1><b style=color:#008800;>2.04
(0.66%)</b></td></tr>"

I am using the following code to get the value 2.04.
if ($buf =~ m/$Change/)
{
   ($Part1, $Part2) = split (/$Change/, $buf);
    # compare with the digit
   if ($Part2 =~ m/(\+|-)?[0-9]+\.[0-9]*/)
   {
     I do not know what to write here to get 2.04
   }
}
Can somebody help me out? I know this is the worst way to parse the
string. Is there some better way to solve it?

5.module for parsing a string into a formula

Sorry if I'm not saying this right...

I am coming from a C++ environment, that newsgroup suggested PERL as a
possible answer for my need. I have no experience with PERL at all, so will
have to do my homework if someone says there is a way.

I need a function ( module? ) that can take a string expression "(1 + 2) *
(3 + 4)" and return the answer of "21". It can be either a numerical value,
or a string value does not matter from this point.

Any pointers in the right direction would be welcome. A commercial software
package would be acceptable as well.

Thank you in advance

Michael

6. regex: parsing out varying length substr from varying string

7. Parsing line with similar strings

8. Parse a String that probably really simple to do



Return to PERL

 

Who is online

Users browsing this forum: No registered users and 27 guest