Sponsored Content
Top Forums Shell Programming and Scripting awk between items including items Post 302267483 by vidyadhar85 on Friday 12th of December 2008 12:47:42 PM
Old 12-12-2008
don't type it in same line..
just type as i showed
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing items in 2 files

Hi, I have 2 files with contents in them and I need to compare each item in each file. File1: item4 item5 File2: item2 item3 item5 item6 The items names can be of different lengths. If the items in the File1 are not in File2, delete the missing item in File1. The resulting... (12 Replies)
Discussion started by: ReV
12 Replies

2. UNIX for Dummies Questions & Answers

queue items...

Hi everyone. I have a lot of programs i want to run on some data files, they need to be done sequentially. Often the output from one program is the input for the next. e.g $ progA data1 > data1.A $ progB data1.A > data1.AB $ progC data1.AB > data1.ABC repeat on data2, 3, 4, 5, 6 etc ... (4 Replies)
Discussion started by: Jay-pea
4 Replies

3. Shell Programming and Scripting

Matching 2 items in a string

Little lost here, I am trying to search a line for both values after the $ signs. My ultimate goal is to get percertage. <?php $string = "Something on sale for $4 and orginal price $10"; $strstr =. strstr($string, '$'); $strrchr =. strrchr($string, '$'); echo "$strstr<br>"; echo... (1 Reply)
Discussion started by: mrlayance
1 Replies

4. Programming

Removing Items In A ListView

Hi everyone! So I have a listView on my Form named "officeView" I already have the code to add and update info into it, but Im having troubles deleting items out of it. :/ Now I know how to delete an Item from the listView, but I want the item before the deleted item to become automatically... (0 Replies)
Discussion started by: romeo5577
0 Replies

5. Shell Programming and Scripting

Moving items to a new line

Hi, Let's I have the following strings (md5): 07177edf8261d28c6a003e583fcbe38c 0717c0037b3a20fc0f0998e673f228d5 0717d611a5d24374628b98e17fd00977,0717d611a5d24374628b98e17fd00977 07189a18afdae558bb5aadfe602e4a91 0719e97d481c239667f38a3e166bed74 071af3225fe50a1fdbb42c43aac313cc... (4 Replies)
Discussion started by: talfiq
4 Replies

6. Shell Programming and Scripting

awk, help me - counting items and listing them

This is my first ever post... please help! :o I have two columns....here is part of the file... 12, 46798 6692, 46799 5710, ... (3 Replies)
Discussion started by: pelhabuan
3 Replies

7. UNIX for Dummies Questions & Answers

awk colon separated items

Hi, I need to filter my data based on items in column 23. Column 1 until column 23 are tab separated. This is how column 23 looks like: PRIMARY=<0/1:504:499,5:.:.:.:0.01:1:15:.> I want to extract lines if items 7 (separated by : ) in column 23 are more than 0.25 . In example above , item... (2 Replies)
Discussion started by: janshamsani
2 Replies

8. Shell Programming and Scripting

awk - function to return permutations of n items out of m

Hi, I'm trying to write an awk function that returns all possible permutations of n items chosen in a list of m items. For example, given the input "a,b,c,d,e" and 3, the function should return the following : a a a a a b a a c a b a a b b ... c a a c a b ... e e c e e d e e e (125... (21 Replies)
Discussion started by: cjnwl
21 Replies

9. UNIX for Beginners Questions & Answers

Foreach not looping through all items

Hello, very new to this and have to use tcsh (not a choice at the moment). I need to convert files within sub directories. My foreach loops don't work. Directories are as follow: sub-001 --ses-T1 ---- anat --------File.nii --ses-T2 ----anat -------File.nii sub-002 and so on. ... (2 Replies)
Discussion started by: IlaBert
2 Replies

10. Shell Programming and Scripting

Pass an array to awk to sequentially look for a list of items in a file

Hello, I need to collect some statistical results from a series of files that are being generated by other software. The files are tab delimited. There are 4 different sets of statistics in each file where there is a line indicating what the statistic set is, followed by 5 lines of values. It... (8 Replies)
Discussion started by: LMHmedchem
8 Replies
Moose::Cookbook::Basics::HTTP_SubtypesAndCoercion(3)	User Contributed Perl Documentation   Moose::Cookbook::Basics::HTTP_SubtypesAndCoercion(3)

NAME
Moose::Cookbook::Basics::HTTP_SubtypesAndCoercion - Demonstrates subtypes and coercion use HTTP-related classes (Request, Protocol, etc.) VERSION
version 2.1202 SYNOPSIS
package Request; use Moose; use Moose::Util::TypeConstraints; use HTTP::Headers (); use Params::Coerce (); use URI (); subtype 'My::Types::HTTP::Headers' => as class_type('HTTP::Headers'); coerce 'My::Types::HTTP::Headers' => from 'ArrayRef' => via { HTTP::Headers->new( @{$_} ) } => from 'HashRef' => via { HTTP::Headers->new( %{$_} ) }; subtype 'My::Types::URI' => as class_type('URI'); coerce 'My::Types::URI' => from 'Object' => via { $_->isa('URI') ? $_ : Params::Coerce::coerce( 'URI', $_ ); } => from 'Str' => via { URI->new( $_, 'http' ) }; subtype 'Protocol' => as 'Str' => where { /^HTTP/[0-9].[0-9]$/ }; has 'base' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 ); has 'uri' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 ); has 'method' => ( is => 'rw', isa => 'Str' ); has 'protocol' => ( is => 'rw', isa => 'Protocol' ); has 'headers' => ( is => 'rw', isa => 'My::Types::HTTP::Headers', coerce => 1, default => sub { HTTP::Headers->new } ); DESCRIPTION
This recipe introduces type coercions, which are defined with the "coerce" sugar function. Coercions are attached to existing type constraints, and define a (one-way) transformation from one type to another. This is very powerful, but it can also have unexpected consequences, so you have to explicitly ask for an attribute to be coerced. To do this, you must set the "coerce" attribute option to a true value. First, we create the subtype to which we will coerce the other types: subtype 'My::Types::HTTP::Headers' => as class_type('HTTP::Headers'); We are creating a subtype rather than using "HTTP::Headers" as a type directly. The reason we do this is that coercions are global, and a coercion defined for "HTTP::Headers" in our "Request" class would then be defined for all Moose-using classes in the current Perl interpreter. It's a best practice to avoid this sort of namespace pollution. The "class_type" sugar function is simply a shortcut for this: subtype 'HTTP::Headers' => as 'Object' => where { $_->isa('HTTP::Headers') }; Internally, Moose creates a type constraint for each Moose-using class, but for non-Moose classes, the type must be declared explicitly. We could go ahead and use this new type directly: has 'headers' => ( is => 'rw', isa => 'My::Types::HTTP::Headers', default => sub { HTTP::Headers->new } ); This creates a simple attribute which defaults to an empty instance of HTTP::Headers. The constructor for HTTP::Headers accepts a list of key-value pairs representing the HTTP header fields. In Perl, such a list could be stored in an ARRAY or HASH reference. We want our "headers" attribute to accept those data structures instead of an HTTP::Headers instance, and just do the right thing. This is exactly what coercion is for: coerce 'My::Types::HTTP::Headers' => from 'ArrayRef' => via { HTTP::Headers->new( @{$_} ) } => from 'HashRef' => via { HTTP::Headers->new( %{$_} ) }; The first argument to "coerce" is the type to which we are coercing. Then we give it a set of "from"/"via" clauses. The "from" function takes some other type name and "via" takes a subroutine reference which actually does the coercion. However, defining the coercion doesn't do anything until we tell Moose we want a particular attribute to be coerced: has 'headers' => ( is => 'rw', isa => 'My::Types::HTTP::Headers', coerce => 1, default => sub { HTTP::Headers->new } ); Now, if we use an "ArrayRef" or "HashRef" to populate "headers", it will be coerced into a new HTTP::Headers instance. With the coercion in place, the following lines of code are all equivalent: $foo->headers( HTTP::Headers->new( bar => 1, baz => 2 ) ); $foo->headers( [ 'bar', 1, 'baz', 2 ] ); $foo->headers( { bar => 1, baz => 2 } ); As you can see, careful use of coercions can produce a very open interface for your class, while still retaining the "safety" of your type constraint checks. (1) Our next coercion shows how we can leverage existing CPAN modules to help implement coercions. In this case we use Params::Coerce. Once again, we need to declare a class type for our non-Moose URI class: subtype 'My::Types::URI' => as class_type('URI'); Then we define the coercion: coerce 'My::Types::URI' => from 'Object' => via { $_->isa('URI') ? $_ : Params::Coerce::coerce( 'URI', $_ ); } => from 'Str' => via { URI->new( $_, 'http' ) }; The first coercion takes any object and makes it a "URI" object. The coercion system isn't that smart, and does not check if the object is already a URI, so we check for that ourselves. If it's not a URI already, we let Params::Coerce do its magic, and we just use its return value. If Params::Coerce didn't return a URI object (for whatever reason), Moose would throw a type constraint error. The other coercion takes a string and converts it to a URI. In this case, we are using the coercion to apply a default behavior, where a string is assumed to be an "http" URI. Finally, we need to make sure our attributes enable coercion. has 'base' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 ); has 'uri' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 ); Re-using the coercion lets us enforce a consistent API across multiple attributes. CONCLUSION
This recipe showed the use of coercions to create a more flexible and DWIM-y API. Like any powerful feature, we recommend some caution. Sometimes it's better to reject a value than just guess at how to DWIM. We also showed the use of the "class_type" sugar function as a shortcut for defining a new subtype of "Object". FOOTNOTES
(1) This particular example could be safer. Really we only want to coerce an array with an even number of elements. We could create a new "EvenElementArrayRef" type, and then coerce from that type, as opposed to a plain "ArrayRef" AUTHORS
o Stevan Little <stevan.little@iinteractive.com> o Dave Rolsky <autarch@urth.org> o Jesse Luehrs <doy@tozt.net> o Shawn M Moore <code@sartak.org> o XXXX XXX'XX (Yuval Kogman) <nothingmuch@woobling.org> o Karen Etheridge <ether@cpan.org> o Florian Ragwitz <rafl@debian.org> o Hans Dieter Pearcey <hdp@weftsoar.net> o Chris Prather <chris@prather.org> o Matt S Trout <mst@shadowcat.co.uk> COPYRIGHT AND LICENSE
This software is copyright (c) 2006 by Infinity Interactive, Inc.. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.18.2 2014-01-19 Moose::Cookbook::Basics::HTTP_SubtypesAndCoercion(3)
All times are GMT -4. The time now is 02:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy